-
Notifications
You must be signed in to change notification settings - Fork 764
/
findstack_subr.c
382 lines (324 loc) · 9.46 KB
/
findstack_subr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <mdb/mdb_modapi.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/avl.h>
#include <sys/lwp.h>
#include <thr_uberdata.h>
#include <stddef.h>
#include "findstack.h"
#if defined(__i386) || defined(__amd64)
struct rwindow {
uintptr_t rw_fp;
uintptr_t rw_rtn;
};
#endif
#ifndef STACK_BIAS
#define STACK_BIAS 0
#endif
#ifdef __amd64
#define STACKS_REGS_FP "rbp"
#define STACKS_REGS_RC "rip"
#else
#ifdef __i386
#define STACKS_REGS_FP "ebp"
#define STACKS_REGS_RC "eip"
#else
#define STACKS_REGS_FP "fp"
#define STACKS_REGS_RC "pc"
#endif
#endif
#define STACKS_SOBJ_MX (uintptr_t)"MX"
#define STACKS_SOBJ_CV (uintptr_t)"CV"
int
thread_text_to_state(const char *state, uint_t *out)
{
if (strcmp(state, "PARKED") == 0) {
*out = B_TRUE;
} else if (strcmp(state, "UNPARKED") == 0) {
*out = B_FALSE;
} else if (strcmp(state, "FREE") == 0) {
/*
* When run with "-i", ::stacks filters out "FREE" threads.
* We therefore need to recognize "FREE", and set it to a
* value that will never match fsi_tstate.
*/
*out = UINT_MAX;
} else {
return (-1);
}
return (0);
}
void
thread_state_to_text(uint_t state, char *out, size_t out_sz)
{
(void) snprintf(out, out_sz, state ? "PARKED" : "UNPARKED");
}
int
sobj_text_to_ops(const char *name, uintptr_t *sobj_ops_out)
{
if (strcmp(name, "MX") == 0) {
*sobj_ops_out = STACKS_SOBJ_MX;
} else if (strcmp(name, "CV") == 0) {
*sobj_ops_out = STACKS_SOBJ_CV;
} else {
mdb_warn("sobj \"%s\" not recognized\n", name);
return (-1);
}
return (0);
}
void
sobj_ops_to_text(uintptr_t addr, char *out, size_t sz)
{
(void) snprintf(out, sz, "%s", addr == 0 ? "<none>" : (char *)addr);
}
static int
stacks_module_callback(mdb_object_t *obj, void *arg)
{
stacks_module_t *smp = arg;
boolean_t match = (strcmp(obj->obj_name, smp->sm_name) == 0);
char *suffix = ".so";
const char *s, *next;
size_t len;
if (smp->sm_size != 0)
return (0);
/*
* It doesn't match the name, but -- for convenience -- we want to
* allow matches before ".so.[suffix]". An aside: why doesn't
* strrstr() exist? (Don't google that. I'm serious, don't do it.
* If you do, and you read the thread of "why doesn't strrstr() exist?"
* circa 2005 you will see things that you will NEVER be able to unsee!)
*/
if (!match && (s = strstr(obj->obj_name, suffix)) != NULL) {
while ((next = strstr(s + 1, suffix)) != NULL) {
s = next;
continue;
}
len = s - obj->obj_name;
match = (strncmp(smp->sm_name, obj->obj_name, len) == 0 &&
smp->sm_name[len] == '\0');
}
/*
* If we have a library that has the libc directory in the path, we
* want to match against anything that would match libc.so.1. (This
* is necessary to be able to easily deal with libc implementations
* that have alternate hardware capabilities.)
*/
if (!match && strstr(obj->obj_fullname, "/libc/") != NULL) {
mdb_object_t libc = *obj;
libc.obj_name = "libc.so.1";
libc.obj_fullname = "";
return (stacks_module_callback(&libc, arg));
}
if (match) {
smp->sm_text = obj->obj_base;
smp->sm_size = obj->obj_size;
}
return (0);
}
int
stacks_module(stacks_module_t *smp)
{
if (mdb_object_iter(stacks_module_callback, smp) != 0)
return (-1);
return (0);
}
typedef struct stacks_ulwp {
avl_node_t sulwp_node;
lwpid_t sulwp_id;
uintptr_t sulwp_addr;
} stacks_ulwp_t;
boolean_t stacks_ulwp_initialized;
avl_tree_t stacks_ulwp_byid;
/*ARGSUSED*/
int
stacks_ulwp_walk(uintptr_t addr, ulwp_t *ulwp, void *ignored)
{
stacks_ulwp_t *sulwp = mdb_alloc(sizeof (stacks_ulwp_t), UM_SLEEP);
sulwp->sulwp_id = ulwp->ul_lwpid;
sulwp->sulwp_addr = addr;
if (avl_find(&stacks_ulwp_byid, sulwp, NULL) != NULL) {
mdb_warn("found multiple LWPs with ID %d!", ulwp->ul_lwpid);
return (WALK_ERR);
}
avl_add(&stacks_ulwp_byid, sulwp);
return (WALK_NEXT);
}
static int
stacks_ulwp_compare(const void *l, const void *r)
{
const stacks_ulwp_t *lhs = l;
const stacks_ulwp_t *rhs = r;
if (lhs->sulwp_id > rhs->sulwp_id)
return (1);
if (lhs->sulwp_id < rhs->sulwp_id)
return (-1);
return (0);
}
/*ARGSUSED*/
int
stacks_findstack(uintptr_t addr, findstack_info_t *fsip, uint_t print_warnings)
{
mdb_reg_t reg;
uintptr_t fp;
struct rwindow frame;
avl_tree_t *tree = &stacks_ulwp_byid;
stacks_ulwp_t *sulwp, cmp;
ulwp_t ulwp;
fsip->fsi_failed = 0;
fsip->fsi_pc = 0;
fsip->fsi_sp = 0;
fsip->fsi_depth = 0;
fsip->fsi_overflow = 0;
if (!stacks_ulwp_initialized) {
avl_create(tree, stacks_ulwp_compare, sizeof (stacks_ulwp_t),
offsetof(stacks_ulwp_t, sulwp_node));
if (mdb_walk("ulwp",
(mdb_walk_cb_t)stacks_ulwp_walk, NULL) != 0) {
mdb_warn("couldn't walk 'ulwp'");
return (-1);
}
stacks_ulwp_initialized = B_TRUE;
}
bzero(&cmp, sizeof (cmp));
cmp.sulwp_id = (lwpid_t)addr;
if ((sulwp = avl_find(tree, &cmp, NULL)) == NULL) {
mdb_warn("couldn't find ulwp_t for tid %d\n", cmp.sulwp_id);
return (-1);
}
if (mdb_vread(&ulwp, sizeof (ulwp), sulwp->sulwp_addr) == -1) {
mdb_warn("couldn't read ulwp_t for tid %d at %p",
cmp.sulwp_id, sulwp->sulwp_addr);
return (-1);
}
fsip->fsi_tstate = ulwp.ul_sleepq != NULL;
fsip->fsi_sobj_ops = (uintptr_t)(ulwp.ul_sleepq == NULL ? 0 :
(ulwp.ul_qtype == MX ? STACKS_SOBJ_MX : STACKS_SOBJ_CV));
if (mdb_getareg(addr, STACKS_REGS_FP, ®) != 0) {
mdb_warn("couldn't read frame pointer for thread 0x%p", addr);
return (-1);
}
fsip->fsi_sp = fp = (uintptr_t)reg;
#if !defined(__i386)
if (mdb_getareg(addr, STACKS_REGS_RC, ®) != 0) {
mdb_warn("couldn't read program counter for thread 0x%p", addr);
return (-1);
}
fsip->fsi_pc = (uintptr_t)reg;
#endif
while (fp != 0) {
if (mdb_vread(&frame, sizeof (frame), fp) == -1) {
mdb_warn("couldn't read frame for thread 0x%p at %p",
addr, fp);
return (-1);
}
if (frame.rw_rtn == 0)
break;
if (fsip->fsi_depth < fsip->fsi_max_depth) {
fsip->fsi_stack[fsip->fsi_depth++] = frame.rw_rtn;
} else {
fsip->fsi_overflow = 1;
break;
}
fp = frame.rw_fp + STACK_BIAS;
}
return (0);
}
void
stacks_findstack_cleanup()
{
avl_tree_t *tree = &stacks_ulwp_byid;
void *cookie = NULL;
stacks_ulwp_t *sulwp;
if (!stacks_ulwp_initialized)
return;
while ((sulwp = avl_destroy_nodes(tree, &cookie)) != NULL)
mdb_free(sulwp, sizeof (stacks_ulwp_t));
bzero(tree, sizeof (*tree));
stacks_ulwp_initialized = B_FALSE;
}
void
stacks_help(void)
{
mdb_printf(
"::stacks processes all of the thread stacks in the process, grouping\n"
"together threads which have the same:\n"
"\n"
" * Thread state,\n"
" * Sync object type, and\n"
" * PCs in their stack trace.\n"
"\n"
"The default output (no address or options) is just a dump of the thread\n"
"groups in the process. For a view of active threads, use \"::stacks -i\",\n"
"which filters out threads sleeping on a CV. More general filtering options\n"
"are described below, in the \"FILTERS\" section.\n"
"\n"
"::stacks can be used in a pipeline. The input to ::stacks is one or more\n"
"thread IDs. When output into a pipe, ::stacks prints all of the threads \n"
"input, filtered by the given filtering options. This means that multiple\n"
"::stacks invocations can be piped together to achieve more complicated\n"
"filters. For example, to get threads which have both '__door_return' and\n"
"'mutex_lock' in their stack trace, you could do:\n"
"\n"
" ::stacks -c __door_return | ::stacks -c mutex_lock\n"
"\n"
"To get the full list of threads in each group, use the '-a' flag:\n"
"\n"
" ::stacks -a\n"
"\n");
mdb_dec_indent(2);
mdb_printf("%<b>OPTIONS%</b>\n");
mdb_inc_indent(2);
mdb_printf("%s",
" -a Print all of the grouped threads, instead of just a count.\n"
" -f Force a re-run of the thread stack gathering.\n"
" -v Be verbose about thread stack gathering.\n"
"\n");
mdb_dec_indent(2);
mdb_printf("%<b>FILTERS%</b>\n");
mdb_inc_indent(2);
mdb_printf("%s",
" -i Show active threads; equivalent to '-S CV'.\n"
" -c func[+offset]\n"
" Only print threads whose stacks contain func/func+offset.\n"
" -C func[+offset]\n"
" Only print threads whose stacks do not contain func/func+offset.\n"
" -m module\n"
" Only print threads whose stacks contain functions from module.\n"
" -M module\n"
" Only print threads whose stacks do not contain functions from\n"
" module.\n"
" -s {type | ALL}\n"
" Only print threads which are on a 'type' synchronization object\n"
" (SOBJ).\n"
" -S {type | ALL}\n"
" Only print threads which are not on a 'type' SOBJ.\n"
" -t tstate\n"
" Only print threads which are in thread state 'tstate'.\n"
" -T tstate\n"
" Only print threads which are not in thread state 'tstate'.\n"
"\n");
}