FreeBSD regtest: add fakes for older versions in scalar
[valgrind.git] / coregrind / m_gdbserver / server.c
blob8ca4de20f6db28d52506a35188a5eb22c6d9ff16
1 /* 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.
6 This file is part of GDB.
7 It has been modified to integrate it in valgrind
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.
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.
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. */
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"
29 #include "pub_core_initimg.h"
30 #include "pub_core_execontext.h"
31 #include "pub_core_syswrap.h" // VG_(show_open_fds)
32 #include "pub_core_scheduler.h"
33 #include "pub_core_transtab.h"
34 #include "pub_core_debuginfo.h"
35 #include "pub_core_addrinfo.h"
36 #include "pub_core_aspacemgr.h"
38 unsigned long cont_thread;
39 unsigned long general_thread;
40 unsigned long step_thread;
41 unsigned long thread_from_wait;
42 unsigned long old_thread_from_wait;
44 int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
46 /* for a gdbserver integrated in valgrind, resuming the process consists
47 in returning the control to valgrind.
48 The guess process resumes its execution.
49 Then at the next error or break or ..., valgrind calls gdbserver again.
50 A resume reply packet must then be built to inform GDB that the
51 resume request is finished.
52 resume_reply_packet_needed records the fact that the next call to gdbserver
53 must send a resume packet to gdb. */
54 static Bool resume_reply_packet_needed = False;
56 VG_MINIMAL_JMP_BUF(toplevel);
58 /* Decode a qXfer read request. Return 0 if everything looks OK,
59 or -1 otherwise. */
61 static
62 int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len)
64 /* Extract and NUL-terminate the annex. */
65 *annex = buf;
66 while (*buf && *buf != ':')
67 buf++;
68 if (*buf == '\0')
69 return -1;
70 *buf++ = 0;
72 /* After the read/write marker and annex, qXfer looks like a
73 traditional 'm' packet. */
74 decode_m_packet (buf, ofs, len);
76 return 0;
79 /* Write the response to a successful qXfer read. Returns the
80 length of the (binary) data stored in BUF, corresponding
81 to as much of DATA/LEN as we could fit. IS_MORE controls
82 the first character of the response. */
83 static
84 int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
86 int out_len;
88 if (is_more)
89 buf[0] = 'm';
90 else
91 buf[0] = 'l';
93 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
94 PBUFSIZ - POVERHSIZ - 1) + 1;
97 static Bool initial_valgrind_sink_saved = False;
98 /* True <=> valgrind log sink saved in initial_valgrind_sink */
99 static OutputSink initial_valgrind_sink;
101 static Bool command_output_to_log = False;
102 /* True <=> command output goes to log instead of gdb */
104 void reset_valgrind_sink(const char *info)
106 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
107 && initial_valgrind_sink_saved) {
108 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
109 VG_(umsg) ("Reset valgrind output to log (%s)\n",
110 (info == NULL ? "" : info));
114 void print_to_initial_valgrind_sink (const char *msg)
116 vg_assert (initial_valgrind_sink_saved);
117 VG_(write) (initial_valgrind_sink.fd, msg, strlen(msg));
121 static
122 void kill_request (const char *msg)
124 VG_(umsg) ("%s", msg);
125 VG_(exit) (0);
128 // s is a NULL terminated string made of O or more words (separated by spaces).
129 // Returns a pointer to the Nth word in s.
130 // If Nth word does not exist, return a pointer to the last (0) byte of s.
131 static
132 const char *wordn (const char *s, int n)
134 int word_seen = 0;
135 Bool searching_word = True;
137 while (*s) {
138 if (*s == ' ')
139 searching_word = True;
140 else {
141 if (searching_word) {
142 searching_word = False;
143 word_seen++;
144 if (word_seen == n)
145 return s;
148 s++;
150 return s;
153 void VG_(print_all_stats) (Bool memory_stats, Bool tool_stats)
155 if (memory_stats) {
156 VG_(message)(Vg_DebugMsg, "\n");
157 VG_(message)(Vg_DebugMsg,
158 "------ Valgrind's internal memory use stats follow ------\n" );
159 VG_(sanity_check_malloc_all)();
160 VG_(message)
161 (Vg_DebugMsg,
162 "------ %'13llu bytes have already been mmap-ed ANONYMOUS.\n",
163 VG_(am_get_anonsize_total)());
164 VG_(print_all_arena_stats)();
165 if (VG_(clo_profile_heap))
166 VG_(print_arena_cc_analysis) ();
167 VG_(message)(Vg_DebugMsg, "\n");
170 VG_(print_translation_stats)();
171 VG_(print_tt_tc_stats)();
172 VG_(print_scheduler_stats)();
173 VG_(print_ExeContext_stats)( False /* with_stacktraces */ );
174 VG_(print_errormgr_stats)();
175 if (tool_stats && VG_(needs).print_stats) {
176 VG_TDICT_CALL(tool_print_stats);
180 /* handle_gdb_valgrind_command handles the provided mon string command.
181 If command is recognised, return 1 else return 0.
182 Note that in case of ambiguous command, 1 is returned.
184 *sink_wanted_at_return is modified if one of the commands
185 'v.set *_output' is handled.
187 static
188 int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return)
190 UWord ret = 0;
191 char s[strlen(mon)+1]; /* copy for strtok_r */
192 char *wcmd;
193 HChar *ssaveptr;
194 const char *endptr;
195 int kwdid;
196 int int_value;
198 const DiEpoch cur_ep = VG_(current_DiEpoch)();
200 vg_assert (initial_valgrind_sink_saved);
202 strcpy (s, mon);
203 wcmd = strtok_r (s, " ", &ssaveptr);
204 /* NB: if possible, avoid introducing a new command below which
205 starts with the same 3 first letters as an already existing
206 command. This ensures a shorter abbreviation for the user. */
207 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate"
208 " v.do v.clo",
209 wcmd, kwd_report_duplicated_matches)) {
210 case -2:
211 ret = 1;
212 break;
213 case -1:
214 break;
215 case 0: /* help */
216 ret = 1;
217 wcmd = strtok_r (NULL, " ", &ssaveptr);
218 if (wcmd == NULL) {
219 int_value = 0;
220 } else {
221 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
222 case -2: int_value = 0; break;
223 case -1: int_value = 0; break;
224 case 0: int_value = 1; break;
225 default: vg_assert (0);
229 VG_(gdb_printf) (
230 "general valgrind monitor commands:\n"
231 " help [debug] : monitor command help. With debug: + debugging commands\n"
232 " v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
233 " v.info all_errors [also_suppressed] : show all errors found so far\n"
234 " v.info last_error : show last error found\n"
235 " v.info location <addr> : show information about location <addr>\n"
236 " v.info n_errs_found [msg] : show the nr of errors found so far and the given msg\n"
237 " v.info open_fds : show open file descriptors (only if --track-fds=[yes|all])\n"
238 " v.kill : kill the Valgrind process\n"
239 " v.clo <clo_option>... : changes one or more dynamic command line options\n"
240 " with no clo_option, show the dynamically changeable options.\n"
241 " v.set gdb_output : set valgrind output to gdb\n"
242 " v.set log_output : set valgrind output to log\n"
243 " v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
244 " v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n"
245 " v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
246 if (int_value) { VG_(gdb_printf) (
247 "debugging valgrind internals monitor commands:\n"
248 " v.do expensive_sanity_check_general : do an expensive sanity check now\n"
249 " v.info gdbserver_status : show gdbserver status\n"
250 " v.info memory [aspacemgr] : show valgrind heap memory stats\n"
251 " (with aspacemgr arg, also shows valgrind segments on log output)\n"
252 " v.info exectxt : show stacktraces and stats of all execontexts\n"
253 " v.info scheduler : show valgrind thread state and stacktrace\n"
254 " v.info stats : show various valgrind and tool stats\n"
255 " v.info unwind <addr> [<len>] : show unwind debug info for <addr> .. <addr+len>\n"
256 " v.set debuglog <level> : set valgrind debug log level to <level>\n"
257 " v.set hostvisibility [yes*|no] : (en/dis)ables access by gdb/gdbserver to\n"
258 " Valgrind internal host status/memory\n"
259 " v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
260 " (default traceflags 0b00100000 : show after instrumentation)\n"
261 " An additional flag 0b100000000 allows one to show gdbserver instrumentation\n");
263 break;
264 case 1: /* v.set */
265 ret = 1;
266 wcmd = strtok_r (NULL, " ", &ssaveptr);
267 switch (kwdid = VG_(keyword_id)
268 ("vgdb-error debuglog merge-recursive-frames"
269 " gdb_output log_output mixed_output hostvisibility",
270 wcmd, kwd_report_all)) {
271 case -2:
272 case -1:
273 break;
274 case 0: /* vgdb-error */
275 case 1: /* debuglog */
276 case 2: /* merge-recursive-frames */
277 wcmd = strtok_r (NULL, " ", &ssaveptr);
278 if (wcmd == NULL) {
279 int_value = 0;
280 endptr = "empty"; /* to report an error below */
281 } else {
282 HChar *the_end;
283 int_value = strtol (wcmd, &the_end, 10);
284 endptr = the_end;
286 if (*endptr != '\0') {
287 VG_(gdb_printf) ("missing or malformed integer value\n");
288 } else if (kwdid == 0) {
289 VG_(printf) ("vgdb-error value changed from %d to %d\n",
290 VG_(clo_vgdb_error), int_value);
291 VG_(clo_vgdb_error) = int_value;
292 } else if (kwdid == 1) {
293 VG_(printf) ("debuglog value changed from %d to %d\n",
294 VG_(debugLog_getLevel)(), int_value);
295 VG_(debugLog_startup) (int_value, "gdbsrv");
296 } else if (kwdid == 2) {
297 VG_(printf)
298 ("merge-recursive-frames value changed from %d to %d\n",
299 VG_(clo_merge_recursive_frames), int_value);
300 VG_(clo_merge_recursive_frames) = int_value;
301 } else {
302 vg_assert (0);
304 break;
305 case 3: /* gdb_output */
306 (*sink_wanted_at_return).fd = -2;
307 command_output_to_log = False;
308 VG_(gdb_printf) ("valgrind output will go to gdb\n");
309 break;
310 case 4: /* log_output */
311 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
312 command_output_to_log = True;
313 VG_(gdb_printf) ("valgrind output will go to log\n");
314 break;
315 case 5: /* mixed output */
316 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
317 command_output_to_log = False;
318 VG_(gdb_printf)
319 ("valgrind output will go to log, "
320 "interactive output will go to gdb\n");
321 break;
322 case 6: /* hostvisibility */
323 wcmd = strtok_r (NULL, " ", &ssaveptr);
324 if (wcmd != NULL) {
325 switch (VG_(keyword_id) ("yes no", wcmd, kwd_report_all)) {
326 case -2:
327 case -1: break;
328 case 0:
329 hostvisibility = True;
330 break;
331 case 1:
332 hostvisibility = False;
333 break;
334 default: vg_assert (0);
336 } else {
337 hostvisibility = True;
339 if (hostvisibility) {
340 const DebugInfo *tooldi
341 = VG_(find_DebugInfo) (cur_ep, (Addr)handle_gdb_valgrind_command);
342 /* Normally, we should always find the tooldi. In case we
343 do not, suggest a 'likely somewhat working' address: */
344 const Addr tool_text_start
345 = tooldi ?
346 VG_(DebugInfo_get_text_avma) (tooldi) : 0x58000000;
347 const NSegment *toolseg
348 = tooldi ?
349 VG_(am_find_nsegment) (VG_(DebugInfo_get_text_avma) (tooldi))
350 : NULL;
351 VG_(gdb_printf)
352 ("Enabled access to Valgrind memory/status by GDB\n"
353 "If not yet done, tell GDB which valgrind file(s) to use, "
354 "typically:\n"
355 "add-symbol-file %s %p\n",
356 toolseg ? VG_(am_get_filename)(toolseg)
357 : "<toolfile> <address> e.g.",
358 (void*)tool_text_start);
359 } else
360 VG_(gdb_printf)
361 ("Disabled access to Valgrind memory/status by GDB\n");
362 break;
363 default:
364 vg_assert (0);
366 break;
367 case 2: /* v.info */ {
368 ret = 1;
369 wcmd = strtok_r (NULL, " ", &ssaveptr);
370 switch (kwdid = VG_(keyword_id)
371 ("all_errors n_errs_found last_error gdbserver_status memory"
372 " scheduler stats open_fds exectxt location unwind",
373 wcmd, kwd_report_all)) {
374 case -2:
375 case -1:
376 break;
377 case 0: // all_errors
379 Int show_error_list = 1;
380 wcmd = strtok_r (NULL, " ", &ssaveptr);
381 if (wcmd != NULL) {
382 switch (VG_(keyword_id) ("also_suppressed", wcmd, kwd_report_all)) {
383 case -2:
384 case -1: break;
385 case 0:
386 show_error_list = 2;
387 break;
388 default: vg_assert (0);
391 // A verbosity of minimum 2 is needed to show the errors.
392 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False, show_error_list);
394 break;
395 case 1: // n_errs_found
396 VG_(printf) ("n_errs_found %u n_errs_shown %u (vgdb-error %d) %s\n",
397 VG_(get_n_errs_found) (),
398 VG_(get_n_errs_shown) (),
399 VG_(clo_vgdb_error),
400 wordn (mon, 3));
401 break;
402 case 2: // last_error
403 VG_(show_last_error)();
404 break;
405 case 3: // gdbserver_status
406 VG_(gdbserver_status_output)();
407 break;
408 case 4: /* memory */
409 VG_(printf) ("%'13llu bytes have already been mmap-ed ANONYMOUS.\n",
410 VG_(am_get_anonsize_total)());
411 VG_(print_all_arena_stats) ();
412 if (VG_(clo_profile_heap))
413 VG_(print_arena_cc_analysis) ();
414 wcmd = strtok_r (NULL, " ", &ssaveptr);
415 if (wcmd != NULL) {
416 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) {
417 case -2:
418 case -1: break;
419 case 0:
420 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr");
421 break;
422 default: vg_assert (0);
426 ret = 1;
427 break;
428 case 5: /* scheduler */
429 VG_(show_sched_status) (True, // host_stacktrace
430 True, // stack_usage
431 True); // exited_threads
432 ret = 1;
433 break;
434 case 6: /* stats */
435 VG_(print_all_stats)(False, /* Memory stats */
436 True /* Tool stats */);
437 ret = 1;
438 break;
439 case 7: /* open_fds */
440 if (VG_(clo_track_fds))
441 VG_(show_open_fds) ("");
442 else
443 VG_(gdb_printf)
444 ("Valgrind must be started with --track-fds=[yes|all]"
445 " to show open fds\n");
446 ret = 1;
447 break;
448 case 8: /* exectxt */
449 VG_(print_ExeContext_stats) (True /* with_stacktraces */);
450 ret = 1;
451 break;
452 case 9: { /* location */
453 /* Note: we prefer 'v.info location' and not 'v.info address' as
454 v.info address is inconsistent with the GDB (native)
455 command 'info address' which gives the address for a symbol.
456 GDB equivalent command of 'v.info location' is 'info symbol'. */
457 Addr address;
458 SizeT dummy_sz = 0x1234;
459 if (VG_(strtok_get_address_and_size) (&address,
460 &dummy_sz, &ssaveptr)) {
461 // If tool provides location information, use that.
462 if (VG_(needs).info_location) {
463 VG_TDICT_CALL(tool_info_location, cur_ep, address);
465 // If tool does not provide location info, use the common one.
466 // Also use the common to compare with tool when debug log is set.
467 if (!VG_(needs).info_location || VG_(debugLog_getLevel)() > 0 ) {
468 AddrInfo ai;
469 ai.tag = Addr_Undescribed;
470 VG_(describe_addr) (cur_ep, address, &ai);
471 VG_(pp_addrinfo) (address, &ai);
472 VG_(clear_addrinfo) (&ai);
475 ret = 1;
476 break;
478 case 10: { /* unwind */
479 Addr address;
480 SizeT sz = 1;
481 if (VG_(strtok_get_address_and_size) (&address,
482 &sz, &ssaveptr)) {
483 VG_(ppUnwindInfo) (address, address + sz - 1);
485 ret = 1;
486 break;
489 default:
490 vg_assert(0);
492 break;
494 case 3: /* v.wait */
495 wcmd = strtok_r (NULL, " ", &ssaveptr);
496 if (wcmd != NULL) {
497 int_value = strtol (wcmd, NULL, 10);
498 VG_(printf) ("gdbserver: continuing in %d ms ...\n", int_value);
499 VG_(poll)(NULL, 0, int_value);
501 VG_(printf) ("gdbserver: continuing after wait ...\n");
502 ret = 1;
503 break;
504 case 4: /* v.kill */
505 kill_request ("monitor command request to kill this process\n");
506 break;
507 case 5: { /* v.translate */
508 Addr address;
509 SizeT verbosity = 0x20;
511 ret = 1;
513 if (VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr)) {
514 /* we need to force the output to log for the translation trace,
515 as low level VEX tracing cannot be redirected to gdb. */
516 int saved_command_output_to_log = command_output_to_log;
517 int saved_fd = VG_(log_output_sink).fd;
518 Bool single_stepping_on_entry = valgrind_single_stepping();
519 int vex_verbosity = verbosity & 0xff;
520 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
521 if ((verbosity & 0x100) && !single_stepping_on_entry) {
522 valgrind_set_single_stepping(True);
523 // to force gdbserver instrumentation.
525 # if defined(VGA_arm)
526 // on arm, we need to (potentially) convert this address
527 // to the thumb form.
528 address = thumb_pc (address);
529 # endif
531 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
532 address,
533 /*debugging*/True,
534 (Int) vex_verbosity,
535 /*bbs_done*/0,
536 /*allow redir?*/True);
537 if ((verbosity & 0x100) && !single_stepping_on_entry) {
538 valgrind_set_single_stepping(False);
539 // reset single stepping.
541 command_output_to_log = saved_command_output_to_log;
542 VG_(log_output_sink).fd = saved_fd;
544 break;
547 case 6: /* v.do */
548 ret = 1;
549 wcmd = strtok_r (NULL, " ", &ssaveptr);
550 switch (VG_(keyword_id) ("expensive_sanity_check_general",
551 wcmd, kwd_report_all)) {
552 case -2:
553 case -1: break;
554 case 0: { /* expensive_sanity_check_general */
555 // Temporarily bump up sanity level to check e.g. the malloc arenas.
556 const Int save_clo_sanity_level = VG_(clo_sanity_level);
557 if (VG_(clo_sanity_level) < 4) VG_(clo_sanity_level) = 4;
558 VG_(sanity_check_general) (/* force_expensive */ True);
559 VG_(clo_sanity_level) = save_clo_sanity_level;
560 break;
562 default: vg_assert (0);
564 break;
566 case 7: /* v.clo */
567 ret = 0;
568 for (wcmd = VG_(strtok_r)(NULL, " ", &ssaveptr);
569 wcmd != NULL;
570 wcmd = VG_(strtok_r)(NULL, " ", &ssaveptr)) {
571 ret++;
572 VG_(process_dynamic_option) (cloD, wcmd);
574 if (ret == 0)
575 VG_(list_dynamic_options) ();
576 ret = 1;
577 break;
579 default:
580 vg_assert (0);
582 return ret;
585 /* handle_gdb_monitor_command handles the provided mon string command,
586 which can be either a "standard" valgrind monitor command
587 or a tool specific monitor command.
588 If command recognised, return 1 else return 0.
589 Note that in case of ambiguous command, 1 is returned.
591 static
592 int handle_gdb_monitor_command (char *mon)
594 UWord ret = 0;
595 UWord tool_ret = 0;
596 // initially, we assume that when returning, the desired sink is the
597 // one we have when entering. It can however be changed by the standard
598 // valgrind command handling.
599 OutputSink sink_wanted_at_return = VG_(log_output_sink);
600 // When using gdbserver, we temporarily disable xml output.
601 Bool save_clo_xml = VG_(clo_xml);
602 VG_(clo_xml) = False;
604 if (!initial_valgrind_sink_saved) {
605 /* first time we enter here, we save the valgrind default log sink */
606 initial_valgrind_sink = sink_wanted_at_return;
607 initial_valgrind_sink_saved = True;
610 if (!command_output_to_log)
611 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
613 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
615 /* Even if command was recognised by valgrind core, we call the
616 tool command handler : this is needed to handle help command
617 and/or to let the tool do some additional processing of a
618 valgrind standard command. Note however that if valgrind
619 recognised the command, we will always return success. */
620 if (VG_(needs).client_requests) {
621 /* If the tool reports an error when handling a monitor command,
622 we need to avoid calling gdbserver during this command
623 handling. So, we temporarily set VG_(clo_vgdb_error) to
624 a huge value to ensure m_errormgr.c does not call gdbserver. */
625 Int save_clo_vgdb_error = VG_(clo_vgdb_error);
626 UWord arg[2];
627 VG_(clo_vgdb_error) = 999999999;
628 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
629 arg[1] = (UWord) mon;
630 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
631 &tool_ret);
632 VG_(clo_vgdb_error) = save_clo_vgdb_error;
635 VG_(message_flush) ();
637 /* restore or set the desired output */
638 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
639 VG_(clo_xml) = save_clo_xml;
641 if (ret | tool_ret)
642 return 1;
643 else
644 return 0;
648 /* Handle all of the extended 'Q' packets. */
649 static
650 void handle_set (char *arg_own_buf, int *new_packet_len_p)
652 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
653 noack_mode = True;
654 write_ok (arg_own_buf);
655 return;
658 if (strcmp ("QCatchSyscalls:0", arg_own_buf) == 0) {
659 dlog (3, "catch syscall all off\n");
660 catching_syscalls = False;
661 write_ok (arg_own_buf);
662 return;
665 const char *q1 = "QCatchSyscalls:1";
666 if (strncmp (q1, arg_own_buf, strlen(q1)) == 0) {
667 Int i;
668 const char *p;
670 if (syscalls_to_catch != NULL) {
671 free (syscalls_to_catch);
672 syscalls_to_catch = NULL;
674 syscalls_to_catch_size = 0;
675 p = arg_own_buf + strlen(q1);
676 while (*p) {
677 if (*p++ == ';')
678 syscalls_to_catch_size++;
680 if (syscalls_to_catch_size > 0) {
681 CORE_ADDR sysno;
682 char *from, *to;
684 syscalls_to_catch = malloc (syscalls_to_catch_size * sizeof (int));
686 from = strchr (arg_own_buf, ';') + 1;
687 for (i = 0; i < syscalls_to_catch_size; i++) {
688 to = strchr (from, ';');
689 if (to == NULL)
690 to = arg_own_buf + strlen (arg_own_buf);
691 decode_address (&sysno, from, to - from);
692 syscalls_to_catch[i] = (Int)sysno;
693 dlog(4, "catch syscall sysno %d\n", (int)sysno);
694 from = to;
695 if (*from == ';') from++;
697 } else
698 dlog (4, "catch syscall all sysno\n");
699 catching_syscalls = True;
700 write_ok (arg_own_buf);
701 return;
704 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
705 int i;
706 char *from, *to;
707 char *end = arg_own_buf + strlen(arg_own_buf);
708 CORE_ADDR sig;
709 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
710 pass_signals[i] = 0;
712 from = arg_own_buf + 13;
713 while (from < end) {
714 to = strchr(from, ';');
715 if (to == NULL) to = end;
716 decode_address (&sig, from, to - from);
717 pass_signals[(int)sig] = 1;
718 dlog(3, "pass_signal gdb_nr %d %s\n",
719 (int)sig, target_signal_to_name(sig));
720 from = to;
721 if (*from == ';') from++;
723 write_ok (arg_own_buf);
724 return;
726 /* Otherwise we didn't know what packet it was. Say we didn't
727 understand it. */
728 arg_own_buf[0] = 0;
731 Bool VG_(client_monitor_command) (HChar *cmd)
733 const Bool connected = remote_connected();
734 const int saved_command_output_to_log = command_output_to_log;
735 Bool handled;
737 if (!connected)
738 command_output_to_log = True;
739 handled = handle_gdb_monitor_command (cmd);
740 if (!connected) {
741 // reset the log output unless cmd changed it.
742 if (command_output_to_log)
743 command_output_to_log = saved_command_output_to_log;
745 if (handled)
746 return False; // recognised
747 else
748 return True; // not recognised
751 /* Handle all of the extended 'q' packets. */
752 static
753 void handle_query (char *arg_own_buf, int *new_packet_len_p)
755 static struct inferior_list_entry *thread_ptr;
757 /* thread local storage query */
758 if (strncmp ("qGetTLSAddr:", arg_own_buf, 12) == 0) {
759 char *from, *to;
760 char *end = arg_own_buf + strlen(arg_own_buf);
761 unsigned long gdb_id;
762 CORE_ADDR lm;
763 CORE_ADDR offset;
764 struct thread_info *ti;
766 from = arg_own_buf + 12;
767 to = strchr(from, ',');
768 *to = 0;
769 gdb_id = strtoul (from, NULL, 16);
770 from = to + 1;
771 to = strchr(from, ',');
772 decode_address (&offset, from, to - from);
773 from = to + 1;
774 to = end;
775 decode_address (&lm, from, to - from);
776 dlog(2, "qGetTLSAddr thread %lu offset %p lm %p\n",
777 gdb_id, (void*)offset, (void*)lm);
779 ti = gdb_id_to_thread (gdb_id);
780 if (ti != NULL) {
781 ThreadState *tst;
782 Addr tls_addr;
784 tst = (ThreadState *) inferior_target_data (ti);
785 if (valgrind_get_tls_addr(tst, offset, lm, &tls_addr)) {
786 VG_(sprintf) (arg_own_buf, "%lx", tls_addr);
787 return;
789 // else we will report we do not support qGetTLSAddr
790 } else {
791 write_enn (arg_own_buf);
792 return;
796 /* qRcmd, monitor command handling. */
797 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
798 char *p = arg_own_buf + 6;
799 int cmdlen = strlen(p)/2;
800 char cmd[cmdlen+1];
802 if (unhexify (cmd, p, cmdlen) != cmdlen) {
803 write_enn (arg_own_buf);
804 return;
806 cmd[cmdlen] = '\0';
808 if (handle_gdb_monitor_command (cmd)) {
809 write_ok (arg_own_buf);
810 return;
811 } else {
812 /* cmd not recognised */
813 VG_(gdb_printf)
814 ("command '%s' not recognised\n"
815 "In gdb, try 'monitor help'\n"
816 "In a shell, try 'vgdb help'\n",
817 cmd);
818 write_ok (arg_own_buf);
819 return;
823 /* provide some valgrind specific info in return to qThreadExtraInfo. */
824 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
825 unsigned long gdb_id;
826 struct thread_info *ti;
827 ThreadState *tst;
829 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
830 ti = gdb_id_to_thread (gdb_id);
831 if (ti != NULL) {
832 tst = (ThreadState *) inferior_target_data (ti);
833 /* Additional info is the tid, the thread status and the thread's
834 name, if any. */
835 SizeT len = strlen(VG_(name_of_ThreadStatus)(tst->status)) + 20;
836 if (tst->thread_name) len += strlen(tst->thread_name);
837 /* As the string will be hexified and copied into own_buf we need
838 to limit the length to avoid buffer overflow. */
839 if (len * 2 > (PBUFSIZ + POVERHSIZ))
840 len = (PBUFSIZ + POVERHSIZ) / 2;
841 char status[len];
842 if (tst->thread_name) {
843 VG_(snprintf) (status, sizeof(status), "tid %u %s %s",
844 tst->tid,
845 VG_(name_of_ThreadStatus)(tst->status),
846 tst->thread_name);
847 } else {
848 VG_(snprintf) (status, sizeof(status), "tid %u %s",
849 tst->tid,
850 VG_(name_of_ThreadStatus)(tst->status));
852 hexify (arg_own_buf, status, strlen(status));
853 return;
854 } else {
855 write_enn (arg_own_buf);
856 return;
860 /* Without argument, traditional remote protocol. */
861 if (strcmp ("qAttached", arg_own_buf) == 0) {
862 /* tell gdb to always detach, never kill the process */
863 arg_own_buf[0] = '1';
864 arg_own_buf[1] = 0;
865 return;
868 /* With argument, extended-remote protocol. */
869 if (strncmp ("qAttached:", arg_own_buf, strlen ("qAttached:")) == 0) {
870 /* We just created this process */
871 arg_own_buf[0] = '0';
872 arg_own_buf[1] = 0;
873 return;
876 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
877 /* We have no symbol to read. */
878 write_ok (arg_own_buf);
879 return;
882 if (strcmp ("qC", arg_own_buf) == 0) {
883 VG_(sprintf) (arg_own_buf, "QC%x",
884 thread_to_gdb_id (current_inferior));
885 return;
888 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
889 thread_ptr = all_threads.head;
890 VG_(sprintf) (arg_own_buf, "m%x",
891 thread_to_gdb_id ((struct thread_info *)thread_ptr));
892 thread_ptr = thread_ptr->next;
893 return;
896 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
897 if (thread_ptr != NULL) {
898 VG_(sprintf) (arg_own_buf, "m%x",
899 thread_to_gdb_id ((struct thread_info *)thread_ptr));
900 thread_ptr = thread_ptr->next;
901 return;
902 } else {
903 VG_(sprintf) (arg_own_buf, "l");
904 return;
908 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
909 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
910 CORE_ADDR ofs;
911 unsigned int len, doc_len;
912 const char *annex = NULL;
913 // First, the annex is extracted from the packet received.
914 // Then, it is replaced by the corresponding file name.
915 int fd;
917 /* Grab the annex, offset, and length. */
918 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
919 strcpy (arg_own_buf, "E00");
920 return;
923 if (strcmp (annex, "target.xml") == 0) {
924 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers));
925 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) {
926 /* Ensure the shadow registers are initialized. */
927 initialize_shadow_low(True);
929 if (annex == NULL) {
930 strcpy (arg_own_buf, "E00");
931 return;
936 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
937 struct vg_stat stat_doc;
938 char toread[len];
939 int len_read;
941 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
942 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
943 if (fd == -1) {
944 strcpy (arg_own_buf, "E00");
945 return;
947 if (VG_(fstat) (fd, &stat_doc) != 0) {
948 VG_(close) (fd);
949 strcpy (arg_own_buf, "E00");
950 return;
952 doc_len = stat_doc.size;
954 if (len > PBUFSIZ - POVERHSIZ)
955 len = PBUFSIZ - POVERHSIZ;
957 if (ofs > doc_len) {
958 write_enn (arg_own_buf);
959 VG_(close) (fd);
960 return;
962 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
963 len_read = VG_(read) (fd, toread, len);
964 *new_packet_len_p = write_qxfer_response (arg_own_buf,
965 (unsigned char *)toread,
966 len_read,
967 ofs + len_read < doc_len);
968 VG_(close) (fd);
969 return;
973 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
974 unsigned char *data;
975 int n;
976 CORE_ADDR ofs;
977 unsigned int len;
978 const char *annex;
980 /* Reject any annex; grab the offset and length. */
981 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
982 || annex[0] != '\0') {
983 strcpy (arg_own_buf, "E00");
984 return;
987 if (len > PBUFSIZ - POVERHSIZ)
988 len = PBUFSIZ - POVERHSIZ;
989 data = malloc (len);
992 UWord *client_auxv = VG_(client_auxv);
993 unsigned int client_auxv_len = 0;
994 while (*client_auxv != 0) {
995 dlog(4, "auxv %llu %llx\n",
996 (ULong)*client_auxv,
997 (ULong)*(client_auxv+1));
998 client_auxv++;
999 client_auxv++;
1000 client_auxv_len += 2 * sizeof(UWord);
1002 client_auxv_len += 2 * sizeof(UWord);
1003 dlog(4, "auxv len %u\n", client_auxv_len);
1005 if (ofs >= client_auxv_len)
1006 n = -1;
1007 else {
1008 n = client_auxv_len - ofs;
1009 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
1013 if (n < 0)
1014 write_enn (arg_own_buf);
1015 else if (n > len)
1016 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
1017 else
1018 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
1020 free (data);
1022 return;
1025 if (strncmp ("qXfer:exec-file:read:", arg_own_buf, 21) == 0) {
1026 unsigned char *data;
1027 int n;
1028 CORE_ADDR ofs;
1029 unsigned int len;
1030 const char *annex;
1031 unsigned long pid;
1032 const HChar *name;
1034 /* grab the annex, offset and length. */
1035 if (decode_xfer_read (arg_own_buf + 21, &annex, &ofs, &len) < 0) {
1036 strcpy (arg_own_buf, "E00");
1037 return;
1040 /* Reject any annex with invalid/unexpected pid */
1041 if (strlen(annex) > 0)
1042 pid = strtoul (annex, NULL, 16);
1043 else
1044 pid = 0;
1045 if ((int)pid != VG_(getpid)() && pid != 0) {
1046 VG_(sprintf) (arg_own_buf,
1047 "E.Valgrind gdbserver pid is %d."
1048 " Cannot give info for pid %d",
1049 VG_(getpid)(), (int) pid);
1050 return;
1053 if (len > PBUFSIZ - 2)
1054 len = PBUFSIZ - 2;
1055 data = malloc (len);
1057 if (!VG_(resolve_filename)(VG_(cl_exec_fd), &name)) {
1058 VG_(sprintf) (arg_own_buf,
1059 "E.Valgrind gdbserver could not"
1060 " resolve pid %d exec filename.",
1061 VG_(getpid)());
1062 return;
1065 if (ofs >= strlen(name))
1066 n = -1;
1067 else {
1068 n = strlen(name) - ofs;
1069 VG_(memcpy) (data, name, n);
1072 if (n < 0)
1073 write_enn (arg_own_buf);
1074 else if (n > len)
1075 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
1076 else
1077 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
1079 free (data);
1081 return;
1084 if (strncmp ("qXfer:siginfo:read:", arg_own_buf, 19) == 0) {
1085 vki_siginfo_t info;
1086 int n;
1087 CORE_ADDR ofs;
1088 unsigned int len;
1089 const char *annex;
1091 /* Reject any annex; grab the offset and length. */
1092 if (decode_xfer_read (arg_own_buf + 19, &annex, &ofs, &len) < 0
1093 || annex[0] != '\0') {
1094 strcpy (arg_own_buf, "E00");
1095 return;
1098 if (len > PBUFSIZ - POVERHSIZ)
1099 len = PBUFSIZ - POVERHSIZ;
1101 gdbserver_pending_signal_to_report(&info);
1103 if (ofs >= sizeof(info))
1104 n = -1;
1105 else
1106 n = sizeof(info) - ofs;
1108 if (n < 0)
1109 write_enn (arg_own_buf);
1110 else if (n > len)
1111 *new_packet_len_p = write_qxfer_response (arg_own_buf,
1112 (unsigned char *)&info,
1113 len, 1);
1114 else
1115 *new_packet_len_p = write_qxfer_response (arg_own_buf,
1116 (unsigned char *)&info,
1117 n, 0);
1119 return;
1122 /* Protocol features query. Keep this in sync with coregind/vgdb.c. */
1123 if (strncmp ("qSupported", arg_own_buf, 10) == 0
1124 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
1125 VG_(sprintf) (arg_own_buf, "PacketSize=%x", (UInt)PBUFSIZ - 1);
1126 /* Note: max packet size including frame and checksum, but without
1127 trailing null byte, which is not sent/received. */
1129 strcat (arg_own_buf, ";QStartNoAckMode+");
1130 strcat (arg_own_buf, ";QPassSignals+");
1131 strcat (arg_own_buf, ";QCatchSyscalls+");
1132 if (VG_(client_auxv))
1133 strcat (arg_own_buf, ";qXfer:auxv:read+");
1135 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
1136 strcat (arg_own_buf, ";qXfer:features:read+");
1137 /* if a new gdb connects to us, we have to reset the register
1138 set to the normal register sets to allow this new gdb to
1139 decide to use or not the shadow registers.
1141 Note that the reset is only done for gdb that are sending
1142 qSupported packets. If a user first connected with a recent
1143 gdb using shadow registers and then with a very old gdb
1144 that does not use qSupported packet, then the old gdb will
1145 not properly connect. */
1146 initialize_shadow_low(False);
1148 strcat (arg_own_buf, ";qXfer:exec-file:read+");
1149 strcat (arg_own_buf, ";qXfer:siginfo:read+");
1150 return;
1153 /* Otherwise we didn't know what packet it was. Say we didn't
1154 understand it. */
1155 arg_own_buf[0] = 0;
1158 /* Handle all of the extended 'v' packets. */
1159 static
1160 void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
1162 /* vcont packet code from gdb 6.6 removed */
1164 /* Otherwise we didn't know what packet it was. Say we didn't
1165 understand it. */
1166 arg_own_buf[0] = 0;
1167 return;
1170 static
1171 void myresume (int step, int sig)
1173 struct thread_resume resume_info[2];
1174 int n = 0;
1176 if (step || sig) {
1177 resume_info[0].step = step;
1178 resume_info[0].sig = sig;
1179 n++;
1181 resume_info[n].step = 0;
1182 resume_info[n].sig = 0;
1184 resume_reply_packet_needed = True;
1185 valgrind_resume (resume_info);
1188 /* server_main global variables */
1189 static char *own_buf;
1190 static unsigned char *mem_buf;
1192 void gdbserver_init (void)
1194 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
1195 noack_mode = False;
1196 valgrind_initialize_target ();
1197 // After a fork, gdbserver_init can be called again.
1198 // We do not have to re-malloc the buffers in such a case.
1199 if (own_buf == NULL)
1200 own_buf = malloc (PBUFSIZ+POVERHSIZ);
1201 if (mem_buf == NULL)
1202 mem_buf = malloc (PBUFSIZ+POVERHSIZ);
1203 // Note: normally, we should only malloc PBUFSIZ. However,
1204 // GDB has a bug, and in some cases, sends e.g. 'm' packets
1205 // asking for slightly more than the PacketSize given at
1206 // connection initialisation. So, we bypass the GDB bug
1207 // by allocating slightly more.
1210 void gdbserver_terminate (void)
1212 /* last call to gdbserver is cleanup call */
1213 if (VG_MINIMAL_SETJMP(toplevel)) {
1214 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
1215 return;
1217 remote_close();
1220 void server_main (void)
1222 static char status;
1223 static int zignal;
1225 char ch;
1226 int i = 0;
1227 unsigned int len;
1228 CORE_ADDR mem_addr;
1230 zignal = valgrind_wait (&status);
1231 if (VG_MINIMAL_SETJMP(toplevel)) {
1232 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
1234 while (1) {
1235 unsigned char sig;
1236 int packet_len;
1237 int new_packet_len = -1;
1239 if (resume_reply_packet_needed) {
1240 /* Send the resume reply to reply to last GDB resume
1241 request. */
1242 resume_reply_packet_needed = False;
1243 prepare_resume_reply (own_buf, status, zignal);
1244 putpkt (own_buf);
1247 /* If our status is terminal (exit or fatal signal) get out
1248 as quickly as we can. We won't be able to handle any request
1249 anymore. */
1250 if (status == 'W' || status == 'X') {
1251 return;
1254 packet_len = getpkt (own_buf);
1255 if (packet_len <= 0)
1256 break;
1258 i = 0;
1259 ch = own_buf[i++];
1260 switch (ch) {
1261 case 'Q':
1262 handle_set (own_buf, &new_packet_len);
1263 break;
1264 case 'q':
1265 handle_query (own_buf, &new_packet_len);
1266 break;
1267 case 'd':
1268 /* set/unset debugging is done through valgrind debug level. */
1269 own_buf[0] = '\0';
1270 break;
1271 case 'D':
1272 reset_valgrind_sink("gdb detaching from process");
1274 /* When detaching or kill the process, gdb expects to get
1275 an packet OK back. Any other output will make gdb
1276 believes detach did not work. */
1277 write_ok (own_buf);
1278 putpkt (own_buf);
1279 remote_finish (reset_after_error);
1280 remote_open (VG_(clo_vgdb_prefix));
1281 myresume (0, 0);
1282 resume_reply_packet_needed = False;
1283 return;
1284 case '!':
1285 /* We can not use the extended protocol with valgrind,
1286 because we can not restart the running
1287 program. So return unrecognized. */
1288 own_buf[0] = '\0';
1289 break;
1290 case '?':
1291 prepare_resume_reply (own_buf, status, zignal);
1292 break;
1293 case 'H':
1294 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
1295 unsigned long gdb_id, thread_id;
1297 gdb_id = strtoul (&own_buf[2], NULL, 16);
1298 thread_id = gdb_id_to_thread_id (gdb_id);
1299 if (thread_id == 0) {
1300 write_enn (own_buf);
1301 break;
1304 if (own_buf[1] == 'g') {
1305 general_thread = thread_id;
1306 set_desired_inferior (1);
1307 } else if (own_buf[1] == 'c') {
1308 cont_thread = thread_id;
1309 } else if (own_buf[1] == 's') {
1310 step_thread = thread_id;
1313 write_ok (own_buf);
1314 } else {
1315 /* Silently ignore it so that gdb can extend the protocol
1316 without compatibility headaches. */
1317 own_buf[0] = '\0';
1319 break;
1320 case 'g':
1321 set_desired_inferior (1);
1322 registers_to_string (own_buf);
1323 break;
1324 case 'G':
1325 set_desired_inferior (1);
1326 registers_from_string (&own_buf[1]);
1327 write_ok (own_buf);
1328 break;
1329 case 'P': {
1330 int regno;
1331 char *regbytes;
1332 ThreadState *tst;
1333 regno = strtol(&own_buf[1], NULL, 16);
1334 regbytes = strchr(&own_buf[0], '=') + 1;
1335 set_desired_inferior (1);
1336 tst = (ThreadState *) inferior_target_data (current_inferior);
1337 /* Only accept changing registers in "runnable state3.
1338 In fact, it would be ok to change most of the registers
1339 except a few "sensitive" registers such as the PC, SP, BP.
1340 We assume we do not need to very specific here, and that we
1341 can just refuse all of these. */
1342 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
1343 supply_register_from_string (regno, regbytes);
1344 write_ok (own_buf);
1345 } else {
1346 /* at least from gdb 6.6 onwards, an E. error
1347 reply is shown to the user. So, we do an error
1348 msg which both is accepted by gdb as an error msg
1349 and is readable by the user. */
1350 VG_(sprintf)
1351 (own_buf,
1352 "E.\n"
1353 "ERROR changing register %s regno %d\n"
1354 "gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
1355 "set pc, calling from gdb a function in the debugged process, ...)\n"
1356 "can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
1357 "Thread status is %s\n",
1358 find_register_by_number (regno)->name, regno,
1359 VG_(name_of_ThreadStatus)(tst->status));
1360 if (VG_(clo_verbosity) > 1)
1361 VG_(umsg) ("%s\n", own_buf);
1363 break;
1365 case 'm':
1366 decode_m_packet (&own_buf[1], &mem_addr, &len);
1367 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
1368 convert_int_to_ascii (mem_buf, own_buf, len);
1369 else
1370 write_enn (own_buf);
1371 break;
1372 case 'M':
1373 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
1374 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
1375 write_ok (own_buf);
1376 else
1377 write_enn (own_buf);
1378 break;
1379 case 'X':
1380 if (decode_X_packet (&own_buf[1], packet_len - 1,
1381 &mem_addr, &len, mem_buf) < 0
1382 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
1383 write_enn (own_buf);
1384 else
1385 write_ok (own_buf);
1386 break;
1387 case 'C':
1388 convert_ascii_to_int (own_buf + 1, &sig, 1);
1389 if (target_signal_to_host_p (sig))
1390 zignal = target_signal_to_host (sig);
1391 else
1392 zignal = 0;
1393 set_desired_inferior (0);
1394 myresume (0, zignal);
1395 return; // return control to valgrind
1396 case 'S':
1397 convert_ascii_to_int (own_buf + 1, &sig, 1);
1398 if (target_signal_to_host_p (sig))
1399 zignal = target_signal_to_host (sig);
1400 else
1401 zignal = 0;
1402 set_desired_inferior (0);
1403 myresume (1, zignal);
1404 return; // return control to valgrind
1405 case 'c':
1406 set_desired_inferior (0);
1407 myresume (0, 0);
1408 return; // return control to valgrind
1409 case 's':
1410 set_desired_inferior (0);
1411 myresume (1, 0);
1412 return; // return control to valgrind
1413 case 'Z': {
1414 char *lenptr;
1415 char *dataptr;
1416 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1417 int zlen = strtol (lenptr + 1, &dataptr, 16);
1418 char type = own_buf[1];
1420 if (type < '0' || type > '4') {
1421 /* Watchpoint command type unrecognized. */
1422 own_buf[0] = '\0';
1423 } else {
1424 int res;
1426 res = valgrind_insert_watchpoint (type, addr, zlen);
1427 if (res == 0)
1428 write_ok (own_buf);
1429 else if (res == 1)
1430 /* Unsupported. */
1431 own_buf[0] = '\0';
1432 else
1433 write_enn (own_buf);
1435 break;
1437 case 'z': {
1438 char *lenptr;
1439 char *dataptr;
1440 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1441 int zlen = strtol (lenptr + 1, &dataptr, 16);
1442 char type = own_buf[1];
1444 if (type < '0' || type > '4') {
1445 /* Watchpoint command type unrecognized. */
1446 own_buf[0] = '\0';
1447 } else {
1448 int res;
1450 res = valgrind_remove_watchpoint (type, addr, zlen);
1451 if (res == 0)
1452 write_ok (own_buf);
1453 else if (res == 1)
1454 /* Unsupported. */
1455 own_buf[0] = '\0';
1456 else
1457 write_enn (own_buf);
1459 break;
1461 case 'k':
1462 kill_request("Gdb request to kill this process\n");
1463 break;
1464 case 'T': {
1465 unsigned long gdb_id, thread_id;
1467 gdb_id = strtoul (&own_buf[1], NULL, 16);
1468 thread_id = gdb_id_to_thread_id (gdb_id);
1469 if (thread_id == 0) {
1470 write_enn (own_buf);
1471 break;
1474 if (valgrind_thread_alive (thread_id))
1475 write_ok (own_buf);
1476 else
1477 write_enn (own_buf);
1478 break;
1480 case 'R':
1481 /* Restarting the inferior is only supported in the
1482 extended protocol.
1483 => It is a request we don't understand. Respond with an
1484 empty packet so that gdb knows that we don't support this
1485 request. */
1486 own_buf[0] = '\0';
1487 break;
1488 case 'v':
1489 /* Extended (long) request. */
1490 handle_v_requests (own_buf, &status, &zignal);
1491 break;
1492 default:
1493 /* It is a request we don't understand. Respond with an
1494 empty packet so that gdb knows that we don't support this
1495 request. */
1496 own_buf[0] = '\0';
1497 break;
1500 if (new_packet_len != -1)
1501 putpkt_binary (own_buf, new_packet_len);
1502 else
1503 putpkt (own_buf);
1505 if (status == 'W')
1506 VG_(umsg) ("\nChild exited with status %d\n", zignal);
1507 if (status == 'X')
1508 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
1509 (UInt)target_signal_to_host (zignal),
1510 target_signal_to_name (zignal));
1511 if (status == 'W' || status == 'X') {
1512 VG_(umsg) ("Process exiting\n");
1513 VG_(exit) (0);
1517 /* We come here when getpkt fails => close the connection,
1518 and re-open. Then return control to valgrind.
1519 We return the control to valgrind as we assume that
1520 the connection was closed due to vgdb having finished
1521 to execute a command. */
1522 if (VG_(clo_verbosity) > 1)
1523 VG_(umsg) ("Remote side has terminated connection. "
1524 "GDBserver will reopen the connection.\n");
1525 remote_finish (reset_after_error);
1526 remote_open (VG_(clo_vgdb_prefix));
1527 myresume (0, 0);
1528 resume_reply_packet_needed = False;
1529 return;