Debian Codegraph @

common/hv_vm_debug.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 *
5 * Hypervisor routines for VM debugging.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <signal.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include <assert.h>
18 #include <stdarg.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <pthread.h>
24
25 #include "cpu.h"
26 #include "vm.h"
27 #include "dynamips.h"
28 #include "device.h"
29 #include "dev_c7200.h"
30 #include "dev_vtty.h"
31 #include "utils.h"
32 #include "registry.h"
33 #include "hypervisor.h"
34
35 /* Show CPU registers */
36 static int cmd_show_cpu_regs(hypervisor_conn_t *conn,int argc,char *argv[])
37 {
38 vm_instance_t *vm;
39 cpu_gen_t *cpu;
40
41 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
42 return(-1);
43
44 if ((cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1]))) != NULL)
45 cpu->reg_dump(cpu);
46
47 vm_release(vm);
48 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
49 return(0);
50 }
51
52 /* Show CPU MMU info */
53 static int cmd_show_cpu_mmu(hypervisor_conn_t *conn,int argc,char *argv[])
54 {
55 vm_instance_t *vm;
56 cpu_gen_t *cpu;
57
58 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
59 return(-1);
60
61 if ((cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1]))) != NULL)
62 cpu->mmu_dump(cpu);
63
64 vm_release(vm);
65 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
66 return(0);
67 }
68
69 /* Set a CPU register */
70 static int cmd_set_cpu_reg(hypervisor_conn_t *conn,int argc,char *argv[])
71 {
72 vm_instance_t *vm;
73 cpu_gen_t *cpu;
74 int reg_index;
75
76 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
77 return(-1);
78
79 cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1]));
80 reg_index = atoi(argv[2]);
81
82 if (!cpu || (reg_index < 1)) {
83 vm_release(vm);
84 hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"Bad CPU or register");
85 return(-1);
86 }
87
88 /* Set register value */
89 cpu->reg_set(cpu,reg_index,strtoull(argv[3],NULL,0));
90
91 vm_release(vm);
92 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
93 return(0);
94 }
95
96 /* Add a breakpoint */
97 static int cmd_add_cpu_breakpoint(hypervisor_conn_t *conn,
98 int argc,char *argv[])
99 {
100 vm_instance_t *vm;
101 cpu_gen_t *cpu;
102 m_uint64_t addr;
103
104 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
105 return(-1);
106
107 if (!(cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1])))) {
108 vm_release(vm);
109 hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"Bad CPU");
110 return(-1);
111 }
112
113 addr = strtoull(argv[2],NULL,0);
114 cpu->add_breakpoint(cpu,addr);
115
116 vm_release(vm);
117 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
118 return(0);
119 }
120
121 /* Remove a breakpoint */
122 static int cmd_remove_cpu_breakpoint(hypervisor_conn_t *conn,
123 int argc,char *argv[])
124 {
125 vm_instance_t *vm;
126 cpu_gen_t *cpu;
127 m_uint64_t addr;
128
129 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
130 return(-1);
131
132 if (!(cpu = cpu_group_find_id(vm->cpu_group,atoi(argv[1])))) {
133 vm_release(vm);
134 hypervisor_send_reply(conn,HSC_ERR_BAD_OBJ,1,"Bad CPU");
135 return(-1);
136 }
137
138 addr = strtoull(argv[2],NULL,0);
139 cpu->remove_breakpoint(cpu,addr);
140
141 vm_release(vm);
142 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
143 return(0);
144 }
145
146 /* Write a 32-bit memory word in physical memory */
147 static int cmd_pmem_w32(hypervisor_conn_t *conn,int argc,char *argv[])
148 {
149 vm_instance_t *vm;
150 m_uint64_t addr;
151 m_uint32_t value;
152
153 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
154 return(-1);
155
156 /* Write word */
157 addr = strtoull(argv[2],NULL,0);
158 value = strtoul(argv[3],NULL,0);
159 physmem_copy_u32_to_vm(vm,addr,value);
160
161 vm_release(vm);
162 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
163 return(0);
164 }
165
166 /* Read a 32-bit memory word */
167 static int cmd_pmem_r32(hypervisor_conn_t *conn,int argc,char *argv[])
168 {
169 vm_instance_t *vm;
170 m_uint64_t addr;
171 m_uint32_t value;
172
173 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
174 return(-1);
175
176 /* Write word */
177 addr = strtoull(argv[2],NULL,0);
178 value = physmem_copy_u32_from_vm(vm,addr);
179
180 vm_release(vm);
181 hypervisor_send_reply(conn,HSC_INFO_OK,1,"0x%8.8x",value);
182 return(0);
183 }
184
185 /* Write a 16-bit memory word */
186 static int cmd_pmem_w16(hypervisor_conn_t *conn,int argc,char *argv[])
187 {
188 vm_instance_t *vm;
189 m_uint64_t addr;
190 m_uint16_t value;
191
192 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
193 return(-1);
194
195 /* Write word */
196 addr = strtoull(argv[2],NULL,0);
197 value = strtoul(argv[3],NULL,0);
198 physmem_copy_u16_to_vm(vm,addr,value);
199
200 vm_release(vm);
201 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
202 return(0);
203 }
204
205 /* Read a 16-bit memory word */
206 static int cmd_pmem_r16(hypervisor_conn_t *conn,int argc,char *argv[])
207 {
208 vm_instance_t *vm;
209 m_uint64_t addr;
210 m_uint16_t value;
211
212 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
213 return(-1);
214
215 /* Write word */
216 addr = strtoull(argv[2],NULL,0);
217 value = physmem_copy_u16_from_vm(vm,addr);
218
219 vm_release(vm);
220 hypervisor_send_reply(conn,HSC_INFO_OK,1,"0x%4.4x",value);
221 return(0);
222 }
223
224 /* Finds a sequence of bytes in cacheable physical memory */
225 static int cmd_pmem_cfind(hypervisor_conn_t *conn,int argc,char *argv[])
226 {
227 vm_instance_t *vm;
228 m_uint64_t start, end, addr;
229 m_uint8_t *bytes;
230 size_t len;
231
232 if (!(vm = hypervisor_find_object(conn,argv[0],OBJ_TYPE_VM)))
233 return(-1);
234
235 len = strlen(argv[2]);
236 if (len == 0 || len % 2 == 1) {
237 vm_release(vm);
238 hypervisor_send_reply(conn,HSC_ERR_INV_PARAM,1,"Invalid byte sequence (invalid length=%u)", (unsigned int)len);
239 return(-1);
240 }
241
242 len = len / 2;
243 bytes = malloc(sizeof(m_uint8_t)*len);
244 if (len != hex_decode(bytes, (unsigned char*)argv[2], len)) {
245 free(bytes);
246 vm_release(vm);
247 hypervisor_send_reply(conn,HSC_ERR_INV_PARAM,1,"Invalid byte sequence (non-hexadecimal character)");
248 return(-1);
249 }
250
251 start = (argc > 3)? strtoull(argv[3],NULL,0): 0;
252 end = (argc > 4)? strtoull(argv[4],NULL,0): 0xFFFFFFFFFFFFFFFFULL;
253 if (physmem_cfind(vm, bytes, len, start, end, &addr)) {
254 free(bytes);
255 vm_release(vm);
256 hypervisor_send_reply(conn,HSC_ERR_NOT_FOUND,1,"Not found");
257 return(-1);
258 }
259
260 free(bytes);
261 vm_release(vm);
262 hypervisor_send_reply(conn,HSC_INFO_OK,1,"0x%4.4llx",addr);
263 return(0);
264 }
265
266 /* VM debug commands */
267 static hypervisor_cmd_t vm_cmd_array[] = {
268 { "show_cpu_regs", 2, 2, cmd_show_cpu_regs, NULL },
269 { "show_cpu_mmu", 2, 2, cmd_show_cpu_mmu, NULL },
270 { "set_cpu_reg", 4, 4, cmd_set_cpu_reg, NULL },
271 { "add_cpu_breakpoint", 3, 3, cmd_add_cpu_breakpoint, NULL },
272 { "remove_cpu_breakpoint", 3, 3, cmd_remove_cpu_breakpoint, NULL },
273 { "pmem_w32", 4, 4, cmd_pmem_w32, NULL },
274 { "pmem_r32", 3, 3, cmd_pmem_r32, NULL },
275 { "pmem_w16", 4, 4, cmd_pmem_w16, NULL },
276 { "pmem_r16", 3, 3, cmd_pmem_r16, NULL },
277 { "pmem_cfind", 3, 5, cmd_pmem_cfind, NULL },
278 { NULL, -1, -1, NULL, NULL },
279 };
280
281 /* Hypervisor VM debugging initialization */
282 int hypervisor_vm_debug_init(void)
283 {
284 hypervisor_module_t *module;
285
286 module = hypervisor_register_module("vm_debug",NULL);
287 assert(module != NULL);
288
289 hypervisor_register_cmd_array(module,vm_cmd_array);
290 return(0);
291 }