grepai-trace-callees

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GrepAI Trace Callees

GrepAI Trace Callees

This skill covers using
grepai trace callees
to find all functions called by a specific function.
本技能介绍如何使用
grepai trace callees
查找某个特定函数调用的所有函数。

When to Use This Skill

适用场景

  • Understanding function dependencies
  • Mapping function behavior
  • Finding deeply nested dependencies
  • Code comprehension and documentation
  • 理解函数依赖关系
  • 映射函数行为
  • 查找深层嵌套依赖
  • 代码理解与文档编写

What is Trace Callees?

什么是Trace Callees?

grepai trace callees
answers: "What does this function call?"
func ProcessOrder(order) {
    validateOrder(order)
    calculateTotal(order)
    sendConfirmation(order.email)
}
┌───────┴───────────────────┐
│  What does ProcessOrder   │
│  call?                    │
├───────────────────────────┤
│ • validateOrder           │
│ • calculateTotal          │
│ • sendConfirmation        │
└───────────────────────────┘
grepai trace callees
用于解答:"该函数调用了哪些内容?"
func ProcessOrder(order) {
    validateOrder(order)
    calculateTotal(order)
    sendConfirmation(order.email)
}
┌───────┴───────────────────┐
│  What does ProcessOrder   │
│  call?                    │
├───────────────────────────┤
│ • validateOrder           │
│ • calculateTotal          │
│ • sendConfirmation        │
└───────────────────────────┘

Basic Usage

基础用法

bash
grepai trace callees "FunctionName"
bash
grepai trace callees "FunctionName"

Example

示例

bash
grepai trace callees "ProcessOrder"
Output:
🔍 Callees of "ProcessOrder"

Found 4 callees:

1. validateOrder
   File: services/order.go:45
   Context: validateOrder(order)

2. calculateTotal
   File: services/order.go:48
   Context: total := calculateTotal(order.Items)

3. applyDiscount
   File: services/order.go:51
   Context: total = applyDiscount(total, order.Coupon)

4. sendConfirmation
   File: services/order.go:55
   Context: sendConfirmation(order.Email, total)
bash
grepai trace callees "ProcessOrder"
输出:
🔍 Callees of "ProcessOrder"

Found 4 callees:

1. validateOrder
   File: services/order.go:45
   Context: validateOrder(order)

2. calculateTotal
   File: services/order.go:48
   Context: total := calculateTotal(order.Items)

3. applyDiscount
   File: services/order.go:51
   Context: total = applyDiscount(total, order.Coupon)

4. sendConfirmation
   File: services/order.go:55
   Context: sendConfirmation(order.Email, total)

JSON Output

JSON格式输出

bash
grepai trace callees "ProcessOrder" --json
Output:
json
{
  "query": "ProcessOrder",
  "mode": "callees",
  "count": 4,
  "results": [
    {
      "file": "services/order.go",
      "line": 45,
      "callee": "validateOrder",
      "context": "validateOrder(order)"
    },
    {
      "file": "services/order.go",
      "line": 48,
      "callee": "calculateTotal",
      "context": "total := calculateTotal(order.Items)"
    },
    {
      "file": "services/order.go",
      "line": 51,
      "callee": "applyDiscount",
      "context": "total = applyDiscount(total, order.Coupon)"
    },
    {
      "file": "services/order.go",
      "line": 55,
      "callee": "sendConfirmation",
      "context": "sendConfirmation(order.Email, total)"
    }
  ]
}
bash
grepai trace callees "ProcessOrder" --json
输出:
json
{
  "query": "ProcessOrder",
  "mode": "callees",
  "count": 4,
  "results": [
    {
      "file": "services/order.go",
      "line": 45,
      "callee": "validateOrder",
      "context": "validateOrder(order)"
    },
    {
      "file": "services/order.go",
      "line": 48,
      "callee": "calculateTotal",
      "context": "total := calculateTotal(order.Items)"
    },
    {
      "file": "services/order.go",
      "line": 51,
      "callee": "applyDiscount",
      "context": "total = applyDiscount(total, order.Coupon)"
    },
    {
      "file": "services/order.go",
      "line": 55,
      "callee": "sendConfirmation",
      "context": "sendConfirmation(order.Email, total)"
    }
  ]
}

Compact JSON (AI Optimized)

紧凑JSON格式(AI优化)

bash
grepai trace callees "ProcessOrder" --json --compact
Output:
json
{
  "q": "ProcessOrder",
  "m": "callees",
  "c": 4,
  "r": [
    {"f": "services/order.go", "l": 45, "fn": "validateOrder"},
    {"f": "services/order.go", "l": 48, "fn": "calculateTotal"},
    {"f": "services/order.go", "l": 51, "fn": "applyDiscount"},
    {"f": "services/order.go", "l": 55, "fn": "sendConfirmation"}
  ]
}
bash
grepai trace callees "ProcessOrder" --json --compact
输出:
json
{
  "q": "ProcessOrder",
  "m": "callees",
  "c": 4,
  "r": [
    {"f": "services/order.go", "l": 45, "fn": "validateOrder"},
    {"f": "services/order.go", "l": 48, "fn": "calculateTotal"},
    {"f": "services/order.go", "l": 51, "fn": "applyDiscount"},
    {"f": "services/order.go", "l": 55, "fn": "sendConfirmation"}
  ]
}

