Last active
July 24, 2024 23:29
-
-
Save whexy/257ec2e9e90dcfdfebdfee11a5c8731d to your computer and use it in GitHub Desktop.
Generate Run-time Call Graph with LLVM SanCov
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdint.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <sanitizer/coverage_interface.h> | |
| static FILE* fp; | |
| void __attribute__((constructor)) trace_begin(void) | |
| { | |
| fp = fopen("/tmp/callgraph.log", "w"); | |
| if (!fp) { | |
| perror("fopen"); | |
| exit(EXIT_FAILURE); | |
| } | |
| } | |
| void __attribute__((destructor)) trace_end(void) | |
| { | |
| if (fp) { | |
| fclose(fp); | |
| } | |
| } | |
| void __sanitizer_cov_trace_pc_guard_init(uint32_t* start, uint32_t* stop) {} | |
| void __sanitizer_cov_trace_pc_guard(uint32_t* guard) | |
| { | |
| void* PC = __builtin_return_address(0); | |
| char Callee[1024]; | |
| __sanitizer_symbolize_pc(PC, "%f", Callee, sizeof(Callee)); | |
| char Caller[1024]; | |
| PC = __builtin_return_address(1); | |
| __sanitizer_symbolize_pc(PC, "%f", Caller, sizeof(Caller)); | |
| fprintf(fp, "%s -> %s\n", Caller, Callee); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile the runtime:
Compile the target (
example.c) with necessary LLVM instrumentations:Link the target with the runtime, enable at least one sanitizer for symbolization: