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