Skip to content

Commit 28d55f8

Browse files
committed
Add colors macros which are only supported on bpftrace 0.24+
1 parent 6668123 commit 28d55f8

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

uprobe-contextvars/print_calls.bt

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,70 @@
11
#define YELLOW "\033[33m"
22
#define NC "\033[0m" // No Color (resets the terminal color)
33

4+
// Consistent coloring for a ptr
5+
macro color_int(val) {
6+
let $hash = val % 7;
7+
8+
// Use 256-color codes: \033[38;5;{ID}m
9+
$hash == 0 ? "\033[38;5;196m" : // Bright Red
10+
$hash == 1 ? "\033[38;5;46m" : // Neon Green
11+
$hash == 2 ? "\033[38;5;226m" : // Bright Yellow
12+
$hash == 3 ? "\033[38;5;39m" : // Bright Blue (easier to see than std blue)
13+
$hash == 4 ? "\033[38;5;201m" : // Hot Pink
14+
$hash == 5 ? "\033[38;5;51m" : // Cyan
15+
"\033[38;5;208m" // Orange
16+
}
17+
18+
macro print_uprobe(func_name, ctx_ptr) {
19+
printf("tid=%s%s%s %s ctx=%s%p%s\n", color_int(tid), tid, NC, func_name, color_int(ctx_ptr), ctx_ptr, NC);
20+
}
21+
macro print_uretprobe_void(func_name, ctx_ptr) {
22+
printf("tid=%s%s%s %s return\n", color_int(tid), tid, NC, func_name);
23+
}
24+
macro print_uretprobe(func_name, ctx_ptr) {
25+
printf("tid=%s%s%s %s -> ctx=%s%p%s\n", color_int(tid), tid, NC, func_name, color_int(ctx_ptr), ctx_ptr, NC);
26+
}
27+
428
// These are the official C API for enter/exiting a context, but is not called by the cpython
529
// internals. Something like uvloop might use this.
630
uprobe:/usr/lib/libpython3.13.so:PyContext_Enter {
7-
printf("%stid=%s%s %s %s\n", YELLOW, tid, NC, func, args);
31+
print_uprobe(func, arg0)
832
}
933

1034
uprobe:/usr/lib/libpython3.13.so:PyContext_Exit {
11-
printf("%stid=%s%s %s %s\n", YELLOW, tid, NC, func, args);
35+
print_uretprobe(func, arg0)
1236
}
1337

1438
// This is the C implementation of contextvars.run() python function
1539
// https://github.com/python/cpython/blob/v3.13.9/Python/context.c#L648-L650. This plus the
1640
// PyContext_Enter/PyContext_Exit is probably the most reliable.
1741
uprobe:/usr/lib/libpython3.13.so:context_run* {
18-
printf("%stid=%s%s %s %s\n", YELLOW, tid, NC, func, args);
42+
print_uprobe(func, arg0);
1943
}
2044
uretprobe:/usr/lib/libpython3.13.so:context_run* {
21-
printf("%stid=%s%s return %s\n", YELLOW, tid, NC, func);
45+
print_uretprobe_void(func, retval);
2246
}
2347

2448
// -----------
2549

2650
// These might be useful for tracking parentage of new Context objects
2751
uretprobe:/usr/lib/libpython3.13.so:PyContext_CopyCurrent {
28-
printf("%stid=%s%s %s retval = %p\n", YELLOW, tid, NC, func, retval);
52+
print_uretprobe(func, retval)
2953
}
3054
uprobe:/usr/lib/libpython3.13.so:PyContext_Copy {
31-
printf("%stid=%s%s %s %s\n", YELLOW, tid, NC, func, args);
55+
print_uprobe(func, arg0)
3256
}
3357
uretprobe:/usr/lib/libpython3.13.so:PyContext_Copy {
34-
printf("%stid=%s%s %s retval = %p\n", YELLOW, tid, NC, func, retval);
58+
print_uretprobe(func, retval)
3559
}
3660

3761
// These might be useful for watching the trace context for OTel python instrumented code
3862
/*
3963
uprobe:/usr/lib/libpython3.13.so:PyContextVar_Get {
40-
printf("%s %s\n", func, args);
64+
// ...
4165
}
4266
uprobe:/usr/lib/libpython3.13.so:PyContextVar_Set {
43-
printf("%s %s\n", func, args);
67+
// ...
4468
}
4569
*/
4670

@@ -51,9 +75,9 @@ uprobe:/usr/lib/libpython3.13.so:PyContextVar_Set {
5175
// See https://github.com/python/cpython/blob/v3.13.9/Python/context.c#L112-L113
5276
/*
5377
uprobe:/usr/lib/libpython3.13.so:_PyContext_Enter {
54-
printf("tid=%s %s %s\n", tid, func, args);
78+
// ...
5579
}
5680
uprobe:/usr/lib/libpython3.13.so:_PyContext_Exit {
57-
printf("tid=%s %s %s\n", tid, func, args);
81+
// ...
5882
}
5983
*/

0 commit comments

Comments
 (0)