kthr.c revision 161621
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 161621 2006-08-25 16:20:17Z 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
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
55static uintptr_t
56lookup(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 = lookup("_allproc");
84	if (addr == 0)
85		return (NULL);
86	kvm_read(kvm, addr, &paddr, sizeof(paddr));
87
88	dumppcb = lookup("_dumppcb");
89	if (dumppcb == 0)
90		return (NULL);
91
92	addr = lookup("_dumptid");
93	if (addr != 0)
94		kvm_read(kvm, addr, &dumptid, sizeof(dumptid));
95	else
96		dumptid = -1;
97
98	addr =  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 = 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		addr = (uintptr_t)TAILQ_FIRST(&p.p_threads);
110		while (addr != 0) {
111			if (kvm_read(kvm, addr, &td, sizeof(td)) != sizeof(td))
112				warnx("kvm_read: %s", kvm_geterr(kvm));
113			kt = malloc(sizeof(*kt));
114			kt->next = first;
115			kt->kaddr = addr;
116			if (td.td_tid == dumptid)
117				kt->pcb = dumppcb;
118			else if (td.td_state == TDS_RUNNING && ((1 << td.td_oncpu) & stopped_cpus)
119				&& stoppcbs != 0)
120				kt->pcb = (uintptr_t) stoppcbs + sizeof(struct pcb) * td.td_oncpu;
121			else
122				kt->pcb = (uintptr_t)td.td_pcb;
123			kt->kstack = td.td_kstack;
124			kt->tid = td.td_tid;
125			kt->pid = p.p_pid;
126			kt->paddr = paddr;
127			first = kt;
128			addr = (uintptr_t)TAILQ_NEXT(&td, td_plist);
129		}
130		paddr = (uintptr_t)LIST_NEXT(&p, p_list);
131	}
132	curkthr = kgdb_thr_lookup_tid(dumptid);
133	if (curkthr == NULL)
134		curkthr = first;
135	return (first);
136}
137
138struct kthr *
139kgdb_thr_lookup_tid(int tid)
140{
141	struct kthr *kt;
142
143	kt = first;
144	while (kt != NULL && kt->tid != tid)
145		kt = kt->next;
146	return (kt);
147}
148
149struct kthr *
150kgdb_thr_lookup_taddr(uintptr_t taddr)
151{
152	struct kthr *kt;
153
154	kt = first;
155	while (kt != NULL && kt->kaddr != taddr)
156		kt = kt->next;
157	return (kt);
158}
159
160struct kthr *
161kgdb_thr_lookup_pid(int pid)
162{
163	struct kthr *kt;
164
165	kt = first;
166	while (kt != NULL && kt->pid != pid)
167		kt = kt->next;
168	return (kt);
169}
170
171struct kthr *
172kgdb_thr_lookup_paddr(uintptr_t paddr)
173{
174	struct kthr *kt;
175
176	kt = first;
177	while (kt != NULL && kt->paddr != paddr)
178		kt = kt->next;
179	return (kt);
180}
181
182struct kthr *
183kgdb_thr_next(struct kthr *kt)
184{
185	return (kt->next);
186}
187
188struct kthr *
189kgdb_thr_select(struct kthr *kt)
190{
191	struct kthr *pcur;
192
193	pcur = curkthr;
194	curkthr = kt;
195	return (pcur);
196}
197
198char *
199kgdb_thr_extra_thread_info(int tid)
200{
201	struct kthr *kt;
202	struct proc *p;
203	static char comm[MAXCOMLEN + 1];
204
205	kt = kgdb_thr_lookup_tid(tid);
206	if (kt == NULL)
207		return (NULL);
208	p = (struct proc *)kt->paddr;
209	if (kvm_read(kvm, (uintptr_t)&p->p_comm[0], &comm, sizeof(comm)) !=
210	    sizeof(comm))
211		return (NULL);
212
213	return (comm);
214}
215