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.1 2006/03/07 15:48:11 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 first = kt; 161 addr = (uintptr_t)TAILQ_NEXT(&td, td_allq); 162 } 163 } 164 165 curkthr = kgdb_thr_lookup_tid(dumptid); 166 if (curkthr == NULL) 167 curkthr = first; 168 return (first); 169 } 170 171 struct kthr * 172 kgdb_thr_lookup_tid(int tid) 173 { 174 struct kthr *kt; 175 176 kt = first; 177 while (kt != NULL && kt->tid != tid) 178 kt = kt->next; 179 return (kt); 180 } 181 182 struct kthr * 183 kgdb_thr_lookup_taddr(uintptr_t taddr) 184 { 185 struct kthr *kt; 186 187 kt = first; 188 while (kt != NULL && kt->kaddr != taddr) 189 kt = kt->next; 190 return (kt); 191 } 192 193 struct kthr * 194 kgdb_thr_lookup_pid(int pid) 195 { 196 struct kthr *kt; 197 198 kt = first; 199 while (kt != NULL && kt->pid != pid) 200 kt = kt->next; 201 return (kt); 202 } 203 204 struct kthr * 205 kgdb_thr_lookup_paddr(uintptr_t paddr) 206 { 207 struct kthr *kt; 208 209 kt = first; 210 while (kt != NULL && kt->paddr != paddr) 211 kt = kt->next; 212 return (kt); 213 } 214 215 struct kthr * 216 kgdb_thr_next(struct kthr *kt) 217 { 218 return (kt->next); 219 } 220 221 struct kthr * 222 kgdb_thr_select(struct kthr *kt) 223 { 224 struct kthr *pcur; 225 226 pcur = curkthr; 227 curkthr = kt; 228 return (pcur); 229 } 230 231 char * 232 kgdb_thr_extra_thread_info(int tid) 233 { 234 struct kthr *kt; 235 static char comm[MAXCOMLEN + 1]; 236 237 kt = kgdb_thr_lookup_tid(tid); 238 if (kt == NULL) 239 return (NULL); 240 if (kvm_read(kvm, kt->kaddr + offsetof(struct thread, td_comm), &comm, 241 sizeof(comm)) != sizeof(comm)) 242 return (NULL); 243 244 return (comm); 245 } 246