kthr.c revision 167142
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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/gnu/usr.bin/gdb/kgdb/kthr.c 167142 2007-03-01 13:55:15Z kib $");
29
30#include <sys/param.h>
31#include <sys/proc.h>
32#include <sys/types.h>
33#include <sys/signal.h>
34#include <err.h>
35#include <inttypes.h>
36#include <kvm.h>
37#include <stdio.h>
38#include <stdlib.h>
39
40#include <defs.h>
41#include <frame-unwind.h>
42
43#include "kgdb.h"
44#include <machine/pcb.h>
45
46static uintptr_t dumppcb;
47static int dumptid;
48
49static uintptr_t stoppcbs;
50static __cpumask_t stopped_cpus;
51
52static struct kthr *first;
53struct kthr *curkthr;
54
55uintptr_t
56kgdb_lookup(const char *sym)
57{
58	struct nlist nl[2];
59
60	nl[0].n_name = (char *)(uintptr_t)sym;
61	nl[1].n_name = NULL;
62	if (kvm_nlist(kvm, nl) != 0) {
63		warnx("kvm_nlist(%s): %s", sym, kvm_geterr(kvm));
64		return (0);
65	}
66	return (nl[0].n_value);
67}
68
69struct kthr *
70kgdb_thr_first(void)
71{
72	return (first);
73}
74
75struct kthr *
76kgdb_thr_init(void)
77{
78	struct proc p;
79	struct thread td;
80	struct kthr *kt;
81	uintptr_t addr, paddr;
82
83	addr = kgdb_lookup("_allproc");
84	if (addr == 0)
85		return (NULL);
86	kvm_read(kvm, addr, &paddr, sizeof(paddr));
87
88	dumppcb = kgdb_lookup("_dumppcb");
89	if (dumppcb == 0)
90		return (NULL);
91
92	addr = kgdb_lookup("_dumptid");
93	if (addr != 0)
94		kvm_read(kvm, addr, &dumptid, sizeof(dumptid));
95	else
96		dumptid = -1;
97
98	addr =  kgdb_lookup("_stopped_cpus");
99	if (addr != 0)
100		kvm_read(kvm, addr, &stopped_cpus, sizeof(stopped_cpus));
101	else
102		stopped_cpus = 0;
103
104	stoppcbs = kgdb_lookup("_stoppcbs");
105
106	while (paddr != 0) {
107		if (kvm_read(kvm, paddr, &p, sizeof(p)) != sizeof(p)) {
108			warnx("kvm_read: %s", kvm_geterr(kvm));
109			break;
110		}
111		addr = (uintptr_t)TAILQ_FIRST(&p.p_threads);
112		while (addr != 0) {
113			if (kvm_read(kvm, addr, &td, sizeof(td)) !=
114			    sizeof(td)) {
115				warnx("kvm_read: %s", kvm_geterr(kvm));
116				break;
117			}
118			kt = malloc(sizeof(*kt));
119			kt->next = first;
120			kt->kaddr = addr;
121			if (td.td_tid == dumptid)
122				kt->pcb = dumppcb;
123			else if (td.td_state == TDS_RUNNING && ((1 << td.td_oncpu) & stopped_cpus)
124				&& stoppcbs != 0)
125				kt->pcb = (uintptr_t) stoppcbs + sizeof(struct pcb) * td.td_oncpu;
126			else
127				kt->pcb = (uintptr_t)td.td_pcb;
128			kt->kstack = td.td_kstack;
129			kt->tid = td.td_tid;
130			kt->pid = p.p_pid;
131			kt->paddr = paddr;
132			first = kt;
133			addr = (uintptr_t)TAILQ_NEXT(&td, td_plist);
134		}
135		paddr = (uintptr_t)LIST_NEXT(&p, p_list);
136	}
137	curkthr = kgdb_thr_lookup_tid(dumptid);
138	if (curkthr == NULL)
139		curkthr = first;
140	return (first);
141}
142
143struct kthr *
144kgdb_thr_lookup_tid(int tid)
145{
146	struct kthr *kt;
147
148	kt = first;
149	while (kt != NULL && kt->tid != tid)
150		kt = kt->next;
151	return (kt);
152}
153
154struct kthr *
155kgdb_thr_lookup_taddr(uintptr_t taddr)
156{
157	struct kthr *kt;
158
159	kt = first;
160	while (kt != NULL && kt->kaddr != taddr)
161		kt = kt->next;
162	return (kt);
163}
164
165struct kthr *
166kgdb_thr_lookup_pid(int pid)
167{
168	struct kthr *kt;
169
170	kt = first;
171	while (kt != NULL && kt->pid != pid)
172		kt = kt->next;
173	return (kt);
174}
175
176struct kthr *
177kgdb_thr_lookup_paddr(uintptr_t paddr)
178{
179	struct kthr *kt;
180
181	kt = first;
182	while (kt != NULL && kt->paddr != paddr)
183		kt = kt->next;
184	return (kt);
185}
186
187struct kthr *
188kgdb_thr_next(struct kthr *kt)
189{
190	return (kt->next);
191}
192
193struct kthr *
194kgdb_thr_select(struct kthr *kt)
195{
196	struct kthr *pcur;
197
198	pcur = curkthr;
199	curkthr = kt;
200	return (pcur);
201}
202
203char *
204kgdb_thr_extra_thread_info(int tid)
205{
206	struct kthr *kt;
207	struct proc *p;
208	static char comm[MAXCOMLEN + 1];
209
210	kt = kgdb_thr_lookup_tid(tid);
211	if (kt == NULL)
212		return (NULL);
213	p = (struct proc *)kt->paddr;
214	if (kvm_read(kvm, (uintptr_t)&p->p_comm[0], &comm, sizeof(comm)) !=
215	    sizeof(comm))
216		return (NULL);
217
218	return (comm);
219}
220