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