4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2018 Joyent, Inc.
28 #include <sys/isa_defs.h>
31 #include <stdio_ext.h>
41 #include <sys/types.h>
43 #include <sys/stack.h>
47 #include <thread_db.h>
57 * To keep the list of user-level threads for a multithreaded process.
60 struct threadinfo
*next
;
69 static struct threadinfo
*thr_head
, *thr_tail
;
77 * To support debugging java programs, we display java frames within a stack.
78 * The logic to walk the java frames is contained in libjvm_db.so, which is
79 * found in the same directory as libjvm.so, linked with the program. If we are
80 * debugging a 32-bit app with a 64-binary, then the debugging library is found
81 * in the '64' subdirectory. If we find libjvm_db.so, then we fill in these
84 typedef struct jvm_agent jvm_agent_t
;
85 typedef int java_stack_f(void *, prgregset_t
, const char *, int, int, void *);
88 * The j_agent_create function takes a version parameter. This ensures that the
89 * interface can evolve appropriately.
91 #define JVM_DB_VERSION 1
93 typedef jvm_agent_t
*(*j_agent_create_f
)(struct ps_prochandle
*, int);
94 typedef void (*j_agent_destroy_f
)(jvm_agent_t
*);
95 typedef int (*j_frame_iter_f
)(jvm_agent_t
*, prgregset_t
, java_stack_f
*,
98 static j_agent_create_f j_agent_create
;
99 static j_agent_destroy_f j_agent_destroy
;
100 static j_frame_iter_f j_frame_iter
;
102 static jvm_agent_t
*load_libjvm(struct ps_prochandle
*P
);
103 static void reset_libjvm(jvm_agent_t
*);
106 * Similar to what's done for debugging java programs, here are prototypes for
107 * the library that allows us to debug Python programs.
109 #define PYDB_VERSION 1
110 static void *libpython
;
112 typedef struct pydb_agent pydb_agent_t
;
114 typedef pydb_agent_t
*(*pydb_agent_create_f
)(struct ps_prochandle
*P
, int vers
);
115 typedef void (*pydb_agent_destroy_f
)(pydb_agent_t
*py
);
116 typedef int (*pydb_pc_frameinfo_f
)(pydb_agent_t
*py
, uintptr_t pc
,
117 uintptr_t frame_addr
, char *fbuf
, size_t bufsz
);
119 static pydb_agent_create_f pydb_agent_create
;
120 static pydb_agent_destroy_f pydb_agent_destroy
;
121 static pydb_pc_frameinfo_f pydb_pc_frameinfo
;
123 static pydb_agent_t
*load_libpython(struct ps_prochandle
*P
);
124 static void reset_libpython(pydb_agent_t
*);
126 * Since we must maintain both a proc handle and a jvm handle, this structure
127 * is the basic type that gets passed around.
129 typedef struct pstack_handle
{
130 struct ps_prochandle
*proc
;
138 static int thr_stack(const td_thrhandle_t
*, void *);
139 static void free_threadinfo(void);
140 static struct threadinfo
*find_thread(id_t
);
141 static int all_call_stacks(pstack_handle_t
*, int);
142 static void tlhead(id_t
, id_t
, const char *);
143 static int print_frame(void *, prgregset_t
, uint_t
, const long *);
144 static void print_zombie(struct ps_prochandle
*, struct threadinfo
*);
145 static void print_syscall(const lwpstatus_t
*, prgregset_t
);
146 static void call_stack(pstack_handle_t
*, const lwpstatus_t
*);
149 * The number of active and zombie threads.
154 main(int argc
, char **argv
)
159 core_content_t content
= CC_CONTENT_DATA
| CC_CONTENT_ANON
|
163 if ((command
= strrchr(argv
[0], '/')) != NULL
)
169 while ((opt
= getopt(argc
, argv
, "F")) != EOF
) {
173 * If the user specifies the force option, we'll
174 * consent to printing out other threads' stacks
175 * even if the main stack is absent.
177 content
&= ~CC_CONTENT_STACK
;
189 if (errflg
|| argc
<= 0) {
190 (void) fprintf(stderr
,
191 "usage:\t%s [-F] { pid | core }[/lwps] ...\n", command
);
192 (void) fprintf(stderr
, " (show process call stack)\n");
193 (void) fprintf(stderr
,
194 " -F: force grabbing of the target process\n");
199 * Make sure we'll have enough file descriptors to handle a target
200 * that has many many mappings.
202 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == 0) {
203 rlim
.rlim_cur
= rlim
.rlim_max
;
204 (void) setrlimit(RLIMIT_NOFILE
, &rlim
);
205 (void) enable_extended_FILE_stdio(-1, -1);
208 (void) proc_initstdio();
210 while (--argc
>= 0) {
213 const psinfo_t
*tpsinfo
;
214 struct ps_prochandle
*Pr
= NULL
;
217 pstack_handle_t handle
;
218 const char *lwps
, *arg
;
220 (void) proc_flushstdio();
224 if ((Pr
= proc_arg_xgrab(arg
, NULL
, PR_ARG_ANY
,
225 Fflag
, &gcode
, &lwps
)) == NULL
) {
226 (void) fprintf(stderr
, "%s: cannot examine %s: %s\n",
227 command
, arg
, Pgrab_error(gcode
));
232 if ((tpsinfo
= Ppsinfo(Pr
)) == NULL
) {
233 (void) fprintf(stderr
, "%s: cannot examine %s: "
234 "lost control of process\n", command
, arg
);
239 (void) memcpy(&psinfo
, tpsinfo
, sizeof (psinfo_t
));
240 proc_unctrl_psinfo(&psinfo
);
242 if (Pstate(Pr
) == PS_DEAD
) {
243 if ((Pcontent(Pr
) & content
) != content
) {
244 (void) fprintf(stderr
, "%s: core '%s' has "
245 "insufficient content\n", command
, arg
);
249 (void) printf("core '%s' of %d:\t%.70s\n",
250 arg
, (int)psinfo
.pr_pid
, psinfo
.pr_psargs
);
252 (void) printf("%d:\t%.70s\n",
253 (int)psinfo
.pr_pid
, psinfo
.pr_psargs
);
256 is64
= (psinfo
.pr_dmodel
== PR_MODEL_LP64
);
258 if (Pgetauxval(Pr
, AT_BASE
) != -1L && Prd_agent(Pr
) == NULL
) {
259 (void) fprintf(stderr
, "%s: warning: librtld_db failed "
260 "to initialize; symbols from shared libraries will "
261 "not be available\n", command
);
265 * First we need to get a thread agent handle.
267 if (td_init() != TD_OK
||
268 td_ta_new(Pr
, &Tap
) != TD_OK
) /* no libc */
272 * Iterate over all threads, calling:
273 * thr_stack(td_thrhandle_t *Thp, NULL);
274 * for each one to generate the list of threads.
277 (void) td_ta_thr_iter(Tap
, thr_stack
, NULL
,
278 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
279 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
281 (void) td_ta_delete(Tap
);
286 handle
.jvm
= load_libjvm(Pr
);
287 handle
.pydb
= load_libpython(Pr
);
291 if (all_call_stacks(&handle
, threaded
) != 0)
296 reset_libjvm(handle
.jvm
);
297 reset_libpython(handle
.pydb
);
300 if (handle
.count
== 0)
301 (void) fprintf(stderr
, "%s: no matching LWPs found\n",
305 (void) proc_finistdio();
311 * Thread iteration call-back function.
312 * Called once for each user-level thread.
313 * Used to build the list of all threads.
317 thr_stack(const td_thrhandle_t
*Thp
, void *cd
)
319 td_thrinfo_t thrinfo
;
320 struct threadinfo
*tip
;
323 if (td_thr_get_info(Thp
, &thrinfo
) != TD_OK
)
326 tip
= malloc(sizeof (struct threadinfo
));
328 tip
->threadid
= thrinfo
.ti_tid
;
329 tip
->lwpid
= thrinfo
.ti_lid
;
330 tip
->state
= thrinfo
.ti_state
;
331 tip
->startfunc
= thrinfo
.ti_startfunc
;
332 tip
->exitval
= (uintptr_t)thrinfo
.ti_exitval
;
335 if (thrinfo
.ti_state
== TD_THR_ZOMBIE
||
336 ((error
= td_thr_getgregs(Thp
, tip
->regs
)) != TD_OK
&&
337 error
!= TD_PARTIALREG
))
338 (void) memset(tip
->regs
, 0, sizeof (prgregset_t
));
341 thr_tail
->next
= tip
;
352 struct threadinfo
*tip
= thr_head
;
353 struct threadinfo
*next
;
361 thr_head
= thr_tail
= NULL
;
365 * Find and eliminate the thread corresponding to the given lwpid.
367 static struct threadinfo
*
368 find_thread(id_t lwpid
)
370 struct threadinfo
*tip
;
372 for (tip
= thr_head
; tip
; tip
= tip
->next
) {
373 if (lwpid
== tip
->lwpid
) {
382 thread_call_stack(void *data
, const lwpstatus_t
*psp
,
383 const lwpsinfo_t
*pip
)
385 char lwpname
[THREAD_NAME_MAX
] = "";
386 pstack_handle_t
*h
= data
;
387 lwpstatus_t lwpstatus
;
388 struct threadinfo
*tip
;
390 if (!proc_lwp_in_set(h
->lwps
, pip
->pr_lwpid
))
394 if ((tip
= find_thread(pip
->pr_lwpid
)) == NULL
)
397 (void) Plwp_getname(h
->proc
, pip
->pr_lwpid
,
398 lwpname
, sizeof (lwpname
));
400 tlhead(tip
->threadid
, pip
->pr_lwpid
, lwpname
);
401 tip
->threadid
= 0; /* finish eliminating tid */
405 if (tip
->state
== TD_THR_ZOMBIE
)
406 print_zombie(h
->proc
, tip
);
408 (void) memset(&lwpstatus
, 0, sizeof (lwpstatus
));
409 (void) memcpy(lwpstatus
.pr_reg
, tip
->regs
,
410 sizeof (prgregset_t
));
411 call_stack(h
, &lwpstatus
);
418 lwp_call_stack(void *data
,
419 const lwpstatus_t
*psp
, const lwpsinfo_t
*pip
)
421 char lwpname
[THREAD_NAME_MAX
] = "";
422 pstack_handle_t
*h
= data
;
424 if (!proc_lwp_in_set(h
->lwps
, pip
->pr_lwpid
))
428 (void) Plwp_getname(h
->proc
, pip
->pr_lwpid
,
429 lwpname
, sizeof (lwpname
));
431 tlhead(0, pip
->pr_lwpid
, lwpname
);
435 (void) printf("\t** zombie "
436 "(exited, not detached, not yet joined) **\n");
441 all_call_stacks(pstack_handle_t
*h
, int dothreads
)
443 struct ps_prochandle
*Pr
= h
->proc
;
444 pstatus_t status
= *Pstatus(Pr
);
446 (void) memset(&sigh
, 0, sizeof (GElf_Sym
));
447 (void) Plookup_by_name(Pr
, "libc.so", "sigacthandler", &sigh
);
449 if ((status
.pr_nlwp
+ status
.pr_nzomb
) <= 1 &&
450 !(dothreads
&& nthreads
> 1)) {
451 if (proc_lwp_in_set(h
->lwps
, status
.pr_lwp
.pr_lwpid
)) {
452 call_stack(h
, &status
.pr_lwp
);
456 lwpstatus_t lwpstatus
;
457 struct threadinfo
*tip
;
461 (void) Plwp_iter_all(Pr
, thread_call_stack
, h
);
463 (void) Plwp_iter_all(Pr
, lwp_call_stack
, h
);
465 /* for each remaining thread w/o an lwp */
466 (void) memset(&lwpstatus
, 0, sizeof (lwpstatus
));
467 for (tip
= thr_head
; tip
; tip
= tip
->next
) {
469 if (!proc_lwp_in_set(h
->lwps
, tip
->lwpid
))
472 if ((tid
= tip
->threadid
) != 0) {
473 (void) memcpy(lwpstatus
.pr_reg
, tip
->regs
,
474 sizeof (prgregset_t
));
475 tlhead(tid
, tip
->lwpid
, NULL
);
476 if (tip
->state
== TD_THR_ZOMBIE
)
477 print_zombie(Pr
, tip
);
479 call_stack(h
, &lwpstatus
);
488 /* The width of the header */
489 #define HEAD_WIDTH (62)
491 tlhead(id_t threadid
, id_t lwpid
, const char *name
)
493 char buf
[128] = { 0 };
498 if (threadid
== 0 && 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
));
508 (void) snprintf(num
, sizeof (num
), "%d", (int)threadid
);
510 (void) strlcat(buf
, " / ", sizeof (buf
));
511 (void) strlcat(buf
, "lwp# ", sizeof (buf
));
512 (void) strlcat(buf
, num
, sizeof (buf
));
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
));
521 amt
= (HEAD_WIDTH
- strlen(buf
) - 2);
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
);
535 print_java_frame(void *cld
, prgregset_t gregs
, const char *name
, int bci
,
536 int line
, void *handle
)
538 int length
= (is64
? 16 : 8);
540 (void) printf(" %.*lx * %s", length
, (long)gregs
[R_PC
], name
);
543 (void) printf("+%d", bci
);
545 (void) printf(" (line %d)", line
);
552 static sigjmp_buf jumpbuf
;
556 fatal_signal(int signo
)
558 siglongjmp(jumpbuf
, 1);
562 print_frame(void *cd
, prgregset_t gregs
, uint_t argc
, const long *argv
)
564 pstack_handle_t
*h
= cd
;
565 struct ps_prochandle
*Pr
= h
->proc
;
566 uintptr_t pc
= gregs
[R_PC
];
570 int length
= (is64
? 16 : 8);
574 * If we are in a system call, we display the entry frame in a more
575 * readable manner, using the name of the system call. In this case, we
576 * want to ignore this first frame, since we already displayed it
579 if (h
->ignore_frame
) {
584 (void) sprintf(buff
, "%.*lx", length
, (long)pc
);
585 (void) strcpy(buff
+ length
, " ????????");
586 if (Plookup_by_addr(Pr
, pc
,
587 buff
+ 1 + length
, sizeof (buff
) - 1 - length
, &sym
) == 0) {
588 start
= sym
.st_value
;
589 } else if (h
->jvm
!= NULL
) {
591 void (*segv
)(int), (*bus
)(int), (*ill
)(int);
593 segv
= signal(SIGSEGV
, fatal_signal
);
594 bus
= signal(SIGBUS
, fatal_signal
);
595 ill
= signal(SIGILL
, fatal_signal
);
597 /* Insure against a bad libjvm_db */
598 if (sigsetjmp(jumpbuf
, 0) == 0)
599 ret
= j_frame_iter(h
->jvm
, gregs
, print_java_frame
,
604 (void) signal(SIGSEGV
, segv
);
605 (void) signal(SIGBUS
, bus
);
606 (void) signal(SIGILL
, ill
);
614 (void) printf(" %-17s (", buff
);
615 for (i
= 0; i
< argc
&& i
< MAX_ARGS
; i
++)
616 (void) printf((i
+1 == argc
) ? "%lx" : "%lx, ", argv
[i
]);
618 (void) printf("...");
619 (void) printf((start
!= pc
) ? ") + %lx\n" : ")\n", (long)(pc
- start
));
621 if (h
->pydb
!= NULL
&& argc
> 0) {
625 rc
= pydb_pc_frameinfo(h
->pydb
, pc
, argv
[0], buf_py
,
628 (void) printf(" %s", buf_py
);
633 * If the frame's pc is in the "sigh" (a.k.a. signal handler, signal
634 * hack, or *sigh* ...) range, then we're about to cross a signal
635 * frame. The signal number is the first argument to this function.
637 if (pc
- sigh
.st_value
< sigh
.st_size
) {
638 if (sig2str((int)argv
[0], buff
) == -1)
639 (void) strcpy(buff
, " Unknown");
640 (void) printf(" --- called from signal handler with "
641 "signal %d (SIG%s) ---\n", (int)argv
[0], buff
);
648 print_zombie(struct ps_prochandle
*Pr
, struct threadinfo
*tip
)
653 int length
= (is64
? 16 : 8);
655 (void) sprintf(buff
, "%.*lx", length
, (long)tip
->startfunc
);
656 (void) strcpy(buff
+ length
, " ????????");
657 if (Plookup_by_addr(Pr
, tip
->startfunc
,
658 buff
+ 1 + length
, sizeof (buff
) - 1 - length
, &sym
) == 0)
659 start
= sym
.st_value
;
661 start
= tip
->startfunc
;
662 (void) printf(" %s()", buff
);
663 if (start
!= tip
->startfunc
) /* doesn't happen? */
664 (void) printf("+%lx", (long)(tip
->startfunc
- start
));
665 (void) printf(", exit value = 0x%.*lx\n", length
, (long)tip
->exitval
);
666 (void) printf("\t** zombie "
667 "(exited, not detached, not yet joined) **\n");
671 print_syscall(const lwpstatus_t
*psp
, prgregset_t reg
)
674 int length
= (is64
? 16 : 8);
677 (void) proc_sysname(psp
->pr_syscall
, sname
, sizeof (sname
));
678 (void) printf(" %.*lx %-8s (", length
, (long)reg
[R_PC
], sname
);
679 for (i
= 0; i
< psp
->pr_nsysarg
; i
++)
680 (void) printf((i
+1 == psp
->pr_nsysarg
)? "%lx" : "%lx, ",
681 (long)psp
->pr_sysarg
[i
]);
682 (void) printf(")\n");
686 call_stack(pstack_handle_t
*h
, const lwpstatus_t
*psp
)
690 (void) memcpy(reg
, psp
->pr_reg
, sizeof (reg
));
692 if ((psp
->pr_flags
& (PR_ASLEEP
|PR_VFORKP
)) ||
693 ((psp
->pr_flags
& PR_ISTOP
) &&
694 (psp
->pr_why
== PR_SYSENTRY
||
695 psp
->pr_why
== PR_SYSEXIT
))) {
696 print_syscall(psp
, reg
);
702 (void) Pstack_iter(h
->proc
, reg
, print_frame
, h
);
707 jvm_object_iter(void *cd
, const prmap_t
*pmp
, const char *obj
)
712 struct ps_prochandle
*Pr
= cd
;
714 if ((name
= strstr(obj
, "/libjvm.so")) == NULL
)
715 name
= strstr(obj
, "/libjvm_g.so");
718 (void) strcpy(path
, obj
);
719 if (Pstatus(Pr
)->pr_dmodel
!= PR_MODEL_NATIVE
) {
721 s2
= path
+ (s1
- obj
);
722 (void) strcpy(s2
, "/64");
724 (void) strcpy(s2
, s1
);
727 s1
= strstr(obj
, ".so");
728 s2
= strstr(path
, ".so");
729 (void) strcpy(s2
, "_db");
731 (void) strcpy(s2
, s1
);
733 if ((libjvm
= dlopen(path
, RTLD_LAZY
|RTLD_GLOBAL
)) != NULL
)
741 load_libjvm(struct ps_prochandle
*Pr
)
746 * Iterate through all the loaded objects in the target, looking
747 * for libjvm.so. If we find libjvm.so we'll try to load the
748 * corresponding libjvm_db.so that lives in the same directory.
750 * At first glance it seems like we'd want to use
751 * Pobject_iter_resolved() here since we'd want to make sure that
752 * we have the full path to the libjvm.so. But really, we don't
753 * want that since we're going to be dlopen()ing a library and
754 * executing code from that path, and therefore we don't want to
755 * load any library code that could be from a zone since it could
756 * have been replaced with a trojan. Hence, we use Pobject_iter().
757 * So if we're debugging java processes in a zone from the global
758 * zone, and we want to get proper java stack stack frames, then
759 * the same jvm that is running within the zone needs to be
760 * installed in the global zone.
762 (void) Pobject_iter(Pr
, jvm_object_iter
, Pr
);
765 j_agent_create
= (j_agent_create_f
)
766 dlsym(libjvm
, "Jagent_create");
767 j_agent_destroy
= (j_agent_destroy_f
)
768 dlsym(libjvm
, "Jagent_destroy");
769 j_frame_iter
= (j_frame_iter_f
)
770 dlsym(libjvm
, "Jframe_iter");
772 if (j_agent_create
== NULL
|| j_agent_destroy
== NULL
||
773 j_frame_iter
== NULL
||
774 (ret
= j_agent_create(Pr
, JVM_DB_VERSION
)) == NULL
) {
786 reset_libjvm(jvm_agent_t
*agent
)
790 j_agent_destroy(agent
);
792 (void) dlclose(libjvm
);
795 j_agent_create
= NULL
;
796 j_agent_destroy
= NULL
;
803 python_object_iter(void *cd
, const prmap_t
*pmp
, const char *obj
)
808 struct ps_prochandle
*Pr
= cd
;
810 name
= strstr(obj
, "/libpython");
813 (void) strcpy(path
, obj
);
814 if (Pstatus(Pr
)->pr_dmodel
!= PR_MODEL_NATIVE
) {
816 s2
= path
+ (s1
- obj
);
817 (void) strcpy(s2
, "/64");
819 (void) strcpy(s2
, s1
);
822 s1
= strstr(obj
, ".so");
823 s2
= strstr(path
, ".so");
824 (void) strcpy(s2
, "_db");
826 (void) strcpy(s2
, s1
);
828 if ((libpython
= dlopen(path
, RTLD_LAZY
|RTLD_GLOBAL
)) != NULL
)
835 static pydb_agent_t
*
836 load_libpython(struct ps_prochandle
*Pr
)
840 (void) Pobject_iter(Pr
, python_object_iter
, Pr
);
843 pydb_agent_create
= (pydb_agent_create_f
)
844 dlsym(libpython
, "pydb_agent_create");
845 pydb_agent_destroy
= (pydb_agent_destroy_f
)
846 dlsym(libpython
, "pydb_agent_destroy");
847 pydb_pc_frameinfo
= (pydb_pc_frameinfo_f
)
848 dlsym(libpython
, "pydb_pc_frameinfo");
850 if (pydb_agent_create
== NULL
|| pydb_agent_destroy
== NULL
||
851 pydb_pc_frameinfo
== NULL
) {
852 (void) dlclose(libpython
);
857 pdb
= pydb_agent_create(Pr
, PYDB_VERSION
);
859 (void) dlclose(libpython
);
870 reset_libpython(pydb_agent_t
*pdb
)
872 if (libpython
!= NULL
) {
874 pydb_agent_destroy(pdb
);
876 (void) dlclose(libpython
);
880 pydb_agent_create
= NULL
;
881 pydb_agent_destroy
= NULL
;
882 pydb_pc_frameinfo
= NULL
;