blob: 46abb7cfa690f5fb9ea4c92e106dc1953ae0a777 [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"
philippef3a6e932013-01-10 20:42:51 +000030#include "pub_core_execontext.h"
floriandbb35842012-10-27 18:39:11 +000031#include "pub_core_syswrap.h" // VG_(show_open_fds)
philippe66e4c732013-01-26 16:45:01 +000032#include "pub_core_scheduler.h"
sewardj3b290482011-05-06 21:02:55 +000033
34unsigned long cont_thread;
35unsigned long general_thread;
36unsigned long step_thread;
37unsigned long thread_from_wait;
38unsigned long old_thread_from_wait;
39
philippe886fde32012-03-29 21:56:47 +000040int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
bart2dafc542011-05-18 16:08:28 +000041
sewardj3b290482011-05-06 21:02:55 +000042/* for a gdbserver integrated in valgrind, resuming the process consists
43 in returning the control to valgrind.
philippe349a3912012-05-23 21:50:36 +000044 The guess process resumes its execution.
sewardj3b290482011-05-06 21:02:55 +000045 Then at the next error or break or ..., valgrind calls gdbserver again.
philippe349a3912012-05-23 21:50:36 +000046 A resume reply packet must then be built to inform GDB that the
47 resume request is finished.
48 resume_reply_packet_needed records the fact that the next call to gdbserver
sewardj3b290482011-05-06 21:02:55 +000049 must send a resume packet to gdb. */
philippe349a3912012-05-23 21:50:36 +000050static Bool resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +000051
52VG_MINIMAL_JMP_BUF(toplevel);
53
54/* Decode a qXfer read request. Return 0 if everything looks OK,
55 or -1 otherwise. */
56
57static
florian6bd9dc12012-11-23 16:17:43 +000058int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len)
sewardj3b290482011-05-06 21:02:55 +000059{
60 /* Extract and NUL-terminate the annex. */
61 *annex = buf;
62 while (*buf && *buf != ':')
63 buf++;
64 if (*buf == '\0')
65 return -1;
66 *buf++ = 0;
67
68 /* After the read/write marker and annex, qXfer looks like a
69 traditional 'm' packet. */
70 decode_m_packet (buf, ofs, len);
71
72 return 0;
73}
74
75/* Write the response to a successful qXfer read. Returns the
76 length of the (binary) data stored in BUF, corresponding
77 to as much of DATA/LEN as we could fit. IS_MORE controls
78 the first character of the response. */
79static
80int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
81{
82 int out_len;
83
84 if (is_more)
85 buf[0] = 'm';
86 else
87 buf[0] = 'l';
88
89 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
90 PBUFSIZ - POVERHSIZ - 1) + 1;
91}
92
93static Bool initial_valgrind_sink_saved = False;
94/* True <=> valgrind log sink saved in initial_valgrind_sink */
95static OutputSink initial_valgrind_sink;
96
97static Bool command_output_to_log = False;
98/* True <=> command output goes to log instead of gdb */
99
florian6bd9dc12012-11-23 16:17:43 +0000100void reset_valgrind_sink(const char *info)
sewardj3b290482011-05-06 21:02:55 +0000101{
102 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
103 && initial_valgrind_sink_saved) {
104 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
105 VG_(umsg) ("Reset valgrind output to log (%s)\n",
106 (info = NULL ? "" : info));
107 }
108}
109
philippe46207652013-01-20 17:11:58 +0000110void print_to_initial_valgrind_sink (const char *msg)
111{
112 vg_assert (initial_valgrind_sink_saved);
113 VG_(write) (initial_valgrind_sink.fd, msg, strlen(msg));
114}
115
116
sewardj3b290482011-05-06 21:02:55 +0000117static
florian6bd9dc12012-11-23 16:17:43 +0000118void kill_request (const char *msg)
sewardj3b290482011-05-06 21:02:55 +0000119{
120 VG_(umsg) ("%s", msg);
121 remote_close();
122 VG_(exit) (0);
123}
124
philippe02ea4132013-09-04 21:42:43 +0000125// s is a NULL terminated string made of O or more words (separated by spaces).
126// Returns a pointer to the Nth word in s.
127// If Nth word does not exist, return a pointer to the last (0) byte of s.
128static
129const char *wordn (const char *s, int n)
130{
131 int word_seen = 0;
132 Bool searching_word = True;
133
134 while (*s) {
135 if (*s == ' ')
136 searching_word = True;
137 else {
138 if (searching_word) {
139 searching_word = False;
140 word_seen++;
141 if (word_seen == n)
142 return s;
143 }
144 }
145 s++;
146 }
147 return s;
148}
149
sewardj3b290482011-05-06 21:02:55 +0000150/* handle_gdb_valgrind_command handles the provided mon string command.
151 If command is recognised, return 1 else return 0.
152 Note that in case of ambiguous command, 1 is returned.
153
154 *sink_wanted_at_return is modified if one of the commands
sewardj30b3eca2011-06-28 08:20:39 +0000155 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000156*/
157static
philippe02ea4132013-09-04 21:42:43 +0000158int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return)
sewardj3b290482011-05-06 21:02:55 +0000159{
160 UWord ret = 0;
161 char s[strlen(mon)+1]; /* copy for strtok_r */
philippe02ea4132013-09-04 21:42:43 +0000162 char *wcmd;
163 HChar *ssaveptr;
164 const char *endptr;
sewardj3b290482011-05-06 21:02:55 +0000165 int kwdid;
166 int int_value;
167
168 vg_assert (initial_valgrind_sink_saved);
169
170 strcpy (s, mon);
171 wcmd = strtok_r (s, " ", &ssaveptr);
172 /* NB: if possible, avoid introducing a new command below which
sewardj30b3eca2011-06-28 08:20:39 +0000173 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000174 command. This ensures a shorter abbreviation for the user. */
philippe6ec8d632013-01-23 22:10:28 +0000175 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate"
176 " v.do",
sewardj3b290482011-05-06 21:02:55 +0000177 wcmd, kwd_report_duplicated_matches)) {
178 case -2:
179 ret = 1;
180 break;
181 case -1:
182 break;
183 case 0: /* help */
184 ret = 1;
185 wcmd = strtok_r (NULL, " ", &ssaveptr);
186 if (wcmd == NULL) {
187 int_value = 0;
188 } else {
189 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
190 case -2: int_value = 0; break;
191 case -1: int_value = 0; break;
192 case 0: int_value = 1; break;
193 default: tl_assert (0);
194 }
195 }
196
197 VG_(gdb_printf) (
198"general valgrind monitor commands:\n"
philippec3360382012-10-21 14:37:14 +0000199" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000200" v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
201" v.info all_errors : show all errors found so far\n"
202" v.info last_error : show last error found\n"
philippe02ea4132013-09-04 21:42:43 +0000203" v.info n_errs_found [msg] : show the nr of errors found so far and the given msg\n"
philippec3360382012-10-21 14:37:14 +0000204" v.info open_fds : show open file descriptors (only if --track-fds=yes)\n"
sewardj30b3eca2011-06-28 08:20:39 +0000205" v.kill : kill the Valgrind process\n"
206" v.set gdb_output : set valgrind output to gdb\n"
207" v.set log_output : set valgrind output to log\n"
208" v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
philippe46207652013-01-20 17:11:58 +0000209" v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n"
sewardj30b3eca2011-06-28 08:20:39 +0000210" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000211 if (int_value) { VG_(gdb_printf) (
212"debugging valgrind internals monitor commands:\n"
philippe6ec8d632013-01-23 22:10:28 +0000213" v.do expensive_sanity_check_general : do an expensive sanity check now\n"
sewardj30b3eca2011-06-28 08:20:39 +0000214" v.info gdbserver_status : show gdbserver status\n"
philippe93a6a8d2012-04-27 22:59:43 +0000215" v.info memory [aspacemgr] : show valgrind heap memory stats\n"
216" (with aspacemgr arg, also shows valgrind segments on log ouput)\n"
philippef3a6e932013-01-10 20:42:51 +0000217" v.info exectxt : show stacktraces and stats of all execontexts\n"
sewardjd6e13d82011-10-22 20:23:30 +0000218" v.info scheduler : show valgrind thread state and stacktrace\n"
sewardj30b3eca2011-06-28 08:20:39 +0000219" v.set debuglog <level> : set valgrind debug log level to <level>\n"
220" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000221" (default traceflags 0b00100000 : show after instrumentation)\n"
222" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
223 }
224 break;
sewardj30b3eca2011-06-28 08:20:39 +0000225 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000226 ret = 1;
227 wcmd = strtok_r (NULL, " ", &ssaveptr);
228 switch (kwdid = VG_(keyword_id)
philippe46207652013-01-20 17:11:58 +0000229 ("vgdb-error debuglog merge-recursive-frames"
230 " gdb_output log_output mixed_output",
sewardj3b290482011-05-06 21:02:55 +0000231 wcmd, kwd_report_all)) {
232 case -2:
233 case -1:
234 break;
235 case 0: /* vgdb-error */
236 case 1: /* debuglog */
philippe46207652013-01-20 17:11:58 +0000237 case 2: /* merge-recursive-frames */
sewardj3b290482011-05-06 21:02:55 +0000238 wcmd = strtok_r (NULL, " ", &ssaveptr);
239 if (wcmd == NULL) {
240 int_value = 0;
241 endptr = "empty"; /* to report an error below */
242 } else {
florian6bd9dc12012-11-23 16:17:43 +0000243 HChar *the_end;
244 int_value = strtol (wcmd, &the_end, 10);
245 endptr = the_end;
sewardj3b290482011-05-06 21:02:55 +0000246 }
247 if (*endptr != '\0') {
248 VG_(gdb_printf) ("missing or malformed integer value\n");
249 } else if (kwdid == 0) {
philippe02ea4132013-09-04 21:42:43 +0000250 VG_(printf) ("vgdb-error value changed from %d to %d\n",
sewardj3b290482011-05-06 21:02:55 +0000251 VG_(dyn_vgdb_error), int_value);
252 VG_(dyn_vgdb_error) = int_value;
253 } else if (kwdid == 1) {
philippe02ea4132013-09-04 21:42:43 +0000254 VG_(printf) ("debuglog value changed from %d to %d\n",
sewardj3b290482011-05-06 21:02:55 +0000255 VG_(debugLog_getLevel)(), int_value);
256 VG_(debugLog_startup) (int_value, "gdbsrv");
philippe46207652013-01-20 17:11:58 +0000257 } else if (kwdid == 2) {
philippe02ea4132013-09-04 21:42:43 +0000258 VG_(printf)
philippe46207652013-01-20 17:11:58 +0000259 ("merge-recursive-frames value changed from %d to %d\n",
260 VG_(clo_merge_recursive_frames), int_value);
261 VG_(clo_merge_recursive_frames) = int_value;
sewardj3b290482011-05-06 21:02:55 +0000262 } else {
263 vg_assert (0);
264 }
265 break;
philippe46207652013-01-20 17:11:58 +0000266 case 3: /* gdb_output */
sewardj3b290482011-05-06 21:02:55 +0000267 (*sink_wanted_at_return).fd = -2;
268 command_output_to_log = False;
269 VG_(gdb_printf) ("valgrind output will go to gdb\n");
270 break;
philippe46207652013-01-20 17:11:58 +0000271 case 4: /* log_output */
sewardj3b290482011-05-06 21:02:55 +0000272 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
273 command_output_to_log = True;
274 VG_(gdb_printf) ("valgrind output will go to log\n");
275 break;
philippe46207652013-01-20 17:11:58 +0000276 case 5: /* mixed output */
sewardj3b290482011-05-06 21:02:55 +0000277 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
278 command_output_to_log = False;
279 VG_(gdb_printf)
280 ("valgrind output will go to log, interactive output will go to gdb\n");
281 break;
282 default:
283 vg_assert (0);
284 }
285 break;
sewardj30b3eca2011-06-28 08:20:39 +0000286 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000287 ret = 1;
288 wcmd = strtok_r (NULL, " ", &ssaveptr);
289 switch (kwdid = VG_(keyword_id)
sewardjd6e13d82011-10-22 20:23:30 +0000290 ("all_errors n_errs_found last_error gdbserver_status memory"
philippef3a6e932013-01-10 20:42:51 +0000291 " scheduler open_fds exectxt",
sewardj3b290482011-05-06 21:02:55 +0000292 wcmd, kwd_report_all)) {
293 case -2:
294 case -1:
295 break;
296 case 0: // all_errors
297 // A verbosity of minimum 2 is needed to show the errors.
298 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
299 break;
300 case 1: // n_errs_found
philippe02ea4132013-09-04 21:42:43 +0000301 VG_(printf) ("n_errs_found %d n_errs_shown %d (vgdb-error %d) %s\n",
302 VG_(get_n_errs_found) (),
303 VG_(get_n_errs_shown) (),
304 VG_(dyn_vgdb_error),
305 wordn (mon, 3));
sewardj3b290482011-05-06 21:02:55 +0000306 break;
307 case 2: // last_error
308 VG_(show_last_error)();
309 break;
310 case 3: // gdbserver_status
311 VG_(gdbserver_status_output)();
312 break;
313 case 4: /* memory */
314 VG_(print_all_arena_stats) ();
315 if (VG_(clo_profile_heap))
316 VG_(print_arena_cc_analysis) ();
philippe93a6a8d2012-04-27 22:59:43 +0000317 wcmd = strtok_r (NULL, " ", &ssaveptr);
318 if (wcmd != NULL) {
319 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) {
320 case -2:
321 case -1: break;
322 case 0:
323 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr");
324 break;
325 default: tl_assert (0);
326 }
327 }
328
sewardj3b290482011-05-06 21:02:55 +0000329 ret = 1;
330 break;
sewardjd6e13d82011-10-22 20:23:30 +0000331 case 5: /* scheduler */
332 VG_(show_sched_status) ();
333 ret = 1;
334 break;
philippec3360382012-10-21 14:37:14 +0000335 case 6: /* open_fds */
336 if (VG_(clo_track_fds))
337 VG_(show_open_fds) ("");
338 else
339 VG_(gdb_printf)
340 ("Valgrind must be started with --track-fds=yes"
341 " to show open fds\n");
342 ret = 1;
343 break;
philippef3a6e932013-01-10 20:42:51 +0000344 case 7: /* exectxt */
345 VG_(print_ExeContext_stats) (True /* with_stacktraces */);
346 ret = 1;
347 break;
sewardj3b290482011-05-06 21:02:55 +0000348 default:
349 vg_assert(0);
350 }
351 break;
352 }
sewardj30b3eca2011-06-28 08:20:39 +0000353 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000354 wcmd = strtok_r (NULL, " ", &ssaveptr);
355 if (wcmd != NULL) {
florian6bd9dc12012-11-23 16:17:43 +0000356 int_value = strtol (wcmd, NULL, 10);
philippe02ea4132013-09-04 21:42:43 +0000357 VG_(printf) ("gdbserver: continuing in %d ms ...\n", int_value);
sewardj3b290482011-05-06 21:02:55 +0000358 VG_(poll)(NULL, 0, int_value);
359 }
philippe02ea4132013-09-04 21:42:43 +0000360 VG_(printf) ("gdbserver: continuing after wait ...\n");
sewardj3b290482011-05-06 21:02:55 +0000361 ret = 1;
362 break;
sewardj30b3eca2011-06-28 08:20:39 +0000363 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000364 kill_request ("monitor command request to kill this process\n");
365 break;
sewardj30b3eca2011-06-28 08:20:39 +0000366 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000367 Addr address;
368 SizeT verbosity = 0x20;
369
370 ret = 1;
371
372 VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr);
373 if (address != (Addr) 0 || verbosity != 0) {
374 /* we need to force the output to log for the translation trace,
375 as low level VEX tracing cannot be redirected to gdb. */
376 int saved_command_output_to_log = command_output_to_log;
377 int saved_fd = VG_(log_output_sink).fd;
378 Bool single_stepping_on_entry = valgrind_single_stepping();
379 int vex_verbosity = verbosity & 0xff;
380 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
381 if ((verbosity & 0x100) && !single_stepping_on_entry) {
382 valgrind_set_single_stepping(True);
383 // to force gdbserver instrumentation.
384 }
sewardj99d61342011-05-17 16:35:11 +0000385# if defined(VGA_arm)
386 // on arm, we need to (potentially) convert this address
387 // to the thumb form.
388 address = thumb_pc (address);
389# endif
390
sewardj3b290482011-05-06 21:02:55 +0000391 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
392 address,
393 /*debugging*/True,
394 (Int) vex_verbosity,
395 /*bbs_done*/0,
396 /*allow redir?*/True);
397 if ((verbosity & 0x100) && !single_stepping_on_entry) {
398 valgrind_set_single_stepping(False);
399 // reset single stepping.
400 }
401 command_output_to_log = saved_command_output_to_log;
402 VG_(log_output_sink).fd = saved_fd;
403 }
404 break;
405 }
406
philippe6ec8d632013-01-23 22:10:28 +0000407 case 6: /* v.do */
408 ret = 1;
409 wcmd = strtok_r (NULL, " ", &ssaveptr);
410 switch (VG_(keyword_id) ("expensive_sanity_check_general",
411 wcmd, kwd_report_all)) {
412 case -2:
413 case -1: break;
414 case 0: { /* expensive_sanity_check_general */
415 // Temporarily bump up sanity level to check e.g. the malloc arenas.
416 const Int save_clo_sanity_level = VG_(clo_sanity_level);
417 if (VG_(clo_sanity_level) < 4) VG_(clo_sanity_level) = 4;
418 VG_(sanity_check_general) (/* force_expensive */ True);
419 VG_(clo_sanity_level) = save_clo_sanity_level;
420 break;
421 }
422 default: tl_assert (0);
423 }
424 break;
425
sewardj3b290482011-05-06 21:02:55 +0000426 default:
427 vg_assert (0);
428 }
429 return ret;
430}
431
432/* handle_gdb_monitor_command handles the provided mon string command,
433 which can be either a "standard" valgrind monitor command
434 or a tool specific monitor command.
435 If command recognised, return 1 else return 0.
436 Note that in case of ambiguous command, 1 is returned.
437*/
438static
philippe02ea4132013-09-04 21:42:43 +0000439int handle_gdb_monitor_command (char *mon)
sewardj3b290482011-05-06 21:02:55 +0000440{
441 UWord ret = 0;
442 UWord tool_ret = 0;
443 // initially, we assume that when returning, the desired sink is the
444 // one we have when entering. It can however be changed by the standard
445 // valgrind command handling.
446 OutputSink sink_wanted_at_return = VG_(log_output_sink);
447
448 if (!initial_valgrind_sink_saved) {
449 /* first time we enter here, we save the valgrind default log sink */
450 initial_valgrind_sink = sink_wanted_at_return;
451 initial_valgrind_sink_saved = True;
452 }
453
454 if (!command_output_to_log)
455 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
456
457 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
458
459 /* Even if command was recognised by valgrind core, we call the
460 tool command handler : this is needed to handle help command
461 and/or to let the tool do some additional processing of a
462 valgrind standard command. Note however that if valgrind
463 recognised the command, we will always return success. */
464 if (VG_(needs).client_requests) {
465 /* If the tool reports an error when handling a monitor command,
466 we need to avoid calling gdbserver during this command
467 handling. So, we temporarily set VG_(dyn_vgdb_error) to
468 a huge value to ensure m_errormgr.c does not call gdbserver. */
469 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
470 UWord arg[2];
471 VG_(dyn_vgdb_error) = 999999999;
472 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
473 arg[1] = (UWord) mon;
474 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
475 &tool_ret);
476 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
477 }
478
philippe02ea4132013-09-04 21:42:43 +0000479 VG_(message_flush) ();
480
sewardj3b290482011-05-06 21:02:55 +0000481 /* restore or set the desired output */
482 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
483 if (ret | tool_ret)
484 return 1;
485 else
486 return 0;
487}
488
489
490/* Handle all of the extended 'Q' packets. */
491static
492void handle_set (char *arg_own_buf, int *new_packet_len_p)
493{
494 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
495 noack_mode = True;
496 write_ok (arg_own_buf);
497 return;
498 }
499
500 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
501 int i;
502 char *from, *to;
503 char *end = arg_own_buf + strlen(arg_own_buf);
504 CORE_ADDR sig;
505 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
506 pass_signals[i] = 0;
507
508 from = arg_own_buf + 13;
509 while (from < end) {
510 to = strchr(from, ';');
511 if (to == NULL) to = end;
512 decode_address (&sig, from, to - from);
513 pass_signals[(int)sig] = 1;
philippe886fde32012-03-29 21:56:47 +0000514 dlog(1, "pass_signal gdb_nr %d %s\n",
515 (int)sig, target_signal_to_name(sig));
sewardj3b290482011-05-06 21:02:55 +0000516 from = to;
517 if (*from == ';') from++;
518 }
519 write_ok (arg_own_buf);
520 return;
521 }
522 /* Otherwise we didn't know what packet it was. Say we didn't
523 understand it. */
524 arg_own_buf[0] = 0;
525}
526
philippe02ea4132013-09-04 21:42:43 +0000527Bool VG_(client_monitor_command) (HChar *cmd)
philippe46207652013-01-20 17:11:58 +0000528{
529 const Bool connected = remote_connected();
530 const int saved_command_output_to_log = command_output_to_log;
531 Bool handled;
532
533 if (!connected)
534 command_output_to_log = True;
535 handled = handle_gdb_monitor_command (cmd);
536 if (!connected) {
537 // reset the log output unless cmd changed it.
538 if (command_output_to_log)
539 command_output_to_log = saved_command_output_to_log;
540 }
541 if (handled)
542 return False; // recognised
543 else
544 return True; // not recognised
545}
546
sewardj3b290482011-05-06 21:02:55 +0000547/* Handle all of the extended 'q' packets. */
548static
549void handle_query (char *arg_own_buf, int *new_packet_len_p)
550{
551 static struct inferior_list_entry *thread_ptr;
552
553 /* qRcmd, monitor command handling. */
554 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
555 char *p = arg_own_buf + 6;
556 int cmdlen = strlen(p)/2;
557 char cmd[cmdlen+1];
558
559 if (unhexify (cmd, p, cmdlen) != cmdlen) {
560 write_enn (arg_own_buf);
561 return;
562 }
563 cmd[cmdlen] = '\0';
564
565 if (handle_gdb_monitor_command (cmd)) {
sewardj3b290482011-05-06 21:02:55 +0000566 write_ok (arg_own_buf);
567 return;
568 } else {
569 /* cmd not recognised */
570 VG_(gdb_printf)
571 ("command '%s' not recognised\n"
572 "In gdb, try 'monitor help'\n"
573 "In a shell, try 'vgdb help'\n",
574 cmd);
575 write_ok (arg_own_buf);
576 return;
577 }
578 }
579
580 /* provide some valgrind specific info in return to qThreadExtraInfo. */
581 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
582 unsigned long gdb_id;
583 struct thread_info *ti;
584 ThreadState *tst;
585 char status[100];
586
587 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
588 ti = gdb_id_to_thread (gdb_id);
589 if (ti != NULL) {
590 tst = (ThreadState *) inferior_target_data (ti);
591 /* Additional info is the tid and the thread status. */
592 VG_(snprintf) (status, sizeof(status), "tid %d %s",
593 tst->tid,
594 VG_(name_of_ThreadStatus)(tst->status));
595 hexify (arg_own_buf, status, strlen(status));
596 return;
597 } else {
598 write_enn (arg_own_buf);
599 return;
600 }
601 }
602
603 if (strcmp ("qAttached", arg_own_buf) == 0) {
604 /* tell gdb to always detach, never kill the process */
605 arg_own_buf[0] = '1';
606 arg_own_buf[1] = 0;
607 return;
608 }
609
610 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
611 /* We have no symbol to read. */
612 write_ok (arg_own_buf);
613 return;
614 }
615
616 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
617 thread_ptr = all_threads.head;
618 VG_(sprintf) (arg_own_buf, "m%x",
619 thread_to_gdb_id ((struct thread_info *)thread_ptr));
620 thread_ptr = thread_ptr->next;
621 return;
622 }
623
624 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
625 if (thread_ptr != NULL) {
626 VG_(sprintf) (arg_own_buf, "m%x",
627 thread_to_gdb_id ((struct thread_info *)thread_ptr));
628 thread_ptr = thread_ptr->next;
629 return;
630 } else {
631 VG_(sprintf) (arg_own_buf, "l");
632 return;
633 }
634 }
635
philippe419d5f22012-05-24 21:33:17 +0000636 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
sewardj3b290482011-05-06 21:02:55 +0000637 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
638 CORE_ADDR ofs;
639 unsigned int len, doc_len;
florian6bd9dc12012-11-23 16:17:43 +0000640 const char *annex = NULL;
sewardj3b290482011-05-06 21:02:55 +0000641 // First, the annex is extracted from the packet received.
642 // Then, it is replaced by the corresponding file name.
643 int fd;
644
645 /* Grab the annex, offset, and length. */
646 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
647 strcpy (arg_own_buf, "E00");
648 return;
649 }
650
651 if (strcmp (annex, "target.xml") == 0) {
philippe419d5f22012-05-24 21:33:17 +0000652 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers));
653 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) {
654 /* Ensure the shadow registers are initialized. */
655 initialize_shadow_low(True);
sewardj3b290482011-05-06 21:02:55 +0000656 }
sewardj3b290482011-05-06 21:02:55 +0000657 if (annex == NULL) {
658 strcpy (arg_own_buf, "E00");
659 return;
660 }
661 }
662
663 {
philippe75a5f782012-02-24 11:25:58 +0000664 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
sewardj3b290482011-05-06 21:02:55 +0000665 struct vg_stat stat_doc;
666 char toread[len];
667 int len_read;
668
669 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
670 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
671 if (fd == -1) {
672 strcpy (arg_own_buf, "E00");
673 return;
674 }
675 if (VG_(fstat) (fd, &stat_doc) != 0) {
676 VG_(close) (fd);
677 strcpy (arg_own_buf, "E00");
678 return;
679 }
680 doc_len = stat_doc.size;
681
682 if (len > PBUFSIZ - POVERHSIZ)
683 len = PBUFSIZ - POVERHSIZ;
684
685 if (ofs > doc_len) {
686 write_enn (arg_own_buf);
687 VG_(close) (fd);
688 return;
689 }
690 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
691 len_read = VG_(read) (fd, toread, len);
florian1636d332012-11-15 04:27:04 +0000692 *new_packet_len_p = write_qxfer_response (arg_own_buf, (unsigned char *)toread,
sewardj3b290482011-05-06 21:02:55 +0000693 len_read, ofs + len_read < doc_len);
694 VG_(close) (fd);
695 return;
696 }
697 }
698
sewardj2a312392011-06-26 09:26:48 +0000699 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
700 unsigned char *data;
701 int n;
702 CORE_ADDR ofs;
703 unsigned int len;
florian6bd9dc12012-11-23 16:17:43 +0000704 const char *annex;
sewardj2a312392011-06-26 09:26:48 +0000705
706 /* Reject any annex; grab the offset and length. */
707 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
708 || annex[0] != '\0') {
709 strcpy (arg_own_buf, "E00");
710 return;
711 }
712
713 if (len > PBUFSIZ - 2)
714 len = PBUFSIZ - 2;
715 data = malloc (len);
716
717 {
718 UWord *client_auxv = VG_(client_auxv);
719 unsigned int client_auxv_len = 0;
720 while (*client_auxv != 0) {
721 dlog(4, "auxv %lld %llx\n",
722 (ULong)*client_auxv,
723 (ULong)*(client_auxv+1));
724 client_auxv++;
725 client_auxv++;
726 client_auxv_len += 2 * sizeof(UWord);
727 }
728 client_auxv_len += 2 * sizeof(UWord);
729 dlog(4, "auxv len %d\n", client_auxv_len);
730
731 if (ofs >= client_auxv_len)
732 n = -1;
733 else {
734 n = client_auxv_len - ofs;
735 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
736 }
737 }
738
739 if (n < 0)
740 write_enn (arg_own_buf);
741 else if (n > len)
742 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
743 else
744 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
745
746 free (data);
747
748 return;
749 }
750
751
sewardj3b290482011-05-06 21:02:55 +0000752 /* Protocol features query. */
753 if (strncmp ("qSupported", arg_own_buf, 10) == 0
754 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
755 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
756 /* Note: max packet size including frame and checksum, but without
757 trailing null byte, which is not sent/received. */
758
759 strcat (arg_own_buf, ";QStartNoAckMode+");
760 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +0000761 if (VG_(client_auxv))
762 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +0000763
philippe419d5f22012-05-24 21:33:17 +0000764 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
sewardj3b290482011-05-06 21:02:55 +0000765 strcat (arg_own_buf, ";qXfer:features:read+");
766 /* if a new gdb connects to us, we have to reset the register
767 set to the normal register sets to allow this new gdb to
768 decide to use or not the shadow registers.
769
770 Note that the reset is only done for gdb that are sending
771 qSupported packets. If a user first connected with a recent
772 gdb using shadow registers and then with a very old gdb
773 that does not use qSupported packet, then the old gdb will
774 not properly connect. */
775 initialize_shadow_low(False);
776 }
777 return;
778 }
779
780 /* Otherwise we didn't know what packet it was. Say we didn't
781 understand it. */
782 arg_own_buf[0] = 0;
783}
784
785/* Handle all of the extended 'v' packets. */
786static
sewardj0bb3c672011-07-26 23:29:25 +0000787void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +0000788{
789 /* vcont packet code from gdb 6.6 removed */
790
791 /* Otherwise we didn't know what packet it was. Say we didn't
792 understand it. */
793 arg_own_buf[0] = 0;
794 return;
795}
796
797static
798void myresume (int step, int sig)
799{
800 struct thread_resume resume_info[2];
801 int n = 0;
802
philippe349a3912012-05-23 21:50:36 +0000803 if (step || sig) {
sewardj3b290482011-05-06 21:02:55 +0000804 resume_info[0].step = step;
805 resume_info[0].sig = sig;
sewardj3b290482011-05-06 21:02:55 +0000806 n++;
807 }
sewardj3b290482011-05-06 21:02:55 +0000808 resume_info[n].step = 0;
809 resume_info[n].sig = 0;
sewardj3b290482011-05-06 21:02:55 +0000810
philippe349a3912012-05-23 21:50:36 +0000811 resume_reply_packet_needed = True;
812 valgrind_resume (resume_info);
sewardj3b290482011-05-06 21:02:55 +0000813}
814
815/* server_main global variables */
816static char *own_buf;
817static unsigned char *mem_buf;
818
819void gdbserver_init (void)
820{
821 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
822 noack_mode = False;
philippe349a3912012-05-23 21:50:36 +0000823 valgrind_initialize_target ();
philippe0e1cac92012-02-28 22:37:44 +0000824 // After a fork, gdbserver_init can be called again.
825 // We do not have to re-malloc the buffers in such a case.
826 if (own_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +0000827 own_buf = malloc (PBUFSIZ+POVERHSIZ);
philippe0e1cac92012-02-28 22:37:44 +0000828 if (mem_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +0000829 mem_buf = malloc (PBUFSIZ+POVERHSIZ);
830 // Note: normally, we should only malloc PBUFSIZ. However,
831 // GDB has a bug, and in some cases, sends e.g. 'm' packets
832 // asking for slightly more than the PacketSize given at
833 // connection initialisation. So, we bypass the GDB bug
834 // by allocating slightly more.
sewardj3b290482011-05-06 21:02:55 +0000835}
836
837void gdbserver_terminate (void)
838{
839 /* last call to gdbserver is cleanup call */
840 if (VG_MINIMAL_SETJMP(toplevel)) {
841 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
842 return;
843 }
844 remote_close();
845}
846
847void server_main (void)
848{
849 static char status;
sewardj0bb3c672011-07-26 23:29:25 +0000850 static int zignal;
sewardj3b290482011-05-06 21:02:55 +0000851
852 char ch;
853 int i = 0;
854 unsigned int len;
855 CORE_ADDR mem_addr;
856
philippe349a3912012-05-23 21:50:36 +0000857 zignal = valgrind_wait (&status);
sewardj3b290482011-05-06 21:02:55 +0000858 if (VG_MINIMAL_SETJMP(toplevel)) {
859 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
860 }
861 while (1) {
862 unsigned char sig;
863 int packet_len;
864 int new_packet_len = -1;
865
philippe349a3912012-05-23 21:50:36 +0000866 if (resume_reply_packet_needed) {
867 /* Send the resume reply to reply to last GDB resume
868 request. */
869 resume_reply_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +0000870 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000871 putpkt (own_buf);
872 }
873
philippe0447bbd2012-10-17 21:32:03 +0000874 /* If we our status is terminal (exit or fatal signal) get out
875 as quickly as we can. We won't be able to handle any request
876 anymore. */
877 if (status == 'W' || status == 'X') {
878 return;
879 }
880
sewardj3b290482011-05-06 21:02:55 +0000881 packet_len = getpkt (own_buf);
882 if (packet_len <= 0)
883 break;
884
885 i = 0;
886 ch = own_buf[i++];
887 switch (ch) {
888 case 'Q':
889 handle_set (own_buf, &new_packet_len);
890 break;
891 case 'q':
892 handle_query (own_buf, &new_packet_len);
893 break;
894 case 'd':
895 /* set/unset debugging is done through valgrind debug level. */
896 own_buf[0] = '\0';
897 break;
898 case 'D':
899 reset_valgrind_sink("gdb detaching from process");
900
901 /* When detaching or kill the process, gdb expects to get
902 an packet OK back. Any other output will make gdb
903 believes detach did not work. */
904 write_ok (own_buf);
905 putpkt (own_buf);
906 remote_finish (reset_after_error);
907 remote_open (VG_(clo_vgdb_prefix));
908 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +0000909 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +0000910 return;
911 case '!':
912 /* We can not use the extended protocol with valgrind,
913 because we can not restart the running
914 program. So return unrecognized. */
915 own_buf[0] = '\0';
916 break;
917 case '?':
sewardj0bb3c672011-07-26 23:29:25 +0000918 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000919 break;
920 case 'H':
921 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
922 unsigned long gdb_id, thread_id;
923
924 gdb_id = strtoul (&own_buf[2], NULL, 16);
925 thread_id = gdb_id_to_thread_id (gdb_id);
926 if (thread_id == 0) {
927 write_enn (own_buf);
928 break;
929 }
930
931 if (own_buf[1] == 'g') {
932 general_thread = thread_id;
933 set_desired_inferior (1);
934 } else if (own_buf[1] == 'c') {
935 cont_thread = thread_id;
936 } else if (own_buf[1] == 's') {
937 step_thread = thread_id;
938 }
939
940 write_ok (own_buf);
941 } else {
942 /* Silently ignore it so that gdb can extend the protocol
943 without compatibility headaches. */
944 own_buf[0] = '\0';
945 }
946 break;
947 case 'g':
948 set_desired_inferior (1);
949 registers_to_string (own_buf);
950 break;
951 case 'G':
952 set_desired_inferior (1);
953 registers_from_string (&own_buf[1]);
954 write_ok (own_buf);
955 break;
956 case 'P': {
957 int regno;
958 char *regbytes;
959 Bool mod;
960 ThreadState *tst;
961 regno = strtol(&own_buf[1], NULL, 16);
962 regbytes = strchr(&own_buf[0], '=') + 1;
963 set_desired_inferior (1);
964 tst = (ThreadState *) inferior_target_data (current_inferior);
965 /* Only accept changing registers in "runnable state3.
966 In fact, it would be ok to change most of the registers
967 except a few "sensitive" registers such as the PC, SP, BP.
968 We assume we do not need to very specific here, and that we
969 can just refuse all of these. */
970 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
971 supply_register_from_string (regno, regbytes, &mod);
972 write_ok (own_buf);
973 } else {
974 /* at least from gdb 6.6 onwards, an E. error
975 reply is shown to the user. So, we do an error
976 msg which both is accepted by gdb as an error msg
977 and is readable by the user. */
978 VG_(sprintf)
979 (own_buf,
980"E.\n"
981"ERROR changing register %s regno %d\n"
982"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
983"set pc, calling from gdb a function in the debugged process, ...)\n"
984"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
985"Thread status is %s\n",
986 find_register_by_number (regno)->name, regno,
987 VG_(name_of_ThreadStatus)(tst->status));
988 if (VG_(clo_verbosity) > 1)
989 VG_(umsg) ("%s\n", own_buf);
990 }
991 break;
992 }
993 case 'm':
994 decode_m_packet (&own_buf[1], &mem_addr, &len);
philippe349a3912012-05-23 21:50:36 +0000995 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +0000996 convert_int_to_ascii (mem_buf, own_buf, len);
997 else
998 write_enn (own_buf);
999 break;
1000 case 'M':
1001 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
philippe349a3912012-05-23 21:50:36 +00001002 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001003 write_ok (own_buf);
1004 else
1005 write_enn (own_buf);
1006 break;
1007 case 'X':
1008 if (decode_X_packet (&own_buf[1], packet_len - 1,
1009 &mem_addr, &len, mem_buf) < 0
philippe349a3912012-05-23 21:50:36 +00001010 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
sewardj3b290482011-05-06 21:02:55 +00001011 write_enn (own_buf);
1012 else
1013 write_ok (own_buf);
1014 break;
1015 case 'C':
1016 convert_ascii_to_int (own_buf + 1, &sig, 1);
1017 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +00001018 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001019 else
sewardj0bb3c672011-07-26 23:29:25 +00001020 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001021 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001022 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +00001023 return; // return control to valgrind
1024 case 'S':
1025 convert_ascii_to_int (own_buf + 1, &sig, 1);
1026 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +00001027 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001028 else
sewardj0bb3c672011-07-26 23:29:25 +00001029 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001030 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001031 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +00001032 return; // return control to valgrind
1033 case 'c':
1034 set_desired_inferior (0);
1035 myresume (0, 0);
1036 return; // return control to valgrind
1037 case 's':
1038 set_desired_inferior (0);
1039 myresume (1, 0);
1040 return; // return control to valgrind
1041 case 'Z': {
1042 char *lenptr;
1043 char *dataptr;
1044 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1045 int zlen = strtol (lenptr + 1, &dataptr, 16);
1046 char type = own_buf[1];
1047
philippe349a3912012-05-23 21:50:36 +00001048 if (type < '0' || type > '4') {
1049 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001050 own_buf[0] = '\0';
1051 } else {
1052 int res;
1053
philippe349a3912012-05-23 21:50:36 +00001054 res = valgrind_insert_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001055 if (res == 0)
1056 write_ok (own_buf);
1057 else if (res == 1)
1058 /* Unsupported. */
1059 own_buf[0] = '\0';
1060 else
1061 write_enn (own_buf);
1062 }
1063 break;
1064 }
1065 case 'z': {
1066 char *lenptr;
1067 char *dataptr;
1068 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1069 int zlen = strtol (lenptr + 1, &dataptr, 16);
1070 char type = own_buf[1];
1071
philippe349a3912012-05-23 21:50:36 +00001072 if (type < '0' || type > '4') {
1073 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001074 own_buf[0] = '\0';
1075 } else {
1076 int res;
1077
philippe349a3912012-05-23 21:50:36 +00001078 res = valgrind_remove_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001079 if (res == 0)
1080 write_ok (own_buf);
1081 else if (res == 1)
1082 /* Unsupported. */
1083 own_buf[0] = '\0';
1084 else
1085 write_enn (own_buf);
1086 }
1087 break;
1088 }
1089 case 'k':
1090 kill_request("Gdb request to kill this process\n");
1091 break;
1092 case 'T': {
1093 unsigned long gdb_id, thread_id;
1094
1095 gdb_id = strtoul (&own_buf[1], NULL, 16);
1096 thread_id = gdb_id_to_thread_id (gdb_id);
1097 if (thread_id == 0) {
1098 write_enn (own_buf);
1099 break;
1100 }
1101
philippe349a3912012-05-23 21:50:36 +00001102 if (valgrind_thread_alive (thread_id))
sewardj3b290482011-05-06 21:02:55 +00001103 write_ok (own_buf);
1104 else
1105 write_enn (own_buf);
1106 break;
1107 }
1108 case 'R':
1109 /* Restarting the inferior is only supported in the
1110 extended protocol.
1111 => It is a request we don't understand. Respond with an
1112 empty packet so that gdb knows that we don't support this
1113 request. */
1114 own_buf[0] = '\0';
1115 break;
1116 case 'v':
1117 /* Extended (long) request. */
sewardj0bb3c672011-07-26 23:29:25 +00001118 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001119 break;
1120 default:
1121 /* It is a request we don't understand. Respond with an
1122 empty packet so that gdb knows that we don't support this
1123 request. */
1124 own_buf[0] = '\0';
1125 break;
1126 }
1127
1128 if (new_packet_len != -1)
1129 putpkt_binary (own_buf, new_packet_len);
1130 else
1131 putpkt (own_buf);
1132
1133 if (status == 'W')
sewardj0bb3c672011-07-26 23:29:25 +00001134 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001135 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001136 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
sewardj0bb3c672011-07-26 23:29:25 +00001137 target_signal_to_host (zignal),
1138 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001139 if (status == 'W' || status == 'X') {
1140 VG_(umsg) ("Process exiting\n");
1141 VG_(exit) (0);
1142 }
1143 }
1144
1145 /* We come here when getpkt fails => close the connection,
1146 and re-open. Then return control to valgrind.
1147 We return the control to valgrind as we assume that
1148 the connection was closed due to vgdb having finished
1149 to execute a command. */
1150 if (VG_(clo_verbosity) > 1)
1151 VG_(umsg) ("Remote side has terminated connection. "
1152 "GDBserver will reopen the connection.\n");
1153 remote_finish (reset_after_error);
1154 remote_open (VG_(clo_vgdb_prefix));
1155 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001156 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001157 return;
1158}