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 thread_info *ti; 90 struct kthr *kt; 91 kvm_t *nkvm; 92 char *temp; 93 int ontop; 94 95 target_preopen (from_tty); 96 if (!filename) 97 error ("No vmcore file specified."); 98 if (!exec_bfd) 99 error ("Can't open a vmcore without a kernel"); 100 101 filename = tilde_expand (filename); 102 if (filename[0] != '/') { 103 temp = concat (current_directory, "/", filename, NULL); 104 xfree(filename); 105 filename = temp; 106 } 107 108 old_chain = make_cleanup (xfree, filename); 109 110 nkvm = kvm_openfiles(bfd_get_filename(exec_bfd), filename, NULL, 111 write_files ? O_RDWR : O_RDONLY, kvm_err); 112 if (nkvm == NULL) 113 error ("Failed to open vmcore: %s", kvm_err); 114 115 /* Don't free the filename now and close any previous vmcore. */ 116 discard_cleanups(old_chain); 117 unpush_target(&kgdb_trgt_ops); 118 119 kvm = nkvm; 120 vmcore = filename; 121 old_chain = make_cleanup(kgdb_core_cleanup, NULL); 122 123 ontop = !push_target (&kgdb_trgt_ops); 124 discard_cleanups (old_chain); 125 126 kgdb_dmesg(); 127 128 init_thread_list(); 129 kt = kgdb_thr_init(); 130 while (kt != NULL) { 131 if (!in_inferior_list(kt->pid)) 132 add_inferior(kt->pid); 133 ti = add_thread_silent(ptid_build(kt->pid, 0, kt->tid)); 134 kt = kgdb_thr_next(kt); 135 } 136 if (curkthr != 0) 137 inferior_ptid = ptid_build(curkthr->pid, 0, curkthr->tid); 138 139 frame_unwind_prepend_unwinder(get_frame_arch(get_current_frame()), &kgdb_trgt_trapframe_unwind); 140 141 if (ontop) { 142 /* XXX: fetch registers? */ 143 kld_init(); 144 reinit_frame_cache(); 145 select_frame (get_current_frame()); 146 print_stack_frame(get_selected_frame(NULL), 147 frame_relative_level(get_selected_frame(NULL)), 1); 148 } else 149 warning( 150 "you won't be able to access this vmcore until you terminate\n\ 151 your %s; do ``info files''", target_longname); 152 } 153 154 static void 155 kgdb_trgt_close(int quitting) 156 { 157 158 if (kvm != NULL) { 159 inferior_ptid = null_ptid; 160 clear_solib(); 161 if (kvm_close(kvm) != 0) 162 warning("cannot close \"%s\": %s", vmcore, 163 kvm_geterr(kvm)); 164 kvm = NULL; 165 xfree(vmcore); 166 vmcore = NULL; 167 } 168 } 169 170 static void 171 kgdb_core_cleanup(void *arg) 172 { 173 174 kgdb_trgt_close(0); 175 } 176 177 static void 178 kgdb_trgt_detach(struct target_ops *target, char *args, int from_tty) 179 { 180 181 if (args) 182 error ("Too many arguments"); 183 unpush_target(target); 184 reinit_frame_cache(); 185 if (from_tty) 186 printf_filtered("No vmcore file now.\n"); 187 } 188 189 static char * 190 kgdb_trgt_extra_thread_info(struct thread_info *ti) 191 { 192 193 return (kgdb_thr_extra_thread_info(ptid_get_tid(ti->ptid))); 194 } 195 196 static void 197 kgdb_trgt_files_info(struct target_ops *target) 198 { 199 200 printf_filtered ("\t`%s', ", vmcore); 201 wrap_here (" "); 202 printf_filtered ("file type %s.\n", "DragonFly kernel vmcore"); 203 } 204 205 static void 206 kgdb_trgt_find_new_threads(struct target_ops *target_ops) 207 { 208 struct target_ops *tb; 209 210 if (kvm != NULL) 211 return; 212 213 tb = find_target_beneath(target_ops); 214 if (tb->to_find_new_threads != NULL) 215 tb->to_find_new_threads(target_ops); 216 } 217 218 static char * 219 kgdb_trgt_pid_to_str(struct target_ops *target_ops __unused, ptid_t ptid) 220 { 221 return (kgdb_thr_pid_to_str(ptid)); 222 } 223 224 static int 225 kgdb_trgt_thread_alive(struct target_ops *target_ops __unused, ptid_t ptid) 226 { 227 return (kgdb_thr_lookup_tid(ptid_get_tid(ptid)) != NULL); 228 } 229 230 static LONGEST 231 kgdb_trgt_xfer_partial(struct target_ops *ops, enum target_object object, 232 const char *annex, gdb_byte *readbuf, 233 const gdb_byte *writebuf, 234 ULONGEST offset, LONGEST len) 235 { 236 if (kvm != NULL) { 237 if (len == 0) 238 return (0); 239 if (writebuf != NULL) 240 return (kvm_write(kvm, offset, writebuf, len)); 241 if (readbuf != NULL) 242 return (kvm_read(kvm, offset, readbuf, len)); 243 } 244 return (ops->beneath->to_xfer_partial(ops->beneath, object, annex, 245 readbuf, writebuf, offset, len)); 246 } 247 248 static void 249 kgdb_switch_to_thread(struct kthr *thr) 250 { 251 char buf[16]; 252 int thread_id; 253 char *err; 254 255 thread_id = pid_to_thread_id(ptid_build(thr->pid, 0, thr->tid)); 256 if (thread_id == 0) 257 error ("invalid tid"); 258 snprintf(buf, sizeof(buf), "%d", thread_id); 259 if (!gdb_thread_select(uiout, buf, &err)) 260 error ("%s", err); 261 } 262 263 static void 264 kgdb_set_proc_cmd (char *arg, int from_tty) 265 { 266 CORE_ADDR addr; 267 struct kthr *thr; 268 269 if (!arg) 270 error_no_arg ("proc address for the new context"); 271 272 if (kvm == NULL) 273 error ("only supported for core file target"); 274 275 addr = (CORE_ADDR) parse_and_eval_address (arg); 276 277 if (!INKERNEL (addr)) { 278 thr = kgdb_thr_lookup_pid((int)addr); 279 if (thr == NULL) 280 error ("invalid pid"); 281 } else { 282 thr = kgdb_thr_lookup_paddr(addr); 283 if (thr == NULL) 284 error("invalid proc address"); 285 } 286 kgdb_switch_to_thread(thr); 287 } 288 289 static void 290 kgdb_set_tid_cmd (char *arg, int from_tty) 291 { 292 CORE_ADDR addr; 293 struct kthr *thr; 294 295 if (!arg) 296 error_no_arg ("Thread address for the new context"); 297 298 addr = (CORE_ADDR) parse_and_eval_address (arg); 299 thr = kgdb_thr_lookup_taddr(addr); 300 301 if (thr == NULL) 302 error("invalid thread address"); 303 304 kgdb_switch_to_thread(thr); 305 } 306 307 int fbsdcoreops_suppress_target = 1; 308 309 void 310 initialize_kgdb_target(void) 311 { 312 kgdb_trgt_ops.to_magic = OPS_MAGIC; 313 kgdb_trgt_ops.to_shortname = "kernel"; 314 kgdb_trgt_ops.to_longname = "kernel core dump file"; 315 kgdb_trgt_ops.to_doc = 316 "Use a vmcore file as a target. Specify the filename of the vmcore file."; 317 kgdb_trgt_ops.to_stratum = core_stratum; 318 kgdb_trgt_ops.to_has_registers = default_child_has_registers; 319 kgdb_trgt_ops.to_has_memory = default_child_has_memory; 320 kgdb_trgt_ops.to_has_stack = default_child_has_stack; 321 322 kgdb_trgt_ops.to_open = kgdb_trgt_open; 323 kgdb_trgt_ops.to_close = kgdb_trgt_close; 324 kgdb_trgt_ops.to_attach = find_default_attach; 325 kgdb_trgt_ops.to_detach = kgdb_trgt_detach; 326 kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info; 327 kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers; 328 kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info; 329 kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads; 330 kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str; 331 /* 332 kgdb_trgt_ops.to_store_registers = NULL; 333 */ 334 kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive; 335 kgdb_trgt_ops.to_xfer_partial = kgdb_trgt_xfer_partial; 336 add_target(&kgdb_trgt_ops); 337 338 add_com ("proc", class_obscure, kgdb_set_proc_cmd, 339 "Set current process context"); 340 add_com ("tid", class_obscure, kgdb_set_tid_cmd, 341 "Set current thread context"); 342 } 343