print_stk.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. Michael Abd-El-Malek contributed this patch. He wrote:
  2. ----------------------------------------
  3. Hello,
  4. This is a patch that enables programmatically dumping the stack of
  5. every thread. This has been useful in debugging deadlocks, etc...
  6. Our usage model is that the SIGUSR2 handler calls the new
  7. _st_print_thread_stacks function, which dumps the stack for all
  8. threads. A convenient feature is that for thread stacks that are the
  9. same (which is common for application with a lot of worker threads
  10. waiting for work), only one stack trace is printed, along with a
  11. count of how many threads have that same stack.
  12. I use the glibc backtrace function to get the backtrace, and then use
  13. popen to execute addr2line and convert memory addresses to file
  14. names, function names, and line numbers. If glibc isn't available,
  15. _st_print_thread_stacks just prints a warning. And this feature is
  16. only available if DEBUG is turned on.
  17. We've found this feature extremely helpful when debugging.
  18. The patch can be a bit more robust (it assumes addr2line exists).
  19. But I didn't want to go through the hassle of doing this, if the
  20. StateThreads community doesn't want to use this patch. (In our
  21. environment, addr2line will always be there.)
  22. Cheers,
  23. Mike
  24. ----------------------------------------
  25. Invoking complex functions from a signal handler is not recommended,
  26. plus this patch changes the behavior of existing API hooks. It will
  27. not become part of State Threads proper but you may find it useful
  28. nonetheless. This patch applies to st-1.5.2.
  29. diff -Nur Makefile.1.5.2 Makefile
  30. --- Makefile.1.5.2 Wed Sep 7 14:19:50 2005
  31. +++ Makefile Wed Sep 7 14:33:08 2005
  32. @@ -255,7 +255,8 @@
  33. $(TARGETDIR)/stk.o \
  34. $(TARGETDIR)/sync.o \
  35. $(TARGETDIR)/key.o \
  36. - $(TARGETDIR)/io.o
  37. + $(TARGETDIR)/io.o \
  38. + $(TARGETDIR)/backtrace.o
  39. OBJS += $(EXTRA_OBJS)
  40. HEADER = $(TARGETDIR)/st.h
  41. SLIBRARY = $(TARGETDIR)/libst.a
  42. diff -Nur backtrace.c.1.5.2 backtrace.c
  43. --- backtrace.c.1.5.2 Wed Dec 31 16:00:00 1969
  44. +++ backtrace.c Wed Sep 7 13:40:21 2005
  45. @@ -0,0 +1,211 @@
  46. +/*
  47. + * The contents of this file are subject to the Mozilla Public
  48. + * License Version 1.1 (the "License"); you may not use this file
  49. + * except in compliance with the License. You may obtain a copy of
  50. + * the License at http://www.mozilla.org/MPL/
  51. + *
  52. + * Software distributed under the License is distributed on an "AS
  53. + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  54. + * implied. See the License for the specific language governing
  55. + * rights and limitations under the License.
  56. + *
  57. + * Contributor(s): Michael Abd-El-Malek (mabdelmalek@cmu.edu)
  58. + * Carnegie Mellon University
  59. + *
  60. + * Alternatively, the contents of this file may be used under the
  61. + * terms of the GNU General Public License Version 2 or later (the
  62. + * "GPL"), in which case the provisions of the GPL are applicable
  63. + * instead of those above. If you wish to allow use of your
  64. + * version of this file only under the terms of the GPL and not to
  65. + * allow others to use your version of this file under the MPL,
  66. + * indicate your decision by deleting the provisions above and
  67. + * replace them with the notice and other provisions required by
  68. + * the GPL. If you do not delete the provisions above, a recipient
  69. + * may use your version of this file under either the MPL or the
  70. + * GPL.
  71. + */
  72. +
  73. +
  74. +
  75. +/*
  76. + * This file contains routines for printing a stack trace of all threads.
  77. + * Only works when DEBUG is defined and where glibc is available, since it
  78. + * provides the backtrace() function.
  79. + */
  80. +
  81. +#define _GNU_SOURCE /* to get program_invocation_name */
  82. +
  83. +#include <stdio.h>
  84. +#include <stdlib.h>
  85. +
  86. +
  87. +#if defined(DEBUG) && defined(__GLIBC__)
  88. +
  89. +#include <errno.h>
  90. +#include "common.h"
  91. +#include <execinfo.h>
  92. +#include <inttypes.h>
  93. +#include <string.h>
  94. +
  95. +
  96. +/* The maximum number of frames to get a stack trace for. If a thread has more
  97. + * frames than this, then we only show the latest X frames. */
  98. +#define MAX_NUM_FRAMES 64
  99. +
  100. +
  101. +typedef struct thread_stack_s {
  102. + uint32_t num_frames;
  103. + void* addresses[MAX_NUM_FRAMES]; /* frame pointers */
  104. + char* locations[MAX_NUM_FRAMES]; /* file/function/line numbers */
  105. + uint32_t num_matches;
  106. +
  107. + struct thread_stack_s* next;
  108. +} thread_stack_t;
  109. +
  110. +static thread_stack_t* stacks = NULL;
  111. +
  112. +
  113. +/* Converts the function's memory addresses to function names, file names, and
  114. + * line numbers. Calls binutil's addr2line program. */
  115. +static void get_symbol_names(thread_stack_t *stack)
  116. +{
  117. + char program_to_run[1024], function[256], filename_lineno[256], temp[19];
  118. + FILE* output;
  119. + int num_bytes_left;
  120. + uint32_t i;
  121. +
  122. + /* Construct the arguments to addr2line */
  123. + num_bytes_left = sizeof(program_to_run);
  124. + num_bytes_left -= snprintf(program_to_run, sizeof(program_to_run),
  125. + "addr2line -fCe %s", program_invocation_name);
  126. + for (i = 0; i < stack->num_frames && num_bytes_left > 0; ++i) {
  127. + num_bytes_left -= snprintf(temp, sizeof(temp), " %p", stack->addresses[i]);
  128. + strncat(program_to_run, temp, num_bytes_left);
  129. + }
  130. +
  131. + /* Use popen to execute addr2line and read its ouput */
  132. + output = popen(program_to_run, "r");
  133. + for (i = 0; i < stack->num_frames; ++i) {
  134. + char* function_listing = (char*) malloc(512);
  135. + fscanf(output, "%255s\n", function);
  136. + fscanf(output, "%255s\n", filename_lineno);
  137. + snprintf(function_listing, 512, "%s at %s", function, filename_lineno);
  138. + stack->locations[i] = function_listing;
  139. + }
  140. + pclose(output);
  141. +}
  142. +
  143. +
  144. +static void print_stack(thread_stack_t* stack)
  145. +{
  146. + int skip_offset = 0, cmp_len;
  147. + uint32_t i;
  148. +
  149. + /* Get the function names/filenames/line numbers */
  150. + get_symbol_names(stack);
  151. +
  152. + cmp_len = strlen("_st_iterate_threads_helper");
  153. +
  154. + /* Print the backtrace */
  155. + for (i = 0; i < stack->num_frames; ++i) {
  156. + /* Skip frames we don't have location info for */
  157. + if (!strncmp(stack->locations[i], "??", 2)) {
  158. + continue;
  159. + }
  160. +
  161. + /* Skip the frames that are used for printing the stack trace */
  162. + if (skip_offset) {
  163. + printf("\t#%2d %s %p\n", i - skip_offset, stack->locations[i],
  164. + stack->addresses[i]);
  165. + } else if (!strncmp(stack->locations[i], "_st_iterate_threads_helper",
  166. + cmp_len)) {
  167. + skip_offset = i + 1;
  168. + }
  169. + }
  170. +}
  171. +
  172. +
  173. +static void add_current_thread_stack(void)
  174. +{
  175. + thread_stack_t *new_stack = malloc(sizeof(thread_stack_t));
  176. + thread_stack_t *search;
  177. +
  178. + /* Call glibc function to get the backtrace */
  179. + new_stack->num_frames = backtrace(new_stack->addresses, MAX_NUM_FRAMES);
  180. +
  181. + /* Check if we have another stacks that is equivalent. If so, then coaelsce
  182. + * two stacks into one, to minimize output to user. */
  183. + search = stacks;
  184. + while (search) {
  185. + if (search->num_frames == new_stack->num_frames &&
  186. + !memcmp(search->addresses, new_stack->addresses,
  187. + search->num_frames * sizeof(void*))) {
  188. + /* Found an existing stack that is the same as this thread's stack */
  189. + ++search->num_matches;
  190. + free(new_stack);
  191. + return;
  192. + } else {
  193. + search = search->next;
  194. + }
  195. + }
  196. +
  197. + /* This is a new stack. Add it to the list of stacks. */
  198. + new_stack->num_matches = 1;
  199. + new_stack->next = stacks;
  200. + stacks = new_stack;
  201. +}
  202. +
  203. +static void print_stack_frames(void)
  204. +{
  205. + while (stacks) {
  206. + printf("\n%u thread(s) with this backtrace:\n", stacks->num_matches);
  207. + print_stack(stacks);
  208. + stacks = stacks->next;
  209. + }
  210. + printf("\n");
  211. +}
  212. +
  213. +static void free_stacks(void)
  214. +{
  215. + uint32_t i;
  216. + while (stacks) {
  217. + thread_stack_t *next = stacks->next;
  218. + for (i = 0; i < stacks->num_frames; ++i) {
  219. + free(stacks->locations[i]);
  220. + }
  221. + free(stacks);
  222. + stacks = next;
  223. + }
  224. + stacks = NULL;
  225. +}
  226. +
  227. +
  228. +static void st_print_thread_stack(_st_thread_t *thread, int start_flag,
  229. + int end_flag)
  230. +{
  231. + if (end_flag == 0) {
  232. + add_current_thread_stack();
  233. + } else {
  234. + print_stack_frames();
  235. + }
  236. +}
  237. +
  238. +
  239. +void _st_print_thread_stacks(int ignore)
  240. +{
  241. + _st_iterate_threads_flag = 1;
  242. + _st_iterate_threads_helper(st_print_thread_stack);
  243. + _st_iterate_threads_flag = 0;
  244. +
  245. + /* Deallocate memory */
  246. + free_stacks();
  247. +}
  248. +
  249. +#else /* defined(DEBUG) && defined(__GLIBC__) */
  250. +
  251. +void _st_print_thread_stacks(int ignore)
  252. +{
  253. + printf("%s: need DEBUG mode and glibc-specific functions to read stack.\n",
  254. + __FUNCTION__);
  255. +}
  256. +#endif /* defined(DEBUG) && defined(__GLIBC__) */
  257. diff -Nur common.h.1.5.2 common.h
  258. --- common.h.1.5.2 Wed Sep 7 14:18:37 2005
  259. +++ common.h Wed Sep 7 14:35:36 2005
  260. @@ -371,8 +371,18 @@
  261. */
  262. #ifdef DEBUG
  263. -void _st_iterate_threads(void);
  264. -#define ST_DEBUG_ITERATE_THREADS() _st_iterate_threads()
  265. +typedef void(*_st_func_ptr_t)(_st_thread_t *thread,
  266. + int start_flag,
  267. + int end_flag);
  268. +/* Pointer to function that will be called on thread switch */
  269. +extern _st_func_ptr_t _st_iterate_func_ptr;
  270. +extern int _st_iterate_threads_flag;
  271. +/* Thread iteration function that will call an arbitrary function */
  272. +extern void _st_iterate_threads_helper(_st_func_ptr_t func);
  273. +#define ST_DEBUG_ITERATE_THREADS() \
  274. + if (_st_iterate_func_ptr) { \
  275. + _st_iterate_threads_helper(_st_iterate_func_ptr); \
  276. + }
  277. #else
  278. #define ST_DEBUG_ITERATE_THREADS()
  279. #endif
  280. diff -Nur public.h.1.5.2 public.h
  281. --- public.h.1.5.2 Wed Sep 7 11:46:58 2005
  282. +++ public.h Wed Sep 7 13:38:46 2005
  283. @@ -171,8 +171,10 @@
  284. extern st_netfd_t st_open(const char *path, int oflags, mode_t mode);
  285. #ifdef DEBUG
  286. -extern void _st_show_thread_stack(st_thread_t thread, const char *messg);
  287. +extern void _st_show_thread_stack(st_thread_t thread, int start_flag,
  288. + int end_flag);
  289. extern void _st_iterate_threads(void);
  290. +extern void _st_print_thread_stacks(int ignore);
  291. #endif
  292. #ifdef __cplusplus
  293. diff -Nur sched.c.1.5.2 sched.c
  294. --- sched.c.1.5.2 Wed Sep 7 10:48:05 2005
  295. +++ sched.c Wed Sep 7 13:38:46 2005
  296. @@ -919,16 +919,13 @@
  297. #ifdef DEBUG
  298. -/* ARGSUSED */
  299. -void _st_show_thread_stack(_st_thread_t *thread, const char *messg)
  300. -{
  301. -
  302. -}
  303. -
  304. /* To be set from debugger */
  305. int _st_iterate_threads_flag = 0;
  306. +/* Thread iteration function that will call an arbitrary function */
  307. +_st_func_ptr_t _st_iterate_func_ptr = NULL;
  308. -void _st_iterate_threads(void)
  309. +/* This function iterates over all threads, calling "func" for each thread. */
  310. +void _st_iterate_threads_helper(_st_func_ptr_t func)
  311. {
  312. static _st_thread_t *thread = NULL;
  313. static jmp_buf orig_jb, save_jb;
  314. @@ -944,16 +941,20 @@
  315. if (thread) {
  316. memcpy(thread->context, save_jb, sizeof(jmp_buf));
  317. - _st_show_thread_stack(thread, NULL);
  318. + func(thread, 0, 0);
  319. } else {
  320. if (MD_SETJMP(orig_jb)) {
  321. _st_iterate_threads_flag = 0;
  322. + _st_iterate_func_ptr = NULL;
  323. thread = NULL;
  324. - _st_show_thread_stack(thread, "Iteration completed");
  325. + /* Last thread to iterate through */
  326. + func(thread, 0, 1);
  327. return;
  328. }
  329. + /* First thread to iterate through */
  330. thread = _ST_CURRENT_THREAD();
  331. - _st_show_thread_stack(thread, "Iteration started");
  332. + _st_iterate_func_ptr = func;
  333. + func(thread, 1, 0);
  334. }
  335. q = thread->tlink.next;
  336. @@ -966,5 +967,17 @@
  337. memcpy(save_jb, thread->context, sizeof(jmp_buf));
  338. MD_LONGJMP(thread->context, 1);
  339. }
  340. +
  341. +/* ARGSUSED */
  342. +void _st_show_thread_stack(_st_thread_t *thread, int start_flag, int end_flag)
  343. +{
  344. +}
  345. +
  346. +/* Iterate over threads inside debugger; see st/README */
  347. +void _st_iterate_threads(void)
  348. +{
  349. + _st_iterate_threads_helper(_st_show_thread_stack);
  350. +}
  351. +
  352. #endif /* DEBUG */