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 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/trgt.c,v 1.12 2008/05/01 20:36:48 jhb Exp $ 27 */ 28 29 #include <sys/cdefs.h> 30 31 #include <sys/param.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 <readline/tilde.h> 41 #include <command.h> 42 #include <exec.h> 43 #include <frame-unwind.h> 44 #include <gdb.h> 45 #include <gdbcore.h> 46 #include <gdbthread.h> 47 #include <inferior.h> 48 #include <language.h> 49 #include <regcache.h> 50 #include <solib.h> 51 #include <target.h> 52 #include <ui-out.h> 53 #include <observer.h> 54 55 #include "kgdb.h" 56 57 static void kgdb_core_cleanup(void *); 58 59 static char *vmcore; 60 static struct target_ops kgdb_trgt_ops; 61 62 kvm_t *kvm; 63 static char kvm_err[_POSIX2_LINE_MAX]; 64 65 #define KERNOFF (kgdb_kernbase ()) 66 #define INKERNEL(x) ((x) >= KERNOFF) 67 68 static CORE_ADDR 69 kgdb_kernbase (void) 70 { 71 static CORE_ADDR kernbase; 72 struct minimal_symbol *sym; 73 74 if (kernbase == 0) { 75 sym = lookup_minimal_symbol ("kernbase", NULL, NULL); 76 if (sym == NULL) { 77 kernbase = KERNBASE; 78 } else { 79 kernbase = SYMBOL_VALUE_ADDRESS (sym); 80 } 81 } 82 return kernbase; 83 } 84 85 static void 86 kgdb_trgt_open(char *filename, int from_tty) 87 { 88 struct cleanup *old_chain; 89 struct kthr *kt; 90 struct inferior *inf8; 91 struct program_space *pspace; 92 kvm_t *nkvm; 93 char *temp; 94 int first_inferior = 1; 95 96 target_preopen (from_tty); 97 if (!filename) 98 error ("No vmcore file specified."); 99 if (!exec_bfd) 100 error ("Can't open a vmcore without a kernel"); 101 102 filename = tilde_expand (filename); 103 if (filename[0] != '/') { 104 temp = concat (current_directory, "/", filename, NULL); 105 xfree(filename); 106 filename = temp; 107 } 108 109 old_chain = make_cleanup (xfree, filename); 110 111 nkvm = kvm_openfiles(bfd_get_filename(exec_bfd), filename, NULL, 112 write_files ? O_RDWR : O_RDONLY, kvm_err); 113 if (nkvm == NULL) 114 error ("Failed to open vmcore: %s", kvm_err); 115 116 /* Don't free the filename now and close any previous vmcore. */ 117 discard_cleanups(old_chain); 118 unpush_target(&kgdb_trgt_ops); 119 120 kvm = nkvm; 121 vmcore = filename; 122 old_chain = make_cleanup(kgdb_core_cleanup, NULL); 123 124 push_target (&kgdb_trgt_ops); 125 discard_cleanups (old_chain); 126 127 kgdb_dmesg(); 128 129 init_thread_list(); 130 kt = kgdb_thr_init(); 131 while (kt != NULL) { 132 if (!in_inferior_list(kt->pid)) { 133 if (first_inferior) { 134 first_inferior = 0; 135 inf8 = current_inferior(); 136 inf8->pid = kt->pid; 137 inferior_appeared (inf8, kt->pid); 138 pspace = current_program_space; 139 pspace->ebfd = 0; 140 pspace->ebfd_mtime = 0; 141 } else { 142 inf8 = add_inferior(kt->pid); 143 pspace = add_program_space(new_address_space()); 144 pspace->symfile_object_file = symfile_objfile; 145 pspace->objfiles = object_files; 146 } 147 inf8->pspace = pspace; 148 inf8->aspace = pspace->aspace; 149 } 150 add_thread(ptid_build(kt->pid, 0, kt->tid)); 151 kt = kgdb_thr_next(kt); 152 } 153 if (curkthr != 0) 154 inferior_ptid = ptid_build(curkthr->pid, 0, curkthr->tid); 155 156 frame_unwind_prepend_unwinder(get_frame_arch(get_current_frame()), &kgdb_trgt_trapframe_unwind); 157 158 /* XXX: fetch registers? */ 159 kld_init(); 160 reinit_frame_cache(); 161 select_frame (get_current_frame()); 162 print_stack_frame(get_selected_frame(NULL), 163 frame_relative_level(get_selected_frame(NULL)), 1); 164 } 165 166 static void 167 kgdb_trgt_close(int quitting) 168 { 169 170 if (kvm != NULL) { 171 inferior_ptid = null_ptid; 172 clear_solib(); 173 if (kvm_close(kvm) != 0) 174 warning("cannot close \"%s\": %s", vmcore, 175 kvm_geterr(kvm)); 176 kvm = NULL; 177 xfree(vmcore); 178 vmcore = NULL; 179 } 180 } 181 182 static void 183 kgdb_core_cleanup(void *arg) 184 { 185 186 kgdb_trgt_close(0); 187 } 188 189 static void 190 kgdb_trgt_detach(struct target_ops *target, char *args, int from_tty) 191 { 192 193 if (args) 194 error ("Too many arguments"); 195 unpush_target(target); 196 reinit_frame_cache(); 197 if (from_tty) 198 printf_filtered("No vmcore file now.\n"); 199 } 200 201 static char * 202 kgdb_trgt_extra_thread_info(struct thread_info *ti) 203 { 204 205 return (kgdb_thr_extra_thread_info(ptid_get_tid(ti->ptid))); 206 } 207 208 static void 209 kgdb_trgt_files_info(struct target_ops *target) 210 { 211 212 printf_filtered ("\t`%s', ", vmcore); 213 wrap_here (" "); 214 printf_filtered ("file type %s.\n", "DragonFly kernel vmcore"); 215 } 216 217 static void 218 kgdb_trgt_find_new_threads(struct target_ops *target_ops) 219 { 220 struct target_ops *tb; 221 222 if (kvm != NULL) 223 return; 224 225 tb = find_target_beneath(target_ops); 226 if (tb->to_find_new_threads != NULL) 227 tb->to_find_new_threads(target_ops); 228 } 229 230 static char * 231 kgdb_trgt_pid_to_str(struct target_ops *target_ops __unused, ptid_t ptid) 232 { 233 return (kgdb_thr_pid_to_str(ptid)); 234 } 235 236 static int 237 kgdb_trgt_thread_alive(struct target_ops *target_ops __unused, ptid_t ptid) 238 { 239 return (kgdb_thr_lookup_tid(ptid_get_tid(ptid)) != NULL); 240 } 241 242 static LONGEST 243 kgdb_trgt_xfer_partial(struct target_ops *ops, enum target_object object, 244 const char *annex, gdb_byte *readbuf, 245 const gdb_byte *writebuf, 246 ULONGEST offset, LONGEST len) 247 { 248 if (kvm != NULL) { 249 if (len == 0) 250 return (0); 251 if (writebuf != NULL) 252 return (kvm_write(kvm, offset, writebuf, len)); 253 if (readbuf != NULL) 254 return (kvm_read(kvm, offset, readbuf, len)); 255 } 256 return (ops->beneath->to_xfer_partial(ops->beneath, object, annex, 257 readbuf, writebuf, offset, len)); 258 } 259 260 static void 261 kgdb_switch_to_thread(struct kthr *thr) 262 { 263 char buf[16]; 264 int thread_id; 265 char *err; 266 267 thread_id = pid_to_thread_id(ptid_build(thr->pid, 0, thr->tid)); 268 if (thread_id == 0) 269 error ("invalid tid"); 270 snprintf(buf, sizeof(buf), "%d", thread_id); 271 if (!gdb_thread_select(uiout, buf, &err)) 272 error ("%s", err); 273 } 274 275 static void 276 kgdb_set_proc_cmd (char *arg, int from_tty) 277 { 278 CORE_ADDR addr; 279 struct kthr *thr; 280 281 if (!arg) 282 error_no_arg ("proc address for the new context"); 283 284 if (kvm == NULL) 285 error ("only supported for core file target"); 286 287 addr = (CORE_ADDR) parse_and_eval_address (arg); 288 289 if (!INKERNEL (addr)) { 290 thr = kgdb_thr_lookup_pid((int)addr); 291 if (thr == NULL) 292 error ("invalid pid"); 293 } else { 294 thr = kgdb_thr_lookup_paddr(addr); 295 if (thr == NULL) 296 error("invalid proc address"); 297 } 298 kgdb_switch_to_thread(thr); 299 } 300 301 static void 302 kgdb_set_tid_cmd (char *arg, int from_tty) 303 { 304 CORE_ADDR addr; 305 struct kthr *thr; 306 307 if (!arg) 308 error_no_arg ("Thread address for the new context"); 309 310 addr = (CORE_ADDR) parse_and_eval_address (arg); 311 thr = kgdb_thr_lookup_taddr(addr); 312 313 if (thr == NULL) 314 error("invalid thread address"); 315 316 kgdb_switch_to_thread(thr); 317 } 318 319 int fbsdcoreops_suppress_target = 1; 320 321 void 322 initialize_kgdb_target(void) 323 { 324 kgdb_trgt_ops.to_magic = OPS_MAGIC; 325 kgdb_trgt_ops.to_shortname = "kernel"; 326 kgdb_trgt_ops.to_longname = "kernel core dump file"; 327 kgdb_trgt_ops.to_doc = 328 "Use a vmcore file as a target. Specify the filename of the vmcore file."; 329 kgdb_trgt_ops.to_stratum = process_stratum; 330 kgdb_trgt_ops.to_has_registers = default_child_has_registers; 331 kgdb_trgt_ops.to_has_memory = default_child_has_memory; 332 kgdb_trgt_ops.to_has_stack = default_child_has_stack; 333 334 kgdb_trgt_ops.to_open = kgdb_trgt_open; 335 kgdb_trgt_ops.to_close = kgdb_trgt_close; 336 kgdb_trgt_ops.to_attach = find_default_attach; 337 kgdb_trgt_ops.to_detach = kgdb_trgt_detach; 338 kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info; 339 kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers; 340 kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info; 341 kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads; 342 kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str; 343 /* 344 kgdb_trgt_ops.to_store_registers = NULL; 345 */ 346 kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive; 347 kgdb_trgt_ops.to_xfer_partial = kgdb_trgt_xfer_partial; 348 add_target(&kgdb_trgt_ops); 349 350 add_com ("proc", class_obscure, kgdb_set_proc_cmd, 351 "Set current process context"); 352 add_com ("tid", class_obscure, kgdb_set_tid_cmd, 353 "Set current thread context"); 354 } 355