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 AUTHOR ``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 AUTHOR 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$"); 29 30#include <sys/param.h> 31#include <sys/proc.h> 32#include <sys/sysctl.h> 33#include <sys/user.h> 34#include <err.h> 35#include <fcntl.h> 36#include <kvm.h> 37 38#include <defs.h> 39#include <readline/readline.h> 40#include <command.h> 41#include <exec.h> 42#include <frame-unwind.h> 43#include <gdb.h> 44#include <gdbcore.h> 45#include <gdbthread.h> 46#include <inferior.h> 47#include <language.h> 48#include <regcache.h> 49#include <solib.h> 50#include <target.h> 51#include <ui-out.h> 52 53#include "kgdb.h" 54 55#ifdef CROSS_DEBUGGER 56/* 57 * We suppress the call to add_target() of core_ops in corelow.c because if 58 * there are multiple core_stratum targets, the find_core_target() function 59 * won't know which one to return and returns none. We need it to return 60 * our target. We only have to do that when we're building a cross-debugger 61 * because fbsd-threads.c is part of a native debugger and it too defines 62 * coreops_suppress_target with 1 as the initializer. 63 */ 64int coreops_suppress_target = 1; 65#endif 66 67static CORE_ADDR stoppcbs; 68 69static void kgdb_core_cleanup(void *); 70 71static char *vmcore; 72static struct target_ops kgdb_trgt_ops; 73 74kvm_t *kvm; 75static char kvm_err[_POSIX2_LINE_MAX]; 76 77#define KERNOFF (kgdb_kernbase ()) 78#define PINKERNEL(x) ((x) >= KERNOFF) 79 80static int 81kgdb_resolve_symbol(const char *name, kvaddr_t *kva) 82{ 83 struct minimal_symbol *ms; 84 85 ms = lookup_minimal_symbol (name, NULL, NULL); 86 if (ms == NULL) 87 return (1); 88 89 *kva = SYMBOL_VALUE_ADDRESS (ms); 90 return (0); 91} 92 93static CORE_ADDR 94kgdb_kernbase (void) 95{ 96 static CORE_ADDR kernbase; 97 struct minimal_symbol *sym; 98 99 if (kernbase == 0) { 100 sym = lookup_minimal_symbol ("kernbase", NULL, NULL); 101 if (sym == NULL) { 102 kernbase = KERNBASE; 103 } else { 104 kernbase = SYMBOL_VALUE_ADDRESS (sym); 105 } 106 } 107 return kernbase; 108} 109 110static void 111kgdb_trgt_open(char *filename, int from_tty) 112{ 113 struct cleanup *old_chain; 114 struct thread_info *ti; 115 struct kthr *kt; 116 kvm_t *nkvm; 117 char *temp; 118 int ontop; 119 120 target_preopen (from_tty); 121 if (!filename) 122 error ("No vmcore file specified."); 123 if (!exec_bfd) 124 error ("Can't open a vmcore without a kernel"); 125 126 filename = tilde_expand (filename); 127 if (filename[0] != '/') { 128 temp = concat (current_directory, "/", filename, NULL); 129 xfree(filename); 130 filename = temp; 131 } 132 133 old_chain = make_cleanup (xfree, filename); 134 135 nkvm = kvm_open2(bfd_get_filename(exec_bfd), filename, 136 write_files ? O_RDWR : O_RDONLY, kvm_err, kgdb_resolve_symbol); 137 if (nkvm == NULL) 138 error ("Failed to open vmcore: %s", kvm_err); 139 140 /* Don't free the filename now and close any previous vmcore. */ 141 discard_cleanups(old_chain); 142 unpush_target(&kgdb_trgt_ops); 143 144 kvm = nkvm; 145 vmcore = filename; 146 old_chain = make_cleanup(kgdb_core_cleanup, NULL); 147 148 ontop = !push_target (&kgdb_trgt_ops); 149 discard_cleanups (old_chain); 150 151 kgdb_dmesg(); 152 153 init_thread_list(); 154 kt = kgdb_thr_init(); 155 while (kt != NULL) { 156 ti = add_thread(pid_to_ptid(kt->tid)); 157 kt = kgdb_thr_next(kt); 158 } 159 if (curkthr != 0) 160 inferior_ptid = pid_to_ptid(curkthr->tid); 161 162 if (ontop) { 163 /* XXX: fetch registers? */ 164 kld_init(); 165 flush_cached_frames(); 166 select_frame (get_current_frame()); 167 print_stack_frame(get_selected_frame(), 168 frame_relative_level(get_selected_frame()), 1); 169 } else 170 warning( 171 "you won't be able to access this vmcore until you terminate\n\ 172your %s; do ``info files''", target_longname); 173} 174 175static void 176kgdb_trgt_close(int quitting) 177{ 178 179 if (kvm != NULL) { 180 inferior_ptid = null_ptid; 181 CLEAR_SOLIB(); 182 if (kvm_close(kvm) != 0) 183 warning("cannot close \"%s\": %s", vmcore, 184 kvm_geterr(kvm)); 185 kvm = NULL; 186 xfree(vmcore); 187 vmcore = NULL; 188 if (kgdb_trgt_ops.to_sections) { 189 xfree(kgdb_trgt_ops.to_sections); 190 kgdb_trgt_ops.to_sections = NULL; 191 kgdb_trgt_ops.to_sections_end = NULL; 192 } 193 } 194} 195 196static void 197kgdb_core_cleanup(void *arg) 198{ 199 200 kgdb_trgt_close(0); 201} 202 203static void 204kgdb_trgt_detach(char *args, int from_tty) 205{ 206 207 if (args) 208 error ("Too many arguments"); 209 unpush_target(&kgdb_trgt_ops); 210 reinit_frame_cache(); 211 if (from_tty) 212 printf_filtered("No vmcore file now.\n"); 213} 214 215static char * 216kgdb_trgt_extra_thread_info(struct thread_info *ti) 217{ 218 219 return (kgdb_thr_extra_thread_info(ptid_get_pid(ti->ptid))); 220} 221 222static void 223kgdb_trgt_files_info(struct target_ops *target) 224{ 225 226 printf_filtered ("\t`%s', ", vmcore); 227 wrap_here (" "); 228 printf_filtered ("file type %s.\n", "FreeBSD kernel vmcore"); 229} 230 231static void 232kgdb_trgt_find_new_threads(void) 233{ 234 struct target_ops *tb; 235 236 if (kvm != NULL) 237 return; 238 239 tb = find_target_beneath(&kgdb_trgt_ops); 240 if (tb->to_find_new_threads != NULL) 241 tb->to_find_new_threads(); 242} 243 244static char * 245kgdb_trgt_pid_to_str(ptid_t ptid) 246{ 247 static char buf[33]; 248 249 snprintf(buf, sizeof(buf), "Thread %d", ptid_get_pid(ptid)); 250 return (buf); 251} 252 253static int 254kgdb_trgt_thread_alive(ptid_t ptid) 255{ 256 return (kgdb_thr_lookup_tid(ptid_get_pid(ptid)) != NULL); 257} 258 259static int 260kgdb_trgt_xfer_memory(CORE_ADDR memaddr, char *myaddr, int len, int write, 261 struct mem_attrib *attrib, struct target_ops *target) 262{ 263 struct target_ops *tb; 264 265 if (kvm != NULL) { 266 if (len == 0) 267 return (0); 268 if (!write) 269 return (kvm_read2(kvm, memaddr, myaddr, len)); 270 else 271 return (kvm_write(kvm, memaddr, myaddr, len)); 272 } 273 tb = find_target_beneath(target); 274 return (tb->to_xfer_memory(memaddr, myaddr, len, write, attrib, tb)); 275} 276 277static int 278kgdb_trgt_ignore_breakpoints(CORE_ADDR addr, char *contents) 279{ 280 281 return 0; 282} 283 284static void 285kgdb_switch_to_thread(int tid) 286{ 287 char buf[16]; 288 int thread_id; 289 290 thread_id = pid_to_thread_id(pid_to_ptid(tid)); 291 if (thread_id == 0) 292 error ("invalid tid"); 293 snprintf(buf, sizeof(buf), "%d", thread_id); 294 gdb_thread_select(uiout, buf); 295} 296 297static void 298kgdb_set_proc_cmd (char *arg, int from_tty) 299{ 300 CORE_ADDR addr; 301 struct kthr *thr; 302 303 if (!arg) 304 error_no_arg ("proc address for the new context"); 305 306 if (kvm == NULL) 307 error ("only supported for core file target"); 308 309 addr = (CORE_ADDR) parse_and_eval_address (arg); 310 311 if (!PINKERNEL (addr)) { 312 thr = kgdb_thr_lookup_pid((int)addr); 313 if (thr == NULL) 314 error ("invalid pid"); 315 } else { 316 thr = kgdb_thr_lookup_paddr(addr); 317 if (thr == NULL) 318 error("invalid proc address"); 319 } 320 kgdb_switch_to_thread(thr->tid); 321} 322 323static void 324kgdb_set_tid_cmd (char *arg, int from_tty) 325{ 326 CORE_ADDR addr; 327 struct kthr *thr; 328 329 if (!arg) 330 error_no_arg ("TID or thread address for the new context"); 331 332 addr = (CORE_ADDR) parse_and_eval_address (arg); 333 334 if (kvm != NULL && PINKERNEL (addr)) { 335 thr = kgdb_thr_lookup_taddr(addr); 336 if (thr == NULL) 337 error("invalid thread address"); 338 addr = thr->tid; 339 } 340 kgdb_switch_to_thread(addr); 341} 342 343int fbsdcoreops_suppress_target = 1; 344 345void 346initialize_kgdb_target(void) 347{ 348 349 kgdb_trgt_ops.to_magic = OPS_MAGIC; 350 kgdb_trgt_ops.to_shortname = "kernel"; 351 kgdb_trgt_ops.to_longname = "kernel core dump file"; 352 kgdb_trgt_ops.to_doc = 353 "Use a vmcore file as a target. Specify the filename of the vmcore file."; 354 kgdb_trgt_ops.to_stratum = core_stratum; 355 kgdb_trgt_ops.to_has_memory = 1; 356 kgdb_trgt_ops.to_has_registers = 1; 357 kgdb_trgt_ops.to_has_stack = 1; 358 359 kgdb_trgt_ops.to_open = kgdb_trgt_open; 360 kgdb_trgt_ops.to_close = kgdb_trgt_close; 361 kgdb_trgt_ops.to_attach = find_default_attach; 362 kgdb_trgt_ops.to_detach = kgdb_trgt_detach; 363 kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info; 364 kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers; 365 kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info; 366 kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads; 367 kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str; 368 kgdb_trgt_ops.to_store_registers = kgdb_trgt_store_registers; 369 kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive; 370 kgdb_trgt_ops.to_xfer_memory = kgdb_trgt_xfer_memory; 371 kgdb_trgt_ops.to_insert_breakpoint = kgdb_trgt_ignore_breakpoints; 372 kgdb_trgt_ops.to_remove_breakpoint = kgdb_trgt_ignore_breakpoints; 373 374 add_target(&kgdb_trgt_ops); 375 376 add_com ("proc", class_obscure, kgdb_set_proc_cmd, 377 "Set current process context"); 378 add_com ("tid", class_obscure, kgdb_set_tid_cmd, 379 "Set current thread context"); 380} 381 382CORE_ADDR 383kgdb_trgt_stop_pcb(u_int cpuid, u_int pcbsz) 384{ 385 static int once = 0; 386 387 if (stoppcbs == 0 && !once) { 388 once = 1; 389 stoppcbs = kgdb_lookup("stoppcbs"); 390 } 391 if (stoppcbs == 0) 392 return 0; 393 394 return (stoppcbs + pcbsz * cpuid); 395} 396