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