heap_symShow.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <symLib.h>
  5. #include <sysSymTbl.h>
  6. #include <taskLib.h>
  7. #include "../private.h"
  8. #include "../context.h"
  9. /*--------------------------------------------------------- */
  10. /* VxWorks version of this doesn't use the program_name */
  11. void lub_heap_init(const char *program_name)
  12. {
  13. program_name = program_name;
  14. /* switch on leak detection */
  15. lub_heap__set_framecount(MAX_BACKTRACE);
  16. }
  17. /*--------------------------------------------------------- */
  18. void lub_heap_symShow(unsigned address)
  19. {
  20. int value;
  21. char *name;
  22. SYM_TYPE type;
  23. if (OK == symByValueFind(sysSymTbl, address, &name, &value, &type)) {
  24. printf(" %s+%d", name, address - value);
  25. free(name);
  26. } else {
  27. printf("UNKNOWN @ %p", (void *)address);
  28. }
  29. }
  30. /*--------------------------------------------------------- */
  31. bool_t lub_heap_symMatch(unsigned address, const char *substring)
  32. {
  33. bool_t result = BOOL_FALSE;
  34. int value;
  35. char *name;
  36. SYM_TYPE type;
  37. if (OK == symByValueFind(sysSymTbl, address, &name, &value, &type)) {
  38. if (strstr(name, substring)) {
  39. result = TRUE;
  40. }
  41. free(name);
  42. }
  43. return result;
  44. }
  45. /*--------------------------------------------------------- */
  46. int leakEnable(unsigned frame_count)
  47. {
  48. taskLock();
  49. lub_heap__set_framecount(frame_count ? frame_count : MAX_BACKTRACE);
  50. taskUnlock();
  51. return 0;
  52. }
  53. /*--------------------------------------------------------- */
  54. int leakDisable(void)
  55. {
  56. taskLock();
  57. lub_heap__set_framecount(0);
  58. taskUnlock();
  59. return 0;
  60. }
  61. /*--------------------------------------------------------- */
  62. int leakShow(unsigned how, const char *substring)
  63. {
  64. /* let's go low priority for this... */
  65. int old_priority = taskPriorityGet(taskIdSelf(), &old_priority);
  66. taskPrioritySet(taskIdSelf(), 254);
  67. lub_heap_leak_report(how, substring);
  68. taskPrioritySet(taskIdSelf(), old_priority);
  69. return 0;
  70. }
  71. /*--------------------------------------------------------- */
  72. int leakScan(unsigned how, const char *substring)
  73. {
  74. /* let's go low priority for this... */
  75. int old_priority = taskPriorityGet(taskIdSelf(), &old_priority);
  76. taskPrioritySet(taskIdSelf(), 254);
  77. lub_heap_leak_scan();
  78. lub_heap_leak_report(how, substring);
  79. taskPrioritySet(taskIdSelf(), old_priority);
  80. return 0;
  81. }
  82. /*--------------------------------------------------------- */
  83. int leakHelp(void)
  84. {
  85. printf("The following commands can be used for leak detection:\n\n");
  86. printf
  87. (" leakHelp - Display this command summary.\n\n");
  88. printf
  89. (" leakEnable [frame_count] - Enable leak detection collecting the \n"
  90. " specified number of stack frames.\n\n");
  91. printf
  92. (" leakDisable - Disable leak detection, clearing out \n"
  93. " any currently monitored allocations\n\n");
  94. printf
  95. (" leakScan [how] [,substring] - Scan and show memory leak details.\n"
  96. " 'how' can be 0 - leaks only [default]\n"
  97. " 1 - leaks and partials\n"
  98. " 2 - all monitored allocations\n\n"
  99. " 'substring' can be specified to only display contexts\n"
  100. " which contain the string in their backtrace\n\n");
  101. printf
  102. (" leakShow [how] [,substring] - Display details for currently monitored allocations.\n"
  103. " Leak information will be as per the last scan performed.\n\n");
  104. printf
  105. ("A 'leak' occurs when there is no reference to any of the memory\n"
  106. "within an allocated block.\n\n"
  107. "A 'partial' leak occurs when there is no reference to the start of\n"
  108. "an allocated block of memory.\n\n");
  109. return 0;
  110. }
  111. /*--------------------------------------------------------- */