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 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/kthr.c,v 1.3 2005/09/10 18:25:53 marcel Exp $ 27 * $DragonFly: src/gnu/usr.bin/gdb/kgdb/kthr.c,v 1.2 2006/07/09 01:38:57 corecode Exp $ 28 */ 29 30 #define _KERNEL_STRUCTURES 31 32 #include <sys/cdefs.h> 33 34 #include <sys/param.h> 35 #include <machine/globaldata.h> 36 #include <sys/user.h> 37 #include <sys/types.h> 38 #include <sys/signal.h> 39 #include <err.h> 40 #include <inttypes.h> 41 #include <kvm.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 45 #include <defs.h> 46 #include <frame-unwind.h> 47 48 #include "kgdb.h" 49 50 static uintptr_t dumppcb; 51 static int dumptid; 52 53 static struct kthr *first; 54 struct kthr *curkthr; 55 56 static uintptr_t 57 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 warnx("kvm_nlist(%s): %s", sym, kvm_geterr(kvm)); 65 return (0); 66 } 67 return (nl[0].n_value); 68 } 69 70 struct kthr * 71 kgdb_thr_first(void) 72 { 73 return (first); 74 } 75 76 struct kthr * 77 kgdb_thr_init(void) 78 { 79 struct proc p; 80 struct lwp lwp; 81 struct thread td; 82 struct mdglobaldata gd; 83 struct kthr *kt; 84 uintptr_t addr, paddr, prvspace; 85 int cpu, ncpus; 86 87 addr = lookup("_ncpus"); 88 if (addr == 0) 89 return (NULL); 90 kvm_read(kvm, addr, &ncpus, sizeof(ncpus)); 91 92 dumppcb = lookup("_dumppcb"); 93 if (dumppcb == 0) 94 return (NULL); 95 96 prvspace = lookup("CPU_prvspace"); 97 if (prvspace == 0) 98 return (NULL); 99 100 addr = lookup("_dumpthread"); 101 if (addr != 0) { 102 kvm_read(kvm, addr, &dumptid, sizeof(dumptid)); 103 } else { 104 /* 105 * XXX Well then. We don't know who dumped us. 106 * We could do some fancy stack matching, but 107 * I doubt this will work. For now just use 108 * cpu0's curthread. 109 * 110 * Actually we don't even know if we were dumped 111 * or if we are life. Find out by querying "dumping". 112 */ 113 int dumping = 0; 114 115 addr = lookup("_dumping"); 116 kvm_read(kvm, addr, &dumping, sizeof(dumping)); 117 if (dumping) { 118 kvm_read(kvm, prvspace + 119 offsetof(struct privatespace, mdglobaldata), 120 &gd, sizeof(struct mdglobaldata)); 121 dumptid = gd.mi.gd_curthread; 122 } else { 123 /* We must be a live system */ 124 dumptid = -1; 125 } 126 } 127 128 for (cpu = 0; cpu < ncpus; cpu++) { 129 kvm_read(kvm, prvspace + 130 cpu * sizeof(struct privatespace) + 131 offsetof(struct privatespace, mdglobaldata), 132 &gd, sizeof(struct mdglobaldata)); 133 134 addr = (uintptr_t)TAILQ_FIRST(&gd.mi.gd_tdallq); 135 while (addr != 0) { 136 if (kvm_read(kvm, addr, &td, sizeof(td)) != sizeof(td)) 137 warnx("kvm_read: %s", kvm_geterr(kvm)); 138 kt = malloc(sizeof(*kt)); 139 kt->next = first; 140 kt->kaddr = addr; 141 kt->tid = addr; /* XXX do we have tids? */ 142 kt->pcb = (kt->tid == dumptid) ? dumppcb : 143 (uintptr_t)td.td_pcb; 144 kt->kstack = (uintptr_t)td.td_kstack; 145 if (td.td_proc != NULL) { 146 paddr = (uintptr_t)td.td_proc; 147 if (kvm_read(kvm, paddr, &p, sizeof(p)) != sizeof(p)) 148 warnx("kvm_read: %s", kvm_geterr(kvm)); 149 kt->pid = p.p_pid; 150 kt->paddr = paddr; 151 } else { 152 /* 153 * XXX for some stupid reason, gdb uses pid == -1 154 * as a marker for "dead" threads, so we have to 155 * hook all kernel threads on a different pid :/ 156 */ 157 kt->pid = -2; 158 kt->paddr = 0; 159 /* 160 * We are a kernel thread, so our td_pcb is 161 * not used anyways. An exception is the 162 * dumping thread. 163 * kt->pcb == NULL is a marker for 164 * "non-dumping kernel thread". 165 */ 166 if (kt->tid != dumptid) 167 kt->pcb = NULL; 168 } 169 first = kt; 170 addr = (uintptr_t)TAILQ_NEXT(&td, td_allq); 171 } 172 } 173 174 curkthr = kgdb_thr_lookup_tid(dumptid); 175 if (curkthr == NULL) 176 curkthr = first; 177 return (first); 178 } 179 180 struct kthr * 181 kgdb_thr_lookup_tid(int tid) 182 { 183 struct kthr *kt; 184 185 kt = first; 186 while (kt != NULL && kt->tid != tid) 187 kt = kt->next; 188 return (kt); 189 } 190 191 struct kthr * 192 kgdb_thr_lookup_taddr(uintptr_t taddr) 193 { 194 struct kthr *kt; 195 196 kt = first; 197 while (kt != NULL && kt->kaddr != taddr) 198 kt = kt->next; 199 return (kt); 200 } 201 202 struct kthr * 203 kgdb_thr_lookup_pid(int pid) 204 { 205 struct kthr *kt; 206 207 kt = first; 208 while (kt != NULL && kt->pid != pid) 209 kt = kt->next; 210 return (kt); 211 } 212 213 struct kthr * 214 kgdb_thr_lookup_paddr(uintptr_t paddr) 215 { 216 struct kthr *kt; 217 218 kt = first; 219 while (kt != NULL && kt->paddr != paddr) 220 kt = kt->next; 221 return (kt); 222 } 223 224 struct kthr * 225 kgdb_thr_next(struct kthr *kt) 226 { 227 return (kt->next); 228 } 229 230 struct kthr * 231 kgdb_thr_select(struct kthr *kt) 232 { 233 struct kthr *pcur; 234 235 pcur = curkthr; 236 curkthr = kt; 237 return (pcur); 238 } 239 240 char * 241 kgdb_thr_extra_thread_info(int tid) 242 { 243 struct kthr *kt; 244 static char comm[MAXCOMLEN + 1]; 245 246 kt = kgdb_thr_lookup_tid(tid); 247 if (kt == NULL) 248 return (NULL); 249 if (kvm_read(kvm, kt->kaddr + offsetof(struct thread, td_comm), &comm, 250 sizeof(comm)) != sizeof(comm)) 251 return (NULL); 252 253 return (comm); 254 } 255