]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - gnu/usr.bin/gdb/kgdb/trgt.c
This commit was generated by cvs2svn to compensate for changes in r146515,
[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 <kvm.h>
35
36 #include <defs.h>
37 #include <command.h>
38 #include <gdbthread.h>
39 #include <inferior.h>
40 #include <regcache.h>
41 #include <target.h>
42
43 #include "kgdb.h"
44
45 static struct target_ops kgdb_trgt_ops;
46
47 #define KERNOFF         (kgdb_kernbase ())
48 #define INKERNEL(x)     ((x) >= KERNOFF)
49
50 static CORE_ADDR
51 kgdb_kernbase (void)
52 {
53         static CORE_ADDR kernbase;
54         struct minimal_symbol *sym;
55  
56         if (kernbase == 0)
57         {
58                 sym = lookup_minimal_symbol ("kernbase", NULL, NULL);
59                 if (sym == NULL) {
60                         kernbase = KERNBASE;
61                 } else {
62                         kernbase = SYMBOL_VALUE_ADDRESS (sym);
63                 }
64         }
65         return kernbase;
66 }
67
68 static char *
69 kgdb_trgt_extra_thread_info(struct thread_info *ti)
70 {
71         return (kgdb_thr_extra_thread_info(ptid_get_tid(ti->ptid)));
72 }
73
74 static void
75 kgdb_trgt_find_new_threads(void)
76 {
77 }
78
79 static char *
80 kgdb_trgt_pid_to_str(ptid_t ptid)
81 {
82         static char buf[33];
83
84         snprintf(buf, sizeof(buf), "PID %5d TID %5ld", ptid_get_pid(ptid),
85                 ptid_get_tid(ptid));
86         return (buf);
87 }
88
89 static int
90 kgdb_trgt_thread_alive(ptid_t ptid)
91 {
92         return (kgdb_thr_lookup_tid(ptid_get_tid(ptid)) != NULL);
93 }
94
95 static int
96 kgdb_trgt_xfer_memory(CORE_ADDR memaddr, char *myaddr, int len, int write,
97     struct mem_attrib *attrib __unused, struct target_ops *target __unused)
98 {
99         if (len == 0)
100                 return (0);
101
102         if (!write)
103                 return (kvm_read(kvm, memaddr, myaddr, len));
104         else
105                 return (kvm_write(kvm, memaddr, myaddr, len));
106 }
107
108 static void
109 kgdb_switch_to_thread(struct kthr *thr)
110 {
111         if (thr->tid == ptid_get_tid(inferior_ptid))
112                 return;
113
114         inferior_ptid = ptid_build(thr->pid, 0, thr->tid);
115         flush_cached_frames ();
116         registers_changed ();
117         stop_pc = read_pc ();
118         select_frame (get_current_frame ());
119 }
120
121 static void
122 kgdb_set_proc_cmd (char *arg, int from_tty)
123 {
124         CORE_ADDR addr;
125         struct kthr *thr;
126
127         if (!arg)
128                 error_no_arg ("proc address for the new context");
129
130         if (kvm == NULL)
131                 error ("no kernel core file");
132
133         addr = (CORE_ADDR) parse_and_eval_address (arg);
134
135         if (!INKERNEL (addr)) {
136                 thr = kgdb_thr_lookup_pid((int)addr);
137                 if (thr == NULL)
138                         error ("invalid pid");
139         } else {
140                 thr = kgdb_thr_lookup_paddr(addr);
141                 if (thr == NULL)
142                         error("invalid proc address");
143         }
144         kgdb_switch_to_thread(thr);
145 }
146
147 static void
148 kgdb_set_tid_cmd (char *arg, int from_tty)
149 {
150         CORE_ADDR addr;
151         struct kthr *thr;
152
153         if (!arg)
154                 error_no_arg ("TID or thread address for the new context");
155
156         if (kvm == NULL)
157                 error ("no kernel core file");
158
159         addr = (CORE_ADDR) parse_and_eval_address (arg);
160
161         if (!INKERNEL (addr)) {
162                 thr = kgdb_thr_lookup_tid((int)addr);
163                 if (thr == NULL)
164                         error ("invalid TID");
165         } else {
166                 thr = kgdb_thr_lookup_taddr(addr);
167                 if (thr == NULL)
168                         error("invalid thread address");
169         }
170         kgdb_switch_to_thread(thr);
171 }
172
173 void
174 kgdb_target(void)
175 {
176         struct kthr *kt;
177         struct thread_info *ti;
178
179         kgdb_trgt_ops.to_magic = OPS_MAGIC;
180         kgdb_trgt_ops.to_shortname = "kernel";
181         kgdb_trgt_ops.to_longname = "kernel core files.";
182         kgdb_trgt_ops.to_doc = "Kernel core files.";
183         kgdb_trgt_ops.to_stratum = thread_stratum;
184         kgdb_trgt_ops.to_has_memory = 1;
185         kgdb_trgt_ops.to_has_registers = 1;
186         kgdb_trgt_ops.to_has_stack = 1;
187
188         kgdb_trgt_ops.to_extra_thread_info = kgdb_trgt_extra_thread_info;
189         kgdb_trgt_ops.to_fetch_registers = kgdb_trgt_fetch_registers;
190         kgdb_trgt_ops.to_find_new_threads = kgdb_trgt_find_new_threads;
191         kgdb_trgt_ops.to_pid_to_str = kgdb_trgt_pid_to_str;
192         kgdb_trgt_ops.to_store_registers = kgdb_trgt_store_registers;
193         kgdb_trgt_ops.to_thread_alive = kgdb_trgt_thread_alive;
194         kgdb_trgt_ops.to_xfer_memory = kgdb_trgt_xfer_memory;
195         add_target(&kgdb_trgt_ops);
196         push_target(&kgdb_trgt_ops);
197
198         kt = kgdb_thr_first();
199         while (kt != NULL) {
200                 ti = add_thread(ptid_build(kt->pid, 0, kt->tid));
201                 kt = kgdb_thr_next(kt);
202         }
203         inferior_ptid = ptid_build(curkthr->pid, 0, curkthr->tid);
204         add_com ("proc", class_obscure, kgdb_set_proc_cmd,
205            "Set current process context");
206         add_com ("tid", class_obscure, kgdb_set_tid_cmd,
207            "Set current process context");
208 }