From 4e6018e246df07db40861a980995c96e245ba6c9 Mon Sep 17 00:00:00 2001 From: yunwei37 <1067852565@qq.com> Date: Sat, 18 Feb 2023 18:09:49 +0800 Subject: [PATCH] add README and install.sh --- README.md | 30 ++++++++++++++++++++++++++++++ doc/talk.md | 13 +++++++++++++ install.sh | 4 ++++ main.py | 7 +------ prompts/1.md | 29 +++++++++++++++++++++++++++++ requirements.txt | 1 + 6 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 doc/talk.md create mode 100755 install.sh create mode 100644 prompts/1.md diff --git a/README.md b/README.md index 659fbf2..91d2e80 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,32 @@ # GPTtrace +## Usage + +```console +python main.py +usage: GPTtrace [-h] [-e | -r TEXT] [-u UUID] [-t ACCESS_TOKEN] + +Use ChatGPT to write eBPF programs (bpftrace, etc.) + +optional arguments: + -h, --help show this help message and exit + -e, --explain Let ChatGPT explain what's eBPF + -r TEXT, --run TEXT Generate commands using your input with ChatGPT, and run it + -u UUID, --uuid UUID Conversion UUID to use, or passed through environment variable `GPTTRACE_CONV_UUID` + -t ACCESS_TOKEN, --access-token ACCESS_TOKEN + ChatGPT access token, see `https://chat.openai.com/api/auth/session` or passed through + `GPTTRACE_ACCESS_TOKEN` +``` + +## 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 diff --git a/doc/talk.md b/doc/talk.md new file mode 100644 index 0000000..df4154e --- /dev/null +++ b/doc/talk.md @@ -0,0 +1,13 @@ +# talk + +当我们开发软件时,经常需要进行调试和追踪,以发现程序中的问题。而 eBPF 技术已经成为了一个越来越受欢迎的工具,它可以帮助我们更加高效地进行追踪和分析。 + +但是,eBPF 的开发需要一定的专业知识,对于一些不熟悉该技术的开发人员来说,可能会有一定的困难。这时,我们的 demo 工具 GPTtrace 就可以帮助你解决这个问题。 + +使用 GPTtrace 工具,你只需要用最自然的语言描述你想要追踪的程序,它就会自动为你生成和运行 eBPF 程序,让你更加高效地进行追踪和分析。而且,如果你不了解 eBPF 技术,也不用担心,GPTtrace 会帮助你解释它是什么以及如何使用。 + +除了自动追踪,GPTtrace 还有其他功能。通过 "-d bpftrace" 和 "-d eunomia" 参数,你可以深入了解 eBPF 的实现细节,并生成内核态代码,提高追踪的效率。而通过 "-o" 参数,你可以生成可执行文件,让你更加方便地运行你的追踪程序。 + +当然,需要注意的是,GPTtrace 只是一个 demo 工具,不能应用于生产环境。但是,它展示了 ChatGPT 和 eBPF 技术结合的巨大潜力,相信这种结合将在未来的软件开发和调试中发挥重要作用。 + +因此,如果你想更加高效地进行追踪和分析,GPTtrace 工具是一个值得尝试的工具。让我们期待 ChatGPT 在 eBPF 开发领域中的更多可能性! \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..599b260 --- /dev/null +++ b/install.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +sudo apt install bpftrace +pip install -r requirements.txt diff --git a/main.py b/main.py index b050360..fd2c603 100755 --- a/main.py +++ b/main.py @@ -11,7 +11,6 @@ from marko.inline import RawText ENV_UUID = "GPTTRACE_CONV_UUID" ENV_ACCESS_TOKEN = "GPTTRACE_ACCESS_TOKEN" - def main(): parser = argparse.ArgumentParser( prog='GPTtrace', @@ -50,9 +49,7 @@ def main(): print(f"Command to run: {parsed[0]}") os.system(parsed[0]) - - -def generate_result(bot: Chatbot, text: str, session: str | None, print_out: bool = False) -> str: +def generate_result(bot: Chatbot, text: str, session: str = None, print_out: bool = False) -> str: from io import StringIO prev_text = "" buf = StringIO() @@ -69,7 +66,6 @@ def generate_result(bot: Chatbot, text: str, session: str | None, print_out: boo print() return buf.getvalue() - def extract_code_blocks(text: str) -> List[str]: result = [] parser = Parser() @@ -80,6 +76,5 @@ def extract_code_blocks(text: str) -> List[str]: result.append(blk.children) return result - if __name__ == "__main__": main() diff --git a/prompts/1.md b/prompts/1.md new file mode 100644 index 0000000..78774d2 --- /dev/null +++ b/prompts/1.md @@ -0,0 +1,29 @@ +# Files opened by process +bpftrace -e 'tracepoint:syscalls:sys_enter_open { printf("%s %s\n", comm, str(args->filename)); }' + +# Syscall count by program +bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); }' + +# Read bytes by process: +bpftrace -e 'tracepoint:syscalls:sys_exit_read /args->ret/ { @[comm] = sum(args->ret); }' + +# Read size distribution by process: +bpftrace -e 'tracepoint:syscalls:sys_exit_read { @[comm] = hist(args->ret); }' + +# Show per-second syscall rates: +bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @ = count(); } interval:s:1 { print(@); clear(@); }' + +# Trace disk size by process +bpftrace -e 'tracepoint:block:block_rq_issue { printf("%d %s %d\n", pid, comm, args->bytes); }' + +# Count page faults by process +bpftrace -e 'software:faults:1 { @[comm] = count(); }' + +# Count LLC cache misses by process name and PID (uses PMCs): +bpftrace -e 'hardware:cache-misses:1000000 { @[comm, pid] = count(); }' + +# Profile user-level stacks at 99 Hertz, for PID 189: +bpftrace -e 'profile:hz:99 /pid == 189/ { @[ustack] = count(); }' + +# Files opened, for processes in the root cgroup-v2 +bpftrace -e 'tracepoint:syscalls:sys_enter_openat /cgroup == cgroupid("/sys/fs/cgroup/unified/mycg")/ { printf("%s\n", str(args->filename)); }' \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 9a37be4..911747d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -35,3 +35,4 @@ tomlkit==0.11.6 typing_extensions==4.5.0 urllib3==1.26.14 wrapt==1.14.1 +marko==1.3.0 \ No newline at end of file