• Stars
    star
    152
  • Rank 243,173 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created over 1 year ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate eBPF programs and tracing with ChatGPT and natural language

GPTtrace 🤖

License: MIT Actions Status DeepSource CodeFactor

An experiment for generating eBPF programs and tracing with GPT and natural language

Key Features 💡

Interact and Tracing your Linux with natural language

example: tracing with Count page faults by process

Image

  • start tracing with natural language
  • let AI explain the result to you

Generate eBPF programs with natural language

example: Write an eBPF program Print entered bash commands from all running shells, save the bpf program to a file and exit without actual run it.

Image

We use examples from bpftrace tools to create vector store and search.

For more detail documents and tutorials about how to write eBPF programs, please refer to: bpf-developer-tutorial (a libbpf tool tutorial to teach ChatGPT to write eBPF programs)

Choose the right bcc command line tool to complete the tracking task

Use the right bcc tools to trace the kernel

$ python3 gpttrace "Trace allocations and display each individual allocator function call"
 Run:  sudo memleak-bpfcc --trace 
Attaching to kernel allocators, Ctrl+C to quit.
(b'Relay(35)', 402, 6, b'd...1', 20299.252425, b'alloc exited, size = 4096, result = ffff8881009cc000')
(b'Relay(35)', 402, 6, b'd...1', 20299.252425, b'free entered, address = ffff8881009cc000, size = 4096')
(b'Relay(35)', 402, 6, b'd...1', 20299.252426, b'free entered, address = 588a6f, size = 4096')
(b'Relay(35)', 402, 6, b'd...1', 20299.252427, b'alloc entered, size = 4096')
(b'Relay(35)', 402, 6, b'd...1', 20299.252427, b'alloc exited, size = 4096, result = ffff8881009cc000')
(b'Relay(35)', 402, 6, b'd...1', 20299.252428, b'free entered, address = ffff8881009cc000, size = 4096')
(b'sudo', 6938, 10, b'd...1', 20299.252437, b'alloc entered, size = 2048')
(b'sudo', 6938, 10, b'd...1', 20299.252439, b'alloc exited, size = 2048, result = ffff88822e845800')
(b'node', 410, 18, b'd...1', 20299.252455, b'alloc entered, size = 256')
(b'node', 410, 18, b'd...1', 20299.252457, b'alloc exited, size = 256, result = ffff8882e9b66400')
(b'node', 410, 18, b'd...1', 20299.252458, b'alloc entered, size = 2048')

How it works

GPTtrace/doc/how-it-works.png

  1. User Input: The user provides their operating system information and kernel version. This information is crucial as it helps to tailor the eBPF program to the specific environment of the user.

  2. Prompt Construction: The user's input, along with the OS info and kernel version, is used to construct a prompt. This prompt is designed to guide the generation of the eBPF program.

  3. Vector Database Query: The constructed prompt is used to query the Vector Database for eBPF program examples. These examples serve as a basis for generating the eBPF program that will be inserted into the kernel.

  4. Hook Point Identification: The GPT API is used to identify potential hook points in the eBPF program. These hook points are locations in the code where the eBPF program can be inserted to monitor or modify the behavior of the kernel.

  5. eBPF Program Generation: The identified hook points, along with the examples from the Vector Database, are used to generate the eBPF program. This program is designed to be inserted into the kernel to perform the desired tracing tasks.

  6. Kernel Insertion: The generated eBPF program is inserted into the kernel. If there are any errors during this process, the tool will retry the steps from querying the Vector Database to kernel insertion a few times.

  7. Result Explanation: Once the eBPF program is successfully inserted into the kernel, the AI will explain the result to the user. This includes an explanation of what the eBPF program is doing and how it is interacting with the kernel.

This process ensures that the eBPF program is tailored to the user's specific environment and needs, and that the user understands how the program works and what it is doing.

Installation 🔧

pip install gpttrace

Usage and Setup 🛠

$ python3 -m gpttrace -h
usage: GPTtrace [-h] [-c CMD_NAME QUERY] [-v] [-k OPENAI_API_KEY]
                input_string

Use ChatGPT to write eBPF programs (bpftrace, etc.)

positional arguments:
  input_string          Your question or request for a bpf program

