kthr.c revision 175771
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 175771 2008-01-28 20:33:19Z 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	addr = kgdb_lookup("_allproc");
83	if (addr == 0) {
84		warnx("kvm_nlist(_allproc): %s", kvm_geterr(kvm));
85		return (NULL);
86	}
87	kvm_read(kvm, addr, &paddr, sizeof(paddr));
88
89	dumppcb = kgdb_lookup("_dumppcb");
90	if (dumppcb == 0) {
91		warnx("kvm_nlist(_dumppcb): %s", kvm_geterr(kvm));
92		return (NULL);
93	}
94
95	addr = kgdb_lookup("_dumptid");
96	if (addr != 0)
97		kvm_read(kvm, addr, &dumptid, sizeof(dumptid));
98	else
99		dumptid = -1;
100
101	addr =  kgdb_lookup("_stopped_cpus");
102	if (addr != 0)
103		kvm_read(kvm, addr, &stopped_cpus, sizeof(stopped_cpus));
104	else
105		stopped_cpus = 0;
106
107	stoppcbs = kgdb_lookup("_stoppcbs");
108
109	while (paddr != 0) {
110		if (kvm_read(kvm, paddr, &p, sizeof(p)) != sizeof(p)) {
111			warnx("kvm_read: %s", kvm_geterr(kvm));
112			break;
113		}
114		addr = (uintptr_t)TAILQ_FIRST(&p.p_threads);
115		while (addr != 0) {
116			if (kvm_read(kvm, addr, &td, sizeof(td)) !=
117			    sizeof(td)) {
118				warnx("kvm_read: %s", kvm_geterr(kvm));
119				break;
120			}
121			kt = malloc(sizeof(*kt));
122			kt->next = first;
123			kt->kaddr = addr;
124			if (td.td_tid == dumptid)
125				kt->pcb = dumppcb;
126			else if (td.td_state == TDS_RUNNING && ((1 << td.td_oncpu) & stopped_cpus)
127				&& stoppcbs != 0)
128				kt->pcb = (uintptr_t) stoppcbs + sizeof(struct pcb) * td.td_oncpu;
129			else
130				kt->pcb = (uintptr_t)td.td_pcb;
131			kt->kstack = td.td_kstack;
132			kt->tid = td.td_tid;
133			kt->pid = p.p_pid;
134			kt->paddr = paddr;
135			kt->cpu = td.td_oncpu;
136			first = kt;
137			addr = (uintptr_t)TAILQ_NEXT(&td, td_plist);
138		}
139		paddr = (uintptr_t)LIST_NEXT(&p, p_list);
140	}
141	curkthr = kgdb_thr_lookup_tid(dumptid);
142	if (curkthr == NULL)
143		curkthr = first;
144	return (first);
145}
146
147struct kthr *
148kgdb_thr_lookup_tid(int tid)
149{
150	struct kthr *kt;
151
152	kt = first;
153	while (kt != NULL && kt->tid != tid)
154		kt = kt->next;
155	return (kt);
156}
157
158struct kthr *
159kgdb_thr_lookup_taddr(uintptr_t taddr)
160{
161	struct kthr *kt;
162
163	kt = first;
164	while (kt != NULL && kt->kaddr != taddr)
165		kt = kt->next;
166	return (kt);
167}
168
169struct kthr *
170kgdb_thr_lookup_pid(int pid)
171{
172	struct kthr *kt;
173
174	kt = first;
175	while (kt != NULL && kt->pid != pid)
176		kt = kt->next;
177	return (kt);
178}
179
180struct kthr *
181kgdb_thr_lookup_paddr(uintptr_t paddr)
182{
183	struct kthr *kt;
184
185	kt = first;
186	while (kt != NULL && kt->paddr != paddr)
187		kt = kt->next;
188	return (kt);
189}
190
191struct kthr *
192kgdb_thr_next(struct kthr *kt)
193{
194	return (kt->next);
195}
196
197struct kthr *
198kgdb_thr_select(struct kthr *kt)
199{
200	struct kthr *pcur;
201
202	pcur = curkthr;
203	curkthr = kt;
204	return (pcur);
205}
206
207char *
208kgdb_thr_extra_thread_info(int tid)
209{
210	char comm[MAXCOMLEN + 1];
211	char td_name[MAXCOMLEN + 1];
212	struct kthr *kt;
213	struct proc *p;
214	struct thread *t;
215	static char info[MAXCOMLEN + 1 + MAXCOMLEN + 1];
216
217	kt = kgdb_thr_lookup_tid(tid);
218	if (kt == NULL)
219		return (NULL);
220	p = (struct proc *)kt->paddr;
221	t = (struct thread *)kt->kaddr;
222	if (kvm_read(kvm, (uintptr_t)&p->p_comm[0], &comm, sizeof(comm)) !=
223	    sizeof(comm))
224		return (NULL);
225	if (kvm_read(kvm, (uintptr_t)&t->td_name[0], &td_name,
226	    sizeof(td_name)) == sizeof(td_name) &&
227	    strcmp(comm, td_name) != 0)
228		snprintf(info, sizeof(info), "%s/%s", comm, td_name);
229	else
230		strlcpy(info, comm, sizeof(info));
231	return (info);
232}
233