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