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