]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - gnu/usr.bin/gdb/kgdb/trgt.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/proc.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
54 #include "kgdb.h"
55
56 static void     kgdb_core_cleanup(void *);
57
58 static char *vmcore;
59 static struct target_ops kgdb_trgt_ops;
60
61 kvm_t *kvm;
62 static char kvm_err[_POSIX2_LINE_MAX];
63
64 #define KERNOFF         (kgdb_kernbase ())
65 #define INKERNEL(x)     ((x) >= KERNOFF)
66
67 static CORE_ADDR
68 kgdb_kernbase (void)
69 {
70         static CORE_ADDR kernbase;
71         struct minimal_symbol *sym;
72
73         if (kernbase == 0) {
74                 sym = lookup_minimal_symbol ("kernbase", NULL, NULL);
75                 if (sym == NULL) {
76                         kernbase = KERNBASE;
77                 } else {
78                         kernbase = SYMBOL_VALUE_ADDRESS (sym);
79                 }
80         }
81         return kernbase;
82 }
83
84 static void
85 kgdb_trgt_open(char *filename, int from_tty)
86 {
87         struct cleanup *old_chain;
88         struct thread_info *ti;
89         struct kthr *kt;
90         kvm_t *nkvm;
91         char *temp;
92         int ontop;
93
94         target_preopen (from_tty);
95         if (!filename)
96                 error ("No vmcore file specified.");
97         if (!exec_bfd)
98                 error ("Can't open a vmcore without a kernel");
99
100         filename = tilde_expand (filename);
101         if (filename[0] != '/') {
102                 temp = concat (current_directory, "/", filename, NULL);
103                 xfree(filename);
104                 filename = temp;
105         }
106
107         old_chain = make_cleanup (xfree, filename);
108
109         nkvm = kvm_openfiles(bfd_get_filename(exec_bfd), filename, NULL,
110             write_files ? O_RDWR : O_RDONLY, kvm_err);
111         if (nkvm == NULL)
112                 error ("Failed to open vmcore: %s", kvm_err);
113
114         /* Don't free the filename now and close any previous vmcore. */
115         discard_cleanups(old_chain);
116         unpush_target(&kgdb_trgt_ops);
117
118         kvm = nkvm;
119         vmcore = filename;
120         old_chain = make_cleanup(kgdb_core_cleanup, NULL);
121
122         ontop = !push_target (&kgdb_trgt_ops);
123         discard_cleanups (old_chain);
124
125         kgdb_dmesg();
126
127         init_thread_list();
128         kt = kgdb_thr_init();
129         while (kt != NULL) {
130                 ti = add_thread(pid_to_ptid(kt->tid));
131                 kt = kgdb_thr_next(kt);
132         }
133         if (curkthr != 0)
134                 inferior_ptid = pid_to_ptid(curkthr->tid);
135
136         if (ontop) {
137                 /* XXX: fetch registers? */
138                 kld_init();
139                 flush_cached_frames();
140                 select_frame (get_current_frame());
141                 print_stack_frame(get_selected_frame(),
142                     frame_relative_level(get_selected_frame()), 1);
143         } else
144                 warning(
145         "you won't be able to access this vmcore until you terminate\n\
146 your %s; do ``info files''", target_longname);
147 }
148
149 static void
150 kgdb_trgt_close(int quitting)
151 {
152
153         if (kvm != NULL) {              
154                 inferior_ptid = null_ptid;
155                 CLEAR_SOLIB();
156                 if (kvm_close(kvm) != 0)
157                         warning("cannot close \"%s\": %s", vmcore,
158                             kvm_geterr(kvm));
159                 kvm = NULL;
160                 xfree(vmcore);
161                 vmcore = NULL;
162                 if (kgdb_trgt_ops.to_sections) {
163                         xfree(kgdb_trgt_ops.to_sections);
164                         kgdb_trgt_ops.to_sections = NULL;
165                         kgdb_trgt_ops.to_sections_end = NULL;
166                 }
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(char *args, int from_tty)
179 {
180
181         if (args)
182                 error ("Too many arguments");
183         unpush_target(&kgdb_trgt_ops);
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_pid(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", "FreeBSD kernel vmcore");
203 }
204
205 static void
206 kgdb_trgt_find_new_threads(void)
207 {
208         struct target_ops *tb;
209
210         if (kvm != NULL)
211                 return;
212
213         tb = find_target_beneath(&kgdb_trgt_ops);
214         if (tb->to_find_new_threads != NULL)
215                 tb->to_find_new_threads();
216 }
217
218 static char *
219 kgdb_trgt_pid_to_str(ptid_t ptid)
220 {
221         static char buf[33];
222
223         snprintf(buf, sizeof(buf), "Thread %d", ptid_get_pid(ptid));
224         return (buf);
225 }
226
227 static int
228 kgdb_trgt_thread_alive(ptid_t ptid)
229 {
230         return (kgdb_thr_lookup_tid(ptid_get_pid(ptid)) != NULL);
231 }
232
233 static int
234 kgdb_trgt_xfer_memory(CORE_ADDR memaddr, char *myaddr, int len, int write,
235     struct mem_attrib *attrib, struct target_ops *target)
236 {
237         struct target_ops *tb;
238
239         if (kvm != NULL) {
240                 if (len == 0)
241                         return (0);
242                 if (!write)
243                         return (kvm_read(kvm, memaddr, myaddr, len));
244                 else
245                         return (kvm_write(kvm, memaddr, myaddr, len));
246         }
247         tb = find_target_beneath(target);
248         return (tb->to_xfer_memory(memaddr, myaddr, len, write, attrib, tb));
249 }
250
251 static int
252 kgdb_trgt_ignore_breakpoints(CORE_ADDR addr, char *contents)
253 {
254
255         return 0;
256 }
257
258 static void
259 kgdb_switch_to_thread(int tid)
260 {
261         char buf[16];
262         int thread_id;
263
264         thread_id = pid_to_thread_id(pid_to_ptid(tid));
265         if (thread_id == 0)
266                 error ("invalid tid");
267         snprintf(buf, sizeof(buf), "%d", thread_id);
268         gdb_thread_select(uiout, buf);
269 }
270
271 static void
272 kgdb_set_proc_cmd (char *arg, int from_tty)
273 {
274         CORE_ADDR addr;
275         struct kthr *thr;
276
277         if (!arg)
278                 error_no_arg ("proc address for the new context");
279
280         if (kvm == NULL)
281                 error ("only supported for core file target");
282
283         addr = (CORE_ADDR) parse_and_eval_address (arg);
284
285         if (!INKERNEL (addr)) {
286                 thr = kgdb_thr_lookup_pid((int)addr);
287                 if (thr == NULL)
288                         error ("invalid pid");
289         } else {
290                 thr = kgdb_thr_lookup_paddr(addr);
291                 if (thr == NULL)
292                         error("invalid proc address");
293         }
294         kgdb_switch_to_thread(thr->tid);
295 }
296
297 static void
298 kgdb_set_tid_cmd (char *arg, int from_tty)
299 {
300         CORE_ADDR addr;
301         struct kthr *thr;
302
303         if (!arg)
304                 error_no_arg ("TID or thread address for the new context");
305
306         addr = (CORE_ADDR) parse_and_eval_address (arg);
307
308         if (kvm != NULL && INKERNEL (addr)) {
309                 thr = kgdb_thr_lookup_taddr(addr);
310                 if (thr == NULL)
311                         error("invalid thread address");
312                 addr = thr->tid;
313         }
314         kgdb_switch_to_thread(addr);
315 }
316
317 int fbsdcoreops_suppress_target = 1;
318
319 void
320 initialize_kgdb_target(void)
321 {
322
323         kgdb_trgt_ops.to_magic = OPS_MAGIC;
324         kgdb_trgt_ops.to_shortname = "kernel";
325         kgdb_trgt_ops.to_longname = "kernel core dump file";
326         kgdb_trgt_ops.to_doc = 
327     "Use a vmcore file as a target.  Specify the filename of the vmcore file.";
328         kgdb_trgt_ops.to_stratum = core_stratum;
329         kgdb_trgt_ops.to_has_memory = 1;
330         kgdb_trgt_ops.to_has_registers = 1;
331         kgdb_trgt_ops.to_has_stack = 1;
332
333         kgdb_trgt_ops.to_open = kgdb_trgt_open;
334         kgdb_trgt_ops.to_close = kgdb_trgt_close;
335         kgdb_trgt_ops.to_attach = find_default_attach;
336         kgdb_trgt_ops.to_detach = kgdb_trgt_detach;
337         kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info;
338         kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers;
339         kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info;
340         kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads;
341         kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str;
342         kgdb_trgt_ops.to_store_registers = kgdb_trgt_store_registers;
343         kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
344         kgdb_trgt_ops.to_xfer_memory = kgdb_trgt_xfer_memory;
345         kgdb_trgt_ops.to_insert_breakpoint = kgdb_trgt_ignore_breakpoints;
346         kgdb_trgt_ops.to_remove_breakpoint = kgdb_trgt_ignore_breakpoints;
347
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 }