options:
  -h, --help            show this help message and exit
  -c CMD_NAME QUERY, --cmd CMD_NAME QUERY
                        Use the bcc tool to complete the trace task
  -v, --verbose         Show more details
  -k OPENAI_API_KEY, --key OPENAI_API_KEY
                        Openai api key, see
                        `https://platform.openai.com/docs/quickstart/add-
                        your-api-key` or passed through `OPENAI_API_KEY`

First: login to ChatGPT

start your tracing! 🚀

For example:

python3 gpttrace "Count page faults by process"

If the eBPF program cannot be loaded into the kernel, The error message will be used to correct ChatGPT, and the result will be printed to the console.

Examples

  • Files opened by process
  • Syscall count by program
  • Read bytes by process:
  • Read size distribution by process:
  • Show per-second syscall rates:
  • Trace disk size by process
  • Count page faults by process
  • Count LLC cache misses by process name and PID (uses PMCs):
  • Profile user-level stacks at 99 Hertz, for PID 189:
  • Files opened, for processes in the root cgroup-v2

LICENSE

MIT

🔗 Links

More Repositories

1

bpf-developer-tutorial

Learn eBPF by examples | eBPF 开发者教程与知识库:通过小工具和示例一步步学习 eBPF,包含性能、网络、安全等多种应用场景
C
597
star
2

eunomia-bpf

Build, Distribute and Run CO-RE eBPF programs easier with JSON and Webassembly OCI images
Rust
281
star
3

wasm-bpf

WebAssembly library, toolchain and runtime for eBPF programs
Rust
187
star
4

awesome-ebpf-zh

与 eBPF 相关的精选项目的中文清单
14
star
5

GPTtrace-web

Generate bpftrace eBPF programs online with GPT or LLM
TypeScript
13
star
6

chatrepo

A Github App to chat with Your GitHub Repo's Issues Using ChatGPT
JavaScript
11
star
7

func-call-for-command

Generate GPT function call API from command-line help info
Python
10
star
8

ebpf-knowledge-base

An ebpf knowledge base, based on llama_index and bpf-developer-tutorial
Rust
8
star
9

eunomia-exporter

A simple OpenTelemetry collector for custom eBPF metrics with Wasm
Rust
7
star
10

btf2wit

Convert BTF types to wit format
Rust
7
star
11

eunomia-cc

Compile and Run eBPF with WASM (Development move to https://github.com/eunomia-bpf/eunomia-bpf)
Rust
7
star
12

linux-trace-ai-agent

An experiment AI agent for automatic monitoring of system performance.
Python
6
star
13

c-struct-bindgen

Generate C structs serialization-free bindings and marshal / unmarshal functions for JSON (Can be used for pass structs between Wasm and eBPF/host)
C++
5
star
14

ebpm

an eBPF package manager demo (see LMP project)
C
5
star
15

docsgpt-backend

Python
4
star
16

eunomia-bpf.github.io

documents
4
star
17

eunomia-template

Compile eBPF program with GitHub Actions and Run locally in one line!
C
4
star
18

Clickhouse-Uring

C
4
star
19

libbpf-rs-starter-template

Template designed to get new developers with libbpf-rs development.
C
4
star
20

wasm-bpf-rs

A WebAssembly eBPF runtime based on wasmtime in rust
Rust
3
star
21

add-table-export

A tool to add an export of the table defined in the wasm module
Rust
3
star
22

code-survey

Do we really know how system works? Let’s do code-survey with AI
Python
3
star
23

eunomia.dev

The eunomia-bpf homepage
HTML
2
star
24

ai-func-call-gen

TypeScript
2
star
25

c-rust-component-test

Demonstration on linking components written in different languages together
Rust
2
star
26

Nginx-eBPF-module

Use userspace eBPF runtime as nginx plugin
C
2
star
27

libbpf-starter-template

Template designed to get new developers with libbpf development.
C
2
star
28

wasmtime-test

Rust
1
star
29

inline-hook-demo

A simple example of how to implement an Inline Hook
C
1
star
30

ebpf-wasm-go-sdk

1
star
31

.github

project template
1
star
32

cilium-ebpf-starter-template

Template designed to get new developers with cilium-ebpf development.
C
1
star
33

ebpf-wasm-rust-sdk

1
star
34

ebpf-wasm-cpp-sdk

C
1
star
35

eunomia-repo-frondend

the frontend website of eBPF hub programs
Ruby
1
star
36

ChatGPT-plugin-bpf

ChatGPT-plugin-eBPF
Python
1
star