Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. int square(int x) { return x*x; }
  3. int main(void) {
  4. square(2);
  5. }
  6.  
  7. $ gcc -O0 --coverage square.c
  8. $ ./a.out
  9. $ gcov -i square.c
  10. $ awk -F '[,:]' '$1 == "function" && $3 > 0 {print $3, $4}' square.c.gcov
  11. 1 square
  12. 1 main
  13.  
  14. $ gcc -O0 -pg square.c
  15. $ ./a.out
  16. $ gprof -b -P
  17. Call graph
  18.  
  19.  
  20. granularity: each sample hit covers 2 byte(s) no time propagated
  21.  
  22. index % time self children called name
  23. 0.00 0.00 1/1 main [7]
  24. [1] 0.0 0.00 0.00 1 square [1]
  25. -----------------------------------------------
  26.  
  27. Index by function name
  28.  
  29. [1] square
  30.  
  31. #define _GNU_SOURCE
  32. #include <dlfcn.h>
  33.  
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36.  
  37. #define TRACE_FD 3
  38.  
  39. void __cyg_profile_func_enter (void *, void *)
  40. __attribute__((no_instrument_function));
  41.  
  42. void __cyg_profile_func_enter (void *func, void *caller)
  43. {
  44. static FILE* trace = NULL;
  45. Dl_info info;
  46.  
  47. if (trace == NULL) {
  48. trace = fdopen(TRACE_FD, "w");
  49. if (trace == NULL) abort();
  50. setbuf(trace, NULL);
  51. }
  52. if (dladdr(func, &info))
  53. fprintf (trace, "%p [%s] %sn",
  54. func,
  55. info.dli_fname ? info.dli_fname : "?",
  56. info.dli_sname ? info.dli_sname : "?");
  57. }
  58.  
  59. $ gcc -O0 -rdynamic -finstrument-functions square.c instrument.c -ldl
  60. $ ./a.out 3>&1
  61. 0x400a8f [./a.out] main
  62. 0x400a4f [./a.out] square
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement