eunomia-bpf / GPTtrace

Generate eBPF programs and tracing with ChatGPT
https://eunomia.dev/GPTtrace/
MIT License
217 stars 21 forks source link

Retrieve examples from vectordb to construct a few-shot prompt. #13

Closed try-agaaain closed 1 year ago

try-agaaain commented 1 year ago

Build a vector database based on the examples in the tools folder, and then retrieve examples relevant to user requests from the database and add them to the prompt.

As an example, the prompt constructed for the request "Trace allocations and display each individual allocator function call" is as follows:

Prompt: 
    As a supportive assistant to a Linux system administrator,
    your role involves leveraging bpftrace to generate eBPF code that aids
    in problem-solving, as well as responding to queries.
    Note that you may not always need to call the bpftrace tool function.
    Here are some pertinent examples that align with the user's requests:

    example: Write a BPF code that traces the kernel OOM killer and prints basic details, including the system load averages, providing context on the system state at the time of the OOM.

\```
#!/usr/bin/env bpftrace

#ifndef BPFTRACE_HAVE_BTF
#include <linux/oom.h>
#endif

BEGIN
{
        printf("Tracing oom_kill_process()... Hit Ctrl-C to end.\n");
}

kprobe:oom_kill_process
{
        $oc = (struct oom_control *)arg0;
        time("%H:%M:%S ");
        printf("Triggered by PID %d (\"%s\"), ", pid, comm);
        printf("OOM kill of PID %d (\"%s\"), %d pages, loadavg: ",
            $oc->chosen->pid, $oc->chosen->comm, $oc->totalpages);
        cat("/proc/loadavg");
}

\```
    ...
    Now, you have received the following request from a user: Trace allocations and display each individual allocator function call
    Please utilize your capabilities to the fullest extent to accomplish this task.

However, GPT will imitate these examples to generate multi-line code for BPFtrace programs, similar to:

sudo bpftrace -e #!/usr/bin/env bpftrace

BEGIN
{
  printf("Tracing allocator functions... Hit Ctrl-C to end.\n");
}

uprobe:/usr/lib/libc.so.6:malloc
{
  printf("%s allocated %lu bytes\n", comm, arg1);
}

uprobe:/usr/lib/libc.so.6:calloc
{
  printf("%s allocated %lu bytes\n", comm, (arg1 * arg2));
}

uprobe:/usr/lib/libc.so.6:realloc
{
  printf("%s reallocated %lu bytes\n", comm, arg2);
}

uprobe:/usr/lib/libc.so.6:free
{
  printf("%s freed memory\n", comm);
}

These multiple lines of code should be saved into a file (e.g., trace.bt) and then executed using sudo bpftrace trace.bt.

Alternatively, we can have bpftrace read the bpftrace program from standard input:

cat <<EOF | sudo bpftrace -
program generated by gpt
EOF