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