Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ let quotient = a / b; // Division
4. Multiplication `/`, Division `/`
5. Addition `+`, Subtraction `-`
6. Comparisons `==`, `!=`, `<`, `<=`, `>`, `>=`
7. Logical AND `&&`
8. Logical OR `||`

### Expression Grouping

Expand All @@ -218,6 +220,69 @@ let result = (a + b) * c;
let complex = (x + y) / (a - b);
```

### Logical Operators

- `&&` (logical AND), `||` (logical OR)
- Operands are treated as booleans with "non-zero is true" semantics
- Current implementation evaluates both sides (no short-circuit yet)

Examples

```ghostscope
trace main:entry {
if a > 10 && b == 0 {
print "AND";
} else if a < 100 || p == 0 {
print "OR";
}
}
```

### Cross-type Operations With DWARF Values

- Arithmetic (+, -, *, /)
- Supported: script int/bool with DWARF integer-like scalars
- BaseType (signed/unsigned 1/2/4/8 bytes), Enum (as underlying integer), Bitfield (extracted integer), char/unsigned char (1 byte)
- Not supported: aggregates (struct/union/array), pointers, floats at runtime
- Comparisons (==, !=, <, <=, >, >=)
- Supported: script int/bool with the DWARF integer-like types above (after width/sign unification)
- Pointer: only equality/inequality (pointer==pointer, pointer==0)
- CString equality: DWARF char* or char[] vs script string literal (==, !=) with bounded read/compare
- Not supported: relational string compares; aggregates; floats with DWARF
- Floats
- Not supported: eBPF does not support floating-point runtime operations. GhostScope scripts do not support float literals or float arithmetic.

Error semantics: If a read fails (null deref/read error/offsets unavailable), comparisons return false and arithmetic returns 0; the event status carries the error code.

Examples

```ghostscope
// Integer arithmetic and comparisons with DWARF locals/globals
trace foo.c:42 {
// DWARF int (e.g., s.counter) mixed with script int
if s.counter > 100 {
print "hot";
}
print "sum:{}", s.counter + 5;

// Enum/bitfield compare (treated as integer)
print "active:{}", a.active == 1;
}

// Pointer equality (no ordering compares)
trace foo.c:50 {
print "isNull:{}", p == 0; // pointer vs NULL
// print "same:{}", p == q; // pointer vs pointer (if both in scope)
}

// CString equality: DWARF char*/char[] vs script string literal
trace foo.c:60 {
print "greet-ok:{}", gm == "Hello, Global!"; // gm: const char* or char[]
}

// Floats are not supported in GhostScope scripts.
```

### Special Variables (In Progress)

Special variables start with `$` and provide access to runtime information:
Expand Down
112 changes: 107 additions & 5 deletions docs/zh/scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ trace /home/user/project/src/utils.c:100 {

## 变量

### 脚本变量声明
### 脚本变量

使用 `let` 关键字声明脚本变量:

Expand All @@ -87,11 +87,44 @@ let message = "hello";
let result = a + b;
```

脚本变量目前支持整数、浮点数和字符串类型。
脚本变量的类型与能力如下:

### 局部变量、参数和全局变量
| 类型 | 字面量/示例 | 描述 | 运算/比较支持 |
| --- | --- | --- | --- |
| 整数(int,内部统一为 i64) | `123`, `-42` | 有符号 64 位整数 | 支持 +、-、*、/;可与 DWARF 整数类标量进行算术与比较 |
| 布尔(bool) | 由比较产生:`a < b` | 通过比较/逻辑表达式得到的布尔值 | 支持逻辑与/或(仅脚本内);与 DWARF 整数类比较时按 0/1 参与比较与算术 |
| 字符串(string) | `"hello"` | UTF-8 字符串字面量 | 支持与 DWARF C 字符串做等值(==、!=);不支持大小关系比较 |

GhostScope 支持复杂的变量访问:
说明:
1. 目前脚本层不支持自定义结构体/数组/指针类型;对于这些聚合类型,请通过 DWARF 变量访问(成员访问、解引用、常量下标)来获取标量后再参与运算。
2. eBPF 不支持浮点运算,故当前脚本变量不支持浮点字面量与浮点运算

### DWARF 变量

DWARF 变量其实就是被跟踪的程序里面定义的**局部变量、参数和全局变量**,这类变量都是根据 DWARF 信息获取,所以在这里被统称为 DWARF 变量。

#### DWARF 变量类型

下表列出了按照 DWARF 类型定义,GhostScope 识别与显示/访问支持的主要类型:

| DWARF 类型 | 示例(来源语言) | 映射/显示 | 访问/运算支持 |
| --- | --- | --- | --- |
| 有符号/无符号整数(1/2/4/8 字节) | `int`, `long`, `unsigned int`, `size_t` | I8/I16/I32/I64 或 U8/U16/U32/U64 | 可打印;可与“脚本变量”的整数/布尔进行算术与比较(统一宽度与符号后) |
| 布尔 | `bool` | Bool(true/false) | 可打印;可与“脚本变量”的布尔/整数比较 |
| 浮点 | `float`, `double` | 不支持 | eBPF 不支持浮点运算;GhostScope 脚本不支持浮点字面量与浮点运算 |
| 字符 | `char`, `unsigned char` | 1 字节整数/字符 | 作为 1 字节整数打印;数组/指针见下 |
| C 字符串 | `char*`, `const char*`, `char[]` | CString(以字符串显示) | 可打印为字符串;可与“脚本变量”的字符串做等值(==、!=) |
| 指针 | `T*`, `void*`, 函数指针 | Pointer/NullPointer(地址显示) | 支持 `*` 解引用、`==`/`!=` 比较;对局部/参数/全局启用“自动解引用” |
| 数组 | `T[n]` | Array | 支持常量下标读取(顶层或链尾);暂不支持动态/中间索引、多维数组 |
| 结构体/类 | `struct Foo`/`class Bar` | Struct | 支持 `.` 成员访问;不直接参与算术/比较(访问到标量成员后即可参与) |
| 联合体 | `union U` | Union | 同上,支持成员访问后再进行标量运算 |
| 枚举 | `enum E` | Enum(按底层整型) | 打印为枚举名;在运算/比较时按底层整数处理 |
| 位域 | `int flags:3` | Bitfield → 整数视图 | 抽取为整数;可与“脚本变量”的整数/布尔混用比较与算术 |
| 类型别名/限定 | `typedef`/`const`/`volatile` | Typedef/QualifiedType | 按底层类型处理(行为与底层类型一致) |
| 优化移除 | 变量被优化掉 | OptimizedOut | 读取失败;打印为 `<OPTIMIZED_OUT>`;运算/比较按失败语义处理 |
| 未知 | 不支持或未知 | Unknown | 打印为 `<UNKNOWN_TYPE_N_BYTES>` |

#### GhostScope 支持对复杂的 DWARF 变量访问:

```ghostscope
// 简单变量
Expand All @@ -118,7 +151,7 @@ print arr[0].name;
```

提示:
- 目前“局部变量、参数、全局变量”均已支持自动解引用(无需显式 `*ptr`,在安全范围内会自动加载并解引用指针值)。
- 目前“局部变量、参数、全局变量”均已支持自动解引用(无需显式 `*ptr`,也不需要 `->`,统一使用 `.`,在安全范围内会自动加载并解引用指针值,类似于 Rust 的自动解引用)。
- 数组访问:已支持顶层 `arr[常量]` 与“链尾”`a.b.c[常量]`。暂不支持:链中间索引(如 `a.b[2].c`)、动态下标(`arr[i]`)和多维数组。

### 特殊变量(实现中)
Expand Down Expand Up @@ -238,6 +271,8 @@ let quotient = a / b; // 除法
4. 乘法 `*`,除法 `/`
5. 加法 `+`,减法 `-`
6. 比较 `==`, `!=`, `<`, `<=`, `>`, `>=`
7. 逻辑与 `&&`
8. 逻辑或 `||`

### 表达式分组

Expand All @@ -247,6 +282,73 @@ let result = (a + b) * c;
let complex = (x + y) / (a - b);
```

### 逻辑运算符

- `&&`(逻辑与)、`||`(逻辑或)
- 操作数按“非零为真”处理
- 当前实现为“非短路”:左右两侧都会被求值

示例

```ghostscope
trace main:entry {
if a > 10 && b == 0 {
print "AND";
} else if a < 100 || p == 0 {
print "OR";
}
}
```

### 脚本变量与 DWARF 变量的跨类型运算

- 算术(+、-、*、/)
- 支持:脚本变量(整数/布尔) 与 DWARF 变量中的“整数类标量”混用。
- 整数类标量包括:BaseType(有符/无符 1/2/4/8 字节)、Enum(按底层整型)、Bitfield(位域抽取为整数)、`char/unsigned char`(1 字节整数)。
- 不支持:聚合(struct/union/array)、指针、浮点(运行时)。
- 比较(==、!=、<、<=、>、>=)
- 支持:脚本变量(整数/布尔) 与上述 DWARF 整数类标量;比较前会对宽度与符号进行统一。
- 指针比较:仅支持等值/不等(DWARF 指针 == DWARF 指针、DWARF 指针 == 0)。
- C 字符串等值:DWARF 变量(`char*` 或 `char[]`) 与 脚本变量(字符串字面量)可做 `==`/`!=` 等值比较(通过有界读取再比较)。
- 不支持:字符串大小关系比较、聚合整体比较、浮点与 DWARF 值混用比较。
- 浮点
- 不支持浮点运算;脚本与 DWARF 层均不支持。

错误语义:当 DWARF 变量读取失败(空指针、读失败、偏移不可用等)时,比较结果为 false、算术结果为 0,同时在事件状态中带出错误码。

示例

```ghostscope
// 与 DWARF 局部/全局的整型混合运算与比较
trace foo.c:42 {
// 脚本 int 与 DWARF int(如 s.counter)
if s.counter > 100 {
print "hot";
}
print "sum:{}", s.counter + 5;

// 枚举/位域比较(当作整数)
print "active:{}", a.active == 1;
}

// 指针等值比较(不支持大小关系)
trace foo.c:50 {
print "isNull:{}", p == 0; // 指针与 NULL
// print "same:{}", p == q; // 指针与指针(若二者在作用域内)
}

// C 字符串等值:DWARF char*/char[] 与脚本字符串字面量
trace foo.c:60 {
print "greet-ok:{}", gm == "Hello, Global!"; // gm: const char* 或 char[]
}

// 纯脚本浮点(编译期折叠)。与 DWARF 混用暂不支持。
trace foo.c:70 {
let x = 1.5 * 2.0; // 编译期折叠
if x > 2.0 { print "ok"; }
}
```

## 栈回溯语句(实现中)

打印当前调用栈:
Expand Down
Loading