kgdb: Tweak to avoid extra inferior
[dragonfly.git] / gnu / usr.bin / gdb / kgdb / trgt.c
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                        pspace->target_sections = 
147                                current_program_space->target_sections;
148                      }
149                      inf8->pspace = pspace;
150                      inf8->aspace = pspace->aspace;
151                 }
152                 add_thread(ptid_build(kt->pid, 0, kt->tid));
153                 kt = kgdb_thr_next(kt);
154         }
155         if (curkthr != 0)
156                 inferior_ptid = ptid_build(curkthr->pid, 0, curkthr->tid);
157
158         frame_unwind_prepend_unwinder(get_frame_arch(get_current_frame()), &kgdb_trgt_trapframe_unwind);
159
160         /* XXX: fetch registers? */
161         kld_init();
162         reinit_frame_cache();
163         select_frame (get_current_frame());
164         print_stack_frame(get_selected_frame(NULL),
165           frame_relative_level(get_selected_frame(NULL)), 1);
166 }
167
168 static void
169 kgdb_trgt_close(int quitting)
170 {
171
172         if (kvm != NULL) {
173                 inferior_ptid = null_ptid;
174                 clear_solib();
175                 if (kvm_close(kvm) != 0)
176                         warning("cannot close \"%s\": %s", vmcore,
177                             kvm_geterr(kvm));
178                 kvm = NULL;
179                 xfree(vmcore);
180                 vmcore = NULL;
181         }
182 }
183
184 static void
185 kgdb_core_cleanup(void *arg)
186 {
187
188         kgdb_trgt_close(0);
189 }
190
191 static void
192 kgdb_trgt_detach(struct target_ops *target, char *args, int from_tty)
193 {
194
195         if (args)
196                 error ("Too many arguments");
197         unpush_target(target);
198         reinit_frame_cache();
199         if (from_tty)
200                 printf_filtered("No vmcore file now.\n");
201 }
202
203 static char *
204 kgdb_trgt_extra_thread_info(struct thread_info *ti)
205 {
206
207         return (kgdb_thr_extra_thread_info(ptid_get_tid(ti->ptid)));
208 }
209
210 static void
211 kgdb_trgt_files_info(struct target_ops *target)
212 {
213
214         printf_filtered ("\t`%s', ", vmcore);
215         wrap_here ("        ");
216         printf_filtered ("file type %s.\n", "DragonFly kernel vmcore");
217 }
218
219 static void
220 kgdb_trgt_find_new_threads(struct target_ops *target_ops)
221 {
222         struct target_ops *tb;
223
224         if (kvm != NULL)
225                 return;
226
227         tb = find_target_beneath(target_ops);
228         if (tb->to_find_new_threads != NULL)
229                 tb->to_find_new_threads(target_ops);
230 }
231
232 static char *
233 kgdb_trgt_pid_to_str(struct target_ops *target_ops __unused, ptid_t ptid)
234 {
235         return (kgdb_thr_pid_to_str(ptid));
236 }
237
238 static int
239 kgdb_trgt_thread_alive(struct target_ops *target_ops __unused, ptid_t ptid)
240 {
241         return (kgdb_thr_lookup_tid(ptid_get_tid(ptid)) != NULL);
242 }
243
244 static LONGEST
245 kgdb_trgt_xfer_partial(struct target_ops *ops, enum target_object object,
246                        const char *annex, gdb_byte *readbuf,
247                        const gdb_byte *writebuf,
248                        ULONGEST offset, LONGEST len)
249 {
250         if (kvm != NULL) {
251                 if (len == 0)
252                         return (0);
253                 if (writebuf != NULL)
254                         return (kvm_write(kvm, offset, writebuf, len));
255                 if (readbuf != NULL)
256                         return (kvm_read(kvm, offset, readbuf, len));
257         }
258         return (ops->beneath->to_xfer_partial(ops->beneath, object, annex,
259                                               readbuf, writebuf, offset, len));
260 }
261
262 static void
263 kgdb_switch_to_thread(struct kthr *thr)
264 {
265         char buf[16];
266         int thread_id;
267         char *err;
268
269         thread_id = pid_to_thread_id(ptid_build(thr->pid, 0, thr->tid));
270         if (thread_id == 0)
271                 error ("invalid tid");
272         snprintf(buf, sizeof(buf), "%d", thread_id);
273         if (!gdb_thread_select(uiout, buf, &err))
274                 error ("%s", err);
275 }
276
277 static void
278 kgdb_set_proc_cmd (char *arg, int from_tty)
279 {
280         CORE_ADDR addr;
281         struct kthr *thr;
282
283         if (!arg)
284                 error_no_arg ("proc address for the new context");
285
286         if (kvm == NULL)
287                 error ("only supported for core file target");
288
289         addr = (CORE_ADDR) parse_and_eval_address (arg);
290
291         if (!INKERNEL (addr)) {
292                 thr = kgdb_thr_lookup_pid((int)addr);
293                 if (thr == NULL)
294                         error ("invalid pid");
295         } else {
296                 thr = kgdb_thr_lookup_paddr(addr);
297                 if (thr == NULL)
298                         error("invalid proc address");
299         }
300         kgdb_switch_to_thread(thr);
301 }
302
303 static void
304 kgdb_set_tid_cmd (char *arg, int from_tty)
305 {
306         CORE_ADDR addr;
307         struct kthr *thr;
308
309         if (!arg)
310                 error_no_arg ("Thread address for the new context");
311
312         addr = (CORE_ADDR) parse_and_eval_address (arg);
313         thr = kgdb_thr_lookup_taddr(addr);
314
315         if (thr == NULL)
316                 error("invalid thread address");
317
318         kgdb_switch_to_thread(thr);
319 }
320
321 int fbsdcoreops_suppress_target = 1;
322
323 void
324 initialize_kgdb_target(void)
325 {
326         kgdb_trgt_ops.to_magic = OPS_MAGIC;
327         kgdb_trgt_ops.to_shortname = "kernel";
328         kgdb_trgt_ops.to_longname = "kernel core dump file";
329         kgdb_trgt_ops.to_doc = 
330     "Use a vmcore file as a target.  Specify the filename of the vmcore file.";
331         kgdb_trgt_ops.to_stratum = process_stratum;
332         kgdb_trgt_ops.to_has_registers = default_child_has_registers;
333         kgdb_trgt_ops.to_has_memory = default_child_has_memory;
334         kgdb_trgt_ops.to_has_stack = default_child_has_stack;
335
336         kgdb_trgt_ops.to_open = kgdb_trgt_open;
337         kgdb_trgt_ops.to_close = kgdb_trgt_close;
338         kgdb_trgt_ops.to_attach = find_default_attach;
339         kgdb_trgt_ops.to_detach = kgdb_trgt_detach;
340         kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info;
341         kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers;
342         kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info;
343         kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads;
344         kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str;
345         /*
346         kgdb_trgt_ops.to_store_registers = NULL;
347         */
348         kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
349         kgdb_trgt_ops.to_xfer_partial = kgdb_trgt_xfer_partial;
350         add_target(&kgdb_trgt_ops);
351
352         add_com ("proc", class_obscure, kgdb_set_proc_cmd,
353            "Set current process context");
354         add_com ("tid", class_obscure, kgdb_set_tid_cmd,
355            "Set current thread context");
356 }