gdb: Adjust makefiles for gdb 7.3
[dragonfly.git] / gnu / usr.bin / gdb / kgdb / trgt.c
CommitLineData
8b6a428f
SS
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 *
f17ea84b 26 * $FreeBSD: src/gnu/usr.bin/gdb/kgdb/trgt.c,v 1.12 2008/05/01 20:36:48 jhb Exp $
8b6a428f
SS
27 */
28
29#include <sys/cdefs.h>
30
31#include <sys/param.h>
8b6a428f
SS
32#include <sys/sysctl.h>
33#include <sys/user.h>
f17ea84b
SS
34#include <err.h>
35#include <fcntl.h>
8b6a428f
SS
36#include <kvm.h>
37
38#include <defs.h>
f17ea84b
SS
39#include <readline/readline.h>
40#include <readline/tilde.h>
8b6a428f 41#include <command.h>
f17ea84b 42#include <exec.h>
8b6a428f 43#include <frame-unwind.h>
f17ea84b
SS
44#include <gdb.h>
45#include <gdbcore.h>
8b6a428f
SS
46#include <gdbthread.h>
47#include <inferior.h>
f17ea84b 48#include <language.h>
8b6a428f 49#include <regcache.h>
f17ea84b 50#include <solib.h>
8b6a428f 51#include <target.h>
f17ea84b
SS
52#include <ui-out.h>
53#include <observer.h>
8b6a428f
SS
54
55#include "kgdb.h"
56
f17ea84b
SS
57static void kgdb_core_cleanup(void *);
58
59static char *vmcore;
8b6a428f
SS
60static struct target_ops kgdb_trgt_ops;
61
f17ea84b
SS
62kvm_t *kvm;
63static char kvm_err[_POSIX2_LINE_MAX];
64
65#define KERNOFF (kgdb_kernbase ())
66#define INKERNEL(x) ((x) >= KERNOFF)
67
68static CORE_ADDR
69kgdb_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
85static void
86kgdb_trgt_open(char *filename, int from_tty)
87{
88 struct cleanup *old_chain;
f17ea84b 89 struct kthr *kt;
a938cc15
JM
90 struct inferior *inf8;
91 struct program_space *pspace;
f17ea84b
SS
92 kvm_t *nkvm;
93 char *temp;
a938cc15 94 int first_inferior = 1;
f17ea84b
SS
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
a938cc15 124 push_target (&kgdb_trgt_ops);
f17ea84b
SS
125 discard_cleanups (old_chain);
126
127 kgdb_dmesg();
128
129 init_thread_list();
130 kt = kgdb_thr_init();
131 while (kt != NULL) {
a938cc15
JM
132 if (!in_inferior_list(kt->pid)) {
133 inf8 = add_inferior(kt->pid);
134 if (first_inferior) {
135 first_inferior = 0;
136 set_current_inferior (inf8);
137 pspace = current_program_space;
138 pspace->ebfd = 0;
139 pspace->ebfd_mtime = 0;
140 } else {
141 pspace = add_program_space(new_address_space());
142 pspace->symfile_object_file = symfile_objfile;
143 pspace->objfiles = object_files;
144 pspace->target_sections =
145 current_program_space->target_sections;
146 }
147 inf8->pspace = pspace;
148 inf8->aspace = pspace->aspace;
149 }
150 add_thread(ptid_build(kt->pid, 0, kt->tid));
f17ea84b
SS
151 kt = kgdb_thr_next(kt);
152 }
153 if (curkthr != 0)
154 inferior_ptid = ptid_build(curkthr->pid, 0, curkthr->tid);
155
939fa31e
MD
156 frame_unwind_prepend_unwinder(get_frame_arch(get_current_frame()), &kgdb_trgt_trapframe_unwind);
157
a938cc15
JM
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);
f17ea84b
SS
164}
165
166static void
167kgdb_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
182static void
183kgdb_core_cleanup(void *arg)
184{
185
186 kgdb_trgt_close(0);
187}
188
189static void
190kgdb_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
8b6a428f
SS
201static char *
202kgdb_trgt_extra_thread_info(struct thread_info *ti)
203{
f17ea84b
SS
204
205 return (kgdb_thr_extra_thread_info(ptid_get_tid(ti->ptid)));
8b6a428f
SS
206}
207
208static void
209kgdb_trgt_files_info(struct target_ops *target)
210{
8b6a428f 211
f17ea84b
SS
212 printf_filtered ("\t`%s', ", vmcore);
213 wrap_here (" ");
214 printf_filtered ("file type %s.\n", "DragonFly kernel vmcore");
8b6a428f
SS
215}
216
217static void
f17ea84b 218kgdb_trgt_find_new_threads(struct target_ops *target_ops)
8b6a428f
SS
219{
220 struct target_ops *tb;
221
222 if (kvm != NULL)
223 return;
224
f17ea84b 225 tb = find_target_beneath(target_ops);
8b6a428f 226 if (tb->to_find_new_threads != NULL)
f17ea84b 227 tb->to_find_new_threads(target_ops);
8b6a428f
SS
228}
229
230static char *
f17ea84b 231kgdb_trgt_pid_to_str(struct target_ops *target_ops __unused, ptid_t ptid)
8b6a428f 232{
f17ea84b 233 return (kgdb_thr_pid_to_str(ptid));
8b6a428f
SS
234}
235
236static int
f17ea84b 237kgdb_trgt_thread_alive(struct target_ops *target_ops __unused, ptid_t ptid)
8b6a428f
SS
238{
239 return (kgdb_thr_lookup_tid(ptid_get_tid(ptid)) != NULL);
240}
241
0c3d4888
SS
242static LONGEST
243kgdb_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)
8b6a428f 247{
8b6a428f
SS
248 if (kvm != NULL) {
249 if (len == 0)
250 return (0);
0c3d4888
SS
251 if (writebuf != NULL)
252 return (kvm_write(kvm, offset, writebuf, len));
253 if (readbuf != NULL)
254 return (kvm_read(kvm, offset, readbuf, len));
8b6a428f 255 }
0c3d4888
SS
256 return (ops->beneath->to_xfer_partial(ops->beneath, object, annex,
257 readbuf, writebuf, offset, len));
8b6a428f
SS
258}
259
f17ea84b
SS
260static void
261kgdb_switch_to_thread(struct kthr *thr)
8b6a428f 262{
f17ea84b
SS
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
275static void
276kgdb_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");
8b6a428f 286
f17ea84b
SS
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
301static void
302kgdb_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
319int fbsdcoreops_suppress_target = 1;
320
321void
322initialize_kgdb_target(void)
323{
8b6a428f
SS
324 kgdb_trgt_ops.to_magic = OPS_MAGIC;
325 kgdb_trgt_ops.to_shortname = "kernel";
f17ea84b
SS
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.";
0c3d3a6f 329 kgdb_trgt_ops.to_stratum = process_stratum;
f17ea84b
SS
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;
8b6a428f 333
f17ea84b
SS
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;
8b6a428f
SS
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;
f17ea84b
SS
343 /*
344 kgdb_trgt_ops.to_store_registers = NULL;
345 */
8b6a428f 346 kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
0c3d4888 347 kgdb_trgt_ops.to_xfer_partial = kgdb_trgt_xfer_partial;
8b6a428f 348 add_target(&kgdb_trgt_ops);
8b6a428f 349
f17ea84b
SS
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");
8b6a428f 354}