]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - gnu/usr.bin/gdb/kgdb/trgt.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 CORE_ADDR stoppcbs;
57
58 static void     kgdb_core_cleanup(void *);
59
60 static char *vmcore;
61 static struct target_ops kgdb_trgt_ops;
62
63 kvm_t *kvm;
64 static char kvm_err[_POSIX2_LINE_MAX];
65
66 #define KERNOFF         (kgdb_kernbase ())
67 #define INKERNEL(x)     ((x) >= KERNOFF)
68
69 static CORE_ADDR
70 kgdb_kernbase (void)
71 {
72         static CORE_ADDR kernbase;
73         struct minimal_symbol *sym;
74
75         if (kernbase == 0) {
76                 sym = lookup_minimal_symbol ("kernbase", NULL, NULL);
77                 if (sym == NULL) {
78                         kernbase = KERNBASE;
79                 } else {
80                         kernbase = SYMBOL_VALUE_ADDRESS (sym);
81                 }
82         }
83         return kernbase;
84 }
85
86 static void
87 kgdb_trgt_open(char *filename, int from_tty)
88 {
89         struct cleanup *old_chain;
90         struct thread_info *ti;
91         struct kthr *kt;
92         kvm_t *nkvm;
93         char *temp;
94         int ontop;
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         ontop = !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                 ti = add_thread(pid_to_ptid(kt->tid));
133                 kt = kgdb_thr_next(kt);
134         }
135         if (curkthr != 0)
136                 inferior_ptid = pid_to_ptid(curkthr->tid);
137
138         if (ontop) {
139                 /* XXX: fetch registers? */
140                 kld_init();
141                 flush_cached_frames();
142                 select_frame (get_current_frame());
143                 print_stack_frame(get_selected_frame(),
144                     frame_relative_level(get_selected_frame()), 1);
145         } else
146                 warning(
147         "you won't be able to access this vmcore until you terminate\n\
148 your %s; do ``info files''", target_longname);
149 }
150
151 static void
152 kgdb_trgt_close(int quitting)
153 {
154
155         if (kvm != NULL) {              
156                 inferior_ptid = null_ptid;
157                 CLEAR_SOLIB();
158                 if (kvm_close(kvm) != 0)
159                         warning("cannot close \"%s\": %s", vmcore,
160                             kvm_geterr(kvm));
161                 kvm = NULL;
162                 xfree(vmcore);
163                 vmcore = NULL;
164                 if (kgdb_trgt_ops.to_sections) {
165                         xfree(kgdb_trgt_ops.to_sections);
166                         kgdb_trgt_ops.to_sections = NULL;
167                         kgdb_trgt_ops.to_sections_end = NULL;
168                 }
169         }
170 }
171
172 static void
173 kgdb_core_cleanup(void *arg)
174 {
175
176         kgdb_trgt_close(0);
177 }
178
179 static void
180 kgdb_trgt_detach(char *args, int from_tty)
181 {
182
183         if (args)
184                 error ("Too many arguments");
185         unpush_target(&kgdb_trgt_ops);
186         reinit_frame_cache();
187         if (from_tty)
188                 printf_filtered("No vmcore file now.\n");
189 }
190
191 static char *
192 kgdb_trgt_extra_thread_info(struct thread_info *ti)
193 {
194
195         return (kgdb_thr_extra_thread_info(ptid_get_pid(ti->ptid)));
196 }
197
198 static void
199 kgdb_trgt_files_info(struct target_ops *target)
200 {
201
202         printf_filtered ("\t`%s', ", vmcore);
203         wrap_here ("        ");
204         printf_filtered ("file type %s.\n", "FreeBSD kernel vmcore");
205 }
206
207 static void
208 kgdb_trgt_find_new_threads(void)
209 {
210         struct target_ops *tb;
211
212         if (kvm != NULL)
213                 return;
214
215         tb = find_target_beneath(&kgdb_trgt_ops);
216         if (tb->to_find_new_threads != NULL)
217                 tb->to_find_new_threads();
218 }
219
220 static char *
221 kgdb_trgt_pid_to_str(ptid_t ptid)
222 {
223         static char buf[33];
224
225         snprintf(buf, sizeof(buf), "Thread %d", ptid_get_pid(ptid));
226         return (buf);
227 }
228
229 static int
230 kgdb_trgt_thread_alive(ptid_t ptid)
231 {
232         return (kgdb_thr_lookup_tid(ptid_get_pid(ptid)) != NULL);
233 }
234
235 static int
236 kgdb_trgt_xfer_memory(CORE_ADDR memaddr, char *myaddr, int len, int write,
237     struct mem_attrib *attrib, struct target_ops *target)
238 {
239         struct target_ops *tb;
240
241         if (kvm != NULL) {
242                 if (len == 0)
243                         return (0);
244                 if (!write)
245                         return (kvm_read(kvm, memaddr, myaddr, len));
246                 else
247                         return (kvm_write(kvm, memaddr, myaddr, len));
248         }
249         tb = find_target_beneath(target);
250         return (tb->to_xfer_memory(memaddr, myaddr, len, write, attrib, tb));
251 }
252
253 static int
254 kgdb_trgt_ignore_breakpoints(CORE_ADDR addr, char *contents)
255 {
256
257         return 0;
258 }
259
260 static void
261 kgdb_switch_to_thread(int tid)
262 {
263         char buf[16];
264         int thread_id;
265
266         thread_id = pid_to_thread_id(pid_to_ptid(tid));
267         if (thread_id == 0)
268                 error ("invalid tid");
269         snprintf(buf, sizeof(buf), "%d", thread_id);
270         gdb_thread_select(uiout, buf);
271 }
272
273 static void
274 kgdb_set_proc_cmd (char *arg, int from_tty)
275 {
276         CORE_ADDR addr;
277         struct kthr *thr;
278
279         if (!arg)
280                 error_no_arg ("proc address for the new context");
281
282         if (kvm == NULL)
283                 error ("only supported for core file target");
284
285         addr = (CORE_ADDR) parse_and_eval_address (arg);
286
287         if (!INKERNEL (addr)) {
288                 thr = kgdb_thr_lookup_pid((int)addr);
289                 if (thr == NULL)
290                         error ("invalid pid");
291         } else {
292                 thr = kgdb_thr_lookup_paddr(addr);
293                 if (thr == NULL)
294                         error("invalid proc address");
295         }
296         kgdb_switch_to_thread(thr->tid);
297 }
298
299 static void
300 kgdb_set_tid_cmd (char *arg, int from_tty)
301 {
302         CORE_ADDR addr;
303         struct kthr *thr;
304
305         if (!arg)
306                 error_no_arg ("TID or thread address for the new context");
307
308         addr = (CORE_ADDR) parse_and_eval_address (arg);
309
310         if (kvm != NULL && INKERNEL (addr)) {
311                 thr = kgdb_thr_lookup_taddr(addr);
312                 if (thr == NULL)
313                         error("invalid thread address");
314                 addr = thr->tid;
315         }
316         kgdb_switch_to_thread(addr);
317 }
318
319 int fbsdcoreops_suppress_target = 1;
320
321 void
322 initialize_kgdb_target(void)
323 {
324
325         kgdb_trgt_ops.to_magic = OPS_MAGIC;
326         kgdb_trgt_ops.to_shortname = "kernel";
327         kgdb_trgt_ops.to_longname = "kernel core dump file";
328         kgdb_trgt_ops.to_doc = 
329     "Use a vmcore file as a target.  Specify the filename of the vmcore file.";
330         kgdb_trgt_ops.to_stratum = core_stratum;
331         kgdb_trgt_ops.to_has_memory = 1;
332         kgdb_trgt_ops.to_has_registers = 1;
333         kgdb_trgt_ops.to_has_stack = 1;
334
335         kgdb_trgt_ops.to_open = kgdb_trgt_open;
336         kgdb_trgt_ops.to_close = kgdb_trgt_close;
337         kgdb_trgt_ops.to_attach = find_default_attach;
338         kgdb_trgt_ops.to_detach = kgdb_trgt_detach;
339         kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info;
340         kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers;
341         kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info;
342         kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads;
343         kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str;
344         kgdb_trgt_ops.to_store_registers = kgdb_trgt_store_registers;
345         kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
346         kgdb_trgt_ops.to_xfer_memory = kgdb_trgt_xfer_memory;
347         kgdb_trgt_ops.to_insert_breakpoint = kgdb_trgt_ignore_breakpoints;
348         kgdb_trgt_ops.to_remove_breakpoint = kgdb_trgt_ignore_breakpoints;
349
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 }
357
358 CORE_ADDR
359 kgdb_trgt_stop_pcb(u_int cpuid, u_int pcbsz)
360 {
361         static int once = 0;
362
363         if (stoppcbs == 0 && !once) {
364                 once = 1;
365                 stoppcbs = kgdb_lookup("stoppcbs");
366         }
367         if (stoppcbs == 0)
368                 return 0;
369
370         return (stoppcbs + pcbsz * cpuid);
371 }