{"id":13467705,"url":"https://github.com/tarun27sh/gdb_graphs","last_synced_at":"2025-03-26T03:30:57.233Z","repository":{"id":55399388,"uuid":"112034605","full_name":"tarun27sh/gdb_graphs","owner":"tarun27sh","description":"To visualize function call flow for a C/C++ program using gdb and python","archived":false,"fork":false,"pushed_at":"2024-05-10T12:39:35.000Z","size":8253,"stargazers_count":96,"open_issues_count":5,"forks_count":14,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-29T21:58:58.052Z","etag":null,"topics":["c","cpp","gdb","graphs","javascript","matplotlib-pyplot","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tarun27sh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-25T21:08:53.000Z","updated_at":"2024-09-16T16:14:48.000Z","dependencies_parsed_at":"2024-10-29T20:51:03.223Z","dependency_job_id":null,"html_url":"https://github.com/tarun27sh/gdb_graphs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarun27sh%2Fgdb_graphs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarun27sh%2Fgdb_graphs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarun27sh%2Fgdb_graphs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarun27sh%2Fgdb_graphs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tarun27sh","download_url":"https://codeload.github.com/tarun27sh/gdb_graphs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245584586,"owners_count":20639579,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cpp","gdb","graphs","javascript","matplotlib-pyplot","python"],"created_at":"2024-07-31T15:00:59.549Z","updated_at":"2025-03-26T03:30:56.387Z","avatar_url":"https://github.com/tarun27sh.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# gen_graph.py\n\n![Alt text](gallery/some_library_graph.png?raw=true \"\")\n\n\n## Description\nPython command line tool to visualize function-call-flow for a C/C++ program using graphviz dot object and matplotlib.\n    \n    Directory structure:\n\n    ├── gallery\n    │   ├── graph_GNU_MAKE_.png\n    │   ├── graph_QEMU.png\n    │   ├── graph_VIM.png\n    │   ├── some_library_graph.png\n    │   ├── test.png\n    │   └── test.svg\n    ├── gen_graph.py\n    ├── LICENSE\n    ├── README.md\n    ├── requiments.txt\n    └── test\n        ├── test.c\n        └── test.log\n\n## Motivation\nI wanted to view call-graph for OS source (written in C, C++) that connected APIs within one layer and APIs across layers.\nThis helps in getting a bigger picture of the source code and makes understanding large code bases QUICK.\n\n## Input\nRequires data from gdb to get nodes (functions) and edges (function 1 calling function 2).\n\n## Output\nDi-graph showing relationship between functions across software layers, the \"bigger picture\".\n\n\n# Usage \n\nThere are two parts to the whole process:\n1. get data by runnning process under gdb\n2. process this data with gen_graph.py\n\n\n## Part I: Get data from GDB\n\n### Test code    \n\n\t$  cat test.c\n\t#include\u003cstdio.h\u003e\n\t#include\u003cstdbool.h\u003e\n\t#include\u003cstdlib.h\u003e\n\t#include\u003ctime.h\u003e\n\t\n\t\n\tstatic void func9(void) { printf(\"leaf\\n\"); }\n\n\tvoid func8(void) { func9(); }\n\t\n\tvoid func7_1(bool is_true, int *p) { printf(\"leaf (is_true=%d, ptr=%p\\n\", is_true, p); }\n\tvoid func7(void) { func8(); }\n\t\n\tvoid func6(void) { func7(); }\n\t\n\tvoid func5_1(const char* str) { printf(\"leaf (%s)\\n\", str); }\n\tvoid func5(void) {\n\t    func5_1(\"graph me\\n\");\n\t    func6();\n\t}\n\t\n\tvoid func4(void) { func5(); }\n\tvoid func3_1(int a, int b, int c) { printf(\"leaf\\n\"); }\n\tvoid func3(void) {\n\t    func3_1(rand(), rand(), rand());\n\t    func4();\n\t}\n\t\n\tvoid func2_1(int a, int b) { printf(\"leaf (%d, %d)\\n\", a, b);}\n\tvoid func2(void) {\n\t    func2_1(rand(),rand());\n\t    func3();\n\t}\n\tvoid func1(void) { func2(); }\n\tint main()\n\t{\n\t    srand(time(NULL));\n\t    for(int i=0; i\u003c10; ++i) {\n\t            func1();\n\t    }\n\t        return 0;\n\t}\n\n### Enable debugging symbols    \n    $ gcc -g test.c\n\n### Start binary under GDB     \n    $ gdb a.out\n    . . .\n    Reading symbols from a.out...done.\n\n#### Insert breakpoints on functions of interest, or to put in every funtion in a file:    \n\n\t(gdb) rbreak test.c:.\n\tBreakpoint 1 at 0x400786: file test/test.c, line 33.\n\tvoid func1(void);\n\tBreakpoint 2 at 0x400760: file test/test.c, line 30.\n\tvoid func2(void);\n\tBreakpoint 3 at 0x40073d: file test/test.c, line 28.\n\tvoid func2_1(int, int);\n\tBreakpoint 4 at 0x400704: file test/test.c, line 24.\n\tvoid func3(void);\n\tBreakpoint 5 at 0x4006f0: file test/test.c, line 22.\n\tvoid func3_1(int, int, int);\n\tBreakpoint 6 at 0x4006d7: file test/test.c, line 21.\n\tvoid func4(void);\n\tBreakpoint 7 at 0x4006c1: file test/test.c, line 17.\n\tvoid func5(void);\n\tBreakpoint 8 at 0x4006a4: file test/test.c, line 15.\n\tvoid func5_1(const char *);\n\tBreakpoint 9 at 0x400690: file test/test.c, line 13.\n\tvoid func6(void);\n\tBreakpoint 10 at 0x400684: file test/test.c, line 11.\n\tvoid func7(void);\n\tBreakpoint 11 at 0x400664: file test/test.c, line 10.\n\tvoid func7_1(_Bool, int *);\n\tBreakpoint 12 at 0x40064b: file test/test.c, line 8.\n\tvoid func8(void);\n\tBreakpoint 13 at 0x400796: file test/test.c, line 36.\n\tint main();\n\tBreakpoint 14 at 0x40063a: file test/test.c, line 7.\n\tstatic void func9(void);\n\n#### Enable logging\n    (gdb) set pagination off\n    (gdb) set print pretty\n    (gdb) set logging file ./test.log\n    (gdb) set logging on\n    Copying output to ./test.log.\n\n#### Tell gdb to print backtrace and continue without asking when it hits  a breakpoint    \n    (gdb) command\n    Type commands for breakpoint(s) 1-10, one per line.\n    End with a line saying just \"end\".\n    \u003ebt\n    \u003ec\n    \u003eend\n\n\n#### Start  the program    \n    (gdb) r\n    Starting program: /home/vagrant/c_code/a.out\n\n    Once the program finishes, it would have dumped the logs to the disk in test.log    \n\n\n## Part II: run gen_graph.py on collected data\n\n### Dependencies    \n\n    sudo apt-get install graphviz\n    sudo python3 -m pip install -r requiments.txt\n\n\n### Run gen_graph.py    \n    $  python3 gen_graph.py\n    usage: gen_graph.py [-h] -i INPUT_FILE [-f {gdb,objdump}]\n    gen_graph.py: error: the following arguments are required: -i/--input_file\n\n    $  python3 gen_graph.py -i test/test.log\n\t01/02/2021 08:36:58 PM [1] processing gdb bt data\n\t01/02/2021 08:36:58 PM [2] adding nodes, edges, #ofnodes=13\n\t01/02/2021 08:36:59 PM [3] Embedding JS\n\t01/02/2021 08:36:59 PM [4] saving graph to:\n\t01/02/2021 08:36:59 PM       /home/vagrant/dwnlds/gdb_graphs/test.svg\n\t01/02/2021 08:36:59 PM Finished\n \n\nOpen the svg file in a browser\n![Alt text](gallery/test.png?raw=true \"\")\n\n\n## New \n- Interactive graphs:\n  Click on a node and entire parent and child chain is highlighted\n  (to reset -  reload page - untill I find a better way to do from JS)\n- C++ Support\n- Node tooltip shows function arguments (max 6)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarun27sh%2Fgdb_graphs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftarun27sh%2Fgdb_graphs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarun27sh%2Fgdb_graphs/lists"}