~ubuntu-branches/ubuntu/gutsy/st/gutsy

« back to all changes in this revision

Viewing changes to extensions/print_stk.patch

  • Committer: Bazaar Package Importer
  • Author(s): Wesley W. Terpstra (Debian)
  • Date: 2007-01-14 21:36:45 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070114213645-34cpn8xpl92dp1lc
Tags: 1.7-1
* New upstream release (closes: #406394)
* Increased standards-version; no changes necessary
* Moved from DH_COMPAT level 2 to 5; no changes necessary
* Added linker options to indicate that an executable stack is not needed
* Downgraded optimization to -O2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Michael Abd-El-Malek contributed this patch.  He wrote:
 
2
----------------------------------------
 
3
Hello,
 
4
 
 
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.
 
13
 
 
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.
 
19
 
 
20
We've found this feature extremely helpful when debugging.
 
21
 
 
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.)
 
26
 
 
27
Cheers,
 
28
Mike
 
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.
 
34
 
 
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
 
38
@@ -255,7 +255,8 @@
 
39
               $(TARGETDIR)/stk.o   \
 
40
               $(TARGETDIR)/sync.o  \
 
41
               $(TARGETDIR)/key.o   \
 
42
-              $(TARGETDIR)/io.o
 
43
+              $(TARGETDIR)/io.o    \
 
44
+             $(TARGETDIR)/backtrace.o
 
45
 OBJS        += $(EXTRA_OBJS)
 
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
 
51
@@ -0,0 +1,211 @@
 
52
+/*
 
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/
 
57
+ *
 
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.
 
62
+ *
 
63
+ * Contributor(s):  Michael Abd-El-Malek (mabdelmalek@cmu.edu)
 
64
+ *                  Carnegie Mellon University
 
65
+ *
 
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
 
76
+ * GPL.
 
77
+ */
 
78
+
 
79
+
 
80
+
 
81
+/*
 
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.
 
85
+ */
 
86
+
 
87
+#define _GNU_SOURCE  /* to get program_invocation_name */
 
88
+
 
89
+#include <stdio.h>
 
90
+#include <stdlib.h>
 
91
+
 
92
+
 
93
+#if defined(DEBUG) && defined(__GLIBC__)
 
94
+
 
95
+#include <errno.h>
 
96
+#include "common.h"
 
97
+#include <execinfo.h>
 
98
+#include <inttypes.h>
 
99
+#include <string.h>
 
100
+
 
101
+
 
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
 
105
+
 
106
+
 
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;
 
112
+
 
113
+   struct thread_stack_s* next;
 
114
+} thread_stack_t;
 
115
+
 
116
+static thread_stack_t* stacks = NULL;
 
117
+
 
118
+
 
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)
 
122
+{
 
123
+     char program_to_run[1024], function[256], filename_lineno[256], temp[19];
 
124
+     FILE* output;
 
125
+     int num_bytes_left;
 
126
+     uint32_t i;
 
127
+
 
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);
 
135
+     }
 
136
+
 
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;
 
145
+     }
 
146
+     pclose(output);
 
147
+}
 
148
+
 
149
+
 
150
+static void print_stack(thread_stack_t* stack)
 
151
+{
 
152
+     int skip_offset = 0, cmp_len;
 
153
+     uint32_t i;
 
154
+
 
155
+     /* Get the function names/filenames/line numbers */
 
156
+     get_symbol_names(stack);
 
157
+
 
158
+     cmp_len = strlen("_st_iterate_threads_helper");
 
159
+
 
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)) {
 
164
+             continue;
 
165
+         }
 
166
+
 
167
+         /* Skip the frames that are used for printing the stack trace */
 
168
+         if (skip_offset) {
 
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",
 
172
+                             cmp_len)) {
 
173
+             skip_offset = i + 1;
 
174
+         }
 
175
+     }
 
176
+}
 
177
+
 
178
+
 
179
+static void add_current_thread_stack(void)
 
180
+{
 
181
+     thread_stack_t *new_stack = malloc(sizeof(thread_stack_t));
 
182
+     thread_stack_t *search;
 
183
+
 
184
+     /* Call glibc function to get the backtrace */
 
185
+     new_stack->num_frames = backtrace(new_stack->addresses, MAX_NUM_FRAMES);
 
186
+
 
187
+     /* Check if we have another stacks that is equivalent.  If so, then coaelsce
 
188
+      *  two stacks into one, to minimize output to user. */
 
189
+     search = stacks;
 
190
+     while (search) {
 
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;
 
196
+             free(new_stack);
 
197
+             return;
 
198
+         } else {
 
199
+             search = search->next;
 
200
+         }
 
201
+     }
 
202
+
 
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;
 
207
+}
 
208
+
 
209
+static void print_stack_frames(void)
 
210
+{
 
211
+     while (stacks) {
 
212
+         printf("\n%u thread(s) with this backtrace:\n", stacks->num_matches);
 
213
+         print_stack(stacks);
 
214
+         stacks = stacks->next;
 
215
+     }
 
216
+     printf("\n");
 
217
+}
 
218
+
 
219
+static void free_stacks(void)
 
220
+{
 
221
+     uint32_t i;
 
222
+     while (stacks) {
 
223
+         thread_stack_t *next = stacks->next;
 
224
+         for (i = 0; i < stacks->num_frames; ++i) {
 
225
+             free(stacks->locations[i]);
 
226
+         }
 
227
+         free(stacks);
 
228
+         stacks = next;
 
229
+     }
 
230
+     stacks = NULL;
 
231
+}
 
232
+
 
233
+
 
234
+static void st_print_thread_stack(_st_thread_t *thread, int start_flag,
 
235
+                                   int end_flag)
 
236
+{
 
237
+     if (end_flag == 0) {
 
238
+         add_current_thread_stack();
 
239
+     } else {
 
240
+         print_stack_frames();
 
241
+     }
 
242
+}
 
243
+
 
244
+
 
245
+void _st_print_thread_stacks(int ignore)
 
246
+{
 
247
+     _st_iterate_threads_flag = 1;
 
248
+     _st_iterate_threads_helper(st_print_thread_stack);
 
249
+     _st_iterate_threads_flag = 0;
 
250
+
 
251
+     /* Deallocate memory */
 
252
+     free_stacks();
 
253
+}
 
254
+
 
255
+#else  /* defined(DEBUG) && defined(__GLIBC__) */
 
256
+
 
257
+void _st_print_thread_stacks(int ignore)
 
258
+{
 
259
+     printf("%s: need DEBUG mode and glibc-specific functions to read stack.\n",
 
260
+            __FUNCTION__);
 
261
+}
 
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
 
266
@@ -371,8 +371,18 @@
 
267
  */
 
268
 
 
269
 #ifdef DEBUG
 
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,
 
273
+                              int           start_flag,
 
274
+                              int           end_flag);
 
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);  \
 
283
+          }
 
284
 #else
 
285
 #define ST_DEBUG_ITERATE_THREADS()
 
286
 #endif
 
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
 
290
@@ -171,8 +171,10 @@
 
291
 extern st_netfd_t st_open(const char *path, int oflags, mode_t mode);
 
292
 
 
293
 #ifdef DEBUG
 
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,
 
296
+                                 int end_flag);
 
297
 extern void _st_iterate_threads(void);
 
298
+extern void _st_print_thread_stacks(int ignore);
 
299
 #endif
 
300
 
 
301
 #ifdef __cplusplus
 
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 @@
 
306
 
 
307
 
 
308
 #ifdef DEBUG
 
309
-/* ARGSUSED */
 
310
-void _st_show_thread_stack(_st_thread_t *thread, const char *messg)
 
311
-{
 
312
-
 
313
-}
 
314
-
 
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;
 
319
 
 
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)
 
323
 {
 
324
   static _st_thread_t *thread = NULL;
 
325
   static jmp_buf orig_jb, save_jb;
 
326
@@ -944,16 +941,20 @@
 
327
 
 
328
   if (thread) {
 
329
     memcpy(thread->context, save_jb, sizeof(jmp_buf));
 
330
-    _st_show_thread_stack(thread, NULL);
 
331
+    func(thread, 0, 0);
 
332
   } else {
 
333
     if (MD_SETJMP(orig_jb)) {
 
334
       _st_iterate_threads_flag = 0;
 
335
+      _st_iterate_func_ptr = NULL;
 
336
       thread = NULL;
 
337
-      _st_show_thread_stack(thread, "Iteration completed");
 
338
+      /* Last thread to iterate through  */
 
339
+      func(thread, 0, 1);
 
340
       return;
 
341
     }
 
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);
 
347
   }
 
348
 
 
349
   q = thread->tlink.next;
 
350
@@ -966,5 +967,17 @@
 
351
   memcpy(save_jb, thread->context, sizeof(jmp_buf));
 
352
   MD_LONGJMP(thread->context, 1);
 
353
 }
 
354
+
 
355
+/* ARGSUSED */
 
356
+void _st_show_thread_stack(_st_thread_t *thread, int start_flag, int end_flag)
 
357
+{
 
358
+}
 
359
+
 
360
+/* Iterate over threads inside debugger; see st/README */
 
361
+void _st_iterate_threads(void)
 
362
+{
 
363
+  _st_iterate_threads_helper(_st_show_thread_stack);
 
364
+}
 
365
+
 
366
 #endif /* DEBUG */
 
367