Skip to content

Instantly share code, notes, and snippets.

@hemslo
Last active November 6, 2022 15:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hemslo/53e9a0b43f84bc37f65edea1bf4e558d to your computer and use it in GitHub Desktop.
Implement racket/trace Like Observability Using eBPF https://hemslo.io/implement-racket-trace-like-observability-using-ebpf/
#!/usr/bin/env python3
import sys
import json
def main():
result = []
for line in sys.stdin:
if line:
data = json.loads(line.strip())
if data['type'] == 'printf':
type, value, timestamp, pid, tid = data['data'].strip().split(',')
args = { 'arg0': int(value) } if type == '>' else { 'ret': int(value) }
result.append({
'ph': 'B' if type == '>' else 'E',
'name': f'fib({value})' if type == '>' else '',
'ts': int(timestamp),
'pid': int(pid),
'tid': int(tid),
'args': args,
})
print(json.dumps(result))
if __name__ == '__main__':
main()
#!/usr/bin/env bpftrace
uprobe:./fib:fib {
printf(">,%d,%d,%d,%d\n", arg0, elapsed / 1000, pid, tid);
}
uretprobe:./fib:fib {
printf("<,%d,%d,%d,%d\n", retval, elapsed / 1000, pid, tid);
}
#include <stdio.h>
int fib(int n)
{
return n < 2 ? n : fib(n - 1) + fib(n - 2);
}
void main(void)
{
int n = 4;
int res = fib(n);
printf("fib(%d) = %d\n", n, res);
}
package main
import (
"fmt"
)
func fib(n int) int {
if n < 2 {
return n
}
return fib(n - 1) + fib(n - 2)
}
func main() {
n := 4
res := fib(n)
fmt.Printf("fib(%d) = %d\n", n, res)
}
[{"ph": "B", "name": "fib(4)", "ts": 4292644, "pid": 3837, "tid": 3837, "args": {"arg0": 4}}, {"ph": "B", "name": "fib(3)", "ts": 4292918, "pid": 3837, "tid": 3837, "args": {"arg0": 3}}, {"ph": "B", "name": "fib(2)", "ts": 4294835, "pid": 3837, "tid": 3837, "args": {"arg0": 2}}, {"ph": "B", "name": "fib(1)", "ts": 4295346, "pid": 3837, "tid": 3837, "args": {"arg0": 1}}, {"ph": "E", "name": "", "ts": 4295570, "pid": 3837, "tid": 3837, "args": {"ret": 1}}, {"ph": "B", "name": "fib(0)", "ts": 4295779, "pid": 3837, "tid": 3837, "args": {"arg0": 0}}, {"ph": "E", "name": "", "ts": 4295998, "pid": 3837, "tid": 3837, "args": {"ret": 0}}, {"ph": "E", "name": "", "ts": 4296198, "pid": 3837, "tid": 3837, "args": {"ret": 1}}, {"ph": "B", "name": "fib(1)", "ts": 4296381, "pid": 3837, "tid": 3837, "args": {"arg0": 1}}, {"ph": "E", "name": "", "ts": 4296637, "pid": 3837, "tid": 3837, "args": {"ret": 1}}, {"ph": "E", "name": "", "ts": 4296835, "pid": 3837, "tid": 3837, "args": {"ret": 2}}, {"ph": "B", "name": "fib(2)", "ts": 4296996, "pid": 3837, "tid": 3837, "args": {"arg0": 2}}, {"ph": "B", "name": "fib(1)", "ts": 4297213, "pid": 3837, "tid": 3837, "args": {"arg0": 1}}, {"ph": "E", "name": "", "ts": 4297470, "pid": 3837, "tid": 3837, "args": {"ret": 1}}, {"ph": "B", "name": "fib(0)", "ts": 4297632, "pid": 3837, "tid": 3837, "args": {"arg0": 0}}, {"ph": "E", "name": "", "ts": 4297887, "pid": 3837, "tid": 3837, "args": {"ret": 0}}, {"ph": "E", "name": "", "ts": 4298044, "pid": 3837, "tid": 3837, "args": {"ret": 1}}, {"ph": "E", "name": "", "ts": 4298331, "pid": 3837, "tid": 3837, "args": {"ret": 3}}]
{"type": "attached_probes", "data": {"probes": 2}}
{"type": "printf", "data": ">,4,4292644,3837,3837\n"}
{"type": "printf", "data": ">,3,4292918,3837,3837\n"}
{"type": "printf", "data": ">,2,4294835,3837,3837\n"}
{"type": "printf", "data": ">,1,4295346,3837,3837\n"}
{"type": "printf", "data": "<,1,4295570,3837,3837\n"}
{"type": "printf", "data": ">,0,4295779,3837,3837\n"}
{"type": "printf", "data": "<,0,4295998,3837,3837\n"}
{"type": "printf", "data": "<,1,4296198,3837,3837\n"}
{"type": "printf", "data": ">,1,4296381,3837,3837\n"}
{"type": "printf", "data": "<,1,4296637,3837,3837\n"}
{"type": "printf", "data": "<,2,4296835,3837,3837\n"}
{"type": "printf", "data": ">,2,4296996,3837,3837\n"}
{"type": "printf", "data": ">,1,4297213,3837,3837\n"}
{"type": "printf", "data": "<,1,4297470,3837,3837\n"}
{"type": "printf", "data": ">,0,4297632,3837,3837\n"}
{"type": "printf", "data": "<,0,4297887,3837,3837\n"}
{"type": "printf", "data": "<,1,4298044,3837,3837\n"}
{"type": "printf", "data": "<,3,4298331,3837,3837\n"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment