blob: 2a6c2a9441fdea23a1259b78e8a8a561a3f5ad91 [file] [log] [blame]
sewardj3b290482011-05-06 21:02:55 +00001/* Main code for remote server for GDB.
2 Copyright (C) 1989, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2006, 2011
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7 It has been modified to integrate it in valgrind
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24#include "server.h"
25#include "regdef.h"
26#include "pub_core_options.h"
27#include "pub_core_translate.h"
28#include "pub_core_mallocfree.h"
sewardj2a312392011-06-26 09:26:48 +000029#include "pub_core_initimg.h"
sewardj3b290482011-05-06 21:02:55 +000030
31unsigned long cont_thread;
32unsigned long general_thread;
33unsigned long step_thread;
34unsigned long thread_from_wait;
35unsigned long old_thread_from_wait;
36
bart2dafc542011-05-18 16:08:28 +000037int pass_signals[TARGET_SIGNAL_LAST];
38
sewardj3b290482011-05-06 21:02:55 +000039/* for a gdbserver integrated in valgrind, resuming the process consists
40 in returning the control to valgrind.
41 Then at the next error or break or ..., valgrind calls gdbserver again.
42 A resume packet must then be built.
43 resume_packet_needed records the fact that the next call to gdbserver
44 must send a resume packet to gdb. */
45static Bool resume_packet_needed = False;
46
47VG_MINIMAL_JMP_BUF(toplevel);
48
49/* Decode a qXfer read request. Return 0 if everything looks OK,
50 or -1 otherwise. */
51
52static
53int decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
54{
55 /* Extract and NUL-terminate the annex. */
56 *annex = buf;
57 while (*buf && *buf != ':')
58 buf++;
59 if (*buf == '\0')
60 return -1;
61 *buf++ = 0;
62
63 /* After the read/write marker and annex, qXfer looks like a
64 traditional 'm' packet. */
65 decode_m_packet (buf, ofs, len);
66
67 return 0;
68}
69
70/* Write the response to a successful qXfer read. Returns the
71 length of the (binary) data stored in BUF, corresponding
72 to as much of DATA/LEN as we could fit. IS_MORE controls
73 the first character of the response. */
74static
75int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
76{
77 int out_len;
78
79 if (is_more)
80 buf[0] = 'm';
81 else
82 buf[0] = 'l';
83
84 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
85 PBUFSIZ - POVERHSIZ - 1) + 1;
86}
87
88static Bool initial_valgrind_sink_saved = False;
89/* True <=> valgrind log sink saved in initial_valgrind_sink */
90static OutputSink initial_valgrind_sink;
91
92static Bool command_output_to_log = False;
93/* True <=> command output goes to log instead of gdb */
94
95void reset_valgrind_sink(char *info)
96{
97 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
98 && initial_valgrind_sink_saved) {
99 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
100 VG_(umsg) ("Reset valgrind output to log (%s)\n",
101 (info = NULL ? "" : info));
102 }
103}
104
105static
106void kill_request (char *msg)
107{
108 VG_(umsg) ("%s", msg);
109 remote_close();
110 VG_(exit) (0);
111}
112
113/* handle_gdb_valgrind_command handles the provided mon string command.
114 If command is recognised, return 1 else return 0.
115 Note that in case of ambiguous command, 1 is returned.
116
117 *sink_wanted_at_return is modified if one of the commands
sewardj30b3eca2011-06-28 08:20:39 +0000118 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000119*/
120static
121int handle_gdb_valgrind_command (char* mon, OutputSink* sink_wanted_at_return)
122{
123 UWord ret = 0;
124 char s[strlen(mon)+1]; /* copy for strtok_r */
125 char* wcmd;
126 Char* ssaveptr;
127 char* endptr;
128 int kwdid;
129 int int_value;
130
131 vg_assert (initial_valgrind_sink_saved);
132
133 strcpy (s, mon);
134 wcmd = strtok_r (s, " ", &ssaveptr);
135 /* NB: if possible, avoid introducing a new command below which
sewardj30b3eca2011-06-28 08:20:39 +0000136 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000137 command. This ensures a shorter abbreviation for the user. */
sewardj30b3eca2011-06-28 08:20:39 +0000138 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate",
sewardj3b290482011-05-06 21:02:55 +0000139 wcmd, kwd_report_duplicated_matches)) {
140 case -2:
141 ret = 1;
142 break;
143 case -1:
144 break;
145 case 0: /* help */
146 ret = 1;
147 wcmd = strtok_r (NULL, " ", &ssaveptr);
148 if (wcmd == NULL) {
149 int_value = 0;
150 } else {
151 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
152 case -2: int_value = 0; break;
153 case -1: int_value = 0; break;
154 case 0: int_value = 1; break;
155 default: tl_assert (0);
156 }
157 }
158
159 VG_(gdb_printf) (
160"general valgrind monitor commands:\n"
161" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000162" v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
163" v.info all_errors : show all errors found so far\n"
164" v.info last_error : show last error found\n"
165" v.info n_errs_found : show the nr of errors found so far\n"
166" v.kill : kill the Valgrind process\n"
167" v.set gdb_output : set valgrind output to gdb\n"
168" v.set log_output : set valgrind output to log\n"
169" v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
170" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000171 if (int_value) { VG_(gdb_printf) (
172"debugging valgrind internals monitor commands:\n"
sewardj30b3eca2011-06-28 08:20:39 +0000173" v.info gdbserver_status : show gdbserver status\n"
174" v.info memory : show valgrind heap memory stats\n"
175" v.set debuglog <level> : set valgrind debug log level to <level>\n"
176" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000177" (default traceflags 0b00100000 : show after instrumentation)\n"
178" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
179 }
180 break;
sewardj30b3eca2011-06-28 08:20:39 +0000181 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000182 ret = 1;
183 wcmd = strtok_r (NULL, " ", &ssaveptr);
184 switch (kwdid = VG_(keyword_id)
185 ("vgdb-error debuglog gdb_output log_output mixed_output",
186 wcmd, kwd_report_all)) {
187 case -2:
188 case -1:
189 break;
190 case 0: /* vgdb-error */
191 case 1: /* debuglog */
192 wcmd = strtok_r (NULL, " ", &ssaveptr);
193 if (wcmd == NULL) {
194 int_value = 0;
195 endptr = "empty"; /* to report an error below */
196 } else {
197 int_value = strtol (wcmd, &endptr, 10);
198 }
199 if (*endptr != '\0') {
200 VG_(gdb_printf) ("missing or malformed integer value\n");
201 } else if (kwdid == 0) {
202 VG_(gdb_printf) ("vgdb-error value changed from %d to %d\n",
203 VG_(dyn_vgdb_error), int_value);
204 VG_(dyn_vgdb_error) = int_value;
205 } else if (kwdid == 1) {
206 VG_(gdb_printf) ("debuglog value changed from %d to %d\n",
207 VG_(debugLog_getLevel)(), int_value);
208 VG_(debugLog_startup) (int_value, "gdbsrv");
209 } else {
210 vg_assert (0);
211 }
212 break;
213 case 2: /* gdb_output */
214 (*sink_wanted_at_return).fd = -2;
215 command_output_to_log = False;
216 VG_(gdb_printf) ("valgrind output will go to gdb\n");
217 break;
218 case 3: /* log_output */
219 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
220 command_output_to_log = True;
221 VG_(gdb_printf) ("valgrind output will go to log\n");
222 break;
223 case 4: /* mixed output */
224 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
225 command_output_to_log = False;
226 VG_(gdb_printf)
227 ("valgrind output will go to log, interactive output will go to gdb\n");
228 break;
229 default:
230 vg_assert (0);
231 }
232 break;
sewardj30b3eca2011-06-28 08:20:39 +0000233 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000234 ret = 1;
235 wcmd = strtok_r (NULL, " ", &ssaveptr);
236 switch (kwdid = VG_(keyword_id)
237 ("all_errors n_errs_found last_error gdbserver_status memory",
238 wcmd, kwd_report_all)) {
239 case -2:
240 case -1:
241 break;
242 case 0: // all_errors
243 // A verbosity of minimum 2 is needed to show the errors.
244 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
245 break;
246 case 1: // n_errs_found
247 VG_(gdb_printf) ("n_errs_found %d (vgdb-error %d)\n",
248 VG_(get_n_errs_found) (),
249 VG_(dyn_vgdb_error));
250 break;
251 case 2: // last_error
252 VG_(show_last_error)();
253 break;
254 case 3: // gdbserver_status
255 VG_(gdbserver_status_output)();
256 break;
257 case 4: /* memory */
258 VG_(print_all_arena_stats) ();
259 if (VG_(clo_profile_heap))
260 VG_(print_arena_cc_analysis) ();
261 ret = 1;
262 break;
263 default:
264 vg_assert(0);
265 }
266 break;
267 }
sewardj30b3eca2011-06-28 08:20:39 +0000268 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000269 wcmd = strtok_r (NULL, " ", &ssaveptr);
270 if (wcmd != NULL) {
271 int_value = strtol (wcmd, &endptr, 10);
272 VG_(gdb_printf) ("gdbserver: continuing in %d ms ...\n", int_value);
273 VG_(poll)(NULL, 0, int_value);
274 }
275 VG_(gdb_printf) ("gdbserver: continuing after wait ...\n");
276 ret = 1;
277 break;
sewardj30b3eca2011-06-28 08:20:39 +0000278 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000279 kill_request ("monitor command request to kill this process\n");
280 break;
sewardj30b3eca2011-06-28 08:20:39 +0000281 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000282 Addr address;
283 SizeT verbosity = 0x20;
284
285 ret = 1;
286
287 VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr);
288 if (address != (Addr) 0 || verbosity != 0) {
289 /* we need to force the output to log for the translation trace,
290 as low level VEX tracing cannot be redirected to gdb. */
291 int saved_command_output_to_log = command_output_to_log;
292 int saved_fd = VG_(log_output_sink).fd;
293 Bool single_stepping_on_entry = valgrind_single_stepping();
294 int vex_verbosity = verbosity & 0xff;
295 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
296 if ((verbosity & 0x100) && !single_stepping_on_entry) {
297 valgrind_set_single_stepping(True);
298 // to force gdbserver instrumentation.
299 }
sewardj99d61342011-05-17 16:35:11 +0000300# if defined(VGA_arm)
301 // on arm, we need to (potentially) convert this address
302 // to the thumb form.
303 address = thumb_pc (address);
304# endif
305
sewardj3b290482011-05-06 21:02:55 +0000306 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
307 address,
308 /*debugging*/True,
309 (Int) vex_verbosity,
310 /*bbs_done*/0,
311 /*allow redir?*/True);
312 if ((verbosity & 0x100) && !single_stepping_on_entry) {
313 valgrind_set_single_stepping(False);
314 // reset single stepping.
315 }
316 command_output_to_log = saved_command_output_to_log;
317 VG_(log_output_sink).fd = saved_fd;
318 }
319 break;
320 }
321
322 default:
323 vg_assert (0);
324 }
325 return ret;
326}
327
328/* handle_gdb_monitor_command handles the provided mon string command,
329 which can be either a "standard" valgrind monitor command
330 or a tool specific monitor command.
331 If command recognised, return 1 else return 0.
332 Note that in case of ambiguous command, 1 is returned.
333*/
334static
335int handle_gdb_monitor_command (char* mon)
336{
337 UWord ret = 0;
338 UWord tool_ret = 0;
339 // initially, we assume that when returning, the desired sink is the
340 // one we have when entering. It can however be changed by the standard
341 // valgrind command handling.
342 OutputSink sink_wanted_at_return = VG_(log_output_sink);
343
344 if (!initial_valgrind_sink_saved) {
345 /* first time we enter here, we save the valgrind default log sink */
346 initial_valgrind_sink = sink_wanted_at_return;
347 initial_valgrind_sink_saved = True;
348 }
349
350 if (!command_output_to_log)
351 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
352
353 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
354
355 /* Even if command was recognised by valgrind core, we call the
356 tool command handler : this is needed to handle help command
357 and/or to let the tool do some additional processing of a
358 valgrind standard command. Note however that if valgrind
359 recognised the command, we will always return success. */
360 if (VG_(needs).client_requests) {
361 /* If the tool reports an error when handling a monitor command,
362 we need to avoid calling gdbserver during this command
363 handling. So, we temporarily set VG_(dyn_vgdb_error) to
364 a huge value to ensure m_errormgr.c does not call gdbserver. */
365 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
366 UWord arg[2];
367 VG_(dyn_vgdb_error) = 999999999;
368 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
369 arg[1] = (UWord) mon;
370 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
371 &tool_ret);
372 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
373 }
374
375 /* restore or set the desired output */
376 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
377 if (ret | tool_ret)
378 return 1;
379 else
380 return 0;
381}
382
383
384/* Handle all of the extended 'Q' packets. */
385static
386void handle_set (char *arg_own_buf, int *new_packet_len_p)
387{
388 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
389 noack_mode = True;
390 write_ok (arg_own_buf);
391 return;
392 }
393
394 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
395 int i;
396 char *from, *to;
397 char *end = arg_own_buf + strlen(arg_own_buf);
398 CORE_ADDR sig;
399 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
400 pass_signals[i] = 0;
401
402 from = arg_own_buf + 13;
403 while (from < end) {
404 to = strchr(from, ';');
405 if (to == NULL) to = end;
406 decode_address (&sig, from, to - from);
407 pass_signals[(int)sig] = 1;
408 dlog(1, "pass_signal %d\n", (int)sig);
409 from = to;
410 if (*from == ';') from++;
411 }
412 write_ok (arg_own_buf);
413 return;
414 }
415 /* Otherwise we didn't know what packet it was. Say we didn't
416 understand it. */
417 arg_own_buf[0] = 0;
418}
419
420/* Handle all of the extended 'q' packets. */
421static
422void handle_query (char *arg_own_buf, int *new_packet_len_p)
423{
424 static struct inferior_list_entry *thread_ptr;
425
426 /* qRcmd, monitor command handling. */
427 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
428 char *p = arg_own_buf + 6;
429 int cmdlen = strlen(p)/2;
430 char cmd[cmdlen+1];
431
432 if (unhexify (cmd, p, cmdlen) != cmdlen) {
433 write_enn (arg_own_buf);
434 return;
435 }
436 cmd[cmdlen] = '\0';
437
438 if (handle_gdb_monitor_command (cmd)) {
439 /* In case the command is from a standalone vgdb,
440 connection will be closed soon => flush the output. */
441 VG_(message_flush) ();
442 write_ok (arg_own_buf);
443 return;
444 } else {
445 /* cmd not recognised */
446 VG_(gdb_printf)
447 ("command '%s' not recognised\n"
448 "In gdb, try 'monitor help'\n"
449 "In a shell, try 'vgdb help'\n",
450 cmd);
451 write_ok (arg_own_buf);
452 return;
453 }
454 }
455
456 /* provide some valgrind specific info in return to qThreadExtraInfo. */
457 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
458 unsigned long gdb_id;
459 struct thread_info *ti;
460 ThreadState *tst;
461 char status[100];
462
463 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
464 ti = gdb_id_to_thread (gdb_id);
465 if (ti != NULL) {
466 tst = (ThreadState *) inferior_target_data (ti);
467 /* Additional info is the tid and the thread status. */
468 VG_(snprintf) (status, sizeof(status), "tid %d %s",
469 tst->tid,
470 VG_(name_of_ThreadStatus)(tst->status));
471 hexify (arg_own_buf, status, strlen(status));
472 return;
473 } else {
474 write_enn (arg_own_buf);
475 return;
476 }
477 }
478
479 if (strcmp ("qAttached", arg_own_buf) == 0) {
480 /* tell gdb to always detach, never kill the process */
481 arg_own_buf[0] = '1';
482 arg_own_buf[1] = 0;
483 return;
484 }
485
486 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
487 /* We have no symbol to read. */
488 write_ok (arg_own_buf);
489 return;
490 }
491
492 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
493 thread_ptr = all_threads.head;
494 VG_(sprintf) (arg_own_buf, "m%x",
495 thread_to_gdb_id ((struct thread_info *)thread_ptr));
496 thread_ptr = thread_ptr->next;
497 return;
498 }
499
500 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
501 if (thread_ptr != NULL) {
502 VG_(sprintf) (arg_own_buf, "m%x",
503 thread_to_gdb_id ((struct thread_info *)thread_ptr));
504 thread_ptr = thread_ptr->next;
505 return;
506 } else {
507 VG_(sprintf) (arg_own_buf, "l");
508 return;
509 }
510 }
511
512 if ( ((*the_target->target_xml)() != NULL
513 || (*the_target->shadow_target_xml)() != NULL)
514 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
515 CORE_ADDR ofs;
516 unsigned int len, doc_len;
517 char *annex = NULL;
518 // First, the annex is extracted from the packet received.
519 // Then, it is replaced by the corresponding file name.
520 int fd;
521
522 /* Grab the annex, offset, and length. */
523 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
524 strcpy (arg_own_buf, "E00");
525 return;
526 }
527
528 if (strcmp (annex, "target.xml") == 0) {
529 annex = NULL; // to replace it by the corresponding filename.
530
531 /* If VG_(clo_vgdb_shadow_registers), try to use
532 shadow_target_xml. Fallback to target_xml
533 if not defined. */
534 if (VG_(clo_vgdb_shadow_registers)) {
535 annex = (*the_target->shadow_target_xml)();
536 if (annex != NULL)
537 /* Ensure the shadow registers are initialized. */
538 initialize_shadow_low(True);
539 }
540 if (annex == NULL)
541 annex = (*the_target->target_xml)();
542 if (annex == NULL) {
543 strcpy (arg_own_buf, "E00");
544 return;
545 }
546 }
547
548 {
549 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex)];
550 struct vg_stat stat_doc;
551 char toread[len];
552 int len_read;
553
554 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
555 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
556 if (fd == -1) {
557 strcpy (arg_own_buf, "E00");
558 return;
559 }
560 if (VG_(fstat) (fd, &stat_doc) != 0) {
561 VG_(close) (fd);
562 strcpy (arg_own_buf, "E00");
563 return;
564 }
565 doc_len = stat_doc.size;
566
567 if (len > PBUFSIZ - POVERHSIZ)
568 len = PBUFSIZ - POVERHSIZ;
569
570 if (ofs > doc_len) {
571 write_enn (arg_own_buf);
572 VG_(close) (fd);
573 return;
574 }
575 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
576 len_read = VG_(read) (fd, toread, len);
577 *new_packet_len_p = write_qxfer_response (arg_own_buf, toread,
578 len_read, ofs + len_read < doc_len);
579 VG_(close) (fd);
580 return;
581 }
582 }
583
sewardj2a312392011-06-26 09:26:48 +0000584 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
585 unsigned char *data;
586 int n;
587 CORE_ADDR ofs;
588 unsigned int len;
589 char *annex;
590
591 /* Reject any annex; grab the offset and length. */
592 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
593 || annex[0] != '\0') {
594 strcpy (arg_own_buf, "E00");
595 return;
596 }
597
598 if (len > PBUFSIZ - 2)
599 len = PBUFSIZ - 2;
600 data = malloc (len);
601
602 {
603 UWord *client_auxv = VG_(client_auxv);
604 unsigned int client_auxv_len = 0;
605 while (*client_auxv != 0) {
606 dlog(4, "auxv %lld %llx\n",
607 (ULong)*client_auxv,
608 (ULong)*(client_auxv+1));
609 client_auxv++;
610 client_auxv++;
611 client_auxv_len += 2 * sizeof(UWord);
612 }
613 client_auxv_len += 2 * sizeof(UWord);
614 dlog(4, "auxv len %d\n", client_auxv_len);
615
616 if (ofs >= client_auxv_len)
617 n = -1;
618 else {
619 n = client_auxv_len - ofs;
620 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
621 }
622 }
623
624 if (n < 0)
625 write_enn (arg_own_buf);
626 else if (n > len)
627 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
628 else
629 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
630
631 free (data);
632
633 return;
634 }
635
636
sewardj3b290482011-05-06 21:02:55 +0000637 /* Protocol features query. */
638 if (strncmp ("qSupported", arg_own_buf, 10) == 0
639 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
640 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
641 /* Note: max packet size including frame and checksum, but without
642 trailing null byte, which is not sent/received. */
643
644 strcat (arg_own_buf, ";QStartNoAckMode+");
645 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +0000646 if (VG_(client_auxv))
647 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +0000648
649 if ((*the_target->target_xml)() != NULL
650 || (*the_target->shadow_target_xml)() != NULL) {
651 strcat (arg_own_buf, ";qXfer:features:read+");
652 /* if a new gdb connects to us, we have to reset the register
653 set to the normal register sets to allow this new gdb to
654 decide to use or not the shadow registers.
655
656 Note that the reset is only done for gdb that are sending
657 qSupported packets. If a user first connected with a recent
658 gdb using shadow registers and then with a very old gdb
659 that does not use qSupported packet, then the old gdb will
660 not properly connect. */
661 initialize_shadow_low(False);
662 }
663 return;
664 }
665
666 /* Otherwise we didn't know what packet it was. Say we didn't
667 understand it. */
668 arg_own_buf[0] = 0;
669}
670
671/* Handle all of the extended 'v' packets. */
672static
673void handle_v_requests (char *arg_own_buf, char *status, int *signal)
674{
675 /* vcont packet code from gdb 6.6 removed */
676
677 /* Otherwise we didn't know what packet it was. Say we didn't
678 understand it. */
679 arg_own_buf[0] = 0;
680 return;
681}
682
683static
684void myresume (int step, int sig)
685{
686 struct thread_resume resume_info[2];
687 int n = 0;
688
689 if (step || sig || (cont_thread != 0 && cont_thread != -1)) {
690 resume_info[0].thread
691 = ((struct inferior_list_entry *) current_inferior)->id;
692 resume_info[0].step = step;
693 resume_info[0].sig = sig;
694 resume_info[0].leave_stopped = 0;
695 n++;
696 }
697 resume_info[n].thread = -1;
698 resume_info[n].step = 0;
699 resume_info[n].sig = 0;
700 resume_info[n].leave_stopped = (cont_thread != 0 && cont_thread != -1);
701
702 resume_packet_needed = True;
703 (*the_target->resume) (resume_info);
704}
705
706/* server_main global variables */
707static char *own_buf;
708static unsigned char *mem_buf;
709
710void gdbserver_init (void)
711{
712 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
713 noack_mode = False;
714 initialize_low ();
715 own_buf = malloc (PBUFSIZ);
716 mem_buf = malloc (PBUFSIZ);
717}
718
719void gdbserver_terminate (void)
720{
721 /* last call to gdbserver is cleanup call */
722 if (VG_MINIMAL_SETJMP(toplevel)) {
723 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
724 return;
725 }
726 remote_close();
727}
728
729void server_main (void)
730{
731 static char status;
732 static int signal;
733
734 char ch;
735 int i = 0;
736 unsigned int len;
737 CORE_ADDR mem_addr;
738
739 signal = mywait (&status);
740 if (VG_MINIMAL_SETJMP(toplevel)) {
741 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
742 }
743 while (1) {
744 unsigned char sig;
745 int packet_len;
746 int new_packet_len = -1;
747
748 if (resume_packet_needed) {
749 resume_packet_needed = False;
750 prepare_resume_reply (own_buf, status, signal);
751 putpkt (own_buf);
752 }
753
754 packet_len = getpkt (own_buf);
755 if (packet_len <= 0)
756 break;
757
758 i = 0;
759 ch = own_buf[i++];
760 switch (ch) {
761 case 'Q':
762 handle_set (own_buf, &new_packet_len);
763 break;
764 case 'q':
765 handle_query (own_buf, &new_packet_len);
766 break;
767 case 'd':
768 /* set/unset debugging is done through valgrind debug level. */
769 own_buf[0] = '\0';
770 break;
771 case 'D':
772 reset_valgrind_sink("gdb detaching from process");
773
774 /* When detaching or kill the process, gdb expects to get
775 an packet OK back. Any other output will make gdb
776 believes detach did not work. */
777 write_ok (own_buf);
778 putpkt (own_buf);
779 remote_finish (reset_after_error);
780 remote_open (VG_(clo_vgdb_prefix));
781 myresume (0, 0);
782 resume_packet_needed = False;
783 return;
784 case '!':
785 /* We can not use the extended protocol with valgrind,
786 because we can not restart the running
787 program. So return unrecognized. */
788 own_buf[0] = '\0';
789 break;
790 case '?':
791 prepare_resume_reply (own_buf, status, signal);
792 break;
793 case 'H':
794 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
795 unsigned long gdb_id, thread_id;
796
797 gdb_id = strtoul (&own_buf[2], NULL, 16);
798 thread_id = gdb_id_to_thread_id (gdb_id);
799 if (thread_id == 0) {
800 write_enn (own_buf);
801 break;
802 }
803
804 if (own_buf[1] == 'g') {
805 general_thread = thread_id;
806 set_desired_inferior (1);
807 } else if (own_buf[1] == 'c') {
808 cont_thread = thread_id;
809 } else if (own_buf[1] == 's') {
810 step_thread = thread_id;
811 }
812
813 write_ok (own_buf);
814 } else {
815 /* Silently ignore it so that gdb can extend the protocol
816 without compatibility headaches. */
817 own_buf[0] = '\0';
818 }
819 break;
820 case 'g':
821 set_desired_inferior (1);
822 registers_to_string (own_buf);
823 break;
824 case 'G':
825 set_desired_inferior (1);
826 registers_from_string (&own_buf[1]);
827 write_ok (own_buf);
828 break;
829 case 'P': {
830 int regno;
831 char *regbytes;
832 Bool mod;
833 ThreadState *tst;
834 regno = strtol(&own_buf[1], NULL, 16);
835 regbytes = strchr(&own_buf[0], '=') + 1;
836 set_desired_inferior (1);
837 tst = (ThreadState *) inferior_target_data (current_inferior);
838 /* Only accept changing registers in "runnable state3.
839 In fact, it would be ok to change most of the registers
840 except a few "sensitive" registers such as the PC, SP, BP.
841 We assume we do not need to very specific here, and that we
842 can just refuse all of these. */
843 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
844 supply_register_from_string (regno, regbytes, &mod);
845 write_ok (own_buf);
846 } else {
847 /* at least from gdb 6.6 onwards, an E. error
848 reply is shown to the user. So, we do an error
849 msg which both is accepted by gdb as an error msg
850 and is readable by the user. */
851 VG_(sprintf)
852 (own_buf,
853"E.\n"
854"ERROR changing register %s regno %d\n"
855"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
856"set pc, calling from gdb a function in the debugged process, ...)\n"
857"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
858"Thread status is %s\n",
859 find_register_by_number (regno)->name, regno,
860 VG_(name_of_ThreadStatus)(tst->status));
861 if (VG_(clo_verbosity) > 1)
862 VG_(umsg) ("%s\n", own_buf);
863 }
864 break;
865 }
866 case 'm':
867 decode_m_packet (&own_buf[1], &mem_addr, &len);
868 if (read_inferior_memory (mem_addr, mem_buf, len) == 0)
869 convert_int_to_ascii (mem_buf, own_buf, len);
870 else
871 write_enn (own_buf);
872 break;
873 case 'M':
874 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
875 if (write_inferior_memory (mem_addr, mem_buf, len) == 0)
876 write_ok (own_buf);
877 else
878 write_enn (own_buf);
879 break;
880 case 'X':
881 if (decode_X_packet (&own_buf[1], packet_len - 1,
882 &mem_addr, &len, mem_buf) < 0
883 || write_inferior_memory (mem_addr, mem_buf, len) != 0)
884 write_enn (own_buf);
885 else
886 write_ok (own_buf);
887 break;
888 case 'C':
889 convert_ascii_to_int (own_buf + 1, &sig, 1);
890 if (target_signal_to_host_p (sig))
891 signal = target_signal_to_host (sig);
892 else
893 signal = 0;
894 set_desired_inferior (0);
895 myresume (0, signal);
896 return; // return control to valgrind
897 case 'S':
898 convert_ascii_to_int (own_buf + 1, &sig, 1);
899 if (target_signal_to_host_p (sig))
900 signal = target_signal_to_host (sig);
901 else
902 signal = 0;
903 set_desired_inferior (0);
904 myresume (1, signal);
905 return; // return control to valgrind
906 case 'c':
907 set_desired_inferior (0);
908 myresume (0, 0);
909 return; // return control to valgrind
910 case 's':
911 set_desired_inferior (0);
912 myresume (1, 0);
913 return; // return control to valgrind
914 case 'Z': {
915 char *lenptr;
916 char *dataptr;
917 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
918 int zlen = strtol (lenptr + 1, &dataptr, 16);
919 char type = own_buf[1];
920
921 if (the_target->insert_watchpoint == NULL
922 || (type < '0' || type > '4')) {
923 /* No watchpoint support or not a watchpoint command;
924 unrecognized either way. */
925 own_buf[0] = '\0';
926 } else {
927 int res;
928
929 res = (*the_target->insert_watchpoint) (type, addr, zlen);
930 if (res == 0)
931 write_ok (own_buf);
932 else if (res == 1)
933 /* Unsupported. */
934 own_buf[0] = '\0';
935 else
936 write_enn (own_buf);
937 }
938 break;
939 }
940 case 'z': {
941 char *lenptr;
942 char *dataptr;
943 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
944 int zlen = strtol (lenptr + 1, &dataptr, 16);
945 char type = own_buf[1];
946
947 if (the_target->remove_watchpoint == NULL
948 || (type < '0' || type > '4')) {
949 /* No watchpoint support or not a watchpoint command;
950 unrecognized either way. */
951 own_buf[0] = '\0';
952 } else {
953 int res;
954
955 res = (*the_target->remove_watchpoint) (type, addr, zlen);
956 if (res == 0)
957 write_ok (own_buf);
958 else if (res == 1)
959 /* Unsupported. */
960 own_buf[0] = '\0';
961 else
962 write_enn (own_buf);
963 }
964 break;
965 }
966 case 'k':
967 kill_request("Gdb request to kill this process\n");
968 break;
969 case 'T': {
970 unsigned long gdb_id, thread_id;
971
972 gdb_id = strtoul (&own_buf[1], NULL, 16);
973 thread_id = gdb_id_to_thread_id (gdb_id);
974 if (thread_id == 0) {
975 write_enn (own_buf);
976 break;
977 }
978
979 if (mythread_alive (thread_id))
980 write_ok (own_buf);
981 else
982 write_enn (own_buf);
983 break;
984 }
985 case 'R':
986 /* Restarting the inferior is only supported in the
987 extended protocol.
988 => It is a request we don't understand. Respond with an
989 empty packet so that gdb knows that we don't support this
990 request. */
991 own_buf[0] = '\0';
992 break;
993 case 'v':
994 /* Extended (long) request. */
995 handle_v_requests (own_buf, &status, &signal);
996 break;
997 default:
998 /* It is a request we don't understand. Respond with an
999 empty packet so that gdb knows that we don't support this
1000 request. */
1001 own_buf[0] = '\0';
1002 break;
1003 }
1004
1005 if (new_packet_len != -1)
1006 putpkt_binary (own_buf, new_packet_len);
1007 else
1008 putpkt (own_buf);
1009
1010 if (status == 'W')
1011 VG_(umsg) ("\nChild exited with status %d\n", signal);
1012 if (status == 'X')
1013 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
1014 target_signal_to_host (signal),
1015 target_signal_to_name (signal));
1016 if (status == 'W' || status == 'X') {
1017 VG_(umsg) ("Process exiting\n");
1018 VG_(exit) (0);
1019 }
1020 }
1021
1022 /* We come here when getpkt fails => close the connection,
1023 and re-open. Then return control to valgrind.
1024 We return the control to valgrind as we assume that
1025 the connection was closed due to vgdb having finished
1026 to execute a command. */
1027 if (VG_(clo_verbosity) > 1)
1028 VG_(umsg) ("Remote side has terminated connection. "
1029 "GDBserver will reopen the connection.\n");
1030 remote_finish (reset_after_error);
1031 remote_open (VG_(clo_vgdb_prefix));
1032 myresume (0, 0);
1033 resume_packet_needed = False;
1034 return;
1035}