Call Stack Management
In order to obtain the traceback in run time, we need to actively manage the call stack. Unfortunately, C doesn’t provide an easy way to do this. Therefore, we will have to wrap function calls or code blocks with a macro. While this approach can be quite verbose, it is what makes our library possible.
API
CTB_WRAP Macro
Wrapper macro for expression to automatically manage call stack frames.
CTB_WRAP(expr)Parameters
| Name | Type | Description |
|---|---|---|
expr | N/A | The expression to be wrapped. |
Expands to
do \
{ \
ctb_push_call_stack_frame(__FILE__, __func__, __LINE__, #expr); \
(expr); \
ctb_pop_call_stack_frame(__FILE__, __func__, __LINE__, #expr); \
} while (0)Usage
int function_a(void)
{
CTB_WRAP(function_b()); // Wraps a function call
}
int function_b(void)
{
int i = 0;
CTB_WRAP(i = 2); // Wraps an assignment
}CTB_Block Macro
Wrapper macro for a code block to automatically manage call stack frames.
CTB_BLOCK(...)Parameters
| Name | Type | Description |
|---|---|---|
... | N/A | The block of code to be wrapped. |
Expands to
do \
{ \
ctb_push_call_stack_frame(__FILE__, __func__, __LINE__, #__VA_ARGS__); \
__VA_ARGS__ \
ctb_pop_call_stack_frame(__FILE__, __func__, __LINE__, #__VA_ARGS__); \
} while (0)Usage
int function_a(void)
{
/* Wraps a code block */
CTB_BLOCK(
int i = 0;
if (i == 0)
{
printf("%d", i);
}
); // <-- Don't forget the semi-colon
// Note: i is not accessible here
// printf("%d", i); <-- Illegal!
}Last updated on