2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/kthr.c,v 1.12 2008/05/01 20:36:48 jhb Exp $
29 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <machine/globaldata.h>
34 #include <sys/types.h>
35 #include <sys/signal.h>
44 #include <frame-unwind.h>
49 static CORE_ADDR dumppcb;
52 static struct kthr *first;
56 kgdb_lookup(const char *sym)
60 nl[0].n_name = (char *)(CORE_ADDR)sym;
62 if (kvm_nlist(kvm, nl) != 0)
64 return (nl[0].n_value);
79 struct mdglobaldata gd;
81 CORE_ADDR addr, paddr, prvspace;
84 while (first != NULL) {
90 addr = kgdb_lookup("_ncpus");
93 kvm_read(kvm, addr, &ncpus, sizeof(ncpus));
95 dumppcb = kgdb_lookup("_dumppcb");
99 prvspace = kgdb_lookup("_CPU_prvspace");
103 addr = kgdb_lookup("_dumpthread");
105 kvm_read(kvm, addr, &dumptid, sizeof(dumptid));
108 * XXX Well then. We don't know who dumped us.
109 * We could do some fancy stack matching, but
110 * I doubt this will work. For now just use
113 * Actually we don't even know if we were dumped
114 * or if we are life. Find out by querying "dumping".
118 addr = kgdb_lookup("_dumping");
119 kvm_read(kvm, addr, &dumping, sizeof(dumping));
121 kvm_read(kvm, prvspace +
122 offsetof(struct privatespace, mdglobaldata),
123 &gd, sizeof(struct mdglobaldata));
124 dumptid = (intptr_t)gd.mi.gd_curthread;
126 /* We must be a live system */
131 for (cpu = 0; cpu < ncpus; cpu++) {
132 kvm_read(kvm, prvspace +
133 cpu * sizeof(struct privatespace) +
134 offsetof(struct privatespace, mdglobaldata),
135 &gd, sizeof(struct mdglobaldata));
137 addr = (uintptr_t)TAILQ_FIRST(&gd.mi.gd_tdallq);
139 if (kvm_read(kvm, addr, &td, sizeof(td)) != sizeof(td)) {
140 warnx("kvm_read: %s, while accessing thread",
144 kt = malloc(sizeof(*kt));
147 kt->tid = addr; /* XXX do we have tids? */
148 kt->pcb = (kt->tid == dumptid) ? dumppcb :
149 (uintptr_t)td.td_pcb;
150 kt->kstack = (uintptr_t)td.td_kstack;
151 if (td.td_proc != NULL) {
152 paddr = (uintptr_t)td.td_proc;
153 if (kvm_read(kvm, paddr, &p, sizeof(p)) != sizeof(p))
154 warnx("kvm_read: %s", kvm_geterr(kvm));
157 addr = (uintptr_t)td.td_lwp;
158 if (kvm_read(kvm, addr, &lwp, sizeof(lwp)) != sizeof(lwp))
159 warnx("kvm_read: %s", kvm_geterr(kvm));
160 kt->lwpid = lwp.lwp_tid;
163 * XXX for some stupid reason, gdb uses pid == -1
164 * as a marker for "dead" threads, so we have to
165 * hook all kernel threads on a different pid :/
170 * We are a kernel thread, so our td_pcb is
171 * not used anyways. An exception is the
173 * kt->pcb == 0 is a marker for
174 * "non-dumping kernel thread".
176 if (kt->tid != dumptid)
180 addr = (uintptr_t)TAILQ_NEXT(&td, td_allq);
184 curkthr = kgdb_thr_lookup_tid(dumptid);
191 kgdb_thr_lookup_tid(int tid)
196 while (kt != NULL && kt->tid != tid)
202 kgdb_thr_lookup_taddr(uintptr_t taddr)
207 while (kt != NULL && kt->kaddr != taddr)
213 kgdb_thr_lookup_pid(int pid)
218 while (kt != NULL && kt->pid != pid)
224 kgdb_thr_lookup_paddr(uintptr_t paddr)
229 while (kt != NULL && kt->paddr != paddr)
235 kgdb_thr_next(struct kthr *kt)
241 kgdb_thr_select(struct kthr *kt)
251 kgdb_thr_extra_thread_info(int tid)
253 #if 0 /* XXX: Doesn't do anything, obsolete? */
257 kt = kgdb_thr_lookup_tid(tid);
267 kgdb_thr_pid_to_str(ptid_t ptid)
269 char comm[MAXCOMLEN + 1];
276 tid = ptid_get_tid(ptid);
278 kt = kgdb_thr_lookup_pid(ptid_get_pid(ptid));
280 kt = kgdb_thr_lookup_tid(tid);
288 snprintf(buf, sizeof(buf), "pid %d", kt->pid);
291 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
294 p = (struct proc *)kt->paddr;
295 if (kvm_read(kvm, (uintptr_t)&p->p_comm[0], &comm, sizeof(comm)) !=
299 strlcat(buf, ", ", sizeof(buf));
300 strlcat(buf, comm, sizeof(buf));
302 strcpy(buf, "kernel");
305 t = (struct thread *)kt->kaddr;
306 if (kvm_read(kvm, (uintptr_t)&t->td_comm[0], &comm,
307 sizeof(comm)) == sizeof(comm)) {
308 strlcat(buf, " ", sizeof(buf));
309 strlcat(buf, comm, sizeof(buf));