blob: 45498d6e2371f6d4d1b0947b70febfe3a2727ce6 [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"
philippe8587b542013-12-15 20:24:43 +000033#include "pub_core_transtab.h"
sewardj3b290482011-05-06 21:02:55 +000034
35unsigned long cont_thread;
36unsigned long general_thread;
37unsigned long step_thread;
38unsigned long thread_from_wait;
39unsigned long old_thread_from_wait;
40
philippe886fde32012-03-29 21:56:47 +000041int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
bart2dafc542011-05-18 16:08:28 +000042
sewardj3b290482011-05-06 21:02:55 +000043/* for a gdbserver integrated in valgrind, resuming the process consists
44 in returning the control to valgrind.
philippe349a3912012-05-23 21:50:36 +000045 The guess process resumes its execution.
sewardj3b290482011-05-06 21:02:55 +000046 Then at the next error or break or ..., valgrind calls gdbserver again.
philippe349a3912012-05-23 21:50:36 +000047 A resume reply packet must then be built to inform GDB that the
48 resume request is finished.
49 resume_reply_packet_needed records the fact that the next call to gdbserver
sewardj3b290482011-05-06 21:02:55 +000050 must send a resume packet to gdb. */
philippe349a3912012-05-23 21:50:36 +000051static Bool resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +000052
53VG_MINIMAL_JMP_BUF(toplevel);
54
55/* Decode a qXfer read request. Return 0 if everything looks OK,
56 or -1 otherwise. */
57
58static
florian6bd9dc12012-11-23 16:17:43 +000059int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len)
sewardj3b290482011-05-06 21:02:55 +000060{
61 /* Extract and NUL-terminate the annex. */
62 *annex = buf;
63 while (*buf && *buf != ':')
64 buf++;
65 if (*buf == '\0')
66 return -1;
67 *buf++ = 0;
68
69 /* After the read/write marker and annex, qXfer looks like a
70 traditional 'm' packet. */
71 decode_m_packet (buf, ofs, len);
72
73 return 0;
74}
75
76/* Write the response to a successful qXfer read. Returns the
77 length of the (binary) data stored in BUF, corresponding
78 to as much of DATA/LEN as we could fit. IS_MORE controls
79 the first character of the response. */
80static
81int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
82{
83 int out_len;
84
85 if (is_more)
86 buf[0] = 'm';
87 else
88 buf[0] = 'l';
89
90 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
91 PBUFSIZ - POVERHSIZ - 1) + 1;
92}
93
94static Bool initial_valgrind_sink_saved = False;
95/* True <=> valgrind log sink saved in initial_valgrind_sink */
96static OutputSink initial_valgrind_sink;
97
98static Bool command_output_to_log = False;
99/* True <=> command output goes to log instead of gdb */
100
florian6bd9dc12012-11-23 16:17:43 +0000101void reset_valgrind_sink(const char *info)
sewardj3b290482011-05-06 21:02:55 +0000102{
103 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
104 && initial_valgrind_sink_saved) {
105 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
106 VG_(umsg) ("Reset valgrind output to log (%s)\n",
107 (info = NULL ? "" : info));
108 }
109}
110
philippe46207652013-01-20 17:11:58 +0000111void print_to_initial_valgrind_sink (const char *msg)
112{
113 vg_assert (initial_valgrind_sink_saved);
114 VG_(write) (initial_valgrind_sink.fd, msg, strlen(msg));
115}
116
117
sewardj3b290482011-05-06 21:02:55 +0000118static
florian6bd9dc12012-11-23 16:17:43 +0000119void kill_request (const char *msg)
sewardj3b290482011-05-06 21:02:55 +0000120{
121 VG_(umsg) ("%s", msg);
sewardj3b290482011-05-06 21:02:55 +0000122 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
philippe4f6f3362014-04-19 00:25:54 +0000150void VG_(print_all_stats) (Bool memory_stats, Bool tool_stats)
151{
152 if (memory_stats) {
153 VG_(message)(Vg_DebugMsg, "\n");
154 VG_(message)(Vg_DebugMsg,
155 "------ Valgrind's internal memory use stats follow ------\n" );
156 VG_(sanity_check_malloc_all)();
157 VG_(message)(Vg_DebugMsg, "------\n" );
158 VG_(print_all_arena_stats)();
159 if (VG_(clo_profile_heap))
160 VG_(print_arena_cc_analysis) ();
161 VG_(message)(Vg_DebugMsg, "\n");
162 }
163
164 VG_(print_translation_stats)();
165 VG_(print_tt_tc_stats)();
166 VG_(print_scheduler_stats)();
167 VG_(print_ExeContext_stats)( False /* with_stacktraces */ );
168 VG_(print_errormgr_stats)();
169 if (tool_stats && VG_(needs).print_stats) {
170 VG_TDICT_CALL(tool_print_stats);
171 }
172}
173
sewardj3b290482011-05-06 21:02:55 +0000174/* handle_gdb_valgrind_command handles the provided mon string command.
175 If command is recognised, return 1 else return 0.
176 Note that in case of ambiguous command, 1 is returned.
177
178 *sink_wanted_at_return is modified if one of the commands
sewardj30b3eca2011-06-28 08:20:39 +0000179 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000180*/
181static
philippe02ea4132013-09-04 21:42:43 +0000182int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return)
sewardj3b290482011-05-06 21:02:55 +0000183{
184 UWord ret = 0;
185 char s[strlen(mon)+1]; /* copy for strtok_r */
philippe02ea4132013-09-04 21:42:43 +0000186 char *wcmd;
187 HChar *ssaveptr;
188 const char *endptr;
sewardj3b290482011-05-06 21:02:55 +0000189 int kwdid;
190 int int_value;
191
192 vg_assert (initial_valgrind_sink_saved);
193
194 strcpy (s, mon);
195 wcmd = strtok_r (s, " ", &ssaveptr);
196 /* NB: if possible, avoid introducing a new command below which
sewardj30b3eca2011-06-28 08:20:39 +0000197 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000198 command. This ensures a shorter abbreviation for the user. */
philippe6ec8d632013-01-23 22:10:28 +0000199 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate"
200 " v.do",
sewardj3b290482011-05-06 21:02:55 +0000201 wcmd, kwd_report_duplicated_matches)) {
202 case -2:
203 ret = 1;
204 break;
205 case -1:
206 break;
207 case 0: /* help */
208 ret = 1;
209 wcmd = strtok_r (NULL, " ", &ssaveptr);
210 if (wcmd == NULL) {
211 int_value = 0;
212 } else {
213 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
214 case -2: int_value = 0; break;
215 case -1: int_value = 0; break;
216 case 0: int_value = 1; break;
217 default: tl_assert (0);
218 }
219 }
220
221 VG_(gdb_printf) (
222"general valgrind monitor commands:\n"
philippec3360382012-10-21 14:37:14 +0000223" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000224" v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
225" v.info all_errors : show all errors found so far\n"
226" v.info last_error : show last error found\n"
philippe02ea4132013-09-04 21:42:43 +0000227" 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 +0000228" v.info open_fds : show open file descriptors (only if --track-fds=yes)\n"
sewardj30b3eca2011-06-28 08:20:39 +0000229" v.kill : kill the Valgrind process\n"
230" v.set gdb_output : set valgrind output to gdb\n"
231" v.set log_output : set valgrind output to log\n"
232" v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
philippe46207652013-01-20 17:11:58 +0000233" v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n"
sewardj30b3eca2011-06-28 08:20:39 +0000234" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000235 if (int_value) { VG_(gdb_printf) (
236"debugging valgrind internals monitor commands:\n"
philippe6ec8d632013-01-23 22:10:28 +0000237" v.do expensive_sanity_check_general : do an expensive sanity check now\n"
sewardj30b3eca2011-06-28 08:20:39 +0000238" v.info gdbserver_status : show gdbserver status\n"
philippe93a6a8d2012-04-27 22:59:43 +0000239" v.info memory [aspacemgr] : show valgrind heap memory stats\n"
240" (with aspacemgr arg, also shows valgrind segments on log ouput)\n"
philippef3a6e932013-01-10 20:42:51 +0000241" v.info exectxt : show stacktraces and stats of all execontexts\n"
sewardjd6e13d82011-10-22 20:23:30 +0000242" v.info scheduler : show valgrind thread state and stacktrace\n"
philippe8587b542013-12-15 20:24:43 +0000243" v.info stats : show various valgrind and tool stats\n"
sewardj30b3eca2011-06-28 08:20:39 +0000244" v.set debuglog <level> : set valgrind debug log level to <level>\n"
philippe180a7502014-04-20 13:41:10 +0000245" v.set hostvisibility [yes*|no] : (en/dis)ables access by gdb/gdbserver to\n"
246" Valgrind internal host status/memory\n"
sewardj30b3eca2011-06-28 08:20:39 +0000247" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000248" (default traceflags 0b00100000 : show after instrumentation)\n"
249" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
250 }
251 break;
sewardj30b3eca2011-06-28 08:20:39 +0000252 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000253 ret = 1;
254 wcmd = strtok_r (NULL, " ", &ssaveptr);
255 switch (kwdid = VG_(keyword_id)
philippe46207652013-01-20 17:11:58 +0000256 ("vgdb-error debuglog merge-recursive-frames"
philippe180a7502014-04-20 13:41:10 +0000257 " gdb_output log_output mixed_output hostvisibility ",
sewardj3b290482011-05-06 21:02:55 +0000258 wcmd, kwd_report_all)) {
259 case -2:
260 case -1:
261 break;
262 case 0: /* vgdb-error */
263 case 1: /* debuglog */
philippe46207652013-01-20 17:11:58 +0000264 case 2: /* merge-recursive-frames */
sewardj3b290482011-05-06 21:02:55 +0000265 wcmd = strtok_r (NULL, " ", &ssaveptr);
266 if (wcmd == NULL) {
267 int_value = 0;
268 endptr = "empty"; /* to report an error below */
269 } else {
florian6bd9dc12012-11-23 16:17:43 +0000270 HChar *the_end;
271 int_value = strtol (wcmd, &the_end, 10);
272 endptr = the_end;
sewardj3b290482011-05-06 21:02:55 +0000273 }
274 if (*endptr != '\0') {
275 VG_(gdb_printf) ("missing or malformed integer value\n");
276 } else if (kwdid == 0) {
philippe02ea4132013-09-04 21:42:43 +0000277 VG_(printf) ("vgdb-error value changed from %d to %d\n",
sewardj3b290482011-05-06 21:02:55 +0000278 VG_(dyn_vgdb_error), int_value);
279 VG_(dyn_vgdb_error) = int_value;
280 } else if (kwdid == 1) {
philippe02ea4132013-09-04 21:42:43 +0000281 VG_(printf) ("debuglog value changed from %d to %d\n",
sewardj3b290482011-05-06 21:02:55 +0000282 VG_(debugLog_getLevel)(), int_value);
283 VG_(debugLog_startup) (int_value, "gdbsrv");
philippe46207652013-01-20 17:11:58 +0000284 } else if (kwdid == 2) {
philippe02ea4132013-09-04 21:42:43 +0000285 VG_(printf)
philippe46207652013-01-20 17:11:58 +0000286 ("merge-recursive-frames value changed from %d to %d\n",
287 VG_(clo_merge_recursive_frames), int_value);
288 VG_(clo_merge_recursive_frames) = int_value;
sewardj3b290482011-05-06 21:02:55 +0000289 } else {
290 vg_assert (0);
291 }
292 break;
philippe46207652013-01-20 17:11:58 +0000293 case 3: /* gdb_output */
sewardj3b290482011-05-06 21:02:55 +0000294 (*sink_wanted_at_return).fd = -2;
295 command_output_to_log = False;
296 VG_(gdb_printf) ("valgrind output will go to gdb\n");
297 break;
philippe46207652013-01-20 17:11:58 +0000298 case 4: /* log_output */
sewardj3b290482011-05-06 21:02:55 +0000299 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
300 command_output_to_log = True;
301 VG_(gdb_printf) ("valgrind output will go to log\n");
302 break;
philippe46207652013-01-20 17:11:58 +0000303 case 5: /* mixed output */
sewardj3b290482011-05-06 21:02:55 +0000304 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
305 command_output_to_log = False;
306 VG_(gdb_printf)
307 ("valgrind output will go to log, interactive output will go to gdb\n");
308 break;
philippe180a7502014-04-20 13:41:10 +0000309 case 6: /* hostvisibility */
310 wcmd = strtok_r (NULL, " ", &ssaveptr);
311 if (wcmd != NULL) {
312 switch (VG_(keyword_id) ("yes no", wcmd, kwd_report_all)) {
313 case -2:
314 case -1: break;
315 case 0:
316 hostvisibility = True;
317 break;
318 case 1:
319 hostvisibility = False;
320 break;
321 default: tl_assert (0);
322 }
323 } else {
324 hostvisibility = True;
325 }
326 if (hostvisibility)
327 VG_(gdb_printf)
328 ("Enabled access to Valgrind memory/status by GDB\n"
329 "If not yet done, tell GDB which valgrind file(s) to use:\n"
330 "add-symbol-file <tool or preloaded file> <loadaddr>\n");
331 else
332 VG_(gdb_printf)
333 ("Disabled access to Valgrind memory/status by GDB\n");
334 break;
sewardj3b290482011-05-06 21:02:55 +0000335 default:
336 vg_assert (0);
337 }
338 break;
sewardj30b3eca2011-06-28 08:20:39 +0000339 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000340 ret = 1;
341 wcmd = strtok_r (NULL, " ", &ssaveptr);
342 switch (kwdid = VG_(keyword_id)
sewardjd6e13d82011-10-22 20:23:30 +0000343 ("all_errors n_errs_found last_error gdbserver_status memory"
philippe8587b542013-12-15 20:24:43 +0000344 " scheduler stats open_fds exectxt",
sewardj3b290482011-05-06 21:02:55 +0000345 wcmd, kwd_report_all)) {
346 case -2:
347 case -1:
348 break;
349 case 0: // all_errors
350 // A verbosity of minimum 2 is needed to show the errors.
351 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
352 break;
353 case 1: // n_errs_found
philippe02ea4132013-09-04 21:42:43 +0000354 VG_(printf) ("n_errs_found %d n_errs_shown %d (vgdb-error %d) %s\n",
355 VG_(get_n_errs_found) (),
356 VG_(get_n_errs_shown) (),
357 VG_(dyn_vgdb_error),
358 wordn (mon, 3));
sewardj3b290482011-05-06 21:02:55 +0000359 break;
360 case 2: // last_error
361 VG_(show_last_error)();
362 break;
363 case 3: // gdbserver_status
364 VG_(gdbserver_status_output)();
365 break;
366 case 4: /* memory */
philippe8587b542013-12-15 20:24:43 +0000367 VG_(printf) ("%llu bytes have already been allocated.\n",
368 VG_(am_get_anonsize_total)());
sewardj3b290482011-05-06 21:02:55 +0000369 VG_(print_all_arena_stats) ();
370 if (VG_(clo_profile_heap))
371 VG_(print_arena_cc_analysis) ();
philippe93a6a8d2012-04-27 22:59:43 +0000372 wcmd = strtok_r (NULL, " ", &ssaveptr);
373 if (wcmd != NULL) {
374 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) {
375 case -2:
376 case -1: break;
377 case 0:
378 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr");
379 break;
380 default: tl_assert (0);
381 }
382 }
383
sewardj3b290482011-05-06 21:02:55 +0000384 ret = 1;
385 break;
sewardjd6e13d82011-10-22 20:23:30 +0000386 case 5: /* scheduler */
philippe4f6f3362014-04-19 00:25:54 +0000387 VG_(show_sched_status) (True, // host_stacktrace
388 True, // valgrind_stack_usage
389 True); // exited_threads
sewardjd6e13d82011-10-22 20:23:30 +0000390 ret = 1;
391 break;
philippe8587b542013-12-15 20:24:43 +0000392 case 6: /* stats */
philippe4f6f3362014-04-19 00:25:54 +0000393 VG_(print_all_stats)(False, /* Memory stats */
394 True /* Tool stats */);
philippe8587b542013-12-15 20:24:43 +0000395 ret = 1;
396 break;
397 case 7: /* open_fds */
philippec3360382012-10-21 14:37:14 +0000398 if (VG_(clo_track_fds))
399 VG_(show_open_fds) ("");
400 else
401 VG_(gdb_printf)
402 ("Valgrind must be started with --track-fds=yes"
403 " to show open fds\n");
404 ret = 1;
405 break;
philippe8587b542013-12-15 20:24:43 +0000406 case 8: /* exectxt */
philippef3a6e932013-01-10 20:42:51 +0000407 VG_(print_ExeContext_stats) (True /* with_stacktraces */);
408 ret = 1;
409 break;
sewardj3b290482011-05-06 21:02:55 +0000410 default:
411 vg_assert(0);
412 }
413 break;
414 }
sewardj30b3eca2011-06-28 08:20:39 +0000415 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000416 wcmd = strtok_r (NULL, " ", &ssaveptr);
417 if (wcmd != NULL) {
florian6bd9dc12012-11-23 16:17:43 +0000418 int_value = strtol (wcmd, NULL, 10);
philippe02ea4132013-09-04 21:42:43 +0000419 VG_(printf) ("gdbserver: continuing in %d ms ...\n", int_value);
sewardj3b290482011-05-06 21:02:55 +0000420 VG_(poll)(NULL, 0, int_value);
421 }
philippe02ea4132013-09-04 21:42:43 +0000422 VG_(printf) ("gdbserver: continuing after wait ...\n");
sewardj3b290482011-05-06 21:02:55 +0000423 ret = 1;
424 break;
sewardj30b3eca2011-06-28 08:20:39 +0000425 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000426 kill_request ("monitor command request to kill this process\n");
427 break;
sewardj30b3eca2011-06-28 08:20:39 +0000428 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000429 Addr address;
430 SizeT verbosity = 0x20;
431
432 ret = 1;
433
434 VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr);
435 if (address != (Addr) 0 || verbosity != 0) {
436 /* we need to force the output to log for the translation trace,
437 as low level VEX tracing cannot be redirected to gdb. */
438 int saved_command_output_to_log = command_output_to_log;
439 int saved_fd = VG_(log_output_sink).fd;
440 Bool single_stepping_on_entry = valgrind_single_stepping();
441 int vex_verbosity = verbosity & 0xff;
442 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
443 if ((verbosity & 0x100) && !single_stepping_on_entry) {
444 valgrind_set_single_stepping(True);
445 // to force gdbserver instrumentation.
446 }
sewardj99d61342011-05-17 16:35:11 +0000447# if defined(VGA_arm)
448 // on arm, we need to (potentially) convert this address
449 // to the thumb form.
450 address = thumb_pc (address);
451# endif
452
sewardj3b290482011-05-06 21:02:55 +0000453 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
454 address,
455 /*debugging*/True,
456 (Int) vex_verbosity,
457 /*bbs_done*/0,
458 /*allow redir?*/True);
459 if ((verbosity & 0x100) && !single_stepping_on_entry) {
460 valgrind_set_single_stepping(False);
461 // reset single stepping.
462 }
463 command_output_to_log = saved_command_output_to_log;
464 VG_(log_output_sink).fd = saved_fd;
465 }
466 break;
467 }
468
philippe6ec8d632013-01-23 22:10:28 +0000469 case 6: /* v.do */
470 ret = 1;
471 wcmd = strtok_r (NULL, " ", &ssaveptr);
472 switch (VG_(keyword_id) ("expensive_sanity_check_general",
473 wcmd, kwd_report_all)) {
474 case -2:
475 case -1: break;
476 case 0: { /* expensive_sanity_check_general */
477 // Temporarily bump up sanity level to check e.g. the malloc arenas.
478 const Int save_clo_sanity_level = VG_(clo_sanity_level);
479 if (VG_(clo_sanity_level) < 4) VG_(clo_sanity_level) = 4;
480 VG_(sanity_check_general) (/* force_expensive */ True);
481 VG_(clo_sanity_level) = save_clo_sanity_level;
482 break;
483 }
484 default: tl_assert (0);
485 }
486 break;
487
sewardj3b290482011-05-06 21:02:55 +0000488 default:
489 vg_assert (0);
490 }
491 return ret;
492}
493
494/* handle_gdb_monitor_command handles the provided mon string command,
495 which can be either a "standard" valgrind monitor command
496 or a tool specific monitor command.
497 If command recognised, return 1 else return 0.
498 Note that in case of ambiguous command, 1 is returned.
499*/
500static
philippe02ea4132013-09-04 21:42:43 +0000501int handle_gdb_monitor_command (char *mon)
sewardj3b290482011-05-06 21:02:55 +0000502{
503 UWord ret = 0;
504 UWord tool_ret = 0;
505 // initially, we assume that when returning, the desired sink is the
506 // one we have when entering. It can however be changed by the standard
507 // valgrind command handling.
508 OutputSink sink_wanted_at_return = VG_(log_output_sink);
509
510 if (!initial_valgrind_sink_saved) {
511 /* first time we enter here, we save the valgrind default log sink */
512 initial_valgrind_sink = sink_wanted_at_return;
513 initial_valgrind_sink_saved = True;
514 }
515
516 if (!command_output_to_log)
517 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
518
519 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
520
521 /* Even if command was recognised by valgrind core, we call the
522 tool command handler : this is needed to handle help command
523 and/or to let the tool do some additional processing of a
524 valgrind standard command. Note however that if valgrind
525 recognised the command, we will always return success. */
526 if (VG_(needs).client_requests) {
527 /* If the tool reports an error when handling a monitor command,
528 we need to avoid calling gdbserver during this command
529 handling. So, we temporarily set VG_(dyn_vgdb_error) to
530 a huge value to ensure m_errormgr.c does not call gdbserver. */
531 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
532 UWord arg[2];
533 VG_(dyn_vgdb_error) = 999999999;
534 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
535 arg[1] = (UWord) mon;
536 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
537 &tool_ret);
538 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
539 }
540
philippe02ea4132013-09-04 21:42:43 +0000541 VG_(message_flush) ();
542
sewardj3b290482011-05-06 21:02:55 +0000543 /* restore or set the desired output */
544 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
545 if (ret | tool_ret)
546 return 1;
547 else
548 return 0;
549}
550
551
552/* Handle all of the extended 'Q' packets. */
553static
554void handle_set (char *arg_own_buf, int *new_packet_len_p)
555{
556 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
557 noack_mode = True;
558 write_ok (arg_own_buf);
559 return;
560 }
561
562 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
563 int i;
564 char *from, *to;
565 char *end = arg_own_buf + strlen(arg_own_buf);
566 CORE_ADDR sig;
567 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
568 pass_signals[i] = 0;
569
570 from = arg_own_buf + 13;
571 while (from < end) {
572 to = strchr(from, ';');
573 if (to == NULL) to = end;
574 decode_address (&sig, from, to - from);
575 pass_signals[(int)sig] = 1;
philippe886fde32012-03-29 21:56:47 +0000576 dlog(1, "pass_signal gdb_nr %d %s\n",
577 (int)sig, target_signal_to_name(sig));
sewardj3b290482011-05-06 21:02:55 +0000578 from = to;
579 if (*from == ';') from++;
580 }
581 write_ok (arg_own_buf);
582 return;
583 }
584 /* Otherwise we didn't know what packet it was. Say we didn't
585 understand it. */
586 arg_own_buf[0] = 0;
587}
588
philippe02ea4132013-09-04 21:42:43 +0000589Bool VG_(client_monitor_command) (HChar *cmd)
philippe46207652013-01-20 17:11:58 +0000590{
591 const Bool connected = remote_connected();
592 const int saved_command_output_to_log = command_output_to_log;
593 Bool handled;
594
595 if (!connected)
596 command_output_to_log = True;
597 handled = handle_gdb_monitor_command (cmd);
598 if (!connected) {
599 // reset the log output unless cmd changed it.
600 if (command_output_to_log)
601 command_output_to_log = saved_command_output_to_log;
602 }
603 if (handled)
604 return False; // recognised
605 else
606 return True; // not recognised
607}
608
sewardj3b290482011-05-06 21:02:55 +0000609/* Handle all of the extended 'q' packets. */
610static
611void handle_query (char *arg_own_buf, int *new_packet_len_p)
612{
613 static struct inferior_list_entry *thread_ptr;
614
615 /* qRcmd, monitor command handling. */
616 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
617 char *p = arg_own_buf + 6;
618 int cmdlen = strlen(p)/2;
619 char cmd[cmdlen+1];
620
621 if (unhexify (cmd, p, cmdlen) != cmdlen) {
622 write_enn (arg_own_buf);
623 return;
624 }
625 cmd[cmdlen] = '\0';
626
627 if (handle_gdb_monitor_command (cmd)) {
sewardj3b290482011-05-06 21:02:55 +0000628 write_ok (arg_own_buf);
629 return;
630 } else {
631 /* cmd not recognised */
632 VG_(gdb_printf)
633 ("command '%s' not recognised\n"
634 "In gdb, try 'monitor help'\n"
635 "In a shell, try 'vgdb help'\n",
636 cmd);
637 write_ok (arg_own_buf);
638 return;
639 }
640 }
641
642 /* provide some valgrind specific info in return to qThreadExtraInfo. */
643 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
644 unsigned long gdb_id;
645 struct thread_info *ti;
646 ThreadState *tst;
647 char status[100];
648
649 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
650 ti = gdb_id_to_thread (gdb_id);
651 if (ti != NULL) {
652 tst = (ThreadState *) inferior_target_data (ti);
florian49789512013-09-16 17:08:50 +0000653 /* Additional info is the tid, the thread status and the thread's
654 name, if any. */
655 if (tst->thread_name) {
656 VG_(snprintf) (status, sizeof(status), "tid %d %s %s",
657 tst->tid,
658 VG_(name_of_ThreadStatus)(tst->status),
659 tst->thread_name);
660 } else {
661 VG_(snprintf) (status, sizeof(status), "tid %d %s",
662 tst->tid,
663 VG_(name_of_ThreadStatus)(tst->status));
664 }
sewardj3b290482011-05-06 21:02:55 +0000665 hexify (arg_own_buf, status, strlen(status));
666 return;
667 } else {
668 write_enn (arg_own_buf);
669 return;
670 }
671 }
672
673 if (strcmp ("qAttached", arg_own_buf) == 0) {
674 /* tell gdb to always detach, never kill the process */
675 arg_own_buf[0] = '1';
676 arg_own_buf[1] = 0;
677 return;
678 }
679
680 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
681 /* We have no symbol to read. */
682 write_ok (arg_own_buf);
683 return;
684 }
685
686 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
687 thread_ptr = all_threads.head;
688 VG_(sprintf) (arg_own_buf, "m%x",
689 thread_to_gdb_id ((struct thread_info *)thread_ptr));
690 thread_ptr = thread_ptr->next;
691 return;
692 }
693
694 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
695 if (thread_ptr != NULL) {
696 VG_(sprintf) (arg_own_buf, "m%x",
697 thread_to_gdb_id ((struct thread_info *)thread_ptr));
698 thread_ptr = thread_ptr->next;
699 return;
700 } else {
701 VG_(sprintf) (arg_own_buf, "l");
702 return;
703 }
704 }
705
philippe419d5f22012-05-24 21:33:17 +0000706 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
sewardj3b290482011-05-06 21:02:55 +0000707 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
708 CORE_ADDR ofs;
709 unsigned int len, doc_len;
florian6bd9dc12012-11-23 16:17:43 +0000710 const char *annex = NULL;
sewardj3b290482011-05-06 21:02:55 +0000711 // First, the annex is extracted from the packet received.
712 // Then, it is replaced by the corresponding file name.
713 int fd;
714
715 /* Grab the annex, offset, and length. */
716 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
717 strcpy (arg_own_buf, "E00");
718 return;
719 }
720
721 if (strcmp (annex, "target.xml") == 0) {
philippe419d5f22012-05-24 21:33:17 +0000722 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers));
723 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) {
724 /* Ensure the shadow registers are initialized. */
725 initialize_shadow_low(True);
sewardj3b290482011-05-06 21:02:55 +0000726 }
sewardj3b290482011-05-06 21:02:55 +0000727 if (annex == NULL) {
728 strcpy (arg_own_buf, "E00");
729 return;
730 }
731 }
732
733 {
philippe75a5f782012-02-24 11:25:58 +0000734 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
sewardj3b290482011-05-06 21:02:55 +0000735 struct vg_stat stat_doc;
736 char toread[len];
737 int len_read;
738
739 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
740 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
741 if (fd == -1) {
742 strcpy (arg_own_buf, "E00");
743 return;
744 }
745 if (VG_(fstat) (fd, &stat_doc) != 0) {
746 VG_(close) (fd);
747 strcpy (arg_own_buf, "E00");
748 return;
749 }
750 doc_len = stat_doc.size;
751
752 if (len > PBUFSIZ - POVERHSIZ)
753 len = PBUFSIZ - POVERHSIZ;
754
755 if (ofs > doc_len) {
756 write_enn (arg_own_buf);
757 VG_(close) (fd);
758 return;
759 }
760 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
761 len_read = VG_(read) (fd, toread, len);
florian1636d332012-11-15 04:27:04 +0000762 *new_packet_len_p = write_qxfer_response (arg_own_buf, (unsigned char *)toread,
sewardj3b290482011-05-06 21:02:55 +0000763 len_read, ofs + len_read < doc_len);
764 VG_(close) (fd);
765 return;
766 }
767 }
768
sewardj2a312392011-06-26 09:26:48 +0000769 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
770 unsigned char *data;
771 int n;
772 CORE_ADDR ofs;
773 unsigned int len;
florian6bd9dc12012-11-23 16:17:43 +0000774 const char *annex;
sewardj2a312392011-06-26 09:26:48 +0000775
776 /* Reject any annex; grab the offset and length. */
777 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
778 || annex[0] != '\0') {
779 strcpy (arg_own_buf, "E00");
780 return;
781 }
782
783 if (len > PBUFSIZ - 2)
784 len = PBUFSIZ - 2;
785 data = malloc (len);
786
787 {
788 UWord *client_auxv = VG_(client_auxv);
789 unsigned int client_auxv_len = 0;
790 while (*client_auxv != 0) {
791 dlog(4, "auxv %lld %llx\n",
792 (ULong)*client_auxv,
793 (ULong)*(client_auxv+1));
794 client_auxv++;
795 client_auxv++;
796 client_auxv_len += 2 * sizeof(UWord);
797 }
798 client_auxv_len += 2 * sizeof(UWord);
799 dlog(4, "auxv len %d\n", client_auxv_len);
800
801 if (ofs >= client_auxv_len)
802 n = -1;
803 else {
804 n = client_auxv_len - ofs;
805 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
806 }
807 }
808
809 if (n < 0)
810 write_enn (arg_own_buf);
811 else if (n > len)
812 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
813 else
814 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
815
816 free (data);
817
818 return;
819 }
820
821
sewardj3b290482011-05-06 21:02:55 +0000822 /* Protocol features query. */
823 if (strncmp ("qSupported", arg_own_buf, 10) == 0
824 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
825 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
826 /* Note: max packet size including frame and checksum, but without
827 trailing null byte, which is not sent/received. */
828
829 strcat (arg_own_buf, ";QStartNoAckMode+");
830 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +0000831 if (VG_(client_auxv))
832 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +0000833
philippe419d5f22012-05-24 21:33:17 +0000834 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
sewardj3b290482011-05-06 21:02:55 +0000835 strcat (arg_own_buf, ";qXfer:features:read+");
836 /* if a new gdb connects to us, we have to reset the register
837 set to the normal register sets to allow this new gdb to
838 decide to use or not the shadow registers.
839
840 Note that the reset is only done for gdb that are sending
841 qSupported packets. If a user first connected with a recent
842 gdb using shadow registers and then with a very old gdb
843 that does not use qSupported packet, then the old gdb will
844 not properly connect. */
845 initialize_shadow_low(False);
846 }
847 return;
848 }
849
850 /* Otherwise we didn't know what packet it was. Say we didn't
851 understand it. */
852 arg_own_buf[0] = 0;
853}
854
855/* Handle all of the extended 'v' packets. */
856static
sewardj0bb3c672011-07-26 23:29:25 +0000857void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +0000858{
859 /* vcont packet code from gdb 6.6 removed */
860
861 /* Otherwise we didn't know what packet it was. Say we didn't
862 understand it. */
863 arg_own_buf[0] = 0;
864 return;
865}
866
867static
868void myresume (int step, int sig)
869{
870 struct thread_resume resume_info[2];
871 int n = 0;
872
philippe349a3912012-05-23 21:50:36 +0000873 if (step || sig) {
sewardj3b290482011-05-06 21:02:55 +0000874 resume_info[0].step = step;
875 resume_info[0].sig = sig;
sewardj3b290482011-05-06 21:02:55 +0000876 n++;
877 }
sewardj3b290482011-05-06 21:02:55 +0000878 resume_info[n].step = 0;
879 resume_info[n].sig = 0;
sewardj3b290482011-05-06 21:02:55 +0000880
philippe349a3912012-05-23 21:50:36 +0000881 resume_reply_packet_needed = True;
882 valgrind_resume (resume_info);
sewardj3b290482011-05-06 21:02:55 +0000883}
884
885/* server_main global variables */
886static char *own_buf;
887static unsigned char *mem_buf;
888
889void gdbserver_init (void)
890{
891 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
892 noack_mode = False;
philippe349a3912012-05-23 21:50:36 +0000893 valgrind_initialize_target ();
philippe0e1cac92012-02-28 22:37:44 +0000894 // After a fork, gdbserver_init can be called again.
895 // We do not have to re-malloc the buffers in such a case.
896 if (own_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +0000897 own_buf = malloc (PBUFSIZ+POVERHSIZ);
philippe0e1cac92012-02-28 22:37:44 +0000898 if (mem_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +0000899 mem_buf = malloc (PBUFSIZ+POVERHSIZ);
900 // Note: normally, we should only malloc PBUFSIZ. However,
901 // GDB has a bug, and in some cases, sends e.g. 'm' packets
902 // asking for slightly more than the PacketSize given at
903 // connection initialisation. So, we bypass the GDB bug
904 // by allocating slightly more.
sewardj3b290482011-05-06 21:02:55 +0000905}
906
907void gdbserver_terminate (void)
908{
909 /* last call to gdbserver is cleanup call */
910 if (VG_MINIMAL_SETJMP(toplevel)) {
911 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
912 return;
913 }
914 remote_close();
915}
916
917void server_main (void)
918{
919 static char status;
sewardj0bb3c672011-07-26 23:29:25 +0000920 static int zignal;
sewardj3b290482011-05-06 21:02:55 +0000921
922 char ch;
923 int i = 0;
924 unsigned int len;
925 CORE_ADDR mem_addr;
926
philippe349a3912012-05-23 21:50:36 +0000927 zignal = valgrind_wait (&status);
sewardj3b290482011-05-06 21:02:55 +0000928 if (VG_MINIMAL_SETJMP(toplevel)) {
929 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
930 }
931 while (1) {
932 unsigned char sig;
933 int packet_len;
934 int new_packet_len = -1;
935
philippe349a3912012-05-23 21:50:36 +0000936 if (resume_reply_packet_needed) {
937 /* Send the resume reply to reply to last GDB resume
938 request. */
939 resume_reply_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +0000940 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000941 putpkt (own_buf);
942 }
943
philippe0447bbd2012-10-17 21:32:03 +0000944 /* If we our status is terminal (exit or fatal signal) get out
945 as quickly as we can. We won't be able to handle any request
946 anymore. */
947 if (status == 'W' || status == 'X') {
948 return;
949 }
950
sewardj3b290482011-05-06 21:02:55 +0000951 packet_len = getpkt (own_buf);
952 if (packet_len <= 0)
953 break;
954
955 i = 0;
956 ch = own_buf[i++];
957 switch (ch) {
958 case 'Q':
959 handle_set (own_buf, &new_packet_len);
960 break;
961 case 'q':
962 handle_query (own_buf, &new_packet_len);
963 break;
964 case 'd':
965 /* set/unset debugging is done through valgrind debug level. */
966 own_buf[0] = '\0';
967 break;
968 case 'D':
969 reset_valgrind_sink("gdb detaching from process");
970
971 /* When detaching or kill the process, gdb expects to get
972 an packet OK back. Any other output will make gdb
973 believes detach did not work. */
974 write_ok (own_buf);
975 putpkt (own_buf);
976 remote_finish (reset_after_error);
977 remote_open (VG_(clo_vgdb_prefix));
978 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +0000979 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +0000980 return;
981 case '!':
982 /* We can not use the extended protocol with valgrind,
983 because we can not restart the running
984 program. So return unrecognized. */
985 own_buf[0] = '\0';
986 break;
987 case '?':
sewardj0bb3c672011-07-26 23:29:25 +0000988 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000989 break;
990 case 'H':
991 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
992 unsigned long gdb_id, thread_id;
993
994 gdb_id = strtoul (&own_buf[2], NULL, 16);
995 thread_id = gdb_id_to_thread_id (gdb_id);
996 if (thread_id == 0) {
997 write_enn (own_buf);
998 break;
999 }
1000
1001 if (own_buf[1] == 'g') {
1002 general_thread = thread_id;
1003 set_desired_inferior (1);
1004 } else if (own_buf[1] == 'c') {
1005 cont_thread = thread_id;
1006 } else if (own_buf[1] == 's') {
1007 step_thread = thread_id;
1008 }
1009
1010 write_ok (own_buf);
1011 } else {
1012 /* Silently ignore it so that gdb can extend the protocol
1013 without compatibility headaches. */
1014 own_buf[0] = '\0';
1015 }
1016 break;
1017 case 'g':
1018 set_desired_inferior (1);
1019 registers_to_string (own_buf);
1020 break;
1021 case 'G':
1022 set_desired_inferior (1);
1023 registers_from_string (&own_buf[1]);
1024 write_ok (own_buf);
1025 break;
1026 case 'P': {
1027 int regno;
1028 char *regbytes;
1029 Bool mod;
1030 ThreadState *tst;
1031 regno = strtol(&own_buf[1], NULL, 16);
1032 regbytes = strchr(&own_buf[0], '=') + 1;
1033 set_desired_inferior (1);
1034 tst = (ThreadState *) inferior_target_data (current_inferior);
1035 /* Only accept changing registers in "runnable state3.
1036 In fact, it would be ok to change most of the registers
1037 except a few "sensitive" registers such as the PC, SP, BP.
1038 We assume we do not need to very specific here, and that we
1039 can just refuse all of these. */
1040 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
1041 supply_register_from_string (regno, regbytes, &mod);
1042 write_ok (own_buf);
1043 } else {
1044 /* at least from gdb 6.6 onwards, an E. error
1045 reply is shown to the user. So, we do an error
1046 msg which both is accepted by gdb as an error msg
1047 and is readable by the user. */
1048 VG_(sprintf)
1049 (own_buf,
1050"E.\n"
1051"ERROR changing register %s regno %d\n"
1052"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
1053"set pc, calling from gdb a function in the debugged process, ...)\n"
1054"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
1055"Thread status is %s\n",
1056 find_register_by_number (regno)->name, regno,
1057 VG_(name_of_ThreadStatus)(tst->status));
1058 if (VG_(clo_verbosity) > 1)
1059 VG_(umsg) ("%s\n", own_buf);
1060 }
1061 break;
1062 }
1063 case 'm':
1064 decode_m_packet (&own_buf[1], &mem_addr, &len);
philippe349a3912012-05-23 21:50:36 +00001065 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001066 convert_int_to_ascii (mem_buf, own_buf, len);
1067 else
1068 write_enn (own_buf);
1069 break;
1070 case 'M':
1071 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
philippe349a3912012-05-23 21:50:36 +00001072 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001073 write_ok (own_buf);
1074 else
1075 write_enn (own_buf);
1076 break;
1077 case 'X':
1078 if (decode_X_packet (&own_buf[1], packet_len - 1,
1079 &mem_addr, &len, mem_buf) < 0
philippe349a3912012-05-23 21:50:36 +00001080 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
sewardj3b290482011-05-06 21:02:55 +00001081 write_enn (own_buf);
1082 else
1083 write_ok (own_buf);
1084 break;
1085 case 'C':
1086 convert_ascii_to_int (own_buf + 1, &sig, 1);
1087 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +00001088 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001089 else
sewardj0bb3c672011-07-26 23:29:25 +00001090 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001091 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001092 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +00001093 return; // return control to valgrind
1094 case 'S':
1095 convert_ascii_to_int (own_buf + 1, &sig, 1);
1096 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +00001097 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001098 else
sewardj0bb3c672011-07-26 23:29:25 +00001099 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001100 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001101 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +00001102 return; // return control to valgrind
1103 case 'c':
1104 set_desired_inferior (0);
1105 myresume (0, 0);
1106 return; // return control to valgrind
1107 case 's':
1108 set_desired_inferior (0);
1109 myresume (1, 0);
1110 return; // return control to valgrind
1111 case 'Z': {
1112 char *lenptr;
1113 char *dataptr;
1114 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1115 int zlen = strtol (lenptr + 1, &dataptr, 16);
1116 char type = own_buf[1];
1117
philippe349a3912012-05-23 21:50:36 +00001118 if (type < '0' || type > '4') {
1119 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001120 own_buf[0] = '\0';
1121 } else {
1122 int res;
1123
philippe349a3912012-05-23 21:50:36 +00001124 res = valgrind_insert_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001125 if (res == 0)
1126 write_ok (own_buf);
1127 else if (res == 1)
1128 /* Unsupported. */
1129 own_buf[0] = '\0';
1130 else
1131 write_enn (own_buf);
1132 }
1133 break;
1134 }
1135 case 'z': {
1136 char *lenptr;
1137 char *dataptr;
1138 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1139 int zlen = strtol (lenptr + 1, &dataptr, 16);
1140 char type = own_buf[1];
1141
philippe349a3912012-05-23 21:50:36 +00001142 if (type < '0' || type > '4') {
1143 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001144 own_buf[0] = '\0';
1145 } else {
1146 int res;
1147
philippe349a3912012-05-23 21:50:36 +00001148 res = valgrind_remove_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001149 if (res == 0)
1150 write_ok (own_buf);
1151 else if (res == 1)
1152 /* Unsupported. */
1153 own_buf[0] = '\0';
1154 else
1155 write_enn (own_buf);
1156 }
1157 break;
1158 }
1159 case 'k':
1160 kill_request("Gdb request to kill this process\n");
1161 break;
1162 case 'T': {
1163 unsigned long gdb_id, thread_id;
1164
1165 gdb_id = strtoul (&own_buf[1], NULL, 16);
1166 thread_id = gdb_id_to_thread_id (gdb_id);
1167 if (thread_id == 0) {
1168 write_enn (own_buf);
1169 break;
1170 }
1171
philippe349a3912012-05-23 21:50:36 +00001172 if (valgrind_thread_alive (thread_id))
sewardj3b290482011-05-06 21:02:55 +00001173 write_ok (own_buf);
1174 else
1175 write_enn (own_buf);
1176 break;
1177 }
1178 case 'R':
1179 /* Restarting the inferior is only supported in the
1180 extended protocol.
1181 => It is a request we don't understand. Respond with an
1182 empty packet so that gdb knows that we don't support this
1183 request. */
1184 own_buf[0] = '\0';
1185 break;
1186 case 'v':
1187 /* Extended (long) request. */
sewardj0bb3c672011-07-26 23:29:25 +00001188 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001189 break;
1190 default:
1191 /* It is a request we don't understand. Respond with an
1192 empty packet so that gdb knows that we don't support this
1193 request. */
1194 own_buf[0] = '\0';
1195 break;
1196 }
1197
1198 if (new_packet_len != -1)
1199 putpkt_binary (own_buf, new_packet_len);
1200 else
1201 putpkt (own_buf);
1202
1203 if (status == 'W')
sewardj0bb3c672011-07-26 23:29:25 +00001204 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001205 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001206 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
sewardj0bb3c672011-07-26 23:29:25 +00001207 target_signal_to_host (zignal),
1208 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001209 if (status == 'W' || status == 'X') {
1210 VG_(umsg) ("Process exiting\n");
1211 VG_(exit) (0);
1212 }
1213 }
1214
1215 /* We come here when getpkt fails => close the connection,
1216 and re-open. Then return control to valgrind.
1217 We return the control to valgrind as we assume that
1218 the connection was closed due to vgdb having finished
1219 to execute a command. */
1220 if (VG_(clo_verbosity) > 1)
1221 VG_(umsg) ("Remote side has terminated connection. "
1222 "GDBserver will reopen the connection.\n");
1223 remote_finish (reset_after_error);
1224 remote_open (VG_(clo_vgdb_prefix));
1225 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001226 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001227 return;
1228}