grepai-trace-callers

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GrepAI Trace Callers

GrepAI Trace 调用方查找

This skill covers using
grepai trace callers
to find all code locations that call a specific function or method.
本技能介绍如何使用
grepai trace callers
查找调用特定函数或方法的所有代码位置。

When to Use This Skill

何时使用此技能

  • Finding all usages of a function before refactoring
  • Understanding function dependencies
  • Impact analysis before changes
  • Code navigation and exploration
  • 重构前查找函数的所有用法
  • 理解函数依赖关系
  • 变更前的影响分析
  • 代码导航与探索

What is Trace Callers?

什么是Trace Callers?

grepai trace callers
answers: "Who calls this function?"
func Login(user, pass) {...}
┌───────┴───────────────────┐
│   Who calls Login()?      │
├───────────────────────────┤
│ • HandleAuth (auth.go:42) │
│ • TestLogin (test.go:15)  │
│ • CLI (main.go:88)        │
└───────────────────────────┘
grepai trace callers
用于解答:“谁调用了这个函数?”
func Login(user, pass) {...}
┌───────┴───────────────────┐
│   Who calls Login()?      │
├───────────────────────────┤
│ • HandleAuth (auth.go:42) │
│ • TestLogin (test.go:15)  │
│ • CLI (main.go:88)        │
└───────────────────────────┘

Basic Usage

基础用法

bash
grepai trace callers "FunctionName"
bash
grepai trace callers "FunctionName"

Example

示例

bash
grepai trace callers "Login"
Output:
🔍 Callers of "Login"

Found 3 callers:

1. HandleAuth
   File: handlers/auth.go:42
   Context: user.Login(ctx, credentials)

2. TestLoginSuccess
   File: handlers/auth_test.go:15
   Context: result := Login(testUser, testPass)

3. RunCLI
   File: cmd/main.go:88
   Context: err := auth.Login(username, password)
bash
grepai trace callers "Login"
输出:
🔍 Callers of "Login"

Found 3 callers:

1. HandleAuth
   File: handlers/auth.go:42
   Context: user.Login(ctx, credentials)

2. TestLoginSuccess
   File: handlers/auth_test.go:15
   Context: result := Login(testUser, testPass)

3. RunCLI
   File: cmd/main.go:88
   Context: err := auth.Login(username, password)

JSON Output

JSON输出

For programmatic use:
bash
grepai trace callers "Login" --json
Output:
json
{
  "query": "Login",
  "mode": "callers",
  "count": 3,
  "results": [
    {
      "file": "handlers/auth.go",
      "line": 42,
      "caller": "HandleAuth",
      "context": "user.Login(ctx, credentials)"
    },
    {
      "file": "handlers/auth_test.go",
      "line": 15,
      "caller": "TestLoginSuccess",
      "context": "result := Login(testUser, testPass)"
    },
    {
      "file": "cmd/main.go",
      "line": 88,
      "caller": "RunCLI",
      "context": "err := auth.Login(username, password)"
    }
  ]
}
供编程使用:
bash
grepai trace callers "Login" --json
输出:
json
{
  "query": "Login",
  "mode": "callers",
  "count": 3,
  "results": [
    {
      "file": "handlers/auth.go",
      "line": 42,
      "caller": "HandleAuth",
      "context": "user.Login(ctx, credentials)"
    },
    {
      "file": "handlers/auth_test.go",
      "line": 15,
      "caller": "TestLoginSuccess",
      "context": "result := Login(testUser, testPass)"
    },
    {
      "file": "cmd/main.go",
      "line": 88,
      "caller": "RunCLI",
      "context": "err := auth.Login(username, password)"
    }
  ]
}

Compact JSON (AI Optimized)

紧凑JSON格式(AI优化)

bash
grepai trace callers "Login" --json --compact
Output:
json
{
  "q": "Login",
  "m": "callers",
  "c": 3,
  "r": [
    {"f": "handlers/auth.go", "l": 42, "fn": "HandleAuth"},
    {"f": "handlers/auth_test.go", "l": 15, "fn": "TestLoginSuccess"},
    {"f": "cmd/main.go", "l": 88, "fn": "RunCLI"}
  ]
}
bash
grepai trace callers "Login" --json --compact
输出:
json
{
  "q": "Login",
  "m": "callers",
  "c": 3,
  "r": [
    {"f": "handlers/auth.go", "l": 42, "fn": "HandleAuth"},
    {"f": "handlers/auth_test.go", "l": 15, "fn": "TestLoginSuccess"},
    {"f": "cmd/main.go", "l": 88, "fn": "RunCLI"}
  ]
}

TOON Output (v0.26.0+)

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

TOON format offers ~50% fewer tokens than JSON:
bash
grepai trace callers "Login" --toon
Output:
callers[3]:
  - call_site:
      context: "user.Login(ctx, credentials)"
      file: handlers/auth.go
      line: 42
    symbol:
      name: HandleAuth
      ...
Note:
--json
and
--toon
are mutually exclusive.
TOON格式的令牌数比JSON少约50%:
bash
grepai trace callers "Login" --toon
输出:
callers[3]:
  - call_site:
      context: "user.Login(ctx, credentials)"
      file: handlers/auth.go
      line: 42
    symbol:
      name: HandleAuth
      ...
注意:
--json
--toon
不能同时使用。

Extraction Modes

提取模式

GrepAI offers two extraction modes:
GrepAI提供两种提取模式:

Fast Mode (Default)

快速模式(默认)

Uses regex patterns. Fast and dependency-free.
bash
grepai trace callers "Login" --mode fast
使用正则表达式模式。速度快且无依赖。
bash
grepai trace callers "Login" --mode fast

Precise Mode

精确模式

Uses tree-sitter AST parsing. More accurate but requires tree-sitter.
bash
grepai trace callers "Login" --mode precise
使用tree-sitter AST解析。准确率更高但需要tree-sitter依赖。
bash
grepai trace callers "Login" --mode precise

Comparison

对比

ModeSpeedAccuracyDependencies
fast
⚡⚡⚡GoodNone
precise
⚡⚡Excellenttree-sitter
模式速度准确率依赖项
fast
⚡⚡⚡良好
precise
⚡⚡优秀tree-sitter

Configuration

配置

Configure trace in
.grepai/config.yaml
:
yaml
trace:
  mode: fast  # fast or precise

  enabled_languages:
    - .go
    - .js
    - .ts
    - .py
    - .php
    - .rs

  exclude_patterns:
    - "*_test.go"
    - "*.spec.ts"
.grepai/config.yaml
中配置trace:
yaml
trace:
  mode: fast  # fast or precise

  enabled_languages:
    - .go
    - .js
    - .ts
    - .py
    - .php
    - .rs

  exclude_patterns:
    - "*_test.go"
    - "*.spec.ts"

Supported Languages

支持的语言

LanguageExtensions
Go
.go
JavaScript
.js
,
.jsx
TypeScript
.ts
,
.tsx
Python
.py
PHP
.php
C/C++
.c
,
.h
,
.cpp
,
.hpp
,
.cc
,
.cxx
Rust
.rs
Zig
.zig
C#
.cs
Java
.java
Pascal/Delphi
.pas
,
.dpr
语言扩展名
Go
.go
JavaScript
.js
,
.jsx
TypeScript
.ts
,
.tsx
Python
.py
PHP
.php
C/C++
.c
,
.h
,
.cpp
,
.hpp
,
.cc
,
.cxx
Rust
.rs
Zig
.zig
C#
.cs
Java
.java
Pascal/Delphi
.pas
,
.dpr

Use Cases

使用场景

Before Refactoring

重构前

bash
undefined
bash
undefined

Find all usages before renaming

重命名前查找所有用法

grepai trace callers "getUserById"
grepai trace callers "getUserById"

Check impact of changing signature

检查修改签名的影响

grepai trace callers "processPayment"
undefined
grepai trace callers "processPayment"
undefined

Understanding Codebase

理解代码库

bash
undefined
bash
undefined

Who uses this core function?

谁在使用这个核心函数?

grepai trace callers "validateToken"
grepai trace callers "validateToken"

Find entry points to a module

查找模块的入口点

grepai trace callers "initialize"
undefined
grepai trace callers "initialize"
undefined

Debugging

调试

bash
undefined
bash
undefined

Where is this function called from?

这个函数从哪里被调用?

grepai trace callers "problematicFunction"
undefined
grepai trace callers "problematicFunction"
undefined

Code Review

代码评审

bash
undefined
bash
undefined

Verify function usage before approving changes

批准变更前验证函数用法

grepai trace callers "deprecatedMethod"
undefined
grepai trace callers "deprecatedMethod"
undefined

Handling Common Names

处理通用名称

If your function name is common, results may include unrelated code:
如果你的函数名称很通用,结果可能包含不相关的代码:

Problem

问题

bash
grepai trace callers "get"  # Too common, many false positives
bash
grepai trace callers "get"  # 过于通用,存在大量误报

Solutions

解决方案

  1. Use more specific name:
bash
grepai trace callers "getUserProfile"
  1. Filter results by path:
bash
grepai trace callers "get" --json | jq '.results[] | select(.file | contains("auth"))'
  1. 使用更具体的名称:
bash
grepai trace callers "getUserProfile"
  1. 按路径过滤结果:
bash
grepai trace callers "get" --json | jq '.results[] | select(.file | contains("auth"))'

Combining with Semantic Search

与语义搜索结合使用

Use together for comprehensive understanding:
bash
undefined
结合使用以全面理解:
bash
undefined

Find what Login does (semantic)

查找Login的功能(语义搜索)

grepai search "user login authentication"
grepai search "user login authentication"

Find who uses Login (trace)

查找谁在使用Login(追踪)

grepai trace callers "Login"
undefined
grepai trace callers "Login"
undefined

Scripting Examples

脚本示例

Bash

Bash

bash
undefined
bash
undefined

Count callers

统计调用方数量

grepai trace callers "MyFunction" --json | jq '.count'
grepai trace callers "MyFunction" --json | jq '.count'

Get caller function names

获取调用方函数名称

grepai trace callers "MyFunction" --json | jq -r '.results[].caller'
grepai trace callers "MyFunction" --json | jq -r '.results[].caller'

Get file paths only

仅获取文件路径

grepai trace callers "MyFunction" --json | jq -r '.results[].file' | sort -u
undefined
grepai trace callers "MyFunction" --json | jq -r '.results[].file' | sort -u
undefined

Python

Python

python
import subprocess
import json

result = subprocess.run(
    ['grepai', 'trace', 'callers', 'Login', '--json'],
    capture_output=True,
    text=True
)

data = json.loads(result.stdout)
print(f"Found {data['count']} callers of Login:")
for r in data['results']:
    print(f"  - {r['caller']} in {r['file']}:{r['line']}")
python
import subprocess
import json

result = subprocess.run(
    ['grepai', 'trace', 'callers', 'Login', '--json'],
    capture_output=True,
    text=True
)

data = json.loads(result.stdout)
print(f"Found {data['count']} callers of Login:")
for r in data['results']:
    print(f"  - {r['caller']} in {r['file']}:{r['line']}")

Common Issues

常见问题

Problem: No callers found ✅ Solutions:
  • Check function name spelling (case-sensitive)
  • Ensure file type is in
    enabled_languages
  • Run
    grepai watch
    to update symbol index
Problem: Too many false positives ✅ Solutions:
  • Use more specific function name
  • Add exclude patterns in config
  • Filter results with
    jq
Problem: Missing some callers ✅ Solutions:
  • Try
    --mode precise
    for better accuracy
  • Check if files are in ignore patterns
问题: 未找到调用方 ✅ 解决方案:
  • 检查函数名称拼写(区分大小写)
  • 确保文件类型在
    enabled_languages
  • 运行
    grepai watch
    更新符号索引
问题: 误报过多 ✅ 解决方案:
  • 使用更具体的函数名称
  • 在配置中添加排除模式
  • 使用
    jq
    过滤结果
问题: 缺少部分调用方 ✅ 解决方案:
  • 尝试
    --mode precise
    以提高准确率
  • 检查文件是否在忽略模式中

Best Practices

最佳实践

  1. Use exact function name: Case matters
  2. Check symbol index: Run
    grepai watch
    first
  3. Use JSON for scripts: Easier to parse
  4. Combine with search: Semantic + trace = full picture
  5. Filter large results: Use
    jq
    or grep
  1. 使用精确的函数名称: 区分大小写
  2. 检查符号索引: 先运行
    grepai watch
  3. 脚本中使用JSON: 更易于解析
  4. 与搜索结合: 语义搜索+追踪=完整视图
  5. 过滤大量结果: 使用
    jq
    或grep

Output Format

输出格式

Trace callers result:
🔍 Callers of "Login"

Mode: fast
Language files scanned: 245

Found 3 callers:

1. HandleAuth
   File: handlers/auth.go:42
   Context: user.Login(ctx, credentials)

2. TestLoginSuccess
   File: handlers/auth_test.go:15
   Context: result := Login(testUser, testPass)

3. RunCLI
   File: cmd/main.go:88
   Context: err := auth.Login(username, password)

Tip: Use --json for machine-readable output
     Use --mode precise for more accurate results
追踪调用方的结果:
🔍 Callers of "Login"

Mode: fast
Language files scanned: 245

Found 3 callers:

1. HandleAuth
   File: handlers/auth.go:42
   Context: user.Login(ctx, credentials)

2. TestLoginSuccess
   File: handlers/auth_test.go:15
   Context: result := Login(testUser, testPass)

3. RunCLI
   File: cmd/main.go:88
   Context: err := auth.Login(username, password)

Tip: Use --json for machine-readable output
     Use --mode precise for more accurate results