TOON Output (v0.26.0+)

TOON格式输出(v0.26.0及以上版本)

TOON format offers ~50% fewer tokens than JSON:
bash
grepai trace callees "ProcessOrder" --toon
Note:
--json
and
--toon
are mutually exclusive.
TOON格式的令牌数比JSON少约50%:
bash
grepai trace callees "ProcessOrder" --toon
注意:
--json
--toon
参数互斥。

Extraction Modes

提取模式

Fast Mode (Default)

快速模式(默认)

bash
grepai trace callees "ProcessOrder" --mode fast
bash
grepai trace callees "ProcessOrder" --mode fast

Precise Mode

精确模式

bash
grepai trace callees "ProcessOrder" --mode precise
ModeSpeedAccuracyDependencies
fast
⚡⚡⚡GoodNone
precise
⚡⚡Excellenttree-sitter
bash
grepai trace callees "ProcessOrder" --mode precise
模式速度准确率依赖
fast
⚡⚡⚡良好
precise
⚡⚡优秀tree-sitter

Use Cases

使用场景

Understanding Function Behavior

理解函数行为

bash
undefined
bash
undefined

What does this complex function do?

这个复杂函数的作用是什么?

grepai trace callees "handleRequest"
grepai trace callees "handleRequest"

Map the data flow

映射数据流

grepai trace callees "processPayment"
undefined
grepai trace callees "processPayment"
undefined

Finding Dependencies

查找依赖关系

bash
undefined
bash
undefined

What external services does this call?

该函数调用了哪些外部服务?

grepai trace callees "syncData"
grepai trace callees "syncData"

What database operations happen?

执行了哪些数据库操作?

grepai trace callees "saveUser"
undefined
grepai trace callees "saveUser"
undefined

Code Review

代码审查

bash
undefined
bash
undefined

What side effects does this function have?

该函数有哪些副作用?

grepai trace callees "updateProfile"
grepai trace callees "updateProfile"

Is this function doing too much?

这个函数是否职责过多?

grepai trace callees "doEverything" # Lots of callees = code smell
undefined
grepai trace callees "doEverything" # 大量被调用方 = 代码异味
undefined

Documentation

文档编写

bash
undefined
bash
undefined

Generate dependency list for docs

为文档生成依赖列表

grepai trace callees "initialize" --json | jq '.results[].callee'
undefined
grepai trace callees "initialize" --json | jq '.results[].callee'
undefined

Callers vs Callees

Callers与Callees对比

CommandQuestionUse Case
trace callers
Who calls me?Impact analysis
trace callees
What do I call?Behavior analysis
bash
undefined
命令解决的问题适用场景
trace callers
谁调用了我?影响分析
trace callees
我调用了什么?行为分析
bash
undefined

Combined analysis

组合分析

grepai trace callers "processOrder" # Who uses this? grepai trace callees "processOrder" # What does it do?
undefined
grepai trace callers "processOrder" # 谁在使用这个函数? grepai trace callees "processOrder" # 这个函数的作用是什么?
undefined

Filtering Results

结果过滤

By File Type

按文件类型过滤

bash
undefined
bash
undefined

Get callees and filter to only .go files

获取被调用方并仅筛选.go文件

grepai trace callees "main" --json | jq '.results[] | select(.file | endswith(".go"))'
undefined
grepai trace callees "main" --json | jq '.results[] | select(.file | endswith(".go"))'
undefined

Exclude Test Functions

排除测试函数

bash
grepai trace callees "Login" --json | jq '.results[] | select(.callee | startswith("Test") | not)'
bash
grepai trace callees "Login" --json | jq '.results[] | select(.callee | startswith("Test") | not)'

Count by Category

按类别统计

bash
undefined
bash
undefined

Count how many database vs. API calls

统计数据库调用与API调用的数量

grepai trace callees "processOrder" --json | jq '.results[].callee' | grep -c "db"
undefined
grepai trace callees "processOrder" --json | jq '.results[].callee' | grep -c "db"
undefined

What Callees Includes

Callees包含的内容

The trace finds:
  • Direct function calls
  • Method calls
  • Built-in function calls (depending on mode)
该追踪功能可查找:
  • 直接函数调用
  • 方法调用
  • 内置函数调用(取决于模式)

Example

示例

go
func ProcessOrder(order Order) error {
    // Direct call
    validateOrder(order)

    // Method call
    order.Validate()

    // Package function
    utils.Log("processing")

    // Built-in (may or may not be captured)
    fmt.Println("done")

    return nil
}
Callees found:
  • validateOrder
  • Validate
    (method)
  • Log
    (from utils)
  • Println
    (depending on mode)
go
func ProcessOrder(order Order) error {
    // 直接调用
    validateOrder(order)

    // 方法调用
    order.Validate()

    // 包函数
    utils.Log("processing")

    // 内置函数(可能会被捕获,也可能不会)
    fmt.Println("done")

    return nil
}
找到的被调用方:
  • validateOrder
  • Validate
    (方法)
  • Log
    (来自utils包)
  • Println
    (取决于模式)

