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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/signal.h>
42 #include <frame-unwind.h>
45 #include <machine/pcb.h>
47 static uintptr_t dumppcb;
50 static uintptr_t stoppcbs;
51 static __cpumask_t stopped_cpus;
53 static struct kthr *first;
57 kgdb_lookup(const char *sym)
61 nl[0].n_type = N_UNDF;
62 nl[0].n_name = (char *)(uintptr_t)sym;
64 if (kvm_nlist(kvm, nl) != 0)
66 return (nl[0].n_value);
81 uintptr_t addr, paddr;
83 while (first != NULL) {
89 addr = kgdb_lookup("_allproc");
91 warnx("kvm_nlist(_allproc): %s", kvm_geterr(kvm));
94 kvm_read(kvm, addr, &paddr, sizeof(paddr));
96 dumppcb = kgdb_lookup("_dumppcb");
98 warnx("kvm_nlist(_dumppcb): %s", kvm_geterr(kvm));
102 addr = kgdb_lookup("_dumptid");
104 kvm_read(kvm, addr, &dumptid, sizeof(dumptid));
108 addr = kgdb_lookup("_stopped_cpus");
110 kvm_read(kvm, addr, &stopped_cpus, sizeof(stopped_cpus));
114 stoppcbs = kgdb_lookup("_stoppcbs");
117 if (kvm_read(kvm, paddr, &p, sizeof(p)) != sizeof(p)) {
118 warnx("kvm_read: %s", kvm_geterr(kvm));
121 addr = (uintptr_t)TAILQ_FIRST(&p.p_threads);
123 if (kvm_read(kvm, addr, &td, sizeof(td)) !=
125 warnx("kvm_read: %s", kvm_geterr(kvm));
128 kt = malloc(sizeof(*kt));
131 if (td.td_tid == dumptid)
133 else if (td.td_state == TDS_RUNNING && ((1 << td.td_oncpu) & stopped_cpus)
135 kt->pcb = (uintptr_t) stoppcbs + sizeof(struct pcb) * td.td_oncpu;
137 kt->pcb = (uintptr_t)td.td_pcb;
138 kt->kstack = td.td_kstack;
142 kt->cpu = td.td_oncpu;
144 addr = (uintptr_t)TAILQ_NEXT(&td, td_plist);
146 paddr = (uintptr_t)LIST_NEXT(&p, p_list);
148 curkthr = kgdb_thr_lookup_tid(dumptid);
155 kgdb_thr_lookup_tid(int tid)
160 while (kt != NULL && kt->tid != tid)
166 kgdb_thr_lookup_taddr(uintptr_t taddr)
171 while (kt != NULL && kt->kaddr != taddr)
177 kgdb_thr_lookup_pid(int pid)
182 while (kt != NULL && kt->pid != pid)
188 kgdb_thr_lookup_paddr(uintptr_t paddr)
193 while (kt != NULL && kt->paddr != paddr)
199 kgdb_thr_next(struct kthr *kt)
205 kgdb_thr_select(struct kthr *kt)
215 kgdb_thr_extra_thread_info(int tid)
217 char comm[MAXCOMLEN + 1];
218 char td_name[MAXCOMLEN + 1];
224 kt = kgdb_thr_lookup_tid(tid);
227 snprintf(buf, sizeof(buf), "PID=%d", kt->pid);
228 p = (struct proc *)kt->paddr;
229 if (kvm_read(kvm, (uintptr_t)&p->p_comm[0], &comm, sizeof(comm)) !=
232 strlcat(buf, ": ", sizeof(buf));
233 strlcat(buf, comm, sizeof(buf));
234 t = (struct thread *)kt->kaddr;
235 if (kvm_read(kvm, (uintptr_t)&t->td_name[0], &td_name,
236 sizeof(td_name)) == sizeof(td_name) &&
237 strcmp(comm, td_name) != 0) {
238 strlcat(buf, "/", sizeof(buf));
239 strlcat(buf, td_name, sizeof(buf));