Browse Source

add generate picture and command

pull/3/head
yunwei37 2 years ago
parent
commit
402b75ada2
4 changed files with 20 additions and 63 deletions
  1. +1
    -1
      README.md
  2. BIN
      doc/generate.png
  3. +19
    -5
      main.py
  4. +0
    -57
      prompts/statsnoop.bt

+ 1
- 1
README.md View File

@@ -10,7 +10,7 @@ Generate eBPF programs and tracing with ChatGPT and natural language


- Generate eBPF programs with natural language - Generate eBPF programs with natural language


![generate](doc/generate.png)


## Usage ## Usage




BIN
doc/generate.png View File

Before After
Width: 1876  |  Height: 942  |  Size: 135 kB

+ 19
- 5
main.py View File

@@ -25,17 +25,13 @@ def main():
group.add_argument( group.add_argument(
"-e", "--execute", help="Generate commands using your input with ChatGPT, and run it", action="store", metavar="TEXT") "-e", "--execute", help="Generate commands using your input with ChatGPT, and run it", action="store", metavar="TEXT")
group.add_argument( group.add_argument(
"-g", "--execute", help="Generate eBPF programs using your input with ChatGPT", action="store", metavar="TEXT")
"-g", "--generate", help="Generate eBPF programs using your input with ChatGPT", action="store", metavar="TEXT")
parser.add_argument( parser.add_argument(
"-u", "--uuid", help=f"Conversion UUID to use, or passed through environment variable `{ENV_UUID}`") "-u", "--uuid", help=f"Conversion UUID to use, or passed through environment variable `{ENV_UUID}`")
parser.add_argument("-t", "--access-token", parser.add_argument("-t", "--access-token",
help=f"ChatGPT access token, see `https://chat.openai.com/api/auth/session` or passed through `{ENV_ACCESS_TOKEN}`") help=f"ChatGPT access token, see `https://chat.openai.com/api/auth/session` or passed through `{ENV_ACCESS_TOKEN}`")
args = parser.parse_args() args = parser.parse_args()


if (args.info or args.execute) is None:
parser.print_help()
return

access_token = args.access_token or os.environ.get(ENV_ACCESS_TOKEN, None) access_token = args.access_token or os.environ.get(ENV_ACCESS_TOKEN, None)
conv_uuid = args.uuid or os.environ.get(ENV_UUID, None) conv_uuid = args.uuid or os.environ.get(ENV_UUID, None)
if access_token is None: if access_token is None:
@@ -55,7 +51,25 @@ def main():
# print(f"Command to run: {parsed}") # print(f"Command to run: {parsed}")
print("Press Ctrl+C to stop the program....") print("Press Ctrl+C to stop the program....")
os.system("sudo " + parsed) os.system("sudo " + parsed)
elif args.generate is not None:
desc: str = args.generate
print("Sending query to ChatGPT: " + desc)
ret_val = generate_result(
chatbot, construct_generate_prompt(desc), conv_uuid, True)
# print(ret_val)
parsed = extract_code_blocks(ret_val)
# print(f"Command to run: {parsed}")
with open("generated.bpf.c", "w") as f:
for code in parsed:
f.write(code)
else:
parser.print_help()


def construct_generate_prompt(text: str) -> str:
return f'''You are now a translater from human language to {os.uname()[0]} eBPF programs.
Please write eBPF programs for me.
No explanation required, no instruction required, don't tell me how to compile and run.
What I want is a eBPF program for: {text}.'''


def construct_running_prompt(text: str) -> str: def construct_running_prompt(text: str) -> str:
return f'''You are now a translater from human language to {os.uname()[0]} shell bpftrace command. return f'''You are now a translater from human language to {os.uname()[0]} shell bpftrace command.


+ 0
- 57
prompts/statsnoop.bt View File

@@ -1,57 +0,0 @@
#!/usr/bin/env bpftrace
/*
* statsnoop Trace stat() syscalls.
* For Linux, uses bpftrace and eBPF.
*
* This traces the tracepoints for statfs(), statx(), newstat(), and
* newlstat(). These aren't the only the stat syscalls: if you are missing
* activity, you may need to add more variants.
*
* Also a basic example of bpftrace.
*
* USAGE: statsnoop.bt
*
* This is a bpftrace version of the bcc tool of the same name.
*
* Copyright 2018 Netflix, Inc.
* Licensed under the Apache License, Version 2.0 (the "License")
*
* 08-Sep-2018 Brendan Gregg Created this.
*/

BEGIN
{
printf("Tracing stat syscalls... Hit Ctrl-C to end.\n");
printf("%-6s %-16s %3s %s\n", "PID", "COMM", "ERR", "PATH");
}

tracepoint:syscalls:sys_enter_statfs
{
@filename[tid] = args->pathname;
}

tracepoint:syscalls:sys_enter_statx,
tracepoint:syscalls:sys_enter_newstat,
tracepoint:syscalls:sys_enter_newlstat
{
@filename[tid] = args->filename;
}

tracepoint:syscalls:sys_exit_statfs,
tracepoint:syscalls:sys_exit_statx,
tracepoint:syscalls:sys_exit_newstat,
tracepoint:syscalls:sys_exit_newlstat
/@filename[tid]/
{
$ret = args->ret;
$errno = $ret >= 0 ? 0 : - $ret;

printf("%-6d %-16s %3d %s\n", pid, comm, $errno,
str(@filename[tid]));
delete(@filename[tid]);
}

END
{
clear(@filename);
}

Loading…
Cancel
Save