xref: /dragonfly/gnu/usr.bin/gdb/kgdb/kthr.c (revision 9ddb8543)
1 /*
2  * Copyright (c) 2004 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/kthr.c,v 1.3 2005/09/10 18:25:53 marcel Exp $
27  * $DragonFly: src/gnu/usr.bin/gdb/kgdb/kthr.c,v 1.5 2008/01/14 21:36:38 corecode Exp $
28  */
29 
30 #define _KERNEL_STRUCTURES
31 
32 #include <sys/cdefs.h>
33 
34 #include <sys/param.h>
35 #include <machine/globaldata.h>
36 #include <sys/user.h>
37 #include <sys/types.h>
38 #include <sys/signal.h>
39 #include <err.h>
40 #include <inttypes.h>
41 #include <kvm.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 
45 #include <defs.h>
46 #include <frame-unwind.h>
47 
48 #include "kgdb.h"
49 
50 static uintptr_t dumppcb;
51 static int dumptid;
52 
53 static struct kthr *first;
54 struct kthr *curkthr;
55 
56 struct kthr *
57 kgdb_thr_first(void)
58 {
59 	return (first);
60 }
61 
62 struct kthr *
63 kgdb_thr_init(void)
64 {
65 	struct proc p;
66 	struct lwp lwp;
67 	struct thread td;
68 	struct mdglobaldata gd;
69 	struct kthr *kt;
70 	uintptr_t addr, paddr, prvspace;
71 	int cpu, ncpus;
72 
73 	addr = lookup("_ncpus");
74 	if (addr == 0)
75 		return (NULL);
76 	kvm_read(kvm, addr, &ncpus, sizeof(ncpus));
77 
78 	dumppcb = lookup("_dumppcb");
79 	if (dumppcb == 0)
80 		return (NULL);
81 
82 	prvspace = lookup("CPU_prvspace");
83 	if (prvspace == 0)
84 		return (NULL);
85 
86 	addr = lookup("_dumpthread");
87 	if (addr != 0) {
88 		kvm_read(kvm, addr, &dumptid, sizeof(dumptid));
89 	} else {
90 		/*
91 		 * XXX Well then.  We don't know who dumped us.
92 		 * We could do some fancy stack matching, but
93 		 * I doubt this will work.  For now just use
94 		 * cpu0's curthread.
95 		 *
96 		 * Actually we don't even know if we were dumped
97 		 * or if we are life.  Find out by querying "dumping".
98 		 */
99 		int dumping = 0;
100 
101 		addr = lookup("_dumping");
102 		kvm_read(kvm, addr, &dumping, sizeof(dumping));
103 		if (dumping) {
104 			kvm_read(kvm, prvspace +
105 				 offsetof(struct privatespace, mdglobaldata),
106 				 &gd, sizeof(struct mdglobaldata));
107 			dumptid = (intptr_t)gd.mi.gd_curthread;
108 		} else {
109 			/* We must be a live system */
110 			dumptid = -1;
111 		}
112 	}
113 
114 	for (cpu = 0; cpu < ncpus; cpu++) {
115 		kvm_read(kvm, prvspace +
116 			 cpu * sizeof(struct privatespace) +
117 			 offsetof(struct privatespace, mdglobaldata),
118 			 &gd, sizeof(struct mdglobaldata));
119 
120 		addr = (uintptr_t)TAILQ_FIRST(&gd.mi.gd_tdallq);
121 		while (addr != 0) {
122 			if (kvm_read(kvm, addr, &td, sizeof(td)) != sizeof(td)) {
123 				warnx("kvm_read: %s, while accessing thread",
124 				      kvm_geterr(kvm));
125 				break;
126 			}
127 			kt = malloc(sizeof(*kt));
128 			kt->next = first;
129 			kt->kaddr = addr;
130 			kt->tid = addr;		/* XXX do we have tids? */
131 			kt->pcb = (kt->tid == dumptid) ? dumppcb :
132 			    (uintptr_t)td.td_pcb;
133 			kt->kstack = (uintptr_t)td.td_kstack;
134 			if (td.td_proc != NULL) {
135 				paddr = (uintptr_t)td.td_proc;
136 				if (kvm_read(kvm, paddr, &p, sizeof(p)) != sizeof(p))
137 					warnx("kvm_read: %s", kvm_geterr(kvm));
138 				kt->pid = p.p_pid;
139 				kt->paddr = paddr;
140 			} else {
141 				/*
142 				 * XXX for some stupid reason, gdb uses pid == -1
143 				 * as a marker for "dead" threads, so we have to
144 				 * hook all kernel threads on a different pid :/
145 				 */
146 				kt->pid = -2;
147 				kt->paddr = 0;
148 				/*
149 				 * We are a kernel thread, so our td_pcb is
150 				 * not used anyways.  An exception is the
151 				 * dumping thread.
152 				 * kt->pcb == 0 is a marker for
153 				 * "non-dumping kernel thread".
154 				 */
155 				if (kt->tid != dumptid)
156 					kt->pcb = 0;
157 			}
158 			first = kt;
159 			addr = (uintptr_t)TAILQ_NEXT(&td, td_allq);
160 		}
161 	}
162 
163 	curkthr = kgdb_thr_lookup_tid(dumptid);
164 	if (curkthr == NULL)
165 		curkthr = first;
166 	return (first);
167 }
168 
169 struct kthr *
170 kgdb_thr_lookup_tid(int tid)
171 {
172 	struct kthr *kt;
173 
174 	kt = first;
175 	while (kt != NULL && kt->tid != tid)
176 		kt = kt->next;
177 	return (kt);
178 }
179 
180 struct kthr *
181 kgdb_thr_lookup_taddr(uintptr_t taddr)
182 {
183 	struct kthr *kt;
184 
185 	kt = first;
186 	while (kt != NULL && kt->kaddr != taddr)
187 		kt = kt->next;
188 	return (kt);
189 }
190 
191 struct kthr *
192 kgdb_thr_lookup_pid(int pid)
193 {
194 	struct kthr *kt;
195 
196 	kt = first;
197 	while (kt != NULL && kt->pid != pid)
198 		kt = kt->next;
199 	return (kt);
200 }
201 
202 struct kthr *
203 kgdb_thr_lookup_paddr(uintptr_t paddr)
204 {
205 	struct kthr *kt;
206 
207 	kt = first;
208 	while (kt != NULL && kt->paddr != paddr)
209 		kt = kt->next;
210 	return (kt);
211 }
212 
213 struct kthr *
214 kgdb_thr_next(struct kthr *kt)
215 {
216 	return (kt->next);
217 }
218 
219 struct kthr *
220 kgdb_thr_select(struct kthr *kt)
221 {
222 	struct kthr *pcur;
223 
224 	pcur = curkthr;
225 	curkthr = kt;
226 	return (pcur);
227 }
228 
229 char *
230 kgdb_thr_extra_thread_info(int tid)
231 {
232 	struct kthr *kt;
233 	static char comm[MAXCOMLEN + 1];
234 
235 	kt = kgdb_thr_lookup_tid(tid);
236 	if (kt == NULL)
237 		return (NULL);
238 	if (kvm_read(kvm, kt->kaddr + offsetof(struct thread, td_comm), &comm,
239 	    sizeof(comm)) != sizeof(comm))
240 		return (NULL);
241 
242 	return (comm);
243 }
244