mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
84 lines
1.8 KiB
Plaintext
84 lines
1.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "28cf2e27-41e2-461c-a39c-147417141a4e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from pythonbpf import bpf, section, bpfglobal, BPF, trace_fields\n",
|
|
"from ctypes import c_void_p, c_int64"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "133190e5-5a99-4585-b6e1-91224ed973c2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"@bpf\n",
|
|
"@section(\"tracepoint/syscalls/sys_enter_clone\")\n",
|
|
"def hello_world(ctx: c_void_p) -> c_int64:\n",
|
|
" print(\"Hello, World!\")\n",
|
|
" return 0\n",
|
|
"\n",
|
|
"\n",
|
|
"@bpf\n",
|
|
"@bpfglobal\n",
|
|
"def LICENSE() -> str:\n",
|
|
" return \"GPL\"\n",
|
|
"\n",
|
|
"\n",
|
|
"# Compile and load\n",
|
|
"b = BPF()\n",
|
|
"b.load()\n",
|
|
"b.attach_all()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "d3934efb-4043-4545-ae4c-c50ec40a24fd",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# header\n",
|
|
"print(f\"{'TIME(s)':<18} {'COMM':<16} {'PID':<6} {'MESSAGE'}\")\n",
|
|
"\n",
|
|
"# format output\n",
|
|
"while True:\n",
|
|
" try:\n",
|
|
" (task, pid, cpu, flags, ts, msg) = trace_fields()\n",
|
|
" except ValueError:\n",
|
|
" continue\n",
|
|
" except KeyboardInterrupt:\n",
|
|
" exit()\n",
|
|
" print(f\"{ts:<18} {task:<16} {pid:<6} {msg}\")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.13.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|