kthr.c revision 149954
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 149954 2005-09-10 18:25:53Z marcel $"); 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 45static uintptr_t dumppcb; 46static int dumptid; 47 48static struct kthr *first; 49struct kthr *curkthr; 50 51static uintptr_t 52lookup(const char *sym) 53{ 54 struct nlist nl[2]; 55 56 nl[0].n_name = (char *)(uintptr_t)sym; 57 nl[1].n_name = NULL; 58 if (kvm_nlist(kvm, nl) != 0) { 59 warnx("kvm_nlist(%s): %s", sym, kvm_geterr(kvm)); 60 return (0); 61 } 62 return (nl[0].n_value); 63} 64 65struct kthr * 66kgdb_thr_first(void) 67{ 68 return (first); 69} 70 71struct kthr * 72kgdb_thr_init(void) 73{ 74 struct proc p; 75 struct thread td; 76 struct kthr *kt; 77 uintptr_t addr, paddr; 78 79 addr = lookup("_allproc"); 80 if (addr == 0) 81 return (NULL); 82 kvm_read(kvm, addr, &paddr, sizeof(paddr)); 83 84 dumppcb = lookup("_dumppcb"); 85 if (dumppcb == 0) 86 return (NULL); 87 88 addr = lookup("_dumptid"); 89 if (addr != 0) 90 kvm_read(kvm, addr, &dumptid, sizeof(dumptid)); 91 else 92 dumptid = -1; 93 94 while (paddr != 0) { 95 if (kvm_read(kvm, paddr, &p, sizeof(p)) != sizeof(p)) 96 warnx("kvm_read: %s", kvm_geterr(kvm)); 97 addr = (uintptr_t)TAILQ_FIRST(&p.p_threads); 98 while (addr != 0) { 99 if (kvm_read(kvm, addr, &td, sizeof(td)) != sizeof(td)) 100 warnx("kvm_read: %s", kvm_geterr(kvm)); 101 kt = malloc(sizeof(*kt)); 102 kt->next = first; 103 kt->kaddr = addr; 104 kt->pcb = (td.td_tid == dumptid) ? dumppcb : 105 (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 first = kt; 111 addr = (uintptr_t)TAILQ_NEXT(&td, td_plist); 112 } 113 paddr = (uintptr_t)LIST_NEXT(&p, p_list); 114 } 115 curkthr = kgdb_thr_lookup_tid(dumptid); 116 if (curkthr == NULL) 117 curkthr = first; 118 return (first); 119} 120 121struct kthr * 122kgdb_thr_lookup_tid(int tid) 123{ 124 struct kthr *kt; 125 126 kt = first; 127 while (kt != NULL && kt->tid != tid) 128 kt = kt->next; 129 return (kt); 130} 131 132struct kthr * 133kgdb_thr_lookup_taddr(uintptr_t taddr) 134{ 135 struct kthr *kt; 136 137 kt = first; 138 while (kt != NULL && kt->kaddr != taddr) 139 kt = kt->next; 140 return (kt); 141} 142 143struct kthr * 144kgdb_thr_lookup_pid(int pid) 145{ 146 struct kthr *kt; 147 148 kt = first; 149 while (kt != NULL && kt->pid != pid) 150 kt = kt->next; 151 return (kt); 152} 153 154struct kthr * 155kgdb_thr_lookup_paddr(uintptr_t paddr) 156{ 157 struct kthr *kt; 158 159 kt = first; 160 while (kt != NULL && kt->paddr != paddr) 161 kt = kt->next; 162 return (kt); 163} 164 165struct kthr * 166kgdb_thr_next(struct kthr *kt) 167{ 168 return (kt->next); 169} 170 171struct kthr * 172kgdb_thr_select(struct kthr *kt) 173{ 174 struct kthr *pcur; 175 176 pcur = curkthr; 177 curkthr = kt; 178 return (pcur); 179} 180 181char * 182kgdb_thr_extra_thread_info(int tid) 183{ 184 struct kthr *kt; 185 struct proc *p; 186 static char comm[MAXCOMLEN + 1]; 187 188 kt = kgdb_thr_lookup_tid(tid); 189 if (kt == NULL) 190 return (NULL); 191 p = (struct proc *)kt->paddr; 192 if (kvm_read(kvm, (uintptr_t)&p->p_comm[0], &comm, sizeof(comm)) != 193 sizeof(comm)) 194 return (NULL); 195 196 return (comm); 197} 198