blob: 9b30e678a51abfe877991e6976a5afab4403e72b [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"
philippe07c08522014-05-14 20:39:27 +000034#include "pub_core_debuginfo.h"
35#include "pub_core_addrinfo.h"
florian7463e492015-02-26 17:48:07 +000036#include "pub_core_aspacemgr.h"
sewardj3b290482011-05-06 21:02:55 +000037
38unsigned long cont_thread;
39unsigned long general_thread;
40unsigned long step_thread;
41unsigned long thread_from_wait;
42unsigned long old_thread_from_wait;
43
philippe886fde32012-03-29 21:56:47 +000044int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
bart2dafc542011-05-18 16:08:28 +000045
sewardj3b290482011-05-06 21:02:55 +000046/* for a gdbserver integrated in valgrind, resuming the process consists
47 in returning the control to valgrind.
philippe349a3912012-05-23 21:50:36 +000048 The guess process resumes its execution.
sewardj3b290482011-05-06 21:02:55 +000049 Then at the next error or break or ..., valgrind calls gdbserver again.
philippe349a3912012-05-23 21:50:36 +000050 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
sewardj3b290482011-05-06 21:02:55 +000053 must send a resume packet to gdb. */
philippe349a3912012-05-23 21:50:36 +000054static Bool resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +000055
56VG_MINIMAL_JMP_BUF(toplevel);
57
58/* Decode a qXfer read request. Return 0 if everything looks OK,
59 or -1 otherwise. */
60
61static
florian6bd9dc12012-11-23 16:17:43 +000062int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len)
sewardj3b290482011-05-06 21:02:55 +000063{
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;
71
72 /* After the read/write marker and annex, qXfer looks like a
73 traditional 'm' packet. */
74 decode_m_packet (buf, ofs, len);
75
76 return 0;
77}
78
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. */
83static
84int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
85{
86 int out_len;
87
88 if (is_more)
89 buf[0] = 'm';
90 else
91 buf[0] = 'l';
92
93 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
94 PBUFSIZ - POVERHSIZ - 1) + 1;
95}
96
97static Bool initial_valgrind_sink_saved = False;
98/* True <=> valgrind log sink saved in initial_valgrind_sink */
99static OutputSink initial_valgrind_sink;
100
101static Bool command_output_to_log = False;
102/* True <=> command output goes to log instead of gdb */
103
florian6bd9dc12012-11-23 16:17:43 +0000104void reset_valgrind_sink(const char *info)
sewardj3b290482011-05-06 21:02:55 +0000105{
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));
111 }
112}
113
philippe46207652013-01-20 17:11:58 +0000114void print_to_initial_valgrind_sink (const char *msg)
115{
116 vg_assert (initial_valgrind_sink_saved);
117 VG_(write) (initial_valgrind_sink.fd, msg, strlen(msg));
118}
119
120
sewardj3b290482011-05-06 21:02:55 +0000121static
florian6bd9dc12012-11-23 16:17:43 +0000122void kill_request (const char *msg)
sewardj3b290482011-05-06 21:02:55 +0000123{
124 VG_(umsg) ("%s", msg);
sewardj3b290482011-05-06 21:02:55 +0000125 VG_(exit) (0);
126}
127
philippe02ea4132013-09-04 21:42:43 +0000128// 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.
131static
132const char *wordn (const char *s, int n)
133{
134 int word_seen = 0;
135 Bool searching_word = True;
136
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;
146 }
147 }
148 s++;
149 }
150 return s;
151}
152
philippe4f6f3362014-04-19 00:25:54 +0000153void VG_(print_all_stats) (Bool memory_stats, Bool tool_stats)
154{
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)(Vg_DebugMsg, "------\n" );
161 VG_(print_all_arena_stats)();
162 if (VG_(clo_profile_heap))
163 VG_(print_arena_cc_analysis) ();
164 VG_(message)(Vg_DebugMsg, "\n");
165 }
166
167 VG_(print_translation_stats)();
168 VG_(print_tt_tc_stats)();
169 VG_(print_scheduler_stats)();
170 VG_(print_ExeContext_stats)( False /* with_stacktraces */ );
171 VG_(print_errormgr_stats)();
172 if (tool_stats && VG_(needs).print_stats) {
173 VG_TDICT_CALL(tool_print_stats);
174 }
175}
176
sewardj3b290482011-05-06 21:02:55 +0000177/* handle_gdb_valgrind_command handles the provided mon string command.
178 If command is recognised, return 1 else return 0.
179 Note that in case of ambiguous command, 1 is returned.
180
181 *sink_wanted_at_return is modified if one of the commands
sewardj30b3eca2011-06-28 08:20:39 +0000182 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000183*/
184static
philippe02ea4132013-09-04 21:42:43 +0000185int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return)
sewardj3b290482011-05-06 21:02:55 +0000186{
187 UWord ret = 0;
188 char s[strlen(mon)+1]; /* copy for strtok_r */
philippe02ea4132013-09-04 21:42:43 +0000189 char *wcmd;
190 HChar *ssaveptr;
191 const char *endptr;
sewardj3b290482011-05-06 21:02:55 +0000192 int kwdid;
193 int int_value;
194
195 vg_assert (initial_valgrind_sink_saved);
196
197 strcpy (s, mon);
198 wcmd = strtok_r (s, " ", &ssaveptr);
199 /* NB: if possible, avoid introducing a new command below which
sewardj30b3eca2011-06-28 08:20:39 +0000200 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000201 command. This ensures a shorter abbreviation for the user. */
philippe6ec8d632013-01-23 22:10:28 +0000202 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate"
203 " v.do",
sewardj3b290482011-05-06 21:02:55 +0000204 wcmd, kwd_report_duplicated_matches)) {
205 case -2:
206 ret = 1;
207 break;
208 case -1:
209 break;
210 case 0: /* help */
211 ret = 1;
212 wcmd = strtok_r (NULL, " ", &ssaveptr);
213 if (wcmd == NULL) {
214 int_value = 0;
215 } else {
216 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
217 case -2: int_value = 0; break;
218 case -1: int_value = 0; break;
219 case 0: int_value = 1; break;
floriane2800c92014-09-15 20:57:45 +0000220 default: vg_assert (0);
sewardj3b290482011-05-06 21:02:55 +0000221 }
222 }
223
224 VG_(gdb_printf) (
225"general valgrind monitor commands:\n"
philippec3360382012-10-21 14:37:14 +0000226" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000227" v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
228" v.info all_errors : show all errors found so far\n"
229" v.info last_error : show last error found\n"
philippe07c08522014-05-14 20:39:27 +0000230" v.info location <addr> : show information about location <addr>\n"
philippe02ea4132013-09-04 21:42:43 +0000231" 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 +0000232" v.info open_fds : show open file descriptors (only if --track-fds=yes)\n"
sewardj30b3eca2011-06-28 08:20:39 +0000233" v.kill : kill the Valgrind process\n"
234" v.set gdb_output : set valgrind output to gdb\n"
235" v.set log_output : set valgrind output to log\n"
236" v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
philippe46207652013-01-20 17:11:58 +0000237" v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n"
sewardj30b3eca2011-06-28 08:20:39 +0000238" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000239 if (int_value) { VG_(gdb_printf) (
240"debugging valgrind internals monitor commands:\n"
philippe6ec8d632013-01-23 22:10:28 +0000241" v.do expensive_sanity_check_general : do an expensive sanity check now\n"
sewardj30b3eca2011-06-28 08:20:39 +0000242" v.info gdbserver_status : show gdbserver status\n"
philippe93a6a8d2012-04-27 22:59:43 +0000243" v.info memory [aspacemgr] : show valgrind heap memory stats\n"
244" (with aspacemgr arg, also shows valgrind segments on log ouput)\n"
philippef3a6e932013-01-10 20:42:51 +0000245" v.info exectxt : show stacktraces and stats of all execontexts\n"
sewardjd6e13d82011-10-22 20:23:30 +0000246" v.info scheduler : show valgrind thread state and stacktrace\n"
philippe8587b542013-12-15 20:24:43 +0000247" v.info stats : show various valgrind and tool stats\n"
sewardj30b3eca2011-06-28 08:20:39 +0000248" v.set debuglog <level> : set valgrind debug log level to <level>\n"
philippe180a7502014-04-20 13:41:10 +0000249" v.set hostvisibility [yes*|no] : (en/dis)ables access by gdb/gdbserver to\n"
250" Valgrind internal host status/memory\n"
sewardj30b3eca2011-06-28 08:20:39 +0000251" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000252" (default traceflags 0b00100000 : show after instrumentation)\n"
253" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
254 }
255 break;
sewardj30b3eca2011-06-28 08:20:39 +0000256 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000257 ret = 1;
258 wcmd = strtok_r (NULL, " ", &ssaveptr);
259 switch (kwdid = VG_(keyword_id)
philippe46207652013-01-20 17:11:58 +0000260 ("vgdb-error debuglog merge-recursive-frames"
philippe64ba5742014-06-19 20:33:27 +0000261 " gdb_output log_output mixed_output hostvisibility",
sewardj3b290482011-05-06 21:02:55 +0000262 wcmd, kwd_report_all)) {
263 case -2:
264 case -1:
265 break;
266 case 0: /* vgdb-error */
267 case 1: /* debuglog */
philippe46207652013-01-20 17:11:58 +0000268 case 2: /* merge-recursive-frames */
sewardj3b290482011-05-06 21:02:55 +0000269 wcmd = strtok_r (NULL, " ", &ssaveptr);
270 if (wcmd == NULL) {
271 int_value = 0;
272 endptr = "empty"; /* to report an error below */
273 } else {
florian6bd9dc12012-11-23 16:17:43 +0000274 HChar *the_end;
275 int_value = strtol (wcmd, &the_end, 10);
276 endptr = the_end;
sewardj3b290482011-05-06 21:02:55 +0000277 }
278 if (*endptr != '\0') {
279 VG_(gdb_printf) ("missing or malformed integer value\n");
280 } else if (kwdid == 0) {
philippe02ea4132013-09-04 21:42:43 +0000281 VG_(printf) ("vgdb-error value changed from %d to %d\n",
sewardj3b290482011-05-06 21:02:55 +0000282 VG_(dyn_vgdb_error), int_value);
283 VG_(dyn_vgdb_error) = int_value;
284 } else if (kwdid == 1) {
philippe02ea4132013-09-04 21:42:43 +0000285 VG_(printf) ("debuglog value changed from %d to %d\n",
sewardj3b290482011-05-06 21:02:55 +0000286 VG_(debugLog_getLevel)(), int_value);
287 VG_(debugLog_startup) (int_value, "gdbsrv");
philippe46207652013-01-20 17:11:58 +0000288 } else if (kwdid == 2) {
philippe02ea4132013-09-04 21:42:43 +0000289 VG_(printf)
philippe46207652013-01-20 17:11:58 +0000290 ("merge-recursive-frames value changed from %d to %d\n",
291 VG_(clo_merge_recursive_frames), int_value);
292 VG_(clo_merge_recursive_frames) = int_value;
sewardj3b290482011-05-06 21:02:55 +0000293 } else {
294 vg_assert (0);
295 }
296 break;
philippe46207652013-01-20 17:11:58 +0000297 case 3: /* gdb_output */
sewardj3b290482011-05-06 21:02:55 +0000298 (*sink_wanted_at_return).fd = -2;
299 command_output_to_log = False;
300 VG_(gdb_printf) ("valgrind output will go to gdb\n");
301 break;
philippe46207652013-01-20 17:11:58 +0000302 case 4: /* log_output */
sewardj3b290482011-05-06 21:02:55 +0000303 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
304 command_output_to_log = True;
305 VG_(gdb_printf) ("valgrind output will go to log\n");
306 break;
philippe46207652013-01-20 17:11:58 +0000307 case 5: /* mixed output */
sewardj3b290482011-05-06 21:02:55 +0000308 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
309 command_output_to_log = False;
310 VG_(gdb_printf)
philipped4c37442015-03-21 16:13:08 +0000311 ("valgrind output will go to log, "
312 "interactive output will go to gdb\n");
sewardj3b290482011-05-06 21:02:55 +0000313 break;
philippe180a7502014-04-20 13:41:10 +0000314 case 6: /* hostvisibility */
315 wcmd = strtok_r (NULL, " ", &ssaveptr);
316 if (wcmd != NULL) {
317 switch (VG_(keyword_id) ("yes no", wcmd, kwd_report_all)) {
318 case -2:
319 case -1: break;
320 case 0:
321 hostvisibility = True;
322 break;
323 case 1:
324 hostvisibility = False;
325 break;
floriane2800c92014-09-15 20:57:45 +0000326 default: vg_assert (0);
philippe180a7502014-04-20 13:41:10 +0000327 }
328 } else {
329 hostvisibility = True;
330 }
philippe3a73b392014-06-09 15:47:46 +0000331 if (hostvisibility) {
philippe72a10e52014-07-31 21:15:42 +0000332 const DebugInfo *tooldi
333 = VG_(find_DebugInfo) ((Addr)handle_gdb_valgrind_command);
philippe45da6322015-03-28 12:23:07 +0000334 /* Normally, we should always find the tooldi. In case we
335 do not, suggest a 'likely somewhat working' address: */
336 const Addr tool_text_start
337 = tooldi ?
338 VG_(DebugInfo_get_text_avma) (tooldi) : 0x38000000;
339 const NSegment *toolseg
philippe72a10e52014-07-31 21:15:42 +0000340 = tooldi ?
341 VG_(am_find_nsegment) (VG_(DebugInfo_get_text_avma) (tooldi))
342 : NULL;
philippe180a7502014-04-20 13:41:10 +0000343 VG_(gdb_printf)
344 ("Enabled access to Valgrind memory/status by GDB\n"
philippe3a73b392014-06-09 15:47:46 +0000345 "If not yet done, tell GDB which valgrind file(s) to use, "
346 "typically:\n"
philippe72a10e52014-07-31 21:15:42 +0000347 "add-symbol-file %s %p\n",
348 toolseg ? VG_(am_get_filename)(toolseg)
349 : "<toolfile> <address> e.g.",
philippe45da6322015-03-28 12:23:07 +0000350 (void*)tool_text_start);
philippe3a73b392014-06-09 15:47:46 +0000351 } else
philippe180a7502014-04-20 13:41:10 +0000352 VG_(gdb_printf)
353 ("Disabled access to Valgrind memory/status by GDB\n");
354 break;
sewardj3b290482011-05-06 21:02:55 +0000355 default:
356 vg_assert (0);
357 }
358 break;
sewardj30b3eca2011-06-28 08:20:39 +0000359 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000360 ret = 1;
361 wcmd = strtok_r (NULL, " ", &ssaveptr);
362 switch (kwdid = VG_(keyword_id)
sewardjd6e13d82011-10-22 20:23:30 +0000363 ("all_errors n_errs_found last_error gdbserver_status memory"
philippe07c08522014-05-14 20:39:27 +0000364 " scheduler stats open_fds exectxt location",
sewardj3b290482011-05-06 21:02:55 +0000365 wcmd, kwd_report_all)) {
366 case -2:
367 case -1:
368 break;
369 case 0: // all_errors
370 // A verbosity of minimum 2 is needed to show the errors.
371 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
372 break;
373 case 1: // n_errs_found
philippe02ea4132013-09-04 21:42:43 +0000374 VG_(printf) ("n_errs_found %d n_errs_shown %d (vgdb-error %d) %s\n",
375 VG_(get_n_errs_found) (),
376 VG_(get_n_errs_shown) (),
377 VG_(dyn_vgdb_error),
378 wordn (mon, 3));
sewardj3b290482011-05-06 21:02:55 +0000379 break;
380 case 2: // last_error
381 VG_(show_last_error)();
382 break;
383 case 3: // gdbserver_status
384 VG_(gdbserver_status_output)();
385 break;
386 case 4: /* memory */
philippe8587b542013-12-15 20:24:43 +0000387 VG_(printf) ("%llu bytes have already been allocated.\n",
388 VG_(am_get_anonsize_total)());
sewardj3b290482011-05-06 21:02:55 +0000389 VG_(print_all_arena_stats) ();
390 if (VG_(clo_profile_heap))
391 VG_(print_arena_cc_analysis) ();
philippe93a6a8d2012-04-27 22:59:43 +0000392 wcmd = strtok_r (NULL, " ", &ssaveptr);
393 if (wcmd != NULL) {
394 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) {
395 case -2:
396 case -1: break;
397 case 0:
398 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr");
399 break;
floriane2800c92014-09-15 20:57:45 +0000400 default: vg_assert (0);
philippe93a6a8d2012-04-27 22:59:43 +0000401 }
402 }
403
sewardj3b290482011-05-06 21:02:55 +0000404 ret = 1;
405 break;
sewardjd6e13d82011-10-22 20:23:30 +0000406 case 5: /* scheduler */
philippe4f6f3362014-04-19 00:25:54 +0000407 VG_(show_sched_status) (True, // host_stacktrace
philippe38a74d22014-08-29 22:53:19 +0000408 True, // stack_usage
philippe4f6f3362014-04-19 00:25:54 +0000409 True); // exited_threads
sewardjd6e13d82011-10-22 20:23:30 +0000410 ret = 1;
411 break;
philippe8587b542013-12-15 20:24:43 +0000412 case 6: /* stats */
philippe4f6f3362014-04-19 00:25:54 +0000413 VG_(print_all_stats)(False, /* Memory stats */
414 True /* Tool stats */);
philippe8587b542013-12-15 20:24:43 +0000415 ret = 1;
416 break;
417 case 7: /* open_fds */
philippec3360382012-10-21 14:37:14 +0000418 if (VG_(clo_track_fds))
419 VG_(show_open_fds) ("");
420 else
421 VG_(gdb_printf)
422 ("Valgrind must be started with --track-fds=yes"
423 " to show open fds\n");
424 ret = 1;
425 break;
philippe8587b542013-12-15 20:24:43 +0000426 case 8: /* exectxt */
philippef3a6e932013-01-10 20:42:51 +0000427 VG_(print_ExeContext_stats) (True /* with_stacktraces */);
428 ret = 1;
429 break;
philippe07c08522014-05-14 20:39:27 +0000430 case 9: { /* location */
431 /* Note: we prefer 'v.info location' and not 'v.info address' as
432 v.info address is inconsistent with the GDB (native)
433 command 'info address' which gives the address for a symbol.
434 GDB equivalent command of 'v.info location' is 'info symbol'. */
435 Addr address;
436 SizeT dummy_sz = 0x1234;
philipped4c37442015-03-21 16:13:08 +0000437 if (VG_(strtok_get_address_and_size) (&address,
438 &dummy_sz, &ssaveptr)) {
philippe07c08522014-05-14 20:39:27 +0000439 // If tool provides location information, use that.
440 if (VG_(needs).info_location) {
441 VG_TDICT_CALL(tool_info_location, address);
442 }
philipped4c37442015-03-21 16:13:08 +0000443 // If tool does not provide location info, use the common one.
philippe07c08522014-05-14 20:39:27 +0000444 // Also use the common to compare with tool when debug log is set.
445 if (!VG_(needs).info_location || VG_(debugLog_getLevel)() > 0 ) {
446 AddrInfo ai;
447 ai.tag = Addr_Undescribed;
448 VG_(describe_addr) (address, &ai);
449 VG_(pp_addrinfo) (address, &ai);
450 VG_(clear_addrinfo) (&ai);
451 }
452 }
453 ret = 1;
454 break;
455 }
sewardj3b290482011-05-06 21:02:55 +0000456 default:
457 vg_assert(0);
458 }
459 break;
460 }
sewardj30b3eca2011-06-28 08:20:39 +0000461 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000462 wcmd = strtok_r (NULL, " ", &ssaveptr);
463 if (wcmd != NULL) {
florian6bd9dc12012-11-23 16:17:43 +0000464 int_value = strtol (wcmd, NULL, 10);
philippe02ea4132013-09-04 21:42:43 +0000465 VG_(printf) ("gdbserver: continuing in %d ms ...\n", int_value);
sewardj3b290482011-05-06 21:02:55 +0000466 VG_(poll)(NULL, 0, int_value);
467 }
philippe02ea4132013-09-04 21:42:43 +0000468 VG_(printf) ("gdbserver: continuing after wait ...\n");
sewardj3b290482011-05-06 21:02:55 +0000469 ret = 1;
470 break;
sewardj30b3eca2011-06-28 08:20:39 +0000471 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000472 kill_request ("monitor command request to kill this process\n");
473 break;
sewardj30b3eca2011-06-28 08:20:39 +0000474 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000475 Addr address;
476 SizeT verbosity = 0x20;
477
478 ret = 1;
479
philippe07c08522014-05-14 20:39:27 +0000480 if (VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr)) {
sewardj3b290482011-05-06 21:02:55 +0000481 /* we need to force the output to log for the translation trace,
482 as low level VEX tracing cannot be redirected to gdb. */
483 int saved_command_output_to_log = command_output_to_log;
484 int saved_fd = VG_(log_output_sink).fd;
485 Bool single_stepping_on_entry = valgrind_single_stepping();
486 int vex_verbosity = verbosity & 0xff;
487 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
488 if ((verbosity & 0x100) && !single_stepping_on_entry) {
489 valgrind_set_single_stepping(True);
490 // to force gdbserver instrumentation.
491 }
sewardj99d61342011-05-17 16:35:11 +0000492# if defined(VGA_arm)
493 // on arm, we need to (potentially) convert this address
494 // to the thumb form.
495 address = thumb_pc (address);
496# endif
497
sewardj3b290482011-05-06 21:02:55 +0000498 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
499 address,
500 /*debugging*/True,
501 (Int) vex_verbosity,
502 /*bbs_done*/0,
503 /*allow redir?*/True);
504 if ((verbosity & 0x100) && !single_stepping_on_entry) {
505 valgrind_set_single_stepping(False);
506 // reset single stepping.
507 }
508 command_output_to_log = saved_command_output_to_log;
509 VG_(log_output_sink).fd = saved_fd;
510 }
511 break;
512 }
513
philippe6ec8d632013-01-23 22:10:28 +0000514 case 6: /* v.do */
515 ret = 1;
516 wcmd = strtok_r (NULL, " ", &ssaveptr);
517 switch (VG_(keyword_id) ("expensive_sanity_check_general",
518 wcmd, kwd_report_all)) {
519 case -2:
520 case -1: break;
521 case 0: { /* expensive_sanity_check_general */
522 // Temporarily bump up sanity level to check e.g. the malloc arenas.
523 const Int save_clo_sanity_level = VG_(clo_sanity_level);
524 if (VG_(clo_sanity_level) < 4) VG_(clo_sanity_level) = 4;
525 VG_(sanity_check_general) (/* force_expensive */ True);
526 VG_(clo_sanity_level) = save_clo_sanity_level;
527 break;
528 }
floriane2800c92014-09-15 20:57:45 +0000529 default: vg_assert (0);
philippe6ec8d632013-01-23 22:10:28 +0000530 }
531 break;
532
sewardj3b290482011-05-06 21:02:55 +0000533 default:
534 vg_assert (0);
535 }
536 return ret;
537}
538
539/* handle_gdb_monitor_command handles the provided mon string command,
540 which can be either a "standard" valgrind monitor command
541 or a tool specific monitor command.
542 If command recognised, return 1 else return 0.
543 Note that in case of ambiguous command, 1 is returned.
544*/
545static
philippe02ea4132013-09-04 21:42:43 +0000546int handle_gdb_monitor_command (char *mon)
sewardj3b290482011-05-06 21:02:55 +0000547{
548 UWord ret = 0;
549 UWord tool_ret = 0;
550 // initially, we assume that when returning, the desired sink is the
551 // one we have when entering. It can however be changed by the standard
552 // valgrind command handling.
553 OutputSink sink_wanted_at_return = VG_(log_output_sink);
philipped4c37442015-03-21 16:13:08 +0000554 // When using gdbserver, we temporarily disable xml output.
555 Bool save_clo_xml = VG_(clo_xml);
556 VG_(clo_xml) = False;
sewardj3b290482011-05-06 21:02:55 +0000557
558 if (!initial_valgrind_sink_saved) {
559 /* first time we enter here, we save the valgrind default log sink */
560 initial_valgrind_sink = sink_wanted_at_return;
561 initial_valgrind_sink_saved = True;
562 }
563
564 if (!command_output_to_log)
565 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
566
567 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
568
569 /* Even if command was recognised by valgrind core, we call the
570 tool command handler : this is needed to handle help command
571 and/or to let the tool do some additional processing of a
572 valgrind standard command. Note however that if valgrind
573 recognised the command, we will always return success. */
574 if (VG_(needs).client_requests) {
575 /* If the tool reports an error when handling a monitor command,
576 we need to avoid calling gdbserver during this command
577 handling. So, we temporarily set VG_(dyn_vgdb_error) to
578 a huge value to ensure m_errormgr.c does not call gdbserver. */
579 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
580 UWord arg[2];
581 VG_(dyn_vgdb_error) = 999999999;
582 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
583 arg[1] = (UWord) mon;
584 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
585 &tool_ret);
586 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
587 }
588
philippe02ea4132013-09-04 21:42:43 +0000589 VG_(message_flush) ();
590
sewardj3b290482011-05-06 21:02:55 +0000591 /* restore or set the desired output */
592 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
philipped4c37442015-03-21 16:13:08 +0000593 VG_(clo_xml) = save_clo_xml;
594
sewardj3b290482011-05-06 21:02:55 +0000595 if (ret | tool_ret)
596 return 1;
597 else
598 return 0;
599}
600
601
602/* Handle all of the extended 'Q' packets. */
603static
604void handle_set (char *arg_own_buf, int *new_packet_len_p)
605{
606 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
607 noack_mode = True;
608 write_ok (arg_own_buf);
609 return;
610 }
611
612 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
613 int i;
614 char *from, *to;
615 char *end = arg_own_buf + strlen(arg_own_buf);
616 CORE_ADDR sig;
617 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
618 pass_signals[i] = 0;
619
620 from = arg_own_buf + 13;
621 while (from < end) {
622 to = strchr(from, ';');
623 if (to == NULL) to = end;
624 decode_address (&sig, from, to - from);
625 pass_signals[(int)sig] = 1;
philippe75d8a4e2015-03-26 21:32:03 +0000626 dlog(3, "pass_signal gdb_nr %d %s\n",
philippe886fde32012-03-29 21:56:47 +0000627 (int)sig, target_signal_to_name(sig));
sewardj3b290482011-05-06 21:02:55 +0000628 from = to;
629 if (*from == ';') from++;
630 }
631 write_ok (arg_own_buf);
632 return;
633 }
634 /* Otherwise we didn't know what packet it was. Say we didn't
635 understand it. */
636 arg_own_buf[0] = 0;
637}
638
philippe02ea4132013-09-04 21:42:43 +0000639Bool VG_(client_monitor_command) (HChar *cmd)
philippe46207652013-01-20 17:11:58 +0000640{
641 const Bool connected = remote_connected();
642 const int saved_command_output_to_log = command_output_to_log;
643 Bool handled;
644
645 if (!connected)
646 command_output_to_log = True;
647 handled = handle_gdb_monitor_command (cmd);
648 if (!connected) {
649 // reset the log output unless cmd changed it.
650 if (command_output_to_log)
651 command_output_to_log = saved_command_output_to_log;
652 }
653 if (handled)
654 return False; // recognised
655 else
656 return True; // not recognised
657}
658
sewardj3b290482011-05-06 21:02:55 +0000659/* Handle all of the extended 'q' packets. */
660static
661void handle_query (char *arg_own_buf, int *new_packet_len_p)
662{
663 static struct inferior_list_entry *thread_ptr;
664
philippe1670b052014-08-15 10:27:52 +0000665 /* thread local storage query */
666 if (strncmp ("qGetTLSAddr:", arg_own_buf, 12) == 0) {
667 char *from, *to;
668 char *end = arg_own_buf + strlen(arg_own_buf);
669 unsigned long gdb_id;
670 CORE_ADDR lm;
671 CORE_ADDR offset;
672 struct thread_info *ti;
673
674 from = arg_own_buf + 12;
675 to = strchr(from, ',');
676 *to = 0;
677 gdb_id = strtoul (from, NULL, 16);
678 from = to + 1;
679 to = strchr(from, ',');
680 decode_address (&offset, from, to - from);
681 from = to + 1;
682 to = end;
683 decode_address (&lm, from, to - from);
684 dlog(2, "qGetTLSAddr thread %lu offset %p lm %p\n",
685 gdb_id, (void*)offset, (void*)lm);
686
687 ti = gdb_id_to_thread (gdb_id);
688 if (ti != NULL) {
689 ThreadState *tst;
690 Addr tls_addr;
691
692 tst = (ThreadState *) inferior_target_data (ti);
693 if (valgrind_get_tls_addr(tst, offset, lm, &tls_addr)) {
694 VG_(sprintf) (arg_own_buf, "%lx", tls_addr);
695 return;
696 }
697 // else we will report we do not support qGetTLSAddr
698 } else {
699 write_enn (arg_own_buf);
700 return;
701 }
702 }
703
sewardj3b290482011-05-06 21:02:55 +0000704 /* qRcmd, monitor command handling. */
705 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
706 char *p = arg_own_buf + 6;
707 int cmdlen = strlen(p)/2;
708 char cmd[cmdlen+1];
709
710 if (unhexify (cmd, p, cmdlen) != cmdlen) {
711 write_enn (arg_own_buf);
712 return;
713 }
714 cmd[cmdlen] = '\0';
715
716 if (handle_gdb_monitor_command (cmd)) {
sewardj3b290482011-05-06 21:02:55 +0000717 write_ok (arg_own_buf);
718 return;
719 } else {
720 /* cmd not recognised */
721 VG_(gdb_printf)
722 ("command '%s' not recognised\n"
723 "In gdb, try 'monitor help'\n"
724 "In a shell, try 'vgdb help'\n",
725 cmd);
726 write_ok (arg_own_buf);
727 return;
728 }
729 }
730
731 /* provide some valgrind specific info in return to qThreadExtraInfo. */
732 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
733 unsigned long gdb_id;
734 struct thread_info *ti;
735 ThreadState *tst;
sewardj3b290482011-05-06 21:02:55 +0000736
737 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
738 ti = gdb_id_to_thread (gdb_id);
739 if (ti != NULL) {
740 tst = (ThreadState *) inferior_target_data (ti);
florian49789512013-09-16 17:08:50 +0000741 /* Additional info is the tid, the thread status and the thread's
742 name, if any. */
florian7b7d5942014-12-19 20:29:22 +0000743 SizeT len = strlen(VG_(name_of_ThreadStatus)(tst->status)) + 20;
744 if (tst->thread_name) len += strlen(tst->thread_name);
745 /* As the string will be hexified and copied into own_buf we need
746 to limit the length to avoid buffer overflow. */
747 if (len * 2 > (PBUFSIZ + POVERHSIZ))
748 len = (PBUFSIZ + POVERHSIZ) / 2;
749 char status[len];
florian49789512013-09-16 17:08:50 +0000750 if (tst->thread_name) {
751 VG_(snprintf) (status, sizeof(status), "tid %d %s %s",
752 tst->tid,
753 VG_(name_of_ThreadStatus)(tst->status),
754 tst->thread_name);
755 } else {
756 VG_(snprintf) (status, sizeof(status), "tid %d %s",
757 tst->tid,
758 VG_(name_of_ThreadStatus)(tst->status));
759 }
sewardj3b290482011-05-06 21:02:55 +0000760 hexify (arg_own_buf, status, strlen(status));
761 return;
762 } else {
763 write_enn (arg_own_buf);
764 return;
765 }
766 }
philippe1670b052014-08-15 10:27:52 +0000767
sewardj3b290482011-05-06 21:02:55 +0000768 if (strcmp ("qAttached", arg_own_buf) == 0) {
769 /* tell gdb to always detach, never kill the process */
770 arg_own_buf[0] = '1';
771 arg_own_buf[1] = 0;
772 return;
773 }
774
775 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
776 /* We have no symbol to read. */
777 write_ok (arg_own_buf);
778 return;
779 }
780
781 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
782 thread_ptr = all_threads.head;
783 VG_(sprintf) (arg_own_buf, "m%x",
784 thread_to_gdb_id ((struct thread_info *)thread_ptr));
785 thread_ptr = thread_ptr->next;
786 return;
787 }
788
789 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
790 if (thread_ptr != NULL) {
791 VG_(sprintf) (arg_own_buf, "m%x",
792 thread_to_gdb_id ((struct thread_info *)thread_ptr));
793 thread_ptr = thread_ptr->next;
794 return;
795 } else {
796 VG_(sprintf) (arg_own_buf, "l");
797 return;
798 }
799 }
800
philippe419d5f22012-05-24 21:33:17 +0000801 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
sewardj3b290482011-05-06 21:02:55 +0000802 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
803 CORE_ADDR ofs;
804 unsigned int len, doc_len;
florian6bd9dc12012-11-23 16:17:43 +0000805 const char *annex = NULL;
sewardj3b290482011-05-06 21:02:55 +0000806 // First, the annex is extracted from the packet received.
807 // Then, it is replaced by the corresponding file name.
808 int fd;
809
810 /* Grab the annex, offset, and length. */
811 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
812 strcpy (arg_own_buf, "E00");
813 return;
814 }
815
816 if (strcmp (annex, "target.xml") == 0) {
philippe419d5f22012-05-24 21:33:17 +0000817 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers));
818 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) {
819 /* Ensure the shadow registers are initialized. */
820 initialize_shadow_low(True);
sewardj3b290482011-05-06 21:02:55 +0000821 }
sewardj3b290482011-05-06 21:02:55 +0000822 if (annex == NULL) {
823 strcpy (arg_own_buf, "E00");
824 return;
825 }
826 }
827
828 {
philippe75a5f782012-02-24 11:25:58 +0000829 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
sewardj3b290482011-05-06 21:02:55 +0000830 struct vg_stat stat_doc;
831 char toread[len];
832 int len_read;
833
834 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
835 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
836 if (fd == -1) {
837 strcpy (arg_own_buf, "E00");
838 return;
839 }
840 if (VG_(fstat) (fd, &stat_doc) != 0) {
841 VG_(close) (fd);
842 strcpy (arg_own_buf, "E00");
843 return;
844 }
845 doc_len = stat_doc.size;
846
847 if (len > PBUFSIZ - POVERHSIZ)
848 len = PBUFSIZ - POVERHSIZ;
849
850 if (ofs > doc_len) {
851 write_enn (arg_own_buf);
852 VG_(close) (fd);
853 return;
854 }
855 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
856 len_read = VG_(read) (fd, toread, len);
florian1636d332012-11-15 04:27:04 +0000857 *new_packet_len_p = write_qxfer_response (arg_own_buf, (unsigned char *)toread,
sewardj3b290482011-05-06 21:02:55 +0000858 len_read, ofs + len_read < doc_len);
859 VG_(close) (fd);
860 return;
861 }
862 }
863
sewardj2a312392011-06-26 09:26:48 +0000864 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
865 unsigned char *data;
866 int n;
867 CORE_ADDR ofs;
868 unsigned int len;
florian6bd9dc12012-11-23 16:17:43 +0000869 const char *annex;
sewardj2a312392011-06-26 09:26:48 +0000870
871 /* Reject any annex; grab the offset and length. */
872 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
873 || annex[0] != '\0') {
874 strcpy (arg_own_buf, "E00");
875 return;
876 }
877
878 if (len > PBUFSIZ - 2)
879 len = PBUFSIZ - 2;
880 data = malloc (len);
881
882 {
883 UWord *client_auxv = VG_(client_auxv);
884 unsigned int client_auxv_len = 0;
885 while (*client_auxv != 0) {
886 dlog(4, "auxv %lld %llx\n",
887 (ULong)*client_auxv,
888 (ULong)*(client_auxv+1));
889 client_auxv++;
890 client_auxv++;
891 client_auxv_len += 2 * sizeof(UWord);
892 }
893 client_auxv_len += 2 * sizeof(UWord);
894 dlog(4, "auxv len %d\n", client_auxv_len);
895
896 if (ofs >= client_auxv_len)
897 n = -1;
898 else {
899 n = client_auxv_len - ofs;
900 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
901 }
902 }
903
904 if (n < 0)
905 write_enn (arg_own_buf);
906 else if (n > len)
907 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
908 else
909 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
910
911 free (data);
912
913 return;
914 }
915
916
sewardj3b290482011-05-06 21:02:55 +0000917 /* Protocol features query. */
918 if (strncmp ("qSupported", arg_own_buf, 10) == 0
919 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
920 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
921 /* Note: max packet size including frame and checksum, but without
922 trailing null byte, which is not sent/received. */
923
924 strcat (arg_own_buf, ";QStartNoAckMode+");
925 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +0000926 if (VG_(client_auxv))
927 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +0000928
philippe419d5f22012-05-24 21:33:17 +0000929 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
sewardj3b290482011-05-06 21:02:55 +0000930 strcat (arg_own_buf, ";qXfer:features:read+");
931 /* if a new gdb connects to us, we have to reset the register
932 set to the normal register sets to allow this new gdb to
933 decide to use or not the shadow registers.
934
935 Note that the reset is only done for gdb that are sending
936 qSupported packets. If a user first connected with a recent
937 gdb using shadow registers and then with a very old gdb
938 that does not use qSupported packet, then the old gdb will
939 not properly connect. */
940 initialize_shadow_low(False);
941 }
942 return;
943 }
944
945 /* Otherwise we didn't know what packet it was. Say we didn't
946 understand it. */
947 arg_own_buf[0] = 0;
948}
949
950/* Handle all of the extended 'v' packets. */
951static
sewardj0bb3c672011-07-26 23:29:25 +0000952void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +0000953{
954 /* vcont packet code from gdb 6.6 removed */
955
956 /* Otherwise we didn't know what packet it was. Say we didn't
957 understand it. */
958 arg_own_buf[0] = 0;
959 return;
960}
961
962static
963void myresume (int step, int sig)
964{
965 struct thread_resume resume_info[2];
966 int n = 0;
967
philippe349a3912012-05-23 21:50:36 +0000968 if (step || sig) {
sewardj3b290482011-05-06 21:02:55 +0000969 resume_info[0].step = step;
970 resume_info[0].sig = sig;
sewardj3b290482011-05-06 21:02:55 +0000971 n++;
972 }
sewardj3b290482011-05-06 21:02:55 +0000973 resume_info[n].step = 0;
974 resume_info[n].sig = 0;
sewardj3b290482011-05-06 21:02:55 +0000975
philippe349a3912012-05-23 21:50:36 +0000976 resume_reply_packet_needed = True;
977 valgrind_resume (resume_info);
sewardj3b290482011-05-06 21:02:55 +0000978}
979
980/* server_main global variables */
981static char *own_buf;
982static unsigned char *mem_buf;
983
984void gdbserver_init (void)
985{
986 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
987 noack_mode = False;
philippe349a3912012-05-23 21:50:36 +0000988 valgrind_initialize_target ();
philippe0e1cac92012-02-28 22:37:44 +0000989 // After a fork, gdbserver_init can be called again.
990 // We do not have to re-malloc the buffers in such a case.
991 if (own_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +0000992 own_buf = malloc (PBUFSIZ+POVERHSIZ);
philippe0e1cac92012-02-28 22:37:44 +0000993 if (mem_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +0000994 mem_buf = malloc (PBUFSIZ+POVERHSIZ);
995 // Note: normally, we should only malloc PBUFSIZ. However,
996 // GDB has a bug, and in some cases, sends e.g. 'm' packets
997 // asking for slightly more than the PacketSize given at
998 // connection initialisation. So, we bypass the GDB bug
999 // by allocating slightly more.
sewardj3b290482011-05-06 21:02:55 +00001000}
1001
1002void gdbserver_terminate (void)
1003{
1004 /* last call to gdbserver is cleanup call */
1005 if (VG_MINIMAL_SETJMP(toplevel)) {
1006 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
1007 return;
1008 }
1009 remote_close();
1010}
1011
1012void server_main (void)
1013{
1014 static char status;
sewardj0bb3c672011-07-26 23:29:25 +00001015 static int zignal;
sewardj3b290482011-05-06 21:02:55 +00001016
1017 char ch;
1018 int i = 0;
1019 unsigned int len;
1020 CORE_ADDR mem_addr;
1021
philippe349a3912012-05-23 21:50:36 +00001022 zignal = valgrind_wait (&status);
sewardj3b290482011-05-06 21:02:55 +00001023 if (VG_MINIMAL_SETJMP(toplevel)) {
1024 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
1025 }
1026 while (1) {
1027 unsigned char sig;
1028 int packet_len;
1029 int new_packet_len = -1;
1030
philippe349a3912012-05-23 21:50:36 +00001031 if (resume_reply_packet_needed) {
1032 /* Send the resume reply to reply to last GDB resume
1033 request. */
1034 resume_reply_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +00001035 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +00001036 putpkt (own_buf);
1037 }
1038
philippe0447bbd2012-10-17 21:32:03 +00001039 /* If we our status is terminal (exit or fatal signal) get out
1040 as quickly as we can. We won't be able to handle any request
1041 anymore. */
1042 if (status == 'W' || status == 'X') {
1043 return;
1044 }
1045
sewardj3b290482011-05-06 21:02:55 +00001046 packet_len = getpkt (own_buf);
1047 if (packet_len <= 0)
1048 break;
1049
1050 i = 0;
1051 ch = own_buf[i++];
1052 switch (ch) {
1053 case 'Q':
1054 handle_set (own_buf, &new_packet_len);
1055 break;
1056 case 'q':
1057 handle_query (own_buf, &new_packet_len);
1058 break;
1059 case 'd':
1060 /* set/unset debugging is done through valgrind debug level. */
1061 own_buf[0] = '\0';
1062 break;
1063 case 'D':
1064 reset_valgrind_sink("gdb detaching from process");
1065
1066 /* When detaching or kill the process, gdb expects to get
1067 an packet OK back. Any other output will make gdb
1068 believes detach did not work. */
1069 write_ok (own_buf);
1070 putpkt (own_buf);
1071 remote_finish (reset_after_error);
1072 remote_open (VG_(clo_vgdb_prefix));
1073 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001074 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001075 return;
1076 case '!':
1077 /* We can not use the extended protocol with valgrind,
1078 because we can not restart the running
1079 program. So return unrecognized. */
1080 own_buf[0] = '\0';
1081 break;
1082 case '?':
sewardj0bb3c672011-07-26 23:29:25 +00001083 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +00001084 break;
1085 case 'H':
1086 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
1087 unsigned long gdb_id, thread_id;
1088
1089 gdb_id = strtoul (&own_buf[2], NULL, 16);
1090 thread_id = gdb_id_to_thread_id (gdb_id);
1091 if (thread_id == 0) {
1092 write_enn (own_buf);
1093 break;
1094 }
1095
1096 if (own_buf[1] == 'g') {
1097 general_thread = thread_id;
1098 set_desired_inferior (1);
1099 } else if (own_buf[1] == 'c') {
1100 cont_thread = thread_id;
1101 } else if (own_buf[1] == 's') {
1102 step_thread = thread_id;
1103 }
1104
1105 write_ok (own_buf);
1106 } else {
1107 /* Silently ignore it so that gdb can extend the protocol
1108 without compatibility headaches. */
1109 own_buf[0] = '\0';
1110 }
1111 break;
1112 case 'g':
1113 set_desired_inferior (1);
1114 registers_to_string (own_buf);
1115 break;
1116 case 'G':
1117 set_desired_inferior (1);
1118 registers_from_string (&own_buf[1]);
1119 write_ok (own_buf);
1120 break;
1121 case 'P': {
1122 int regno;
1123 char *regbytes;
1124 Bool mod;
1125 ThreadState *tst;
1126 regno = strtol(&own_buf[1], NULL, 16);
1127 regbytes = strchr(&own_buf[0], '=') + 1;
1128 set_desired_inferior (1);
1129 tst = (ThreadState *) inferior_target_data (current_inferior);
1130 /* Only accept changing registers in "runnable state3.
1131 In fact, it would be ok to change most of the registers
1132 except a few "sensitive" registers such as the PC, SP, BP.
1133 We assume we do not need to very specific here, and that we
1134 can just refuse all of these. */
1135 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
1136 supply_register_from_string (regno, regbytes, &mod);
1137 write_ok (own_buf);
1138 } else {
1139 /* at least from gdb 6.6 onwards, an E. error
1140 reply is shown to the user. So, we do an error
1141 msg which both is accepted by gdb as an error msg
1142 and is readable by the user. */
1143 VG_(sprintf)
1144 (own_buf,
1145"E.\n"
1146"ERROR changing register %s regno %d\n"
1147"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
1148"set pc, calling from gdb a function in the debugged process, ...)\n"
1149"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
1150"Thread status is %s\n",
1151 find_register_by_number (regno)->name, regno,
1152 VG_(name_of_ThreadStatus)(tst->status));
1153 if (VG_(clo_verbosity) > 1)
1154 VG_(umsg) ("%s\n", own_buf);
1155 }
1156 break;
1157 }
1158 case 'm':
1159 decode_m_packet (&own_buf[1], &mem_addr, &len);
philippe349a3912012-05-23 21:50:36 +00001160 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001161 convert_int_to_ascii (mem_buf, own_buf, len);
1162 else
1163 write_enn (own_buf);
1164 break;
1165 case 'M':
1166 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
philippe349a3912012-05-23 21:50:36 +00001167 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001168 write_ok (own_buf);
1169 else
1170 write_enn (own_buf);
1171 break;
1172 case 'X':
1173 if (decode_X_packet (&own_buf[1], packet_len - 1,
1174 &mem_addr, &len, mem_buf) < 0
philippe349a3912012-05-23 21:50:36 +00001175 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
sewardj3b290482011-05-06 21:02:55 +00001176 write_enn (own_buf);
1177 else
1178 write_ok (own_buf);
1179 break;
1180 case 'C':
1181 convert_ascii_to_int (own_buf + 1, &sig, 1);
1182 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +00001183 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001184 else
sewardj0bb3c672011-07-26 23:29:25 +00001185 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001186 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001187 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +00001188 return; // return control to valgrind
1189 case 'S':
1190 convert_ascii_to_int (own_buf + 1, &sig, 1);
1191 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +00001192 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001193 else
sewardj0bb3c672011-07-26 23:29:25 +00001194 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001195 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001196 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +00001197 return; // return control to valgrind
1198 case 'c':
1199 set_desired_inferior (0);
1200 myresume (0, 0);
1201 return; // return control to valgrind
1202 case 's':
1203 set_desired_inferior (0);
1204 myresume (1, 0);
1205 return; // return control to valgrind
1206 case 'Z': {
1207 char *lenptr;
1208 char *dataptr;
1209 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1210 int zlen = strtol (lenptr + 1, &dataptr, 16);
1211 char type = own_buf[1];
1212
philippe349a3912012-05-23 21:50:36 +00001213 if (type < '0' || type > '4') {
1214 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001215 own_buf[0] = '\0';
1216 } else {
1217 int res;
1218
philippe349a3912012-05-23 21:50:36 +00001219 res = valgrind_insert_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001220 if (res == 0)
1221 write_ok (own_buf);
1222 else if (res == 1)
1223 /* Unsupported. */
1224 own_buf[0] = '\0';
1225 else
1226 write_enn (own_buf);
1227 }
1228 break;
1229 }
1230 case 'z': {
1231 char *lenptr;
1232 char *dataptr;
1233 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1234 int zlen = strtol (lenptr + 1, &dataptr, 16);
1235 char type = own_buf[1];
1236
philippe349a3912012-05-23 21:50:36 +00001237 if (type < '0' || type > '4') {
1238 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001239 own_buf[0] = '\0';
1240 } else {
1241 int res;
1242
philippe349a3912012-05-23 21:50:36 +00001243 res = valgrind_remove_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001244 if (res == 0)
1245 write_ok (own_buf);
1246 else if (res == 1)
1247 /* Unsupported. */
1248 own_buf[0] = '\0';
1249 else
1250 write_enn (own_buf);
1251 }
1252 break;
1253 }
1254 case 'k':
1255 kill_request("Gdb request to kill this process\n");
1256 break;
1257 case 'T': {
1258 unsigned long gdb_id, thread_id;
1259
1260 gdb_id = strtoul (&own_buf[1], NULL, 16);
1261 thread_id = gdb_id_to_thread_id (gdb_id);
1262 if (thread_id == 0) {
1263 write_enn (own_buf);
1264 break;
1265 }
1266
philippe349a3912012-05-23 21:50:36 +00001267 if (valgrind_thread_alive (thread_id))
sewardj3b290482011-05-06 21:02:55 +00001268 write_ok (own_buf);
1269 else
1270 write_enn (own_buf);
1271 break;
1272 }
1273 case 'R':
1274 /* Restarting the inferior is only supported in the
1275 extended protocol.
1276 => It is a request we don't understand. Respond with an
1277 empty packet so that gdb knows that we don't support this
1278 request. */
1279 own_buf[0] = '\0';
1280 break;
1281 case 'v':
1282 /* Extended (long) request. */
sewardj0bb3c672011-07-26 23:29:25 +00001283 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001284 break;
1285 default:
1286 /* It is a request we don't understand. Respond with an
1287 empty packet so that gdb knows that we don't support this
1288 request. */
1289 own_buf[0] = '\0';
1290 break;
1291 }
1292
1293 if (new_packet_len != -1)
1294 putpkt_binary (own_buf, new_packet_len);
1295 else
1296 putpkt (own_buf);
1297
1298 if (status == 'W')
sewardj0bb3c672011-07-26 23:29:25 +00001299 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001300 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001301 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
sewardj0bb3c672011-07-26 23:29:25 +00001302 target_signal_to_host (zignal),
1303 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001304 if (status == 'W' || status == 'X') {
1305 VG_(umsg) ("Process exiting\n");
1306 VG_(exit) (0);
1307 }
1308 }
1309
1310 /* We come here when getpkt fails => close the connection,
1311 and re-open. Then return control to valgrind.
1312 We return the control to valgrind as we assume that
1313 the connection was closed due to vgdb having finished
1314 to execute a command. */
1315 if (VG_(clo_verbosity) > 1)
1316 VG_(umsg) ("Remote side has terminated connection. "
1317 "GDBserver will reopen the connection.\n");
1318 remote_finish (reset_after_error);
1319 remote_open (VG_(clo_vgdb_prefix));
1320 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001321 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001322 return;
1323}