xref: /dragonfly/gnu/usr.bin/gdb/kgdb/kthr.c (revision 878f9070)
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 #include <sys/cdefs.h>
31 
32 #include <sys/param.h>
33 #include <machine/globaldata.h>
34 #include <sys/user.h>
35 #include <sys/types.h>
36 #include <sys/signal.h>
37 #include <err.h>
38 #include <inttypes.h>
39 #include <kvm.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 
43 #include <defs.h>
44 #include <frame-unwind.h>
45 
46 #include "kgdb.h"
47 
48 static uintptr_t dumppcb;
49 static int dumptid;
50 
51 static struct kthr *first;
52 struct kthr *curkthr;
53 
54 struct kthr *
55 kgdb_thr_first(void)
56 {
57 	return (first);
58 }
59 
60 struct kthr *
61 kgdb_thr_init(void)
62 {
63 	struct proc p;
64 	struct lwp lwp;
65 	struct thread td;
66 	struct mdglobaldata gd;
67 	struct kthr *kt;
68 	uintptr_t addr, paddr, prvspace;
69 	int cpu, ncpus;
70 
71 	addr = lookup("_ncpus");
72 	if (addr == 0)
73 		return (NULL);
74 	kvm_read(kvm, addr, &ncpus, sizeof(ncpus));
75 
76 	dumppcb = lookup("_dumppcb");
77 	if (dumppcb == 0)
78 		return (NULL);
79 
80 	prvspace = lookup("CPU_prvspace");
81 	if (prvspace == 0)
82 		return (NULL);
83 
84 	addr = lookup("_dumpthread");
85 	if (addr != 0) {
86 		kvm_read(kvm, addr, &dumptid, sizeof(dumptid));
87 	} else {
88 		/*
89 		 * XXX Well then.  We don't know who dumped us.
90 		 * We could do some fancy stack matching, but
91 		 * I doubt this will work.  For now just use
92 		 * cpu0's curthread.
93 		 *
94 		 * Actually we don't even know if we were dumped
95 		 * or if we are life.  Find out by querying "dumping".
96 		 */
97 		int dumping = 0;
98 
99 		addr = lookup("_dumping");
100 		kvm_read(kvm, addr, &dumping, sizeof(dumping));
101 		if (dumping) {
102 			kvm_read(kvm, prvspace +
103 				 offsetof(struct privatespace, mdglobaldata),
104 				 &gd, sizeof(struct mdglobaldata));
105 			dumptid = (intptr_t)gd.mi.gd_curthread;
106 		} else {
107 			/* We must be a live system */
108 			dumptid = -1;
109 		}
110 	}
111 
112 	for (cpu = 0; cpu < ncpus; cpu++) {
113 		kvm_read(kvm, prvspace +
114 			 cpu * sizeof(struct privatespace) +
115 			 offsetof(struct privatespace, mdglobaldata),
116 			 &gd, sizeof(struct mdglobaldata));
117 
118 		addr = (uintptr_t)TAILQ_FIRST(&gd.mi.gd_tdallq);
119 		while (addr != 0) {
120 			if (kvm_read(kvm, addr, &td, sizeof(td)) != sizeof(td)) {
121 				warnx("kvm_read: %s, while accessing thread",
122 				      kvm_geterr(kvm));
123 				break;
124 			}
125 			kt = malloc(sizeof(*kt));
126 			kt->next = first;
127 			kt->kaddr = addr;
128 			kt->tid = addr;		/* XXX do we have tids? */
129 			kt->pcb = (kt->tid == dumptid) ? dumppcb :
130 			    (uintptr_t)td.td_pcb;
131 			kt->kstack = (uintptr_t)td.td_kstack;
132 			if (td.td_proc != NULL) {
133 				paddr = (uintptr_t)td.td_proc;
134 				if (kvm_read(kvm, paddr, &p, sizeof(p)) != sizeof(p))
135 					warnx("kvm_read: %s", kvm_geterr(kvm));
136 				kt->pid = p.p_pid;
137 				kt->paddr = paddr;
138 			} else {
139 				/*
140 				 * XXX for some stupid reason, gdb uses pid == -1
141 				 * as a marker for "dead" threads, so we have to
142 				 * hook all kernel threads on a different pid :/
143 				 */
144 				kt->pid = -2;
145 				kt->paddr = 0;
146 				/*
147 				 * We are a kernel thread, so our td_pcb is
148 				 * not used anyways.  An exception is the
149 				 * dumping thread.
150 				 * kt->pcb == 0 is a marker for
151 				 * "non-dumping kernel thread".
152 				 */
153 				if (kt->tid != dumptid)
154 					kt->pcb = 0;
155 			}
156 			first = kt;
157 			addr = (uintptr_t)TAILQ_NEXT(&td, td_allq);
158 		}
159 	}
160 
161 	curkthr = kgdb_thr_lookup_tid(dumptid);
162 	if (curkthr == NULL)
163 		curkthr = first;
164 	return (first);
165 }
166 
167 struct kthr *
168 kgdb_thr_lookup_tid(int tid)
169 {
170 	struct kthr *kt;
171 
172 	kt = first;
173 	while (kt != NULL && kt->tid != tid)
174 		kt = kt->next;
175 	return (kt);
176 }
177 
178 struct kthr *
179 kgdb_thr_lookup_taddr(uintptr_t taddr)
180 {
181 	struct kthr *kt;
182 
183 	kt = first;
184 	while (kt != NULL && kt->kaddr != taddr)
185 		kt = kt->next;
186 	return (kt);
187 }
188 
189 struct kthr *
190 kgdb_thr_lookup_pid(int pid)
191 {
192 	struct kthr *kt;
193 
194 	kt = first;
195 	while (kt != NULL && kt->pid != pid)
196 		kt = kt->next;
197 	return (kt);
198 }
199 
200 struct kthr *
201 kgdb_thr_lookup_paddr(uintptr_t paddr)
202 {
203 	struct kthr *kt;
204 
205 	kt = first;
206 	while (kt != NULL && kt->paddr != paddr)
207 		kt = kt->next;
208 	return (kt);
209 }
210 
211 struct kthr *
212 kgdb_thr_next(struct kthr *kt)
213 {
214 	return (kt->next);
215 }
216 
217 struct kthr *
218 kgdb_thr_select(struct kthr *kt)
219 {
220 	struct kthr *pcur;
221 
222 	pcur = curkthr;
223 	curkthr = kt;
224 	return (pcur);
225 }
226 
227 char *
228 kgdb_thr_extra_thread_info(int tid)
229 {
230 	struct kthr *kt;
231 	static char comm[MAXCOMLEN + 1];
232 
233 	kt = kgdb_thr_lookup_tid(tid);
234 	if (kt == NULL)
235 		return (NULL);
236 	if (kvm_read(kvm, kt->kaddr + offsetof(struct thread, td_comm), &comm,
237 	    sizeof(comm)) != sizeof(comm))
238 		return (NULL);
239 
240 	return (comm);
241 }
242