StackTrace.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2018 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. *******************************************************************************/
  16. #include "StackTrace.h"
  17. #include "Log.h"
  18. #include "LinkedList.h"
  19. #include "Clients.h"
  20. #include "Thread.h"
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #if defined(WIN32) || defined(WIN64)
  25. #define snprintf _snprintf
  26. #endif
  27. /*BE
  28. def STACKENTRY
  29. {
  30. n32 ptr STRING open "name"
  31. n32 dec "line"
  32. }
  33. defList(STACKENTRY)
  34. BE*/
  35. #define MAX_STACK_DEPTH 50
  36. #define MAX_FUNCTION_NAME_LENGTH 30
  37. #define MAX_THREADS 255
  38. typedef struct
  39. {
  40. thread_id_type threadid;
  41. char name[MAX_FUNCTION_NAME_LENGTH];
  42. int line;
  43. } stackEntry;
  44. typedef struct
  45. {
  46. thread_id_type id;
  47. int maxdepth;
  48. int current_depth;
  49. stackEntry callstack[MAX_STACK_DEPTH];
  50. } threadEntry;
  51. #include "StackTrace.h"
  52. static int thread_count = 0;
  53. static threadEntry threads[MAX_THREADS];
  54. static threadEntry *my_thread = NULL;
  55. #if defined(WIN32) || defined(WIN64)
  56. mutex_type stack_mutex;
  57. #else
  58. static pthread_mutex_t stack_mutex_store = PTHREAD_MUTEX_INITIALIZER;
  59. static mutex_type stack_mutex = &stack_mutex_store;
  60. #endif
  61. int setStack(int create);
  62. int setStack(int create)
  63. {
  64. int i = -1;
  65. thread_id_type curid = Thread_getid();
  66. my_thread = NULL;
  67. for (i = 0; i < MAX_THREADS && i < thread_count; ++i)
  68. {
  69. if (threads[i].id == curid)
  70. {
  71. my_thread = &threads[i];
  72. break;
  73. }
  74. }
  75. if (my_thread == NULL && create && thread_count < MAX_THREADS)
  76. {
  77. my_thread = &threads[thread_count];
  78. my_thread->id = curid;
  79. my_thread->maxdepth = 0;
  80. my_thread->current_depth = 0;
  81. ++thread_count;
  82. }
  83. return my_thread != NULL; /* good == 1 */
  84. }
  85. void StackTrace_entry(const char* name, int line, enum LOG_LEVELS trace_level)
  86. {
  87. Thread_lock_mutex(stack_mutex);
  88. if (!setStack(1))
  89. goto exit;
  90. if (trace_level != -1)
  91. Log_stackTrace(trace_level, 9, (int)my_thread->id, my_thread->current_depth, name, line, NULL);
  92. strncpy(my_thread->callstack[my_thread->current_depth].name, name, sizeof(my_thread->callstack[0].name)-1);
  93. my_thread->callstack[(my_thread->current_depth)++].line = line;
  94. if (my_thread->current_depth > my_thread->maxdepth)
  95. my_thread->maxdepth = my_thread->current_depth;
  96. if (my_thread->current_depth >= MAX_STACK_DEPTH)
  97. Log(LOG_FATAL, -1, "Max stack depth exceeded");
  98. exit:
  99. Thread_unlock_mutex(stack_mutex);
  100. }
  101. void StackTrace_exit(const char* name, int line, void* rc, enum LOG_LEVELS trace_level)
  102. {
  103. Thread_lock_mutex(stack_mutex);
  104. if (!setStack(0))
  105. goto exit;
  106. if (--(my_thread->current_depth) < 0)
  107. Log(LOG_FATAL, -1, "Minimum stack depth exceeded for thread %lu", my_thread->id);
  108. if (strncmp(my_thread->callstack[my_thread->current_depth].name, name, sizeof(my_thread->callstack[0].name)-1) != 0)
  109. Log(LOG_FATAL, -1, "Stack mismatch. Entry:%s Exit:%s\n", my_thread->callstack[my_thread->current_depth].name, name);
  110. if (trace_level != -1)
  111. {
  112. if (rc == NULL)
  113. Log_stackTrace(trace_level, 10, (int)my_thread->id, my_thread->current_depth, name, line, NULL);
  114. else
  115. Log_stackTrace(trace_level, 11, (int)my_thread->id, my_thread->current_depth, name, line, (int*)rc);
  116. }
  117. exit:
  118. Thread_unlock_mutex(stack_mutex);
  119. }
  120. void StackTrace_printStack(FILE* dest)
  121. {
  122. FILE* file = stdout;
  123. int t = 0;
  124. if (dest)
  125. file = dest;
  126. for (t = 0; t < thread_count; ++t)
  127. {
  128. threadEntry *cur_thread = &threads[t];
  129. if (cur_thread->id > 0)
  130. {
  131. int i = cur_thread->current_depth - 1;
  132. fprintf(file, "=========== Start of stack trace for thread %lu ==========\n", (unsigned long)cur_thread->id);
  133. if (i >= 0)
  134. {
  135. fprintf(file, "%s (%d)\n", cur_thread->callstack[i].name, cur_thread->callstack[i].line);
  136. while (--i >= 0)
  137. fprintf(file, " at %s (%d)\n", cur_thread->callstack[i].name, cur_thread->callstack[i].line);
  138. }
  139. fprintf(file, "=========== End of stack trace for thread %lu ==========\n\n", (unsigned long)cur_thread->id);
  140. }
  141. }
  142. if (file != stdout && file != stderr && file != NULL)
  143. fclose(file);
  144. }
  145. char* StackTrace_get(thread_id_type threadid, char* buf, int bufsize)
  146. {
  147. int t = 0;
  148. if (bufsize < 100)
  149. goto exit;
  150. buf[0] = '\0';
  151. for (t = 0; t < thread_count; ++t)
  152. {
  153. threadEntry *cur_thread = &threads[t];
  154. if (cur_thread->id == threadid)
  155. {
  156. int i = cur_thread->current_depth - 1;
  157. int curpos = 0;
  158. if (i >= 0)
  159. {
  160. curpos += snprintf(&buf[curpos], bufsize - curpos -1,
  161. "%s (%d)\n", cur_thread->callstack[i].name, cur_thread->callstack[i].line);
  162. while (--i >= 0)
  163. curpos += snprintf(&buf[curpos], bufsize - curpos -1, /* lgtm [cpp/overflowing-snprintf] */
  164. " at %s (%d)\n", cur_thread->callstack[i].name, cur_thread->callstack[i].line);
  165. if (buf[--curpos] == '\n')
  166. buf[curpos] = '\0';
  167. }
  168. break;
  169. }
  170. }
  171. exit:
  172. return buf;
  173. }