Skip to content

Debugging Skills

Trace Function Enter and Exit

Use __cyg_profile_func_enter and __cyg_profile_func_exit to trace entering and exiting a function.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// dump function enter and exit

/* https://blog.csdn.net/jasonchen_gbd/article/details/44044899
$ gcc -L/usr/lib/x86_64-linux-gnu -finstrument-functions main.c -o main
$ ./main
*/

#include <stdio.h>

#define DUMP(func, call) \
  printf("%s: func = %p, called by = %p\n", __FUNCTION__, func, call)

void __attribute__((no_instrument_function))
__cyg_profile_func_enter(void *this_func, void *call_site)
{
  DUMP(this_func, call_site);
}

void __attribute__((no_instrument_function))
__cyg_profile_func_exit(void *this_func, void *call_site)
{
  DUMP(this_func, call_site);
}

int do_multi(int a, int b)
{
  return a * b;
}

int do_calc(int a, int b)
{
  return do_multi(a, b);
}

int main(void)
{
  int a = 4, b = 5;
  printf("result: %d\n", do_calc(a, b));

  // TIP: print function point in the code (both main and &main are OK) if no add2line from toolchain
  printf("The address of main:               %p %p\n", main,     &main);
  printf("The address of do_calc  function:  %p %p\n", do_calc,  &do_calc);
  printf("The address of do_multi function:  %p %p\n", do_multi, &do_multi);

  return 0;
}

Result:

$ ./main
__cyg_profile_func_enter: func = 0x56206e5437b2, called by = 0x7fa8fc459bf7
__cyg_profile_func_enter: func = 0x56206e543763, called by = 0x56206e5437eb
__cyg_profile_func_enter: func = 0x56206e54371c, called by = 0x56206e543794
__cyg_profile_func_exit: func = 0x56206e54371c, called by = 0x56206e543794
__cyg_profile_func_exit: func = 0x56206e543763, called by = 0x56206e5437eb
result: 20
The address of main:               0x56206e5437b2 0x56206e5437b2
The address of do_calc  function:  0x56206e543763 0x56206e543763
The address of do_multi function:  0x56206e54371c 0x56206e54371c
__cyg_profile_func_exit: func = 0x56206e5437b2, called by = 0x7fa8fc459bf7

Debug Macro

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// https://www.codenong.com/cs106497648/
// https://stackoverflow.com/questions/1644868/define-macro-for-debug-printing-in-c

// an example of using debug MACRO

#include <stdio.h>
#include <time.h>

#define DEBUG        /* comment this to disable Debug Message */

// #line 206         /* Uncomment this to redeinfe LINE number */

#ifdef DEBUG
#define debug_print(fmt, ...) \
        do { fprintf(stderr, "[%lu | %s:%d:%s()] " fmt, time(0), __FILE__, __LINE__, __func__, __VA_ARGS__); } while (0)
#else
#define debug_print(fmt, ...)
#endif

void silly_function(void)
{
  debug_print("%s\n", "Here I am");
}

int main(void)
{
  debug_print("%s\n", "Here I am");
  silly_function();

  return 0;
}

Result:

[1627893524 | main.c:27:main()] Here I am
[1627893524 | main.c:22:silly_function()] Here I am

Deassemable

~~ TBD ~~