July 28th, 2024

Logging C function calls with cosmopolitan Libc (2022)

The Cosmopolitan Libc runtime's --ftrace flag logs C function calls, aiding debugging by tracing execution history and identifying issues like NULL pointers, while also supporting --strace for system call logging.

Read original articleLink Icon
Logging C function calls with cosmopolitan Libc (2022)

The Cosmopolitan Libc runtime features a --ftrace flag that logs C function calls, providing a straightforward debugging tool. This feature is particularly useful for diagnosing issues like NULL function pointers, which can confuse traditional debuggers. By reviewing the function call logs, developers can trace back the execution history to identify the source of the problem. The logs include details such as process ID, elapsed time in nanoseconds, stack usage, and function names, which help pinpoint latency issues that are often overlooked by other tools.

The logging mechanism operates at the object code level, meaning inlined functions may not appear in the trace, and repeated calls to pure functions are consolidated into a single log entry. The implementation of the --ftrace flag involves modifying the program's memory to insert profiling hooks, allowing for efficient function call tracking with minimal overhead.

Additionally, Cosmopolitan Libc supports a --strace flag for system call logging, which can be beneficial for understanding program behavior during execution. The integration of ftrace into various programming languages, including Python, enhances its utility for performance testing and debugging. Overall, the logging capabilities of Cosmopolitan Libc provide developers with valuable insights into function calls and system interactions, facilitating more effective debugging and performance optimization.

Link Icon 4 comments
By @rwmj - 6 months
Note you can use 'ltrace' with any Linux shared library to trace library calls. https://www.ltrace.org/ https://en.wikipedia.org/wiki/Ltrace
By @a2code - 6 months
> I found no evidence of someone having invented it before.

Most Lisp/Scheme implementations offer tracing. Amazingly, the SICP book even shows tracing as a means of analyzing whether a recursive function is tail-recursive.

What I'm curious about is the Lisp history of tracing. Does anyone know which implementations were the first to provide tracing functionality?