Print this page
8158 Want named threads API
9857 proc manpages should have LIBRARY section
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/ptools/pstack/pstack.c
+++ new/usr/src/cmd/ptools/pstack/pstack.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 + *
25 + * Copyright 2018 Joyent, Inc.
24 26 */
25 27
26 28 #include <sys/isa_defs.h>
27 29
28 30 #include <stdio.h>
29 31 #include <stdio_ext.h>
30 32 #include <fcntl.h>
31 33 #include <ctype.h>
32 34 #include <string.h>
33 35 #include <signal.h>
34 36 #include <dirent.h>
35 37 #include <errno.h>
36 38 #include <stdlib.h>
37 39 #include <stdarg.h>
38 40 #include <unistd.h>
39 41 #include <sys/types.h>
40 42 #include <sys/stat.h>
41 43 #include <sys/stack.h>
42 44 #include <link.h>
43 45 #include <limits.h>
44 46 #include <libelf.h>
45 47 #include <thread_db.h>
46 48 #include <libproc.h>
47 49 #include <setjmp.h>
48 50
49 51 static char *command;
50 52 static int Fflag;
51 53 static int is64;
52 54 static GElf_Sym sigh;
53 55
54 56 /*
55 57 * To keep the list of user-level threads for a multithreaded process.
56 58 */
57 59 struct threadinfo {
58 60 struct threadinfo *next;
59 61 id_t threadid;
60 62 id_t lwpid;
61 63 td_thr_state_e state;
62 64 uintptr_t startfunc;
63 65 uintptr_t exitval;
64 66 prgregset_t regs;
65 67 };
66 68
67 69 static struct threadinfo *thr_head, *thr_tail;
68 70
69 71 #define TRUE 1
70 72 #define FALSE 0
71 73
72 74 #define MAX_ARGS 8
73 75
74 76 /*
75 77 * To support debugging java programs, we display java frames within a stack.
76 78 * The logic to walk the java frames is contained in libjvm_db.so, which is
77 79 * found in the same directory as libjvm.so, linked with the program. If we are
78 80 * debugging a 32-bit app with a 64-binary, then the debugging library is found
79 81 * in the '64' subdirectory. If we find libjvm_db.so, then we fill in these
80 82 * stub routines.
81 83 */
82 84 typedef struct jvm_agent jvm_agent_t;
83 85 typedef int java_stack_f(void *, prgregset_t, const char *, int, int, void *);
84 86
85 87 /*
86 88 * The j_agent_create function takes a version parameter. This ensures that the
87 89 * interface can evolve appropriately.
88 90 */
89 91 #define JVM_DB_VERSION 1
90 92 static void *libjvm;
91 93 typedef jvm_agent_t *(*j_agent_create_f)(struct ps_prochandle *, int);
92 94 typedef void (*j_agent_destroy_f)(jvm_agent_t *);
93 95 typedef int (*j_frame_iter_f)(jvm_agent_t *, prgregset_t, java_stack_f *,
94 96 void *);
95 97
96 98 static j_agent_create_f j_agent_create;
97 99 static j_agent_destroy_f j_agent_destroy;
98 100 static j_frame_iter_f j_frame_iter;
99 101
100 102 static jvm_agent_t *load_libjvm(struct ps_prochandle *P);
101 103 static void reset_libjvm(jvm_agent_t *);
102 104
103 105 /*
104 106 * Similar to what's done for debugging java programs, here are prototypes for
105 107 * the library that allows us to debug Python programs.
106 108 */
107 109 #define PYDB_VERSION 1
108 110 static void *libpython;
109 111
110 112 typedef struct pydb_agent pydb_agent_t;
111 113
112 114 typedef pydb_agent_t *(*pydb_agent_create_f)(struct ps_prochandle *P, int vers);
113 115 typedef void (*pydb_agent_destroy_f)(pydb_agent_t *py);
114 116 typedef int (*pydb_pc_frameinfo_f)(pydb_agent_t *py, uintptr_t pc,
115 117 uintptr_t frame_addr, char *fbuf, size_t bufsz);
116 118
117 119 static pydb_agent_create_f pydb_agent_create;
118 120 static pydb_agent_destroy_f pydb_agent_destroy;
119 121 static pydb_pc_frameinfo_f pydb_pc_frameinfo;
120 122
121 123 static pydb_agent_t *load_libpython(struct ps_prochandle *P);
122 124 static void reset_libpython(pydb_agent_t *);
123 125 /*
124 126 * Since we must maintain both a proc handle and a jvm handle, this structure
125 127 * is the basic type that gets passed around.
126 128 */
127 129 typedef struct pstack_handle {
128 130 struct ps_prochandle *proc;
129 131 jvm_agent_t *jvm;
↓ open down ↓ |
96 lines elided |
↑ open up ↑ |
130 132 int ignore_frame;
131 133 const char *lwps;
132 134 int count;
133 135 pydb_agent_t *pydb;
134 136 } pstack_handle_t;
135 137
136 138 static int thr_stack(const td_thrhandle_t *, void *);
137 139 static void free_threadinfo(void);
138 140 static struct threadinfo *find_thread(id_t);
139 141 static int all_call_stacks(pstack_handle_t *, int);
140 -static void tlhead(id_t, id_t);
142 +static void tlhead(id_t, id_t, const char *);
141 143 static int print_frame(void *, prgregset_t, uint_t, const long *);
142 144 static void print_zombie(struct ps_prochandle *, struct threadinfo *);
143 145 static void print_syscall(const lwpstatus_t *, prgregset_t);
144 146 static void call_stack(pstack_handle_t *, const lwpstatus_t *);
145 147
146 148 /*
147 149 * The number of active and zombie threads.
148 150 */
149 151 static int nthreads;
150 152
151 153 int
152 154 main(int argc, char **argv)
153 155 {
154 156 int retc = 0;
155 157 int opt;
156 158 int errflg = FALSE;
157 159 core_content_t content = CC_CONTENT_DATA | CC_CONTENT_ANON |
158 160 CC_CONTENT_STACK;
159 161 struct rlimit rlim;
160 162
161 163 if ((command = strrchr(argv[0], '/')) != NULL)
162 164 command++;
163 165 else
164 166 command = argv[0];
165 167
166 168 /* options */
167 169 while ((opt = getopt(argc, argv, "F")) != EOF) {
168 170 switch (opt) {
169 171 case 'F':
170 172 /*
171 173 * If the user specifies the force option, we'll
172 174 * consent to printing out other threads' stacks
173 175 * even if the main stack is absent.
174 176 */
175 177 content &= ~CC_CONTENT_STACK;
176 178 Fflag = PGRAB_FORCE;
177 179 break;
178 180 default:
179 181 errflg = TRUE;
180 182 break;
181 183 }
182 184 }
183 185
184 186 argc -= optind;
185 187 argv += optind;
186 188
187 189 if (errflg || argc <= 0) {
188 190 (void) fprintf(stderr,
189 191 "usage:\t%s [-F] { pid | core }[/lwps] ...\n", command);
190 192 (void) fprintf(stderr, " (show process call stack)\n");
191 193 (void) fprintf(stderr,
192 194 " -F: force grabbing of the target process\n");
193 195 exit(2);
194 196 }
195 197
196 198 /*
197 199 * Make sure we'll have enough file descriptors to handle a target
198 200 * that has many many mappings.
199 201 */
200 202 if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
201 203 rlim.rlim_cur = rlim.rlim_max;
202 204 (void) setrlimit(RLIMIT_NOFILE, &rlim);
203 205 (void) enable_extended_FILE_stdio(-1, -1);
204 206 }
205 207
206 208 (void) proc_initstdio();
207 209
208 210 while (--argc >= 0) {
209 211 int gcode;
210 212 psinfo_t psinfo;
211 213 const psinfo_t *tpsinfo;
212 214 struct ps_prochandle *Pr = NULL;
213 215 td_thragent_t *Tap;
214 216 int threaded;
215 217 pstack_handle_t handle;
216 218 const char *lwps, *arg;
217 219
218 220 (void) proc_flushstdio();
219 221
220 222 arg = *argv++;
221 223
222 224 if ((Pr = proc_arg_xgrab(arg, NULL, PR_ARG_ANY,
223 225 Fflag, &gcode, &lwps)) == NULL) {
224 226 (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
225 227 command, arg, Pgrab_error(gcode));
226 228 retc++;
227 229 continue;
228 230 }
229 231
230 232 if ((tpsinfo = Ppsinfo(Pr)) == NULL) {
231 233 (void) fprintf(stderr, "%s: cannot examine %s: "
232 234 "lost control of process\n", command, arg);
233 235 Prelease(Pr, 0);
234 236 retc++;
235 237 continue;
236 238 }
237 239 (void) memcpy(&psinfo, tpsinfo, sizeof (psinfo_t));
238 240 proc_unctrl_psinfo(&psinfo);
239 241
240 242 if (Pstate(Pr) == PS_DEAD) {
241 243 if ((Pcontent(Pr) & content) != content) {
242 244 (void) fprintf(stderr, "%s: core '%s' has "
243 245 "insufficient content\n", command, arg);
244 246 retc++;
245 247 continue;
246 248 }
247 249 (void) printf("core '%s' of %d:\t%.70s\n",
248 250 arg, (int)psinfo.pr_pid, psinfo.pr_psargs);
249 251 } else {
250 252 (void) printf("%d:\t%.70s\n",
251 253 (int)psinfo.pr_pid, psinfo.pr_psargs);
252 254 }
253 255
254 256 is64 = (psinfo.pr_dmodel == PR_MODEL_LP64);
255 257
256 258 if (Pgetauxval(Pr, AT_BASE) != -1L && Prd_agent(Pr) == NULL) {
257 259 (void) fprintf(stderr, "%s: warning: librtld_db failed "
258 260 "to initialize; symbols from shared libraries will "
259 261 "not be available\n", command);
260 262 }
261 263
262 264 /*
263 265 * First we need to get a thread agent handle.
264 266 */
265 267 if (td_init() != TD_OK ||
266 268 td_ta_new(Pr, &Tap) != TD_OK) /* no libc */
267 269 threaded = FALSE;
268 270 else {
269 271 /*
270 272 * Iterate over all threads, calling:
271 273 * thr_stack(td_thrhandle_t *Thp, NULL);
272 274 * for each one to generate the list of threads.
273 275 */
274 276 nthreads = 0;
275 277 (void) td_ta_thr_iter(Tap, thr_stack, NULL,
276 278 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
277 279 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
278 280
279 281 (void) td_ta_delete(Tap);
280 282 threaded = TRUE;
281 283 }
282 284
283 285 handle.proc = Pr;
284 286 handle.jvm = load_libjvm(Pr);
285 287 handle.pydb = load_libpython(Pr);
286 288 handle.lwps = lwps;
287 289 handle.count = 0;
288 290
289 291 if (all_call_stacks(&handle, threaded) != 0)
290 292 retc++;
291 293 if (threaded)
292 294 free_threadinfo();
293 295
294 296 reset_libjvm(handle.jvm);
295 297 reset_libpython(handle.pydb);
296 298 Prelease(Pr, 0);
297 299
298 300 if (handle.count == 0)
299 301 (void) fprintf(stderr, "%s: no matching LWPs found\n",
300 302 command);
301 303 }
302 304
303 305 (void) proc_finistdio();
304 306
305 307 return (retc);
306 308 }
307 309
308 310 /*
309 311 * Thread iteration call-back function.
310 312 * Called once for each user-level thread.
311 313 * Used to build the list of all threads.
312 314 */
313 315 /* ARGSUSED1 */
314 316 static int
315 317 thr_stack(const td_thrhandle_t *Thp, void *cd)
316 318 {
317 319 td_thrinfo_t thrinfo;
318 320 struct threadinfo *tip;
319 321 td_err_e error;
320 322
321 323 if (td_thr_get_info(Thp, &thrinfo) != TD_OK)
322 324 return (0);
323 325
324 326 tip = malloc(sizeof (struct threadinfo));
325 327 tip->next = NULL;
326 328 tip->threadid = thrinfo.ti_tid;
327 329 tip->lwpid = thrinfo.ti_lid;
328 330 tip->state = thrinfo.ti_state;
329 331 tip->startfunc = thrinfo.ti_startfunc;
330 332 tip->exitval = (uintptr_t)thrinfo.ti_exitval;
331 333 nthreads++;
332 334
333 335 if (thrinfo.ti_state == TD_THR_ZOMBIE ||
334 336 ((error = td_thr_getgregs(Thp, tip->regs)) != TD_OK &&
335 337 error != TD_PARTIALREG))
336 338 (void) memset(tip->regs, 0, sizeof (prgregset_t));
337 339
338 340 if (thr_tail)
339 341 thr_tail->next = tip;
340 342 else
341 343 thr_head = tip;
342 344 thr_tail = tip;
343 345
344 346 return (0);
345 347 }
346 348
347 349 static void
348 350 free_threadinfo()
349 351 {
350 352 struct threadinfo *tip = thr_head;
351 353 struct threadinfo *next;
352 354
353 355 while (tip) {
354 356 next = tip->next;
355 357 free(tip);
356 358 tip = next;
357 359 }
358 360
359 361 thr_head = thr_tail = NULL;
360 362 }
361 363
362 364 /*
363 365 * Find and eliminate the thread corresponding to the given lwpid.
364 366 */
365 367 static struct threadinfo *
366 368 find_thread(id_t lwpid)
367 369 {
368 370 struct threadinfo *tip;
369 371
370 372 for (tip = thr_head; tip; tip = tip->next) {
371 373 if (lwpid == tip->lwpid) {
372 374 tip->lwpid = 0;
↓ open down ↓ |
222 lines elided |
↑ open up ↑ |
373 375 return (tip);
374 376 }
375 377 }
376 378 return (NULL);
377 379 }
378 380
379 381 static int
380 382 thread_call_stack(void *data, const lwpstatus_t *psp,
381 383 const lwpsinfo_t *pip)
382 384 {
385 + char lwpname[THREAD_NAME_MAX] = "";
383 386 pstack_handle_t *h = data;
384 387 lwpstatus_t lwpstatus;
385 388 struct threadinfo *tip;
386 389
387 390 if (!proc_lwp_in_set(h->lwps, pip->pr_lwpid))
388 391 return (0);
389 392 h->count++;
390 393
391 394 if ((tip = find_thread(pip->pr_lwpid)) == NULL)
392 395 return (0);
393 396
394 - tlhead(tip->threadid, pip->pr_lwpid);
397 + (void) Plwp_getname(h->proc, pip->pr_lwpid,
398 + lwpname, sizeof (lwpname));
399 +
400 + tlhead(tip->threadid, pip->pr_lwpid, lwpname);
395 401 tip->threadid = 0; /* finish eliminating tid */
396 402 if (psp)
397 403 call_stack(h, psp);
398 404 else {
399 405 if (tip->state == TD_THR_ZOMBIE)
400 406 print_zombie(h->proc, tip);
401 407 else {
402 408 (void) memset(&lwpstatus, 0, sizeof (lwpstatus));
403 409 (void) memcpy(lwpstatus.pr_reg, tip->regs,
404 410 sizeof (prgregset_t));
405 411 call_stack(h, &lwpstatus);
406 412 }
407 413 }
408 414 return (0);
409 415 }
410 416
411 417 static int
412 418 lwp_call_stack(void *data,
413 - const lwpstatus_t *psp, const lwpsinfo_t *pip)
419 + const lwpstatus_t *psp, const lwpsinfo_t *pip)
414 420 {
421 + char lwpname[THREAD_NAME_MAX] = "";
415 422 pstack_handle_t *h = data;
416 423
417 424 if (!proc_lwp_in_set(h->lwps, pip->pr_lwpid))
418 425 return (0);
419 426 h->count++;
420 427
421 - tlhead(0, pip->pr_lwpid);
428 + (void) Plwp_getname(h->proc, pip->pr_lwpid,
429 + lwpname, sizeof (lwpname));
430 +
431 + tlhead(0, pip->pr_lwpid, lwpname);
422 432 if (psp)
423 433 call_stack(h, psp);
424 434 else
425 435 (void) printf("\t** zombie "
426 436 "(exited, not detached, not yet joined) **\n");
427 437 return (0);
428 438 }
429 439
430 440 static int
431 441 all_call_stacks(pstack_handle_t *h, int dothreads)
432 442 {
433 443 struct ps_prochandle *Pr = h->proc;
434 444 pstatus_t status = *Pstatus(Pr);
435 445
436 446 (void) memset(&sigh, 0, sizeof (GElf_Sym));
437 447 (void) Plookup_by_name(Pr, "libc.so", "sigacthandler", &sigh);
438 448
439 449 if ((status.pr_nlwp + status.pr_nzomb) <= 1 &&
440 450 !(dothreads && nthreads > 1)) {
441 451 if (proc_lwp_in_set(h->lwps, status.pr_lwp.pr_lwpid)) {
442 452 call_stack(h, &status.pr_lwp);
443 453 h->count++;
444 454 }
445 455 } else {
446 456 lwpstatus_t lwpstatus;
447 457 struct threadinfo *tip;
448 458 id_t tid;
449 459
450 460 if (dothreads)
451 461 (void) Plwp_iter_all(Pr, thread_call_stack, h);
452 462 else
453 463 (void) Plwp_iter_all(Pr, lwp_call_stack, h);
454 464
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
455 465 /* for each remaining thread w/o an lwp */
456 466 (void) memset(&lwpstatus, 0, sizeof (lwpstatus));
457 467 for (tip = thr_head; tip; tip = tip->next) {
458 468
459 469 if (!proc_lwp_in_set(h->lwps, tip->lwpid))
460 470 tip->threadid = 0;
461 471
462 472 if ((tid = tip->threadid) != 0) {
463 473 (void) memcpy(lwpstatus.pr_reg, tip->regs,
464 474 sizeof (prgregset_t));
465 - tlhead(tid, tip->lwpid);
475 + tlhead(tid, tip->lwpid, NULL);
466 476 if (tip->state == TD_THR_ZOMBIE)
467 477 print_zombie(Pr, tip);
468 478 else
469 479 call_stack(h, &lwpstatus);
470 480 }
471 481 tip->threadid = 0;
472 482 tip->lwpid = 0;
473 483 }
474 484 }
475 485 return (0);
476 486 }
477 487
488 +/* The width of the header */
489 +#define HEAD_WIDTH (62)
478 490 static void
479 -tlhead(id_t threadid, id_t lwpid)
491 +tlhead(id_t threadid, id_t lwpid, const char *name)
480 492 {
493 + char buf[128] = { 0 };
494 + char num[16];
495 + ssize_t amt = 0;
496 + int i;
497 +
481 498 if (threadid == 0 && lwpid == 0)
482 499 return;
483 500
484 - (void) printf("-----------------");
501 + if (lwpid > 0) {
502 + (void) snprintf(num, sizeof (num), "%d", (int)lwpid);
503 + (void) strlcat(buf, "thread# ", sizeof (buf));
504 + (void) strlcat(buf, num, sizeof (buf));
505 + }
485 506
486 - if (threadid && lwpid)
487 - (void) printf(" lwp# %d / thread# %d ",
488 - (int)lwpid, (int)threadid);
489 - else if (threadid)
490 - (void) printf("--------- thread# %d ", (int)threadid);
491 - else if (lwpid)
492 - (void) printf(" lwp# %d ------------", (int)lwpid);
507 + if (threadid > 0) {
508 + (void) snprintf(num, sizeof (num), "%d", (int)threadid);
509 + if (lwpid > 0)
510 + (void) strlcat(buf, " / ", sizeof (buf));
511 + (void) strlcat(buf, "lwp# ", sizeof (buf));
512 + (void) strlcat(buf, num, sizeof (buf));
513 + }
493 514
494 - (void) printf("--------------------\n");
515 + if (name != NULL && strlen(name) > 0) {
516 + (void) strlcat(buf, " [", sizeof (buf));
517 + (void) strlcat(buf, name, sizeof (buf));
518 + (void) strlcat(buf, "]", sizeof (buf));
519 + }
520 +
521 + amt = (HEAD_WIDTH - strlen(buf) - 2);
522 + if (amt < 4)
523 + amt = 4;
524 +
525 + for (i = 0; i < amt / 2; i++)
526 + (void) putc('-', stdout);
527 + (void) printf(" %s ", buf);
528 + for (i = 0; i < (amt / 2) + (amt % 2); i++)
529 + (void) putc('-', stdout);
530 + (void) putc('\n', stdout);
495 531 }
496 532
497 533 /*ARGSUSED*/
498 534 static int
499 535 print_java_frame(void *cld, prgregset_t gregs, const char *name, int bci,
500 536 int line, void *handle)
501 537 {
502 538 int length = (is64 ? 16 : 8);
503 539
504 540 (void) printf(" %.*lx * %s", length, (long)gregs[R_PC], name);
505 541
506 542 if (bci != -1) {
507 543 (void) printf("+%d", bci);
508 544 if (line)
509 545 (void) printf(" (line %d)", line);
510 546 }
511 547 (void) printf("\n");
512 548
513 549 return (0);
514 550 }
515 551
516 552 static sigjmp_buf jumpbuf;
517 553
518 554 /*ARGSUSED*/
519 555 static void
520 556 fatal_signal(int signo)
521 557 {
522 558 siglongjmp(jumpbuf, 1);
523 559 }
524 560
525 561 static int
526 562 print_frame(void *cd, prgregset_t gregs, uint_t argc, const long *argv)
527 563 {
528 564 pstack_handle_t *h = cd;
529 565 struct ps_prochandle *Pr = h->proc;
530 566 uintptr_t pc = gregs[R_PC];
531 567 char buff[255];
532 568 GElf_Sym sym;
533 569 uintptr_t start;
534 570 int length = (is64? 16 : 8);
535 571 int i;
536 572
537 573 /*
538 574 * If we are in a system call, we display the entry frame in a more
539 575 * readable manner, using the name of the system call. In this case, we
540 576 * want to ignore this first frame, since we already displayed it
541 577 * separately.
542 578 */
543 579 if (h->ignore_frame) {
544 580 h->ignore_frame = 0;
545 581 return (0);
546 582 }
547 583
548 584 (void) sprintf(buff, "%.*lx", length, (long)pc);
549 585 (void) strcpy(buff + length, " ????????");
550 586 if (Plookup_by_addr(Pr, pc,
551 587 buff + 1 + length, sizeof (buff) - 1 - length, &sym) == 0) {
552 588 start = sym.st_value;
553 589 } else if (h->jvm != NULL) {
554 590 int ret;
555 591 void (*segv)(int), (*bus)(int), (*ill)(int);
556 592
557 593 segv = signal(SIGSEGV, fatal_signal);
558 594 bus = signal(SIGBUS, fatal_signal);
559 595 ill = signal(SIGILL, fatal_signal);
560 596
561 597 /* Insure against a bad libjvm_db */
562 598 if (sigsetjmp(jumpbuf, 0) == 0)
563 599 ret = j_frame_iter(h->jvm, gregs, print_java_frame,
564 600 NULL);
565 601 else
566 602 ret = -1;
567 603
568 604 (void) signal(SIGSEGV, segv);
569 605 (void) signal(SIGBUS, bus);
570 606 (void) signal(SIGILL, ill);
571 607
572 608 if (ret == 0)
573 609 return (ret);
574 610 } else {
575 611 start = pc;
576 612 }
577 613
578 614 (void) printf(" %-17s (", buff);
579 615 for (i = 0; i < argc && i < MAX_ARGS; i++)
580 616 (void) printf((i+1 == argc) ? "%lx" : "%lx, ", argv[i]);
581 617 if (i != argc)
582 618 (void) printf("...");
583 619 (void) printf((start != pc) ? ") + %lx\n" : ")\n", (long)(pc - start));
584 620
585 621 if (h->pydb != NULL && argc > 0) {
586 622 char buf_py[1024];
587 623 int rc;
588 624
589 625 rc = pydb_pc_frameinfo(h->pydb, pc, argv[0], buf_py,
590 626 sizeof (buf_py));
591 627 if (rc == 0) {
592 628 (void) printf(" %s", buf_py);
593 629 }
594 630 }
595 631
596 632 /*
597 633 * If the frame's pc is in the "sigh" (a.k.a. signal handler, signal
598 634 * hack, or *sigh* ...) range, then we're about to cross a signal
599 635 * frame. The signal number is the first argument to this function.
600 636 */
601 637 if (pc - sigh.st_value < sigh.st_size) {
602 638 if (sig2str((int)argv[0], buff) == -1)
603 639 (void) strcpy(buff, " Unknown");
604 640 (void) printf(" --- called from signal handler with "
605 641 "signal %d (SIG%s) ---\n", (int)argv[0], buff);
606 642 }
607 643
608 644 return (0);
609 645 }
610 646
611 647 static void
612 648 print_zombie(struct ps_prochandle *Pr, struct threadinfo *tip)
613 649 {
614 650 char buff[255];
615 651 GElf_Sym sym;
616 652 uintptr_t start;
617 653 int length = (is64? 16 : 8);
618 654
619 655 (void) sprintf(buff, "%.*lx", length, (long)tip->startfunc);
620 656 (void) strcpy(buff + length, " ????????");
621 657 if (Plookup_by_addr(Pr, tip->startfunc,
622 658 buff + 1 + length, sizeof (buff) - 1 - length, &sym) == 0)
623 659 start = sym.st_value;
624 660 else
625 661 start = tip->startfunc;
626 662 (void) printf(" %s()", buff);
627 663 if (start != tip->startfunc) /* doesn't happen? */
628 664 (void) printf("+%lx", (long)(tip->startfunc - start));
629 665 (void) printf(", exit value = 0x%.*lx\n", length, (long)tip->exitval);
630 666 (void) printf("\t** zombie "
631 667 "(exited, not detached, not yet joined) **\n");
632 668 }
633 669
634 670 static void
635 671 print_syscall(const lwpstatus_t *psp, prgregset_t reg)
636 672 {
637 673 char sname[32];
638 674 int length = (is64? 16 : 8);
639 675 uint_t i;
640 676
641 677 (void) proc_sysname(psp->pr_syscall, sname, sizeof (sname));
642 678 (void) printf(" %.*lx %-8s (", length, (long)reg[R_PC], sname);
643 679 for (i = 0; i < psp->pr_nsysarg; i++)
644 680 (void) printf((i+1 == psp->pr_nsysarg)? "%lx" : "%lx, ",
645 681 (long)psp->pr_sysarg[i]);
646 682 (void) printf(")\n");
647 683 }
648 684
649 685 static void
650 686 call_stack(pstack_handle_t *h, const lwpstatus_t *psp)
651 687 {
652 688 prgregset_t reg;
653 689
654 690 (void) memcpy(reg, psp->pr_reg, sizeof (reg));
655 691
656 692 if ((psp->pr_flags & (PR_ASLEEP|PR_VFORKP)) ||
657 693 ((psp->pr_flags & PR_ISTOP) &&
658 694 (psp->pr_why == PR_SYSENTRY ||
659 695 psp->pr_why == PR_SYSEXIT))) {
660 696 print_syscall(psp, reg);
661 697 h->ignore_frame = 1;
662 698 } else {
663 699 h->ignore_frame = 0;
664 700 }
665 701
666 702 (void) Pstack_iter(h->proc, reg, print_frame, h);
667 703 }
668 704
669 705 /*ARGSUSED*/
670 706 static int
671 707 jvm_object_iter(void *cd, const prmap_t *pmp, const char *obj)
672 708 {
673 709 char path[PATH_MAX];
674 710 char *name;
675 711 char *s1, *s2;
676 712 struct ps_prochandle *Pr = cd;
677 713
678 714 if ((name = strstr(obj, "/libjvm.so")) == NULL)
679 715 name = strstr(obj, "/libjvm_g.so");
680 716
681 717 if (name) {
682 718 (void) strcpy(path, obj);
683 719 if (Pstatus(Pr)->pr_dmodel != PR_MODEL_NATIVE) {
684 720 s1 = name;
685 721 s2 = path + (s1 - obj);
686 722 (void) strcpy(s2, "/64");
687 723 s2 += 3;
688 724 (void) strcpy(s2, s1);
689 725 }
690 726
691 727 s1 = strstr(obj, ".so");
692 728 s2 = strstr(path, ".so");
693 729 (void) strcpy(s2, "_db");
694 730 s2 += 3;
695 731 (void) strcpy(s2, s1);
696 732
697 733 if ((libjvm = dlopen(path, RTLD_LAZY|RTLD_GLOBAL)) != NULL)
698 734 return (1);
699 735 }
700 736
701 737 return (0);
702 738 }
703 739
704 740 static jvm_agent_t *
705 741 load_libjvm(struct ps_prochandle *Pr)
706 742 {
707 743 jvm_agent_t *ret;
708 744
709 745 /*
710 746 * Iterate through all the loaded objects in the target, looking
711 747 * for libjvm.so. If we find libjvm.so we'll try to load the
712 748 * corresponding libjvm_db.so that lives in the same directory.
713 749 *
714 750 * At first glance it seems like we'd want to use
715 751 * Pobject_iter_resolved() here since we'd want to make sure that
716 752 * we have the full path to the libjvm.so. But really, we don't
717 753 * want that since we're going to be dlopen()ing a library and
718 754 * executing code from that path, and therefore we don't want to
719 755 * load any library code that could be from a zone since it could
720 756 * have been replaced with a trojan. Hence, we use Pobject_iter().
721 757 * So if we're debugging java processes in a zone from the global
722 758 * zone, and we want to get proper java stack stack frames, then
723 759 * the same jvm that is running within the zone needs to be
724 760 * installed in the global zone.
725 761 */
726 762 (void) Pobject_iter(Pr, jvm_object_iter, Pr);
727 763
728 764 if (libjvm) {
729 765 j_agent_create = (j_agent_create_f)
730 766 dlsym(libjvm, "Jagent_create");
731 767 j_agent_destroy = (j_agent_destroy_f)
732 768 dlsym(libjvm, "Jagent_destroy");
733 769 j_frame_iter = (j_frame_iter_f)
734 770 dlsym(libjvm, "Jframe_iter");
735 771
736 772 if (j_agent_create == NULL || j_agent_destroy == NULL ||
737 773 j_frame_iter == NULL ||
738 774 (ret = j_agent_create(Pr, JVM_DB_VERSION)) == NULL) {
739 775 reset_libjvm(NULL);
740 776 return (NULL);
741 777 }
742 778
743 779 return (ret);
744 780 }
745 781
746 782 return (NULL);
747 783 }
748 784
749 785 static void
750 786 reset_libjvm(jvm_agent_t *agent)
751 787 {
752 788 if (libjvm) {
753 789 if (agent)
754 790 j_agent_destroy(agent);
755 791
756 792 (void) dlclose(libjvm);
757 793 }
758 794
759 795 j_agent_create = NULL;
760 796 j_agent_destroy = NULL;
761 797 j_frame_iter = NULL;
762 798 libjvm = NULL;
763 799 }
764 800
765 801 /*ARGSUSED*/
766 802 static int
767 803 python_object_iter(void *cd, const prmap_t *pmp, const char *obj)
768 804 {
769 805 char path[PATH_MAX];
770 806 char *name;
771 807 char *s1, *s2;
772 808 struct ps_prochandle *Pr = cd;
773 809
774 810 name = strstr(obj, "/libpython");
775 811
776 812 if (name) {
777 813 (void) strcpy(path, obj);
778 814 if (Pstatus(Pr)->pr_dmodel != PR_MODEL_NATIVE) {
779 815 s1 = name;
780 816 s2 = path + (s1 - obj);
781 817 (void) strcpy(s2, "/64");
782 818 s2 += 3;
783 819 (void) strcpy(s2, s1);
784 820 }
785 821
786 822 s1 = strstr(obj, ".so");
787 823 s2 = strstr(path, ".so");
788 824 (void) strcpy(s2, "_db");
789 825 s2 += 3;
790 826 (void) strcpy(s2, s1);
791 827
792 828 if ((libpython = dlopen(path, RTLD_LAZY|RTLD_GLOBAL)) != NULL)
793 829 return (1);
794 830 }
795 831
796 832 return (0);
797 833 }
798 834
799 835 static pydb_agent_t *
800 836 load_libpython(struct ps_prochandle *Pr)
801 837 {
802 838 pydb_agent_t *pdb;
803 839
804 840 (void) Pobject_iter(Pr, python_object_iter, Pr);
805 841
806 842 if (libpython) {
807 843 pydb_agent_create = (pydb_agent_create_f)
808 844 dlsym(libpython, "pydb_agent_create");
809 845 pydb_agent_destroy = (pydb_agent_destroy_f)
810 846 dlsym(libpython, "pydb_agent_destroy");
811 847 pydb_pc_frameinfo = (pydb_pc_frameinfo_f)
812 848 dlsym(libpython, "pydb_pc_frameinfo");
813 849
814 850 if (pydb_agent_create == NULL || pydb_agent_destroy == NULL ||
815 851 pydb_pc_frameinfo == NULL) {
816 852 (void) dlclose(libpython);
817 853 libpython = NULL;
818 854 return (NULL);
819 855 }
820 856
821 857 pdb = pydb_agent_create(Pr, PYDB_VERSION);
822 858 if (pdb == NULL) {
823 859 (void) dlclose(libpython);
824 860 libpython = NULL;
825 861 return (NULL);
826 862 }
827 863 return (pdb);
828 864 }
829 865
830 866 return (NULL);
831 867 }
832 868
833 869 static void
834 870 reset_libpython(pydb_agent_t *pdb)
835 871 {
836 872 if (libpython != NULL) {
837 873 if (pdb != NULL) {
838 874 pydb_agent_destroy(pdb);
839 875 }
840 876 (void) dlclose(libpython);
841 877 }
842 878
843 879 libpython = NULL;
844 880 pydb_agent_create = NULL;
845 881 pydb_agent_destroy = NULL;
846 882 pydb_pc_frameinfo = NULL;
847 883 }
↓ open down ↓ |
343 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX