]>
Commit | Line | Data |
---|---|---|
53197fc4 JW |
1 | /* |
2 | * Kernel Debug Core | |
3 | * | |
4 | * Maintainer: Jason Wessel <jason.wessel@windriver.com> | |
5 | * | |
6 | * Copyright (C) 2000-2001 VERITAS Software Corporation. | |
7 | * Copyright (C) 2002-2004 Timesys Corporation | |
8 | * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com> | |
9 | * Copyright (C) 2004 Pavel Machek <pavel@suse.cz> | |
10 | * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org> | |
11 | * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd. | |
12 | * Copyright (C) 2005-2009 Wind River Systems, Inc. | |
13 | * Copyright (C) 2007 MontaVista Software, Inc. | |
14 | * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | |
15 | * | |
16 | * Contributors at various stages not listed above: | |
17 | * Jason Wessel ( jason.wessel@windriver.com ) | |
18 | * George Anzinger <george@mvista.com> | |
19 | * Anurekh Saxena (anurekh.saxena@timesys.com) | |
20 | * Lake Stevens Instrument Division (Glenn Engel) | |
21 | * Jim Kingdon, Cygnus Support. | |
22 | * | |
23 | * Original KGDB stub: David Grothe <dave@gcom.com>, | |
24 | * Tigran Aivazian <tigran@sco.com> | |
25 | * | |
26 | * This file is licensed under the terms of the GNU General Public License | |
27 | * version 2. This program is licensed "as is" without any warranty of any | |
28 | * kind, whether express or implied. | |
29 | */ | |
30 | ||
31 | #include <linux/kernel.h> | |
32 | #include <linux/kgdb.h> | |
f5316b4a | 33 | #include <linux/kdb.h> |
53197fc4 JW |
34 | #include <linux/reboot.h> |
35 | #include <linux/uaccess.h> | |
36 | #include <asm/cacheflush.h> | |
37 | #include <asm/unaligned.h> | |
38 | #include "debug_core.h" | |
39 | ||
40 | #define KGDB_MAX_THREAD_QUERY 17 | |
41 | ||
42 | /* Our I/O buffers. */ | |
43 | static char remcom_in_buffer[BUFMAX]; | |
44 | static char remcom_out_buffer[BUFMAX]; | |
45 | ||
46 | /* Storage for the registers, in GDB format. */ | |
47 | static unsigned long gdb_regs[(NUMREGBYTES + | |
48 | sizeof(unsigned long) - 1) / | |
49 | sizeof(unsigned long)]; | |
50 | ||
51 | /* | |
52 | * GDB remote protocol parser: | |
53 | */ | |
54 | ||
55 | static int hex(char ch) | |
56 | { | |
57 | if ((ch >= 'a') && (ch <= 'f')) | |
58 | return ch - 'a' + 10; | |
59 | if ((ch >= '0') && (ch <= '9')) | |
60 | return ch - '0'; | |
61 | if ((ch >= 'A') && (ch <= 'F')) | |
62 | return ch - 'A' + 10; | |
63 | return -1; | |
64 | } | |
65 | ||
f5316b4a JW |
66 | #ifdef CONFIG_KGDB_KDB |
67 | static int gdbstub_read_wait(void) | |
68 | { | |
69 | int ret = -1; | |
70 | int i; | |
71 | ||
72 | /* poll any additional I/O interfaces that are defined */ | |
73 | while (ret < 0) | |
74 | for (i = 0; kdb_poll_funcs[i] != NULL; i++) { | |
75 | ret = kdb_poll_funcs[i](); | |
76 | if (ret > 0) | |
77 | break; | |
78 | } | |
79 | return ret; | |
80 | } | |
81 | #else | |
82 | static int gdbstub_read_wait(void) | |
83 | { | |
84 | int ret = dbg_io_ops->read_char(); | |
85 | while (ret == NO_POLL_CHAR) | |
86 | ret = dbg_io_ops->read_char(); | |
87 | return ret; | |
88 | } | |
89 | #endif | |
53197fc4 JW |
90 | /* scan for the sequence $<data>#<checksum> */ |
91 | static void get_packet(char *buffer) | |
92 | { | |
93 | unsigned char checksum; | |
94 | unsigned char xmitcsum; | |
95 | int count; | |
96 | char ch; | |
97 | ||
98 | do { | |
99 | /* | |
100 | * Spin and wait around for the start character, ignore all | |
101 | * other characters: | |
102 | */ | |
f5316b4a | 103 | while ((ch = (gdbstub_read_wait())) != '$') |
53197fc4 JW |
104 | /* nothing */; |
105 | ||
106 | kgdb_connected = 1; | |
107 | checksum = 0; | |
108 | xmitcsum = -1; | |
109 | ||
110 | count = 0; | |
111 | ||
112 | /* | |
113 | * now, read until a # or end of buffer is found: | |
114 | */ | |
115 | while (count < (BUFMAX - 1)) { | |
f5316b4a | 116 | ch = gdbstub_read_wait(); |
53197fc4 JW |
117 | if (ch == '#') |
118 | break; | |
119 | checksum = checksum + ch; | |
120 | buffer[count] = ch; | |
121 | count = count + 1; | |
122 | } | |
123 | buffer[count] = 0; | |
124 | ||
125 | if (ch == '#') { | |
f5316b4a JW |
126 | xmitcsum = hex(gdbstub_read_wait()) << 4; |
127 | xmitcsum += hex(gdbstub_read_wait()); | |
53197fc4 JW |
128 | |
129 | if (checksum != xmitcsum) | |
130 | /* failed checksum */ | |
131 | dbg_io_ops->write_char('-'); | |
132 | else | |
133 | /* successful transfer */ | |
134 | dbg_io_ops->write_char('+'); | |
135 | if (dbg_io_ops->flush) | |
136 | dbg_io_ops->flush(); | |
137 | } | |
138 | } while (checksum != xmitcsum); | |
139 | } | |
140 | ||
141 | /* | |
142 | * Send the packet in buffer. | |
143 | * Check for gdb connection if asked for. | |
144 | */ | |
145 | static void put_packet(char *buffer) | |
146 | { | |
147 | unsigned char checksum; | |
148 | int count; | |
149 | char ch; | |
150 | ||
151 | /* | |
152 | * $<packet info>#<checksum>. | |
153 | */ | |
154 | while (1) { | |
155 | dbg_io_ops->write_char('$'); | |
156 | checksum = 0; | |
157 | count = 0; | |
158 | ||
159 | while ((ch = buffer[count])) { | |
160 | dbg_io_ops->write_char(ch); | |
161 | checksum += ch; | |
162 | count++; | |
163 | } | |
164 | ||
165 | dbg_io_ops->write_char('#'); | |
166 | dbg_io_ops->write_char(hex_asc_hi(checksum)); | |
167 | dbg_io_ops->write_char(hex_asc_lo(checksum)); | |
168 | if (dbg_io_ops->flush) | |
169 | dbg_io_ops->flush(); | |
170 | ||
171 | /* Now see what we get in reply. */ | |
f5316b4a | 172 | ch = gdbstub_read_wait(); |
53197fc4 JW |
173 | |
174 | if (ch == 3) | |
f5316b4a | 175 | ch = gdbstub_read_wait(); |
53197fc4 JW |
176 | |
177 | /* If we get an ACK, we are done. */ | |
178 | if (ch == '+') | |
179 | return; | |
180 | ||
181 | /* | |
182 | * If we get the start of another packet, this means | |
183 | * that GDB is attempting to reconnect. We will NAK | |
184 | * the packet being sent, and stop trying to send this | |
185 | * packet. | |
186 | */ | |
187 | if (ch == '$') { | |
188 | dbg_io_ops->write_char('-'); | |
189 | if (dbg_io_ops->flush) | |
190 | dbg_io_ops->flush(); | |
191 | return; | |
192 | } | |
193 | } | |
194 | } | |
195 | ||
196 | static char gdbmsgbuf[BUFMAX + 1]; | |
197 | ||
198 | void gdbstub_msg_write(const char *s, int len) | |
199 | { | |
200 | char *bufptr; | |
201 | int wcount; | |
202 | int i; | |
203 | ||
a0de055c JW |
204 | if (len == 0) |
205 | len = strlen(s); | |
206 | ||
53197fc4 JW |
207 | /* 'O'utput */ |
208 | gdbmsgbuf[0] = 'O'; | |
209 | ||
210 | /* Fill and send buffers... */ | |
211 | while (len > 0) { | |
212 | bufptr = gdbmsgbuf + 1; | |
213 | ||
214 | /* Calculate how many this time */ | |
215 | if ((len << 1) > (BUFMAX - 2)) | |
216 | wcount = (BUFMAX - 2) >> 1; | |
217 | else | |
218 | wcount = len; | |
219 | ||
220 | /* Pack in hex chars */ | |
221 | for (i = 0; i < wcount; i++) | |
222 | bufptr = pack_hex_byte(bufptr, s[i]); | |
223 | *bufptr = '\0'; | |
224 | ||
225 | /* Move up */ | |
226 | s += wcount; | |
227 | len -= wcount; | |
228 | ||
229 | /* Write packet */ | |
230 | put_packet(gdbmsgbuf); | |
231 | } | |
232 | } | |
233 | ||
234 | /* | |
235 | * Convert the memory pointed to by mem into hex, placing result in | |
236 | * buf. Return a pointer to the last char put in buf (null). May | |
237 | * return an error. | |
238 | */ | |
239 | int kgdb_mem2hex(char *mem, char *buf, int count) | |
240 | { | |
241 | char *tmp; | |
242 | int err; | |
243 | ||
244 | /* | |
245 | * We use the upper half of buf as an intermediate buffer for the | |
246 | * raw memory copy. Hex conversion will work against this one. | |
247 | */ | |
248 | tmp = buf + count; | |
249 | ||
250 | err = probe_kernel_read(tmp, mem, count); | |
251 | if (!err) { | |
252 | while (count > 0) { | |
253 | buf = pack_hex_byte(buf, *tmp); | |
254 | tmp++; | |
255 | count--; | |
256 | } | |
257 | ||
258 | *buf = 0; | |
259 | } | |
260 | ||
261 | return err; | |
262 | } | |
263 | ||
264 | /* | |
265 | * Convert the hex array pointed to by buf into binary to be placed in | |
266 | * mem. Return a pointer to the character AFTER the last byte | |
267 | * written. May return an error. | |
268 | */ | |
269 | int kgdb_hex2mem(char *buf, char *mem, int count) | |
270 | { | |
271 | char *tmp_raw; | |
272 | char *tmp_hex; | |
273 | ||
274 | /* | |
275 | * We use the upper half of buf as an intermediate buffer for the | |
276 | * raw memory that is converted from hex. | |
277 | */ | |
278 | tmp_raw = buf + count * 2; | |
279 | ||
280 | tmp_hex = tmp_raw - 1; | |
281 | while (tmp_hex >= buf) { | |
282 | tmp_raw--; | |
283 | *tmp_raw = hex(*tmp_hex--); | |
284 | *tmp_raw |= hex(*tmp_hex--) << 4; | |
285 | } | |
286 | ||
287 | return probe_kernel_write(mem, tmp_raw, count); | |
288 | } | |
289 | ||
290 | /* | |
291 | * While we find nice hex chars, build a long_val. | |
292 | * Return number of chars processed. | |
293 | */ | |
294 | int kgdb_hex2long(char **ptr, unsigned long *long_val) | |
295 | { | |
296 | int hex_val; | |
297 | int num = 0; | |
298 | int negate = 0; | |
299 | ||
300 | *long_val = 0; | |
301 | ||
302 | if (**ptr == '-') { | |
303 | negate = 1; | |
304 | (*ptr)++; | |
305 | } | |
306 | while (**ptr) { | |
307 | hex_val = hex(**ptr); | |
308 | if (hex_val < 0) | |
309 | break; | |
310 | ||
311 | *long_val = (*long_val << 4) | hex_val; | |
312 | num++; | |
313 | (*ptr)++; | |
314 | } | |
315 | ||
316 | if (negate) | |
317 | *long_val = -*long_val; | |
318 | ||
319 | return num; | |
320 | } | |
321 | ||
322 | /* | |
323 | * Copy the binary array pointed to by buf into mem. Fix $, #, and | |
324 | * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success. | |
325 | * The input buf is overwitten with the result to write to mem. | |
326 | */ | |
327 | static int kgdb_ebin2mem(char *buf, char *mem, int count) | |
328 | { | |
329 | int size = 0; | |
330 | char *c = buf; | |
331 | ||
332 | while (count-- > 0) { | |
333 | c[size] = *buf++; | |
334 | if (c[size] == 0x7d) | |
335 | c[size] = *buf++ ^ 0x20; | |
336 | size++; | |
337 | } | |
338 | ||
339 | return probe_kernel_write(mem, c, size); | |
340 | } | |
341 | ||
342 | /* Write memory due to an 'M' or 'X' packet. */ | |
343 | static int write_mem_msg(int binary) | |
344 | { | |
345 | char *ptr = &remcom_in_buffer[1]; | |
346 | unsigned long addr; | |
347 | unsigned long length; | |
348 | int err; | |
349 | ||
350 | if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' && | |
351 | kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') { | |
352 | if (binary) | |
353 | err = kgdb_ebin2mem(ptr, (char *)addr, length); | |
354 | else | |
355 | err = kgdb_hex2mem(ptr, (char *)addr, length); | |
356 | if (err) | |
357 | return err; | |
358 | if (CACHE_FLUSH_IS_SAFE) | |
359 | flush_icache_range(addr, addr + length); | |
360 | return 0; | |
361 | } | |
362 | ||
363 | return -EINVAL; | |
364 | } | |
365 | ||
366 | static void error_packet(char *pkt, int error) | |
367 | { | |
368 | error = -error; | |
369 | pkt[0] = 'E'; | |
370 | pkt[1] = hex_asc[(error / 10)]; | |
371 | pkt[2] = hex_asc[(error % 10)]; | |
372 | pkt[3] = '\0'; | |
373 | } | |
374 | ||
375 | /* | |
376 | * Thread ID accessors. We represent a flat TID space to GDB, where | |
377 | * the per CPU idle threads (which under Linux all have PID 0) are | |
378 | * remapped to negative TIDs. | |
379 | */ | |
380 | ||
381 | #define BUF_THREAD_ID_SIZE 16 | |
382 | ||
383 | static char *pack_threadid(char *pkt, unsigned char *id) | |
384 | { | |
385 | char *limit; | |
386 | ||
387 | limit = pkt + BUF_THREAD_ID_SIZE; | |
388 | while (pkt < limit) | |
389 | pkt = pack_hex_byte(pkt, *id++); | |
390 | ||
391 | return pkt; | |
392 | } | |
393 | ||
394 | static void int_to_threadref(unsigned char *id, int value) | |
395 | { | |
396 | unsigned char *scan; | |
397 | int i = 4; | |
398 | ||
399 | scan = (unsigned char *)id; | |
400 | while (i--) | |
401 | *scan++ = 0; | |
402 | put_unaligned_be32(value, scan); | |
403 | } | |
404 | ||
405 | static struct task_struct *getthread(struct pt_regs *regs, int tid) | |
406 | { | |
407 | /* | |
408 | * Non-positive TIDs are remapped to the cpu shadow information | |
409 | */ | |
410 | if (tid == 0 || tid == -1) | |
411 | tid = -atomic_read(&kgdb_active) - 2; | |
412 | if (tid < -1 && tid > -NR_CPUS - 2) { | |
413 | if (kgdb_info[-tid - 2].task) | |
414 | return kgdb_info[-tid - 2].task; | |
415 | else | |
416 | return idle_task(-tid - 2); | |
417 | } | |
418 | if (tid <= 0) { | |
419 | printk(KERN_ERR "KGDB: Internal thread select error\n"); | |
420 | dump_stack(); | |
421 | return NULL; | |
422 | } | |
423 | ||
424 | /* | |
425 | * find_task_by_pid_ns() does not take the tasklist lock anymore | |
426 | * but is nicely RCU locked - hence is a pretty resilient | |
427 | * thing to use: | |
428 | */ | |
429 | return find_task_by_pid_ns(tid, &init_pid_ns); | |
430 | } | |
431 | ||
432 | ||
433 | /* | |
434 | * Remap normal tasks to their real PID, | |
435 | * CPU shadow threads are mapped to -CPU - 2 | |
436 | */ | |
437 | static inline int shadow_pid(int realpid) | |
438 | { | |
439 | if (realpid) | |
440 | return realpid; | |
441 | ||
442 | return -raw_smp_processor_id() - 2; | |
443 | } | |
444 | ||
445 | /* | |
446 | * All the functions that start with gdb_cmd are the various | |
447 | * operations to implement the handlers for the gdbserial protocol | |
448 | * where KGDB is communicating with an external debugger | |
449 | */ | |
450 | ||
451 | /* Handle the '?' status packets */ | |
452 | static void gdb_cmd_status(struct kgdb_state *ks) | |
453 | { | |
454 | /* | |
455 | * We know that this packet is only sent | |
456 | * during initial connect. So to be safe, | |
457 | * we clear out our breakpoints now in case | |
458 | * GDB is reconnecting. | |
459 | */ | |
460 | dbg_remove_all_break(); | |
461 | ||
462 | remcom_out_buffer[0] = 'S'; | |
463 | pack_hex_byte(&remcom_out_buffer[1], ks->signo); | |
464 | } | |
465 | ||
466 | /* Handle the 'g' get registers request */ | |
467 | static void gdb_cmd_getregs(struct kgdb_state *ks) | |
468 | { | |
469 | struct task_struct *thread; | |
470 | void *local_debuggerinfo; | |
471 | int i; | |
472 | ||
473 | thread = kgdb_usethread; | |
474 | if (!thread) { | |
475 | thread = kgdb_info[ks->cpu].task; | |
476 | local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo; | |
477 | } else { | |
478 | local_debuggerinfo = NULL; | |
479 | for_each_online_cpu(i) { | |
480 | /* | |
481 | * Try to find the task on some other | |
482 | * or possibly this node if we do not | |
483 | * find the matching task then we try | |
484 | * to approximate the results. | |
485 | */ | |
486 | if (thread == kgdb_info[i].task) | |
487 | local_debuggerinfo = kgdb_info[i].debuggerinfo; | |
488 | } | |
489 | } | |
490 | ||
491 | /* | |
492 | * All threads that don't have debuggerinfo should be | |
493 | * in schedule() sleeping, since all other CPUs | |
494 | * are in kgdb_wait, and thus have debuggerinfo. | |
495 | */ | |
496 | if (local_debuggerinfo) { | |
497 | pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo); | |
498 | } else { | |
499 | /* | |
500 | * Pull stuff saved during switch_to; nothing | |
501 | * else is accessible (or even particularly | |
502 | * relevant). | |
503 | * | |
504 | * This should be enough for a stack trace. | |
505 | */ | |
506 | sleeping_thread_to_gdb_regs(gdb_regs, thread); | |
507 | } | |
508 | kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES); | |
509 | } | |
510 | ||
511 | /* Handle the 'G' set registers request */ | |
512 | static void gdb_cmd_setregs(struct kgdb_state *ks) | |
513 | { | |
514 | kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES); | |
515 | ||
516 | if (kgdb_usethread && kgdb_usethread != current) { | |
517 | error_packet(remcom_out_buffer, -EINVAL); | |
518 | } else { | |
519 | gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs); | |
520 | strcpy(remcom_out_buffer, "OK"); | |
521 | } | |
522 | } | |
523 | ||
524 | /* Handle the 'm' memory read bytes */ | |
525 | static void gdb_cmd_memread(struct kgdb_state *ks) | |
526 | { | |
527 | char *ptr = &remcom_in_buffer[1]; | |
528 | unsigned long length; | |
529 | unsigned long addr; | |
530 | int err; | |
531 | ||
532 | if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' && | |
533 | kgdb_hex2long(&ptr, &length) > 0) { | |
534 | err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length); | |
535 | if (err) | |
536 | error_packet(remcom_out_buffer, err); | |
537 | } else { | |
538 | error_packet(remcom_out_buffer, -EINVAL); | |
539 | } | |
540 | } | |
541 | ||
542 | /* Handle the 'M' memory write bytes */ | |
543 | static void gdb_cmd_memwrite(struct kgdb_state *ks) | |
544 | { | |
545 | int err = write_mem_msg(0); | |
546 | ||
547 | if (err) | |
548 | error_packet(remcom_out_buffer, err); | |
549 | else | |
550 | strcpy(remcom_out_buffer, "OK"); | |
551 | } | |
552 | ||
553 | /* Handle the 'X' memory binary write bytes */ | |
554 | static void gdb_cmd_binwrite(struct kgdb_state *ks) | |
555 | { | |
556 | int err = write_mem_msg(1); | |
557 | ||
558 | if (err) | |
559 | error_packet(remcom_out_buffer, err); | |
560 | else | |
561 | strcpy(remcom_out_buffer, "OK"); | |
562 | } | |
563 | ||
564 | /* Handle the 'D' or 'k', detach or kill packets */ | |
565 | static void gdb_cmd_detachkill(struct kgdb_state *ks) | |
566 | { | |
567 | int error; | |
568 | ||
569 | /* The detach case */ | |
570 | if (remcom_in_buffer[0] == 'D') { | |
571 | error = dbg_remove_all_break(); | |
572 | if (error < 0) { | |
573 | error_packet(remcom_out_buffer, error); | |
574 | } else { | |
575 | strcpy(remcom_out_buffer, "OK"); | |
576 | kgdb_connected = 0; | |
577 | } | |
578 | put_packet(remcom_out_buffer); | |
579 | } else { | |
580 | /* | |
581 | * Assume the kill case, with no exit code checking, | |
582 | * trying to force detach the debugger: | |
583 | */ | |
584 | dbg_remove_all_break(); | |
585 | kgdb_connected = 0; | |
586 | } | |
587 | } | |
588 | ||
589 | /* Handle the 'R' reboot packets */ | |
590 | static int gdb_cmd_reboot(struct kgdb_state *ks) | |
591 | { | |
592 | /* For now, only honor R0 */ | |
593 | if (strcmp(remcom_in_buffer, "R0") == 0) { | |
594 | printk(KERN_CRIT "Executing emergency reboot\n"); | |
595 | strcpy(remcom_out_buffer, "OK"); | |
596 | put_packet(remcom_out_buffer); | |
597 | ||
598 | /* | |
599 | * Execution should not return from | |
600 | * machine_emergency_restart() | |
601 | */ | |
602 | machine_emergency_restart(); | |
603 | kgdb_connected = 0; | |
604 | ||
605 | return 1; | |
606 | } | |
607 | return 0; | |
608 | } | |
609 | ||
610 | /* Handle the 'q' query packets */ | |
611 | static void gdb_cmd_query(struct kgdb_state *ks) | |
612 | { | |
613 | struct task_struct *g; | |
614 | struct task_struct *p; | |
615 | unsigned char thref[8]; | |
616 | char *ptr; | |
617 | int i; | |
618 | int cpu; | |
619 | int finished = 0; | |
620 | ||
621 | switch (remcom_in_buffer[1]) { | |
622 | case 's': | |
623 | case 'f': | |
fb82c0ff | 624 | if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) |
53197fc4 | 625 | break; |
53197fc4 JW |
626 | |
627 | i = 0; | |
628 | remcom_out_buffer[0] = 'm'; | |
629 | ptr = remcom_out_buffer + 1; | |
630 | if (remcom_in_buffer[1] == 'f') { | |
631 | /* Each cpu is a shadow thread */ | |
632 | for_each_online_cpu(cpu) { | |
633 | ks->thr_query = 0; | |
634 | int_to_threadref(thref, -cpu - 2); | |
635 | pack_threadid(ptr, thref); | |
636 | ptr += BUF_THREAD_ID_SIZE; | |
637 | *(ptr++) = ','; | |
638 | i++; | |
639 | } | |
640 | } | |
641 | ||
642 | do_each_thread(g, p) { | |
643 | if (i >= ks->thr_query && !finished) { | |
644 | int_to_threadref(thref, p->pid); | |
645 | pack_threadid(ptr, thref); | |
646 | ptr += BUF_THREAD_ID_SIZE; | |
647 | *(ptr++) = ','; | |
648 | ks->thr_query++; | |
649 | if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0) | |
650 | finished = 1; | |
651 | } | |
652 | i++; | |
653 | } while_each_thread(g, p); | |
654 | ||
655 | *(--ptr) = '\0'; | |
656 | break; | |
657 | ||
658 | case 'C': | |
659 | /* Current thread id */ | |
660 | strcpy(remcom_out_buffer, "QC"); | |
661 | ks->threadid = shadow_pid(current->pid); | |
662 | int_to_threadref(thref, ks->threadid); | |
663 | pack_threadid(remcom_out_buffer + 2, thref); | |
664 | break; | |
665 | case 'T': | |
fb82c0ff | 666 | if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) |
53197fc4 | 667 | break; |
fb82c0ff | 668 | |
53197fc4 JW |
669 | ks->threadid = 0; |
670 | ptr = remcom_in_buffer + 17; | |
671 | kgdb_hex2long(&ptr, &ks->threadid); | |
672 | if (!getthread(ks->linux_regs, ks->threadid)) { | |
673 | error_packet(remcom_out_buffer, -EINVAL); | |
674 | break; | |
675 | } | |
676 | if ((int)ks->threadid > 0) { | |
677 | kgdb_mem2hex(getthread(ks->linux_regs, | |
678 | ks->threadid)->comm, | |
679 | remcom_out_buffer, 16); | |
680 | } else { | |
681 | static char tmpstr[23 + BUF_THREAD_ID_SIZE]; | |
682 | ||
683 | sprintf(tmpstr, "shadowCPU%d", | |
684 | (int)(-ks->threadid - 2)); | |
685 | kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr)); | |
686 | } | |
687 | break; | |
a0de055c JW |
688 | #ifdef CONFIG_KGDB_KDB |
689 | case 'R': | |
690 | if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) { | |
691 | int len = strlen(remcom_in_buffer + 6); | |
692 | ||
693 | if ((len % 2) != 0) { | |
694 | strcpy(remcom_out_buffer, "E01"); | |
695 | break; | |
696 | } | |
697 | kgdb_hex2mem(remcom_in_buffer + 6, | |
698 | remcom_out_buffer, len); | |
699 | len = len / 2; | |
700 | remcom_out_buffer[len++] = 0; | |
701 | ||
702 | kdb_parse(remcom_out_buffer); | |
703 | strcpy(remcom_out_buffer, "OK"); | |
704 | } | |
705 | break; | |
706 | #endif | |
53197fc4 JW |
707 | } |
708 | } | |
709 | ||
710 | /* Handle the 'H' task query packets */ | |
711 | static void gdb_cmd_task(struct kgdb_state *ks) | |
712 | { | |
713 | struct task_struct *thread; | |
714 | char *ptr; | |
715 | ||
716 | switch (remcom_in_buffer[1]) { | |
717 | case 'g': | |
718 | ptr = &remcom_in_buffer[2]; | |
719 | kgdb_hex2long(&ptr, &ks->threadid); | |
720 | thread = getthread(ks->linux_regs, ks->threadid); | |
721 | if (!thread && ks->threadid > 0) { | |
722 | error_packet(remcom_out_buffer, -EINVAL); | |
723 | break; | |
724 | } | |
725 | kgdb_usethread = thread; | |
726 | ks->kgdb_usethreadid = ks->threadid; | |
727 | strcpy(remcom_out_buffer, "OK"); | |
728 | break; | |
729 | case 'c': | |
730 | ptr = &remcom_in_buffer[2]; | |
731 | kgdb_hex2long(&ptr, &ks->threadid); | |
732 | if (!ks->threadid) { | |
733 | kgdb_contthread = NULL; | |
734 | } else { | |
735 | thread = getthread(ks->linux_regs, ks->threadid); | |
736 | if (!thread && ks->threadid > 0) { | |
737 | error_packet(remcom_out_buffer, -EINVAL); | |
738 | break; | |
739 | } | |
740 | kgdb_contthread = thread; | |
741 | } | |
742 | strcpy(remcom_out_buffer, "OK"); | |
743 | break; | |
744 | } | |
745 | } | |
746 | ||
747 | /* Handle the 'T' thread query packets */ | |
748 | static void gdb_cmd_thread(struct kgdb_state *ks) | |
749 | { | |
750 | char *ptr = &remcom_in_buffer[1]; | |
751 | struct task_struct *thread; | |
752 | ||
753 | kgdb_hex2long(&ptr, &ks->threadid); | |
754 | thread = getthread(ks->linux_regs, ks->threadid); | |
755 | if (thread) | |
756 | strcpy(remcom_out_buffer, "OK"); | |
757 | else | |
758 | error_packet(remcom_out_buffer, -EINVAL); | |
759 | } | |
760 | ||
761 | /* Handle the 'z' or 'Z' breakpoint remove or set packets */ | |
762 | static void gdb_cmd_break(struct kgdb_state *ks) | |
763 | { | |
764 | /* | |
765 | * Since GDB-5.3, it's been drafted that '0' is a software | |
766 | * breakpoint, '1' is a hardware breakpoint, so let's do that. | |
767 | */ | |
768 | char *bpt_type = &remcom_in_buffer[1]; | |
769 | char *ptr = &remcom_in_buffer[2]; | |
770 | unsigned long addr; | |
771 | unsigned long length; | |
772 | int error = 0; | |
773 | ||
774 | if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') { | |
775 | /* Unsupported */ | |
776 | if (*bpt_type > '4') | |
777 | return; | |
778 | } else { | |
779 | if (*bpt_type != '0' && *bpt_type != '1') | |
780 | /* Unsupported. */ | |
781 | return; | |
782 | } | |
783 | ||
784 | /* | |
785 | * Test if this is a hardware breakpoint, and | |
786 | * if we support it: | |
787 | */ | |
788 | if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)) | |
789 | /* Unsupported. */ | |
790 | return; | |
791 | ||
792 | if (*(ptr++) != ',') { | |
793 | error_packet(remcom_out_buffer, -EINVAL); | |
794 | return; | |
795 | } | |
796 | if (!kgdb_hex2long(&ptr, &addr)) { | |
797 | error_packet(remcom_out_buffer, -EINVAL); | |
798 | return; | |
799 | } | |
800 | if (*(ptr++) != ',' || | |
801 | !kgdb_hex2long(&ptr, &length)) { | |
802 | error_packet(remcom_out_buffer, -EINVAL); | |
803 | return; | |
804 | } | |
805 | ||
806 | if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0') | |
807 | error = dbg_set_sw_break(addr); | |
808 | else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0') | |
809 | error = dbg_remove_sw_break(addr); | |
810 | else if (remcom_in_buffer[0] == 'Z') | |
811 | error = arch_kgdb_ops.set_hw_breakpoint(addr, | |
812 | (int)length, *bpt_type - '0'); | |
813 | else if (remcom_in_buffer[0] == 'z') | |
814 | error = arch_kgdb_ops.remove_hw_breakpoint(addr, | |
815 | (int) length, *bpt_type - '0'); | |
816 | ||
817 | if (error == 0) | |
818 | strcpy(remcom_out_buffer, "OK"); | |
819 | else | |
820 | error_packet(remcom_out_buffer, error); | |
821 | } | |
822 | ||
823 | /* Handle the 'C' signal / exception passing packets */ | |
824 | static int gdb_cmd_exception_pass(struct kgdb_state *ks) | |
825 | { | |
826 | /* C09 == pass exception | |
827 | * C15 == detach kgdb, pass exception | |
828 | */ | |
829 | if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') { | |
830 | ||
831 | ks->pass_exception = 1; | |
832 | remcom_in_buffer[0] = 'c'; | |
833 | ||
834 | } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') { | |
835 | ||
836 | ks->pass_exception = 1; | |
837 | remcom_in_buffer[0] = 'D'; | |
838 | dbg_remove_all_break(); | |
839 | kgdb_connected = 0; | |
840 | return 1; | |
841 | ||
842 | } else { | |
843 | gdbstub_msg_write("KGDB only knows signal 9 (pass)" | |
844 | " and 15 (pass and disconnect)\n" | |
845 | "Executing a continue without signal passing\n", 0); | |
846 | remcom_in_buffer[0] = 'c'; | |
847 | } | |
848 | ||
849 | /* Indicate fall through */ | |
850 | return -1; | |
851 | } | |
852 | ||
853 | /* | |
854 | * This function performs all gdbserial command procesing | |
855 | */ | |
856 | int gdb_serial_stub(struct kgdb_state *ks) | |
857 | { | |
858 | int error = 0; | |
859 | int tmp; | |
860 | ||
861 | /* Clear the out buffer. */ | |
862 | memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); | |
863 | ||
864 | if (kgdb_connected) { | |
865 | unsigned char thref[8]; | |
866 | char *ptr; | |
867 | ||
868 | /* Reply to host that an exception has occurred */ | |
869 | ptr = remcom_out_buffer; | |
870 | *ptr++ = 'T'; | |
871 | ptr = pack_hex_byte(ptr, ks->signo); | |
872 | ptr += strlen(strcpy(ptr, "thread:")); | |
873 | int_to_threadref(thref, shadow_pid(current->pid)); | |
874 | ptr = pack_threadid(ptr, thref); | |
875 | *ptr++ = ';'; | |
876 | put_packet(remcom_out_buffer); | |
877 | } | |
878 | ||
879 | kgdb_usethread = kgdb_info[ks->cpu].task; | |
880 | ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid); | |
881 | ks->pass_exception = 0; | |
882 | ||
883 | while (1) { | |
884 | error = 0; | |
885 | ||
886 | /* Clear the out buffer. */ | |
887 | memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); | |
888 | ||
889 | get_packet(remcom_in_buffer); | |
890 | ||
891 | switch (remcom_in_buffer[0]) { | |
892 | case '?': /* gdbserial status */ | |
893 | gdb_cmd_status(ks); | |
894 | break; | |
895 | case 'g': /* return the value of the CPU registers */ | |
896 | gdb_cmd_getregs(ks); | |
897 | break; | |
898 | case 'G': /* set the value of the CPU registers - return OK */ | |
899 | gdb_cmd_setregs(ks); | |
900 | break; | |
901 | case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ | |
902 | gdb_cmd_memread(ks); | |
903 | break; | |
904 | case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */ | |
905 | gdb_cmd_memwrite(ks); | |
906 | break; | |
907 | case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */ | |
908 | gdb_cmd_binwrite(ks); | |
909 | break; | |
910 | /* kill or detach. KGDB should treat this like a | |
911 | * continue. | |
912 | */ | |
913 | case 'D': /* Debugger detach */ | |
914 | case 'k': /* Debugger detach via kill */ | |
915 | gdb_cmd_detachkill(ks); | |
916 | goto default_handle; | |
917 | case 'R': /* Reboot */ | |
918 | if (gdb_cmd_reboot(ks)) | |
919 | goto default_handle; | |
920 | break; | |
921 | case 'q': /* query command */ | |
922 | gdb_cmd_query(ks); | |
923 | break; | |
924 | case 'H': /* task related */ | |
925 | gdb_cmd_task(ks); | |
926 | break; | |
927 | case 'T': /* Query thread status */ | |
928 | gdb_cmd_thread(ks); | |
929 | break; | |
930 | case 'z': /* Break point remove */ | |
931 | case 'Z': /* Break point set */ | |
932 | gdb_cmd_break(ks); | |
933 | break; | |
dcc78711 JW |
934 | #ifdef CONFIG_KGDB_KDB |
935 | case '3': /* Escape into back into kdb */ | |
936 | if (remcom_in_buffer[1] == '\0') { | |
937 | gdb_cmd_detachkill(ks); | |
938 | return DBG_PASS_EVENT; | |
939 | } | |
940 | #endif | |
53197fc4 JW |
941 | case 'C': /* Exception passing */ |
942 | tmp = gdb_cmd_exception_pass(ks); | |
943 | if (tmp > 0) | |
944 | goto default_handle; | |
945 | if (tmp == 0) | |
946 | break; | |
947 | /* Fall through on tmp < 0 */ | |
948 | case 'c': /* Continue packet */ | |
949 | case 's': /* Single step packet */ | |
950 | if (kgdb_contthread && kgdb_contthread != current) { | |
951 | /* Can't switch threads in kgdb */ | |
952 | error_packet(remcom_out_buffer, -EINVAL); | |
953 | break; | |
954 | } | |
955 | dbg_activate_sw_breakpoints(); | |
956 | /* Fall through to default processing */ | |
957 | default: | |
958 | default_handle: | |
959 | error = kgdb_arch_handle_exception(ks->ex_vector, | |
960 | ks->signo, | |
961 | ks->err_code, | |
962 | remcom_in_buffer, | |
963 | remcom_out_buffer, | |
964 | ks->linux_regs); | |
965 | /* | |
966 | * Leave cmd processing on error, detach, | |
967 | * kill, continue, or single step. | |
968 | */ | |
969 | if (error >= 0 || remcom_in_buffer[0] == 'D' || | |
970 | remcom_in_buffer[0] == 'k') { | |
971 | error = 0; | |
972 | goto kgdb_exit; | |
973 | } | |
974 | ||
975 | } | |
976 | ||
977 | /* reply to the request */ | |
978 | put_packet(remcom_out_buffer); | |
979 | } | |
980 | ||
981 | kgdb_exit: | |
982 | if (ks->pass_exception) | |
983 | error = 1; | |
984 | return error; | |
985 | } | |
dcc78711 JW |
986 | |
987 | int gdbstub_state(struct kgdb_state *ks, char *cmd) | |
988 | { | |
989 | int error; | |
990 | ||
991 | switch (cmd[0]) { | |
992 | case 'e': | |
993 | error = kgdb_arch_handle_exception(ks->ex_vector, | |
994 | ks->signo, | |
995 | ks->err_code, | |
996 | remcom_in_buffer, | |
997 | remcom_out_buffer, | |
998 | ks->linux_regs); | |
999 | return error; | |
1000 | case 's': | |
1001 | case 'c': | |
1002 | strcpy(remcom_in_buffer, cmd); | |
1003 | return 0; | |
1004 | case '?': | |
1005 | gdb_cmd_status(ks); | |
1006 | break; | |
1007 | case '\0': | |
1008 | strcpy(remcom_out_buffer, ""); | |
1009 | break; | |
1010 | } | |
1011 | dbg_io_ops->write_char('+'); | |
1012 | put_packet(remcom_out_buffer); | |
1013 | return 0; | |
1014 | } |