Skip to content

Instantly share code, notes, and snippets.

@ssrlive
Created June 19, 2020 14:08
Show Gist options
  • Save ssrlive/de14e5528c428299bb6d282d667d1344 to your computer and use it in GitHub Desktop.
Save ssrlive/de14e5528c428299bb6d282d667d1344 to your computer and use it in GitHub Desktop.
call stack dump
#include <string.h>
#include <stdio.h>
#include "callstack.h"
#define FUNC_COUNT 100
#define FUNC_NAME_LEN_MAX 256
static char func_name_array[FUNC_COUNT][FUNC_NAME_LEN_MAX] = { { 0 } };
void call_stack_push_func_name(const char *func_name) {
int iter;
for (iter = FUNC_COUNT - 1; iter > 0; --iter) {
if ( strlen(func_name_array[iter - 1]) ) {
strncpy(func_name_array[iter], func_name_array[iter - 1], FUNC_NAME_LEN_MAX);
}
}
strncpy(func_name_array[0], func_name, FUNC_NAME_LEN_MAX);
}
void call_stack_dump_funcs(void) {
int iter;
for (iter = 0; iter < FUNC_COUNT; ++iter) {
if (strlen(func_name_array[iter]) == 0) {
break;
}
printf("%s\n", func_name_array[iter]);
}
}
#ifndef __CALL_STACK_H__
#define __CALL_STACK_H__ 1
void call_stack_push_func_name(const char* func_name);
void call_stack_dump_funcs(void);
#if defined(__PRINT_INFO__)
#define CALL_STACK_PUSH_FUNC_NAME() call_stack_push_func_name(__FUNCTION__)
#define CALL_STACK_DUMP_FUNCS() call_stack_dump_funcs()
#else
#define CALL_STACK_PUSH_FUNC_NAME() do { (void)0; } while (0)
#define CALL_STACK_DUMP_FUNCS() do { (void)0; } while (0)
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment