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>
31 #include <sys/cpuset.h>
33 #include <sys/types.h>
34 #include <sys/signal.h>
44 #include <frame-unwind.h>
48 static CORE_ADDR dumppcb;
51 static cpuset_t stopped_cpus;
53 static struct kthr *first;
57 kgdb_lookup(const char *sym)
62 asprintf(&name, "&%s", sym);
63 addr = kgdb_parse(name);
75 kgdb_thr_add_procs(uintptr_t paddr)
83 if (kvm_read(kvm, paddr, &p, sizeof(p)) != sizeof(p)) {
84 warnx("kvm_read: %s", kvm_geterr(kvm));
87 addr = (uintptr_t)TAILQ_FIRST(&p.p_threads);
89 if (kvm_read(kvm, addr, &td, sizeof(td)) !=
91 warnx("kvm_read: %s", kvm_geterr(kvm));
94 kt = malloc(sizeof(*kt));
97 if (td.td_tid == dumptid)
99 else if (td.td_oncpu != NOCPU &&
100 CPU_ISSET(td.td_oncpu, &stopped_cpus))
101 kt->pcb = kgdb_trgt_core_pcb(td.td_oncpu);
103 kt->pcb = (uintptr_t)td.td_pcb;
104 kt->kstack = td.td_kstack;
108 kt->cpu = td.td_oncpu;
110 addr = (uintptr_t)TAILQ_NEXT(&td, td_plist);
112 paddr = (uintptr_t)LIST_NEXT(&p, p_list);
124 while (first != NULL) {
130 addr = kgdb_lookup("allproc");
133 kvm_read(kvm, addr, &paddr, sizeof(paddr));
135 dumppcb = kgdb_lookup("dumppcb");
139 addr = kgdb_lookup("dumptid");
141 kvm_read(kvm, addr, &dumptid, sizeof(dumptid));
145 addr = kgdb_lookup("stopped_cpus");
146 CPU_ZERO(&stopped_cpus);
147 cpusetsize = sysconf(_SC_CPUSET_SIZE);
148 if (cpusetsize != -1 && (u_long)cpusetsize <= sizeof(cpuset_t) &&
150 kvm_read(kvm, addr, &stopped_cpus, cpusetsize);
152 kgdb_thr_add_procs(paddr);
153 addr = kgdb_lookup("zombproc");
155 kvm_read(kvm, addr, &paddr, sizeof(paddr));
156 kgdb_thr_add_procs(paddr);
158 curkthr = kgdb_thr_lookup_tid(dumptid);
165 kgdb_thr_lookup_tid(int tid)
170 while (kt != NULL && kt->tid != tid)
176 kgdb_thr_lookup_taddr(uintptr_t taddr)
181 while (kt != NULL && kt->kaddr != taddr)
187 kgdb_thr_lookup_pid(int pid)
192 while (kt != NULL && kt->pid != pid)
198 kgdb_thr_lookup_paddr(uintptr_t paddr)
203 while (kt != NULL && kt->paddr != paddr)
209 kgdb_thr_next(struct kthr *kt)
215 kgdb_thr_select(struct kthr *kt)
225 kgdb_thr_extra_thread_info(int tid)
227 char comm[MAXCOMLEN + 1];
228 char td_name[MAXCOMLEN + 1];
234 kt = kgdb_thr_lookup_tid(tid);
237 snprintf(buf, sizeof(buf), "PID=%d", kt->pid);
238 p = (struct proc *)kt->paddr;
239 if (kvm_read(kvm, (uintptr_t)&p->p_comm[0], &comm, sizeof(comm)) !=
242 strlcat(buf, ": ", sizeof(buf));
243 strlcat(buf, comm, sizeof(buf));
244 t = (struct thread *)kt->kaddr;
245 if (kvm_read(kvm, (uintptr_t)&t->td_name[0], &td_name,
246 sizeof(td_name)) == sizeof(td_name) &&
247 strcmp(comm, td_name) != 0) {
248 strlcat(buf, "/", sizeof(buf));
249 strlcat(buf, td_name, sizeof(buf));