Limitations

局限性

What Callees Might Miss

Callees可能遗漏的内容

  • Dynamic/runtime calls
  • Callbacks and closures
  • Interface method calls (may show interface, not implementation)
  • Reflection-based calls
  • 动态/运行时调用
  • 回调与闭包
  • 接口方法调用(可能显示接口而非实现)
  • 基于反射的调用

Example of Undetected Call

未被检测到的调用示例

go
func process(fn func()) {
    fn()  // Callee is unknown at static analysis time
}
go
func process(fn func()) {
    fn()  // 静态分析时无法确定被调用方
}

Combining with Trace Graph

与Trace Graph结合使用

For recursive dependency analysis, use trace graph:
bash
undefined
如需进行递归依赖分析,可使用trace graph:
bash
undefined

Direct callees only

仅查看直接被调用方

grepai trace callees "main"
grepai trace callees "main"

Full dependency tree (recursive)

完整依赖树(递归)

grepai trace graph "main" --depth 3
undefined
grepai trace graph "main" --depth 3
undefined

Scripting Examples

脚本示例

List All Callees

列出所有被调用方

bash
grepai trace callees "main" --json | jq -r '.results[].callee' | sort -u
bash
grepai trace callees "main" --json | jq -r '.results[].callee' | sort -u

Check for Specific Callee

检查特定被调用方

bash
undefined
bash
undefined

Does processOrder call sendEmail?

processOrder是否调用了sendEmail?

grepai trace callees "processOrder" --json | jq -e '.results[] | select(.callee == "sendEmail")' && echo "Yes" || echo "No"
undefined
grepai trace callees "processOrder" --json | jq -e '.results[] | select(.callee == "sendEmail")' && echo "是" || echo "否"
undefined

Generate Dependency Report

生成依赖报告

bash
#!/bin/bash
echo "# Function Dependencies Report"
echo ""
for fn in main initialize processOrder; do
    echo "## $fn"
    grepai trace callees "$fn" --json | jq -r '.results[].callee' | sed 's/^/- /'
    echo ""
done
bash
#!/bin/bash
echo "# 函数依赖报告"
echo ""
for fn in main initialize processOrder; do
    echo "## $fn"
    grepai trace callees "$fn" --json | jq -r '.results[].callee' | sed 's/^/- /'
    echo ""
done

Common Issues

常见问题

Problem: Function not found ✅ Solution: Check spelling and ensure function exists in indexed files
Problem: No callees found (but function has calls) ✅ Solutions:
  • Try
    --mode precise
  • Check language is in
    enabled_languages
  • Ensure symbols.gob is up to date (
    grepai watch
    )
Problem: Missing some callees ✅ Solution: Use
--mode precise
for better accuracy
问题: 未找到函数 ✅ 解决方案: 检查拼写并确保函数存在于已索引的文件中
问题: 未找到被调用方(但函数确实有调用) ✅ 解决方案:
  • 尝试使用
    --mode precise
    参数
  • 检查语言是否在
    enabled_languages
  • 确保symbols.gob是最新的(使用
    grepai watch
问题: 遗漏部分被调用方 ✅ 解决方案: 使用
--mode precise
参数以获得更高的准确率

Best Practices

最佳实践

  1. Use for understanding: Great for learning new codebases
  2. Combine with callers: Full dependency picture
  3. Use graph for deep analysis: When you need recursion
  4. Filter results: Focus on relevant callees
  5. Document findings: Export to markdown for docs
  1. 用于代码理解: 非常适合学习新代码库
  2. 与callers结合使用: 获得完整的依赖关系图
  3. 使用graph进行深度分析: 需要递归分析时使用
  4. 过滤结果: 聚焦于相关的被调用方
  5. 记录结果: 导出为Markdown格式用于文档

Output Format

输出格式

Trace callees result:
🔍 Callees of "ProcessOrder"

Mode: fast
Function found in: services/order.go:40

Found 4 callees:

1. validateOrder
   File: services/order.go:45
   Context: validateOrder(order)

2. calculateTotal
   File: services/order.go:48
   Context: total := calculateTotal(order.Items)

3. applyDiscount
   File: services/order.go:51
   Context: total = applyDiscount(total, order.Coupon)

4. sendConfirmation
   File: services/order.go:55
   Context: sendConfirmation(order.Email, total)

Tip: Use 'grepai trace graph ProcessOrder' for recursive analysis
Trace callees的结果示例:
🔍 Callees of "ProcessOrder"

Mode: fast
Function found in: services/order.go:40

Found 4 callees:

1. validateOrder
   File: services/order.go:45
   Context: validateOrder(order)

2. calculateTotal
   File: services/order.go:48
   Context: total := calculateTotal(order.Items)

3. applyDiscount
   File: services/order.go:51
   Context: total = applyDiscount(total, order.Coupon)

4. sendConfirmation
   File: services/order.go:55
   Context: sendConfirmation(order.Email, total)

Tip: Use 'grepai trace graph ProcessOrder' for recursive analysis