]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - gnu/usr.bin/gdb/kgdb/trgt.c
Use single instance of the identical INKERNEL() and PMC_IN_KERNEL()
[FreeBSD/FreeBSD.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 #ifdef CROSS_DEBUGGER
57 /*
58  * We suppress the call to add_target() of core_ops in corelow.c because if
59  * there are multiple core_stratum targets, the find_core_target() function
60  * won't know which one to return and returns none. We need it to return
61  * our target. We only have to do that when we're building a cross-debugger
62  * because fbsd-threads.c is part of a native debugger and it too defines
63  * coreops_suppress_target with 1 as the initializer.
64  */
65 int coreops_suppress_target = 1;
66 #endif
67
68 static CORE_ADDR stoppcbs;
69
70 static void     kgdb_core_cleanup(void *);
71
72 static char *vmcore;
73 static struct target_ops kgdb_trgt_ops;
74
75 kvm_t *kvm;
76 static char kvm_err[_POSIX2_LINE_MAX];
77
78 #define KERNOFF         (kgdb_kernbase ())
79 #define PINKERNEL(x)    ((x) >= KERNOFF)
80
81 static CORE_ADDR
82 kgdb_kernbase (void)
83 {
84         static CORE_ADDR kernbase;
85         struct minimal_symbol *sym;
86
87         if (kernbase == 0) {
88                 sym = lookup_minimal_symbol ("kernbase", NULL, NULL);
89                 if (sym == NULL) {
90                         kernbase = KERNBASE;
91                 } else {
92                         kernbase = SYMBOL_VALUE_ADDRESS (sym);
93                 }
94         }
95         return kernbase;
96 }
97
98 static void
99 kgdb_trgt_open(char *filename, int from_tty)
100 {
101         struct cleanup *old_chain;
102         struct thread_info *ti;
103         struct kthr *kt;
104         kvm_t *nkvm;
105         char *temp;
106         int ontop;
107
108         target_preopen (from_tty);
109         if (!filename)
110                 error ("No vmcore file specified.");
111         if (!exec_bfd)
112                 error ("Can't open a vmcore without a kernel");
113
114         filename = tilde_expand (filename);
115         if (filename[0] != '/') {
116                 temp = concat (current_directory, "/", filename, NULL);
117                 xfree(filename);
118                 filename = temp;
119         }
120
121         old_chain = make_cleanup (xfree, filename);
122
123         nkvm = kvm_openfiles(bfd_get_filename(exec_bfd), filename, NULL,
124             write_files ? O_RDWR : O_RDONLY, kvm_err);
125         if (nkvm == NULL)
126                 error ("Failed to open vmcore: %s", kvm_err);
127
128         /* Don't free the filename now and close any previous vmcore. */
129         discard_cleanups(old_chain);
130         unpush_target(&kgdb_trgt_ops);
131
132         kvm = nkvm;
133         vmcore = filename;
134         old_chain = make_cleanup(kgdb_core_cleanup, NULL);
135
136         ontop = !push_target (&kgdb_trgt_ops);
137         discard_cleanups (old_chain);
138
139         kgdb_dmesg();
140
141         init_thread_list();
142         kt = kgdb_thr_init();
143         while (kt != NULL) {
144                 ti = add_thread(pid_to_ptid(kt->tid));
145                 kt = kgdb_thr_next(kt);
146         }
147         if (curkthr != 0)
148                 inferior_ptid = pid_to_ptid(curkthr->tid);
149
150         if (ontop) {
151                 /* XXX: fetch registers? */
152                 kld_init();
153                 flush_cached_frames();
154                 select_frame (get_current_frame());
155                 print_stack_frame(get_selected_frame(),
156                     frame_relative_level(get_selected_frame()), 1);
157         } else
158                 warning(
159         "you won't be able to access this vmcore until you terminate\n\
160 your %s; do ``info files''", target_longname);
161 }
162
163 static void
164 kgdb_trgt_close(int quitting)
165 {
166
167         if (kvm != NULL) {              
168                 inferior_ptid = null_ptid;
169                 CLEAR_SOLIB();
170                 if (kvm_close(kvm) != 0)
171                         warning("cannot close \"%s\": %s", vmcore,
172                             kvm_geterr(kvm));
173                 kvm = NULL;
174                 xfree(vmcore);
175                 vmcore = NULL;
176                 if (kgdb_trgt_ops.to_sections) {
177                         xfree(kgdb_trgt_ops.to_sections);
178                         kgdb_trgt_ops.to_sections = NULL;
179                         kgdb_trgt_ops.to_sections_end = NULL;
180                 }
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(char *args, int from_tty)
193 {
194
195         if (args)
196                 error ("Too many arguments");
197         unpush_target(&kgdb_trgt_ops);
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_pid(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", "FreeBSD kernel vmcore");
217 }
218
219 static void
220 kgdb_trgt_find_new_threads(void)
221 {
222         struct target_ops *tb;
223
224         if (kvm != NULL)
225                 return;
226
227         tb = find_target_beneath(&kgdb_trgt_ops);
228         if (tb->to_find_new_threads != NULL)
229                 tb->to_find_new_threads();
230 }
231
232 static char *
233 kgdb_trgt_pid_to_str(ptid_t ptid)
234 {
235         static char buf[33];
236
237         snprintf(buf, sizeof(buf), "Thread %d", ptid_get_pid(ptid));
238         return (buf);
239 }
240
241 static int
242 kgdb_trgt_thread_alive(ptid_t ptid)
243 {
244         return (kgdb_thr_lookup_tid(ptid_get_pid(ptid)) != NULL);
245 }
246
247 static int
248 kgdb_trgt_xfer_memory(CORE_ADDR memaddr, char *myaddr, int len, int write,
249     struct mem_attrib *attrib, struct target_ops *target)
250 {
251         struct target_ops *tb;
252
253         if (kvm != NULL) {
254                 if (len == 0)
255                         return (0);
256                 if (!write)
257                         return (kvm_read(kvm, memaddr, myaddr, len));
258                 else
259                         return (kvm_write(kvm, memaddr, myaddr, len));
260         }
261         tb = find_target_beneath(target);
262         return (tb->to_xfer_memory(memaddr, myaddr, len, write, attrib, tb));
263 }
264
265 static int
266 kgdb_trgt_ignore_breakpoints(CORE_ADDR addr, char *contents)
267 {
268
269         return 0;
270 }
271
272 static void
273 kgdb_switch_to_thread(int tid)
274 {
275         char buf[16];
276         int thread_id;
277
278         thread_id = pid_to_thread_id(pid_to_ptid(tid));
279         if (thread_id == 0)
280                 error ("invalid tid");
281         snprintf(buf, sizeof(buf), "%d", thread_id);
282         gdb_thread_select(uiout, buf);
283 }
284
285 static void
286 kgdb_set_proc_cmd (char *arg, int from_tty)
287 {
288         CORE_ADDR addr;
289         struct kthr *thr;
290
291         if (!arg)
292                 error_no_arg ("proc address for the new context");
293
294         if (kvm == NULL)
295                 error ("only supported for core file target");
296
297         addr = (CORE_ADDR) parse_and_eval_address (arg);
298
299         if (!PINKERNEL (addr)) {
300                 thr = kgdb_thr_lookup_pid((int)addr);
301                 if (thr == NULL)
302                         error ("invalid pid");
303         } else {
304                 thr = kgdb_thr_lookup_paddr(addr);
305                 if (thr == NULL)
306                         error("invalid proc address");
307         }
308         kgdb_switch_to_thread(thr->tid);
309 }
310
311 static void
312 kgdb_set_tid_cmd (char *arg, int from_tty)
313 {
314         CORE_ADDR addr;
315         struct kthr *thr;
316
317         if (!arg)
318                 error_no_arg ("TID or thread address for the new context");
319
320         addr = (CORE_ADDR) parse_and_eval_address (arg);
321
322         if (kvm != NULL && PINKERNEL (addr)) {
323                 thr = kgdb_thr_lookup_taddr(addr);
324                 if (thr == NULL)
325                         error("invalid thread address");
326                 addr = thr->tid;
327         }
328         kgdb_switch_to_thread(addr);
329 }
330
331 int fbsdcoreops_suppress_target = 1;
332
333 void
334 initialize_kgdb_target(void)
335 {
336
337         kgdb_trgt_ops.to_magic = OPS_MAGIC;
338         kgdb_trgt_ops.to_shortname = "kernel";
339         kgdb_trgt_ops.to_longname = "kernel core dump file";
340         kgdb_trgt_ops.to_doc = 
341     "Use a vmcore file as a target.  Specify the filename of the vmcore file.";
342         kgdb_trgt_ops.to_stratum = core_stratum;
343         kgdb_trgt_ops.to_has_memory = 1;
344         kgdb_trgt_ops.to_has_registers = 1;
345         kgdb_trgt_ops.to_has_stack = 1;
346
347         kgdb_trgt_ops.to_open = kgdb_trgt_open;
348         kgdb_trgt_ops.to_close = kgdb_trgt_close;
349         kgdb_trgt_ops.to_attach = find_default_attach;
350         kgdb_trgt_ops.to_detach = kgdb_trgt_detach;
351         kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info;
352         kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers;
353         kgdb_trgt_ops.to_files_info = kgdb_trgt_files_info;
354         kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads;
355         kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str;
356         kgdb_trgt_ops.to_store_registers = kgdb_trgt_store_registers;
357         kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
358         kgdb_trgt_ops.to_xfer_memory = kgdb_trgt_xfer_memory;
359         kgdb_trgt_ops.to_insert_breakpoint = kgdb_trgt_ignore_breakpoints;
360         kgdb_trgt_ops.to_remove_breakpoint = kgdb_trgt_ignore_breakpoints;
361
362         add_target(&kgdb_trgt_ops);
363
364         add_com ("proc", class_obscure, kgdb_set_proc_cmd,
365            "Set current process context");
366         add_com ("tid", class_obscure, kgdb_set_tid_cmd,
367            "Set current thread context");
368 }
369
370 CORE_ADDR
371 kgdb_trgt_stop_pcb(u_int cpuid, u_int pcbsz)
372 {
373         static int once = 0;
374
375         if (stoppcbs == 0 && !once) {
376                 once = 1;
377                 stoppcbs = kgdb_lookup("stoppcbs");
378         }
379         if (stoppcbs == 0)
380                 return 0;
381
382         return (stoppcbs + pcbsz * cpuid);
383 }