Last active
October 1, 2023 19:33
-
-
Save AhmedSamara/e7136044e8e6946450f9b92b8099a9cd to your computer and use it in GitHub Desktop.
Python trace calls.
This file contains 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
// gcc -finstrument-functions your_file.c -o your_program | |
void __attribute__((no_instrument_function)) __cyg_profile_func_enter(void *this_fn, void *call_site) { | |
printf("Entering %p\n", this_fn); | |
} | |
void __attribute__((no_instrument_function)) __cyg_profile_func_exit(void *this_fn, void *call_site) { | |
printf("Exiting %p\n", this_fn); | |
} | |
void my_function() { | |
// Your code | |
} | |
int main() { | |
my_function(); | |
return 0; | |
} |
This file contains 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
import sys | |
import os | |
def trace_calls(frame, event, arg): | |
if event != "call": | |
return | |
module_filename = frame.f_globals.get("__file__") | |
if module_filename: | |
if "site-packages" not in module_filename and "lib" not in module_filename: | |
print(f"{module_filename}: Function {frame.f_code.co_name} called from {frame.f_back.f_code.co_name}") | |
def main(): | |
print("Hello, World!") | |
other_function() | |
def other_function(): | |
print("This is another function.") | |
if __name__ == "__main__": | |
sys.settrace(trace_calls) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment