xref: /f-stack/freebsd/ddb/db_thread.c (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 2004 Marcel Moolenaar
5a9643ea8Slogwang  * All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang  * modification, are permitted provided that the following conditions
9a9643ea8Slogwang  * are met:
10a9643ea8Slogwang  *
11a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
12a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
13a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
14a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
15a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
16a9643ea8Slogwang  *
17a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18a9643ea8Slogwang  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19a9643ea8Slogwang  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20a9643ea8Slogwang  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21a9643ea8Slogwang  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22a9643ea8Slogwang  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a9643ea8Slogwang  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a9643ea8Slogwang  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a9643ea8Slogwang  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26a9643ea8Slogwang  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a9643ea8Slogwang  */
28a9643ea8Slogwang 
29a9643ea8Slogwang #include <sys/cdefs.h>
30a9643ea8Slogwang __FBSDID("$FreeBSD$");
31a9643ea8Slogwang 
32a9643ea8Slogwang #include <sys/param.h>
33a9643ea8Slogwang #include <sys/systm.h>
34a9643ea8Slogwang #include <sys/kdb.h>
35a9643ea8Slogwang #include <sys/proc.h>
36a9643ea8Slogwang 
37a9643ea8Slogwang #include <machine/pcb.h>
38a9643ea8Slogwang 
39a9643ea8Slogwang #include <ddb/ddb.h>
40a9643ea8Slogwang #include <ddb/db_command.h>
41a9643ea8Slogwang #include <ddb/db_sym.h>
42a9643ea8Slogwang 
43a9643ea8Slogwang void
db_print_thread(void)44a9643ea8Slogwang db_print_thread(void)
45a9643ea8Slogwang {
46a9643ea8Slogwang 	pid_t pid;
47a9643ea8Slogwang 
48a9643ea8Slogwang 	pid = -1;
49a9643ea8Slogwang 	if (kdb_thread->td_proc != NULL)
50a9643ea8Slogwang 		pid = kdb_thread->td_proc->p_pid;
51a9643ea8Slogwang 	db_printf("[ thread pid %d tid %ld ]\n", pid, (long)kdb_thread->td_tid);
52a9643ea8Slogwang }
53a9643ea8Slogwang 
54a9643ea8Slogwang void
db_set_thread(db_expr_t tid,bool hastid,db_expr_t cnt,char * mod)55a9643ea8Slogwang db_set_thread(db_expr_t tid, bool hastid, db_expr_t cnt, char *mod)
56a9643ea8Slogwang {
57a9643ea8Slogwang 	struct thread *thr;
58a9643ea8Slogwang 	int err;
59a9643ea8Slogwang 
60a9643ea8Slogwang 	if (hastid) {
61*22ce4affSfengbojiang 		thr = db_lookup_thread(tid, false);
62a9643ea8Slogwang 		if (thr != NULL) {
63a9643ea8Slogwang 			err = kdb_thr_select(thr);
64a9643ea8Slogwang 			if (err != 0) {
65a9643ea8Slogwang 				db_printf("unable to switch to thread %ld\n",
66a9643ea8Slogwang 				    (long)thr->td_tid);
67a9643ea8Slogwang 				return;
68a9643ea8Slogwang 			}
69a9643ea8Slogwang 			db_dot = PC_REGS();
70a9643ea8Slogwang 		} else {
71a9643ea8Slogwang 			db_printf("%d: invalid thread\n", (int)tid);
72a9643ea8Slogwang 			return;
73a9643ea8Slogwang 		}
74a9643ea8Slogwang 	}
75a9643ea8Slogwang 
76a9643ea8Slogwang 	db_print_thread();
77a9643ea8Slogwang 	db_print_loc_and_inst(PC_REGS());
78a9643ea8Slogwang }
79a9643ea8Slogwang 
80a9643ea8Slogwang void
db_show_threads(db_expr_t addr,bool hasaddr,db_expr_t cnt,char * mod)81a9643ea8Slogwang db_show_threads(db_expr_t addr, bool hasaddr, db_expr_t cnt, char *mod)
82a9643ea8Slogwang {
83a9643ea8Slogwang 	jmp_buf jb;
84a9643ea8Slogwang 	void *prev_jb;
85a9643ea8Slogwang 	struct thread *thr;
86a9643ea8Slogwang 
87a9643ea8Slogwang 	thr = kdb_thr_first();
88a9643ea8Slogwang 	while (!db_pager_quit && thr != NULL) {
89a9643ea8Slogwang 		db_printf("  %6ld (%p) (stack %p)  ", (long)thr->td_tid, thr,
90a9643ea8Slogwang 		    (void *)thr->td_kstack);
91a9643ea8Slogwang 		prev_jb = kdb_jmpbuf(jb);
92a9643ea8Slogwang 		if (setjmp(jb) == 0) {
93*22ce4affSfengbojiang 			if (thr->td_proc->p_flag & P_INMEM) {
94a9643ea8Slogwang 				if (db_trace_thread(thr, 1) != 0)
95a9643ea8Slogwang 					db_printf("***\n");
96*22ce4affSfengbojiang 			} else
97*22ce4affSfengbojiang 				db_printf("*** swapped out\n");
98a9643ea8Slogwang 		}
99a9643ea8Slogwang 		kdb_jmpbuf(prev_jb);
100a9643ea8Slogwang 		thr = kdb_thr_next(thr);
101a9643ea8Slogwang 	}
102a9643ea8Slogwang }
103a9643ea8Slogwang 
104a9643ea8Slogwang /*
105a9643ea8Slogwang  * Lookup a thread based on a db expression address.  We assume that the
106a9643ea8Slogwang  * address was parsed in hexadecimal.  We reparse the address in decimal
107a9643ea8Slogwang  * first and try to treat it as a thread ID to find an associated thread.
108a9643ea8Slogwang  * If that fails and check_pid is true, we treat the decimal value as a
109a9643ea8Slogwang  * PID.  If that matches a process, we return the first thread in that
110a9643ea8Slogwang  * process.  Otherwise, we treat the addr as a pointer to a thread.
111a9643ea8Slogwang  */
112a9643ea8Slogwang struct thread *
db_lookup_thread(db_expr_t addr,bool check_pid)113a9643ea8Slogwang db_lookup_thread(db_expr_t addr, bool check_pid)
114a9643ea8Slogwang {
115a9643ea8Slogwang 	struct thread *td;
116a9643ea8Slogwang 	db_expr_t decaddr;
117a9643ea8Slogwang 
118a9643ea8Slogwang 	/*
119a9643ea8Slogwang 	 * If the parsed address was not a valid decimal expression,
120a9643ea8Slogwang 	 * assume it is a thread pointer.
121a9643ea8Slogwang 	 */
122a9643ea8Slogwang 	decaddr = db_hex2dec(addr);
123a9643ea8Slogwang 	if (decaddr == -1)
124a9643ea8Slogwang 		return ((struct thread *)addr);
125a9643ea8Slogwang 
126a9643ea8Slogwang 	td = kdb_thr_lookup(decaddr);
127a9643ea8Slogwang 	if (td != NULL)
128a9643ea8Slogwang 		return (td);
129a9643ea8Slogwang 	if (check_pid) {
130*22ce4affSfengbojiang 		td = kdb_thr_from_pid(decaddr);
131*22ce4affSfengbojiang 		if (td != NULL)
132*22ce4affSfengbojiang 			return (td);
133a9643ea8Slogwang 	}
134a9643ea8Slogwang 	return ((struct thread *)addr);
135a9643ea8Slogwang }
136a9643ea8Slogwang 
137a9643ea8Slogwang /*
138a9643ea8Slogwang  * Lookup a process based on a db expression address.  We assume that the
139a9643ea8Slogwang  * address was parsed in hexadecimal.  We reparse the address in decimal
140a9643ea8Slogwang  * first and try to treat it as a PID to find an associated process.
141a9643ea8Slogwang  * If that fails we treat the addr as a pointer to a process.
142a9643ea8Slogwang  */
143a9643ea8Slogwang struct proc *
db_lookup_proc(db_expr_t addr)144a9643ea8Slogwang db_lookup_proc(db_expr_t addr)
145a9643ea8Slogwang {
146a9643ea8Slogwang 	db_expr_t decaddr;
147a9643ea8Slogwang 	struct proc *p;
148a9643ea8Slogwang 
149a9643ea8Slogwang 	decaddr = db_hex2dec(addr);
150a9643ea8Slogwang 	if (decaddr != -1) {
151*22ce4affSfengbojiang 		LIST_FOREACH(p, PIDHASH(decaddr), p_hash) {
152a9643ea8Slogwang 			if (p->p_pid == decaddr)
153a9643ea8Slogwang 				return (p);
154a9643ea8Slogwang 		}
155a9643ea8Slogwang 	}
156a9643ea8Slogwang 	return ((struct proc *)addr);
157a9643ea8Slogwang }
158