1
Michael Abd-El-Malek contributed this patch. He wrote:
2
----------------------------------------
5
This is a patch that enables programmatically dumping the stack of
6
every thread. This has been useful in debugging deadlocks, etc...
7
Our usage model is that the SIGUSR2 handler calls the new
8
_st_print_thread_stacks function, which dumps the stack for all
9
threads. A convenient feature is that for thread stacks that are the
10
same (which is common for application with a lot of worker threads
11
waiting for work), only one stack trace is printed, along with a
12
count of how many threads have that same stack.
14
I use the glibc backtrace function to get the backtrace, and then use
15
popen to execute addr2line and convert memory addresses to file
16
names, function names, and line numbers. If glibc isn't available,
17
_st_print_thread_stacks just prints a warning. And this feature is
18
only available if DEBUG is turned on.
20
We've found this feature extremely helpful when debugging.
22
The patch can be a bit more robust (it assumes addr2line exists).
23
But I didn't want to go through the hassle of doing this, if the
24
StateThreads community doesn't want to use this patch. (In our
25
environment, addr2line will always be there.)
29
----------------------------------------
30
Invoking complex functions from a signal handler is not recommended,
31
plus this patch changes the behavior of existing API hooks. It will
32
not become part of State Threads proper but you may find it useful
33
nonetheless. This patch applies to st-1.5.2.
35
diff -Nur Makefile.1.5.2 Makefile
36
--- Makefile.1.5.2 Wed Sep 7 14:19:50 2005
37
+++ Makefile Wed Sep 7 14:33:08 2005
44
+ $(TARGETDIR)/backtrace.o
46
HEADER = $(TARGETDIR)/st.h
47
SLIBRARY = $(TARGETDIR)/libst.a
48
diff -Nur backtrace.c.1.5.2 backtrace.c
49
--- backtrace.c.1.5.2 Wed Dec 31 16:00:00 1969
50
+++ backtrace.c Wed Sep 7 13:40:21 2005
53
+ * The contents of this file are subject to the Mozilla Public
54
+ * License Version 1.1 (the "License"); you may not use this file
55
+ * except in compliance with the License. You may obtain a copy of
56
+ * the License at http://www.mozilla.org/MPL/
58
+ * Software distributed under the License is distributed on an "AS
59
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
60
+ * implied. See the License for the specific language governing
61
+ * rights and limitations under the License.
63
+ * Contributor(s): Michael Abd-El-Malek (mabdelmalek@cmu.edu)
64
+ * Carnegie Mellon University
66
+ * Alternatively, the contents of this file may be used under the
67
+ * terms of the GNU General Public License Version 2 or later (the
68
+ * "GPL"), in which case the provisions of the GPL are applicable
69
+ * instead of those above. If you wish to allow use of your
70
+ * version of this file only under the terms of the GPL and not to
71
+ * allow others to use your version of this file under the MPL,
72
+ * indicate your decision by deleting the provisions above and
73
+ * replace them with the notice and other provisions required by
74
+ * the GPL. If you do not delete the provisions above, a recipient
75
+ * may use your version of this file under either the MPL or the
82
+ * This file contains routines for printing a stack trace of all threads.
83
+ * Only works when DEBUG is defined and where glibc is available, since it
84
+ * provides the backtrace() function.
87
+#define _GNU_SOURCE /* to get program_invocation_name */
93
+#if defined(DEBUG) && defined(__GLIBC__)
97
+#include <execinfo.h>
98
+#include <inttypes.h>
102
+/* The maximum number of frames to get a stack trace for. If a thread has more
103
+ * frames than this, then we only show the latest X frames. */
104
+#define MAX_NUM_FRAMES 64
107
+typedef struct thread_stack_s {
108
+ uint32_t num_frames;
109
+ void* addresses[MAX_NUM_FRAMES]; /* frame pointers */
110
+ char* locations[MAX_NUM_FRAMES]; /* file/function/line numbers */
111
+ uint32_t num_matches;
113
+ struct thread_stack_s* next;
116
+static thread_stack_t* stacks = NULL;
119
+/* Converts the function's memory addresses to function names, file names, and
120
+ * line numbers. Calls binutil's addr2line program. */
121
+static void get_symbol_names(thread_stack_t *stack)
123
+ char program_to_run[1024], function[256], filename_lineno[256], temp[19];
125
+ int num_bytes_left;
128
+ /* Construct the arguments to addr2line */
129
+ num_bytes_left = sizeof(program_to_run);
130
+ num_bytes_left -= snprintf(program_to_run, sizeof(program_to_run),
131
+ "addr2line -fCe %s", program_invocation_name);
132
+ for (i = 0; i < stack->num_frames && num_bytes_left > 0; ++i) {
133
+ num_bytes_left -= snprintf(temp, sizeof(temp), " %p", stack->addresses[i]);
134
+ strncat(program_to_run, temp, num_bytes_left);
137
+ /* Use popen to execute addr2line and read its ouput */
138
+ output = popen(program_to_run, "r");
139
+ for (i = 0; i < stack->num_frames; ++i) {
140
+ char* function_listing = (char*) malloc(512);
141
+ fscanf(output, "%255s\n", function);
142
+ fscanf(output, "%255s\n", filename_lineno);
143
+ snprintf(function_listing, 512, "%s at %s", function, filename_lineno);
144
+ stack->locations[i] = function_listing;
150
+static void print_stack(thread_stack_t* stack)
152
+ int skip_offset = 0, cmp_len;
155
+ /* Get the function names/filenames/line numbers */
156
+ get_symbol_names(stack);
158
+ cmp_len = strlen("_st_iterate_threads_helper");
160
+ /* Print the backtrace */
161
+ for (i = 0; i < stack->num_frames; ++i) {
162
+ /* Skip frames we don't have location info for */
163
+ if (!strncmp(stack->locations[i], "??", 2)) {
167
+ /* Skip the frames that are used for printing the stack trace */
169
+ printf("\t#%2d %s %p\n", i - skip_offset, stack->locations[i],
170
+ stack->addresses[i]);
171
+ } else if (!strncmp(stack->locations[i], "_st_iterate_threads_helper",
173
+ skip_offset = i + 1;
179
+static void add_current_thread_stack(void)
181
+ thread_stack_t *new_stack = malloc(sizeof(thread_stack_t));
182
+ thread_stack_t *search;
184
+ /* Call glibc function to get the backtrace */
185
+ new_stack->num_frames = backtrace(new_stack->addresses, MAX_NUM_FRAMES);
187
+ /* Check if we have another stacks that is equivalent. If so, then coaelsce
188
+ * two stacks into one, to minimize output to user. */
191
+ if (search->num_frames == new_stack->num_frames &&
192
+ !memcmp(search->addresses, new_stack->addresses,
193
+ search->num_frames * sizeof(void*))) {
194
+ /* Found an existing stack that is the same as this thread's stack */
195
+ ++search->num_matches;
199
+ search = search->next;
203
+ /* This is a new stack. Add it to the list of stacks. */
204
+ new_stack->num_matches = 1;
205
+ new_stack->next = stacks;
206
+ stacks = new_stack;
209
+static void print_stack_frames(void)
212
+ printf("\n%u thread(s) with this backtrace:\n", stacks->num_matches);
213
+ print_stack(stacks);
214
+ stacks = stacks->next;
219
+static void free_stacks(void)
223
+ thread_stack_t *next = stacks->next;
224
+ for (i = 0; i < stacks->num_frames; ++i) {
225
+ free(stacks->locations[i]);
234
+static void st_print_thread_stack(_st_thread_t *thread, int start_flag,
237
+ if (end_flag == 0) {
238
+ add_current_thread_stack();
240
+ print_stack_frames();
245
+void _st_print_thread_stacks(int ignore)
247
+ _st_iterate_threads_flag = 1;
248
+ _st_iterate_threads_helper(st_print_thread_stack);
249
+ _st_iterate_threads_flag = 0;
251
+ /* Deallocate memory */
255
+#else /* defined(DEBUG) && defined(__GLIBC__) */
257
+void _st_print_thread_stacks(int ignore)
259
+ printf("%s: need DEBUG mode and glibc-specific functions to read stack.\n",
262
+#endif /* defined(DEBUG) && defined(__GLIBC__) */
263
diff -Nur common.h.1.5.2 common.h
264
--- common.h.1.5.2 Wed Sep 7 14:18:37 2005
265
+++ common.h Wed Sep 7 14:35:36 2005
270
-void _st_iterate_threads(void);
271
-#define ST_DEBUG_ITERATE_THREADS() _st_iterate_threads()
272
+typedef void(*_st_func_ptr_t)(_st_thread_t *thread,
275
+/* Pointer to function that will be called on thread switch */
276
+extern _st_func_ptr_t _st_iterate_func_ptr;
277
+extern int _st_iterate_threads_flag;
278
+/* Thread iteration function that will call an arbitrary function */
279
+extern void _st_iterate_threads_helper(_st_func_ptr_t func);
280
+#define ST_DEBUG_ITERATE_THREADS() \
281
+ if (_st_iterate_func_ptr) { \
282
+ _st_iterate_threads_helper(_st_iterate_func_ptr); \
285
#define ST_DEBUG_ITERATE_THREADS()
287
diff -Nur public.h.1.5.2 public.h
288
--- public.h.1.5.2 Wed Sep 7 11:46:58 2005
289
+++ public.h Wed Sep 7 13:38:46 2005
291
extern st_netfd_t st_open(const char *path, int oflags, mode_t mode);
294
-extern void _st_show_thread_stack(st_thread_t thread, const char *messg);
295
+extern void _st_show_thread_stack(st_thread_t thread, int start_flag,
297
extern void _st_iterate_threads(void);
298
+extern void _st_print_thread_stacks(int ignore);
302
diff -Nur sched.c.1.5.2 sched.c
303
--- sched.c.1.5.2 Wed Sep 7 10:48:05 2005
304
+++ sched.c Wed Sep 7 13:38:46 2005
305
@@ -919,16 +919,13 @@
310
-void _st_show_thread_stack(_st_thread_t *thread, const char *messg)
315
/* To be set from debugger */
316
int _st_iterate_threads_flag = 0;
317
+/* Thread iteration function that will call an arbitrary function */
318
+_st_func_ptr_t _st_iterate_func_ptr = NULL;
320
-void _st_iterate_threads(void)
321
+/* This function iterates over all threads, calling "func" for each thread. */
322
+void _st_iterate_threads_helper(_st_func_ptr_t func)
324
static _st_thread_t *thread = NULL;
325
static jmp_buf orig_jb, save_jb;
326
@@ -944,16 +941,20 @@
329
memcpy(thread->context, save_jb, sizeof(jmp_buf));
330
- _st_show_thread_stack(thread, NULL);
331
+ func(thread, 0, 0);
333
if (MD_SETJMP(orig_jb)) {
334
_st_iterate_threads_flag = 0;
335
+ _st_iterate_func_ptr = NULL;
337
- _st_show_thread_stack(thread, "Iteration completed");
338
+ /* Last thread to iterate through */
339
+ func(thread, 0, 1);
342
+ /* First thread to iterate through */
343
thread = _ST_CURRENT_THREAD();
344
- _st_show_thread_stack(thread, "Iteration started");
345
+ _st_iterate_func_ptr = func;
346
+ func(thread, 1, 0);
349
q = thread->tlink.next;
351
memcpy(save_jb, thread->context, sizeof(jmp_buf));
352
MD_LONGJMP(thread->context, 1);
356
+void _st_show_thread_stack(_st_thread_t *thread, int start_flag, int end_flag)
360
+/* Iterate over threads inside debugger; see st/README */
361
+void _st_iterate_threads(void)
363
+ _st_iterate_threads_helper(_st_show_thread_stack);