blob: 72d6660d3e7f8048231edf0d789d13e5b177b7bf [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"
sewardj3b290482011-05-06 21:02:55 +000036
37unsigned long cont_thread;
38unsigned long general_thread;
39unsigned long step_thread;
40unsigned long thread_from_wait;
41unsigned long old_thread_from_wait;
42
philippe886fde32012-03-29 21:56:47 +000043int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
bart2dafc542011-05-18 16:08:28 +000044
sewardj3b290482011-05-06 21:02:55 +000045/* for a gdbserver integrated in valgrind, resuming the process consists
46 in returning the control to valgrind.
philippe349a3912012-05-23 21:50:36 +000047 The guess process resumes its execution.
sewardj3b290482011-05-06 21:02:55 +000048 Then at the next error or break or ..., valgrind calls gdbserver again.
philippe349a3912012-05-23 21:50:36 +000049 A resume reply packet must then be built to inform GDB that the
50 resume request is finished.
51 resume_reply_packet_needed records the fact that the next call to gdbserver
sewardj3b290482011-05-06 21:02:55 +000052 must send a resume packet to gdb. */
philippe349a3912012-05-23 21:50:36 +000053static Bool resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +000054
55VG_MINIMAL_JMP_BUF(toplevel);
56
57/* Decode a qXfer read request. Return 0 if everything looks OK,
58 or -1 otherwise. */
59
60static
florian6bd9dc12012-11-23 16:17:43 +000061int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len)
sewardj3b290482011-05-06 21:02:55 +000062{
63 /* Extract and NUL-terminate the annex. */
64 *annex = buf;
65 while (*buf && *buf != ':')
66 buf++;
67 if (*buf == '\0')
68 return -1;
69 *buf++ = 0;
70
71 /* After the read/write marker and annex, qXfer looks like a
72 traditional 'm' packet. */
73 decode_m_packet (buf, ofs, len);
74
75 return 0;
76}
77
78/* Write the response to a successful qXfer read. Returns the
79 length of the (binary) data stored in BUF, corresponding
80 to as much of DATA/LEN as we could fit. IS_MORE controls
81 the first character of the response. */
82static
83int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
84{
85 int out_len;
86
87 if (is_more)
88 buf[0] = 'm';
89 else
90 buf[0] = 'l';
91
92 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
93 PBUFSIZ - POVERHSIZ - 1) + 1;
94}
95
96static Bool initial_valgrind_sink_saved = False;
97/* True <=> valgrind log sink saved in initial_valgrind_sink */
98static OutputSink initial_valgrind_sink;
99
100static Bool command_output_to_log = False;
101/* True <=> command output goes to log instead of gdb */
102
florian6bd9dc12012-11-23 16:17:43 +0000103void reset_valgrind_sink(const char *info)
sewardj3b290482011-05-06 21:02:55 +0000104{
105 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
106 && initial_valgrind_sink_saved) {
107 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
108 VG_(umsg) ("Reset valgrind output to log (%s)\n",
109 (info = NULL ? "" : info));
110 }
111}
112
philippe46207652013-01-20 17:11:58 +0000113void print_to_initial_valgrind_sink (const char *msg)
114{
115 vg_assert (initial_valgrind_sink_saved);
116 VG_(write) (initial_valgrind_sink.fd, msg, strlen(msg));
117}
118
119
sewardj3b290482011-05-06 21:02:55 +0000120static
florian6bd9dc12012-11-23 16:17:43 +0000121void kill_request (const char *msg)
sewardj3b290482011-05-06 21:02:55 +0000122{
123 VG_(umsg) ("%s", msg);
sewardj3b290482011-05-06 21:02:55 +0000124 VG_(exit) (0);
125}
126
philippe02ea4132013-09-04 21:42:43 +0000127// s is a NULL terminated string made of O or more words (separated by spaces).
128// Returns a pointer to the Nth word in s.
129// If Nth word does not exist, return a pointer to the last (0) byte of s.
130static
131const char *wordn (const char *s, int n)
132{
133 int word_seen = 0;
134 Bool searching_word = True;
135
136 while (*s) {
137 if (*s == ' ')
138 searching_word = True;
139 else {
140 if (searching_word) {
141 searching_word = False;
142 word_seen++;
143 if (word_seen == n)
144 return s;
145 }
146 }
147 s++;
148 }
149 return s;
150}
151
philippe4f6f3362014-04-19 00:25:54 +0000152void VG_(print_all_stats) (Bool memory_stats, Bool tool_stats)
153{
154 if (memory_stats) {
155 VG_(message)(Vg_DebugMsg, "\n");
156 VG_(message)(Vg_DebugMsg,
157 "------ Valgrind's internal memory use stats follow ------\n" );
158 VG_(sanity_check_malloc_all)();
159 VG_(message)(Vg_DebugMsg, "------\n" );
160 VG_(print_all_arena_stats)();
161 if (VG_(clo_profile_heap))
162 VG_(print_arena_cc_analysis) ();
163 VG_(message)(Vg_DebugMsg, "\n");
164 }
165
166 VG_(print_translation_stats)();
167 VG_(print_tt_tc_stats)();
168 VG_(print_scheduler_stats)();
169 VG_(print_ExeContext_stats)( False /* with_stacktraces */ );
170 VG_(print_errormgr_stats)();
171 if (tool_stats && VG_(needs).print_stats) {
172 VG_TDICT_CALL(tool_print_stats);
173 }
174}
175
sewardj3b290482011-05-06 21:02:55 +0000176/* handle_gdb_valgrind_command handles the provided mon string command.
177 If command is recognised, return 1 else return 0.
178 Note that in case of ambiguous command, 1 is returned.
179
180 *sink_wanted_at_return is modified if one of the commands
sewardj30b3eca2011-06-28 08:20:39 +0000181 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000182*/
183static
philippe02ea4132013-09-04 21:42:43 +0000184int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return)
sewardj3b290482011-05-06 21:02:55 +0000185{
186 UWord ret = 0;
187 char s[strlen(mon)+1]; /* copy for strtok_r */
philippe02ea4132013-09-04 21:42:43 +0000188 char *wcmd;
189 HChar *ssaveptr;
190 const char *endptr;
sewardj3b290482011-05-06 21:02:55 +0000191 int kwdid;
192 int int_value;
193
194 vg_assert (initial_valgrind_sink_saved);
195
196 strcpy (s, mon);
197 wcmd = strtok_r (s, " ", &ssaveptr);
198 /* NB: if possible, avoid introducing a new command below which
sewardj30b3eca2011-06-28 08:20:39 +0000199 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000200 command. This ensures a shorter abbreviation for the user. */
philippe6ec8d632013-01-23 22:10:28 +0000201 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate"
202 " v.do",
sewardj3b290482011-05-06 21:02:55 +0000203 wcmd, kwd_report_duplicated_matches)) {
204 case -2:
205 ret = 1;
206 break;
207 case -1:
208 break;
209 case 0: /* help */
210 ret = 1;
211 wcmd = strtok_r (NULL, " ", &ssaveptr);
212 if (wcmd == NULL) {
213 int_value = 0;
214 } else {
215 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
216 case -2: int_value = 0; break;
217 case -1: int_value = 0; break;
218 case 0: int_value = 1; break;
219 default: tl_assert (0);
220 }
221 }
222
223 VG_(gdb_printf) (
224"general valgrind monitor commands:\n"
philippec3360382012-10-21 14:37:14 +0000225" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000226" v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
227" v.info all_errors : show all errors found so far\n"
228" v.info last_error : show last error found\n"
philippe07c08522014-05-14 20:39:27 +0000229" v.info location <addr> : show information about location <addr>\n"
philippe02ea4132013-09-04 21:42:43 +0000230" 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 +0000231" v.info open_fds : show open file descriptors (only if --track-fds=yes)\n"
sewardj30b3eca2011-06-28 08:20:39 +0000232" v.kill : kill the Valgrind process\n"
233" v.set gdb_output : set valgrind output to gdb\n"
234" v.set log_output : set valgrind output to log\n"
235" v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
philippe46207652013-01-20 17:11:58 +0000236" v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n"
sewardj30b3eca2011-06-28 08:20:39 +0000237" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000238 if (int_value) { VG_(gdb_printf) (
239"debugging valgrind internals monitor commands:\n"
philippe6ec8d632013-01-23 22:10:28 +0000240" v.do expensive_sanity_check_general : do an expensive sanity check now\n"
sewardj30b3eca2011-06-28 08:20:39 +0000241" v.info gdbserver_status : show gdbserver status\n"
philippe93a6a8d2012-04-27 22:59:43 +0000242" v.info memory [aspacemgr] : show valgrind heap memory stats\n"
243" (with aspacemgr arg, also shows valgrind segments on log ouput)\n"
philippef3a6e932013-01-10 20:42:51 +0000244" v.info exectxt : show stacktraces and stats of all execontexts\n"
sewardjd6e13d82011-10-22 20:23:30 +0000245" v.info scheduler : show valgrind thread state and stacktrace\n"
philippe8587b542013-12-15 20:24:43 +0000246" v.info stats : show various valgrind and tool stats\n"
sewardj30b3eca2011-06-28 08:20:39 +0000247" v.set debuglog <level> : set valgrind debug log level to <level>\n"
philippe180a7502014-04-20 13:41:10 +0000248" v.set hostvisibility [yes*|no] : (en/dis)ables access by gdb/gdbserver to\n"
249" Valgrind internal host status/memory\n"
sewardj30b3eca2011-06-28 08:20:39 +0000250" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000251" (default traceflags 0b00100000 : show after instrumentation)\n"
252" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
253 }
254 break;
sewardj30b3eca2011-06-28 08:20:39 +0000255 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000256 ret = 1;
257 wcmd = strtok_r (NULL, " ", &ssaveptr);
258 switch (kwdid = VG_(keyword_id)
philippe46207652013-01-20 17:11:58 +0000259 ("vgdb-error debuglog merge-recursive-frames"
philippe64ba5742014-06-19 20:33:27 +0000260 " gdb_output log_output mixed_output hostvisibility",
sewardj3b290482011-05-06 21:02:55 +0000261 wcmd, kwd_report_all)) {
262 case -2:
263 case -1:
264 break;
265 case 0: /* vgdb-error */
266 case 1: /* debuglog */
philippe46207652013-01-20 17:11:58 +0000267 case 2: /* merge-recursive-frames */
sewardj3b290482011-05-06 21:02:55 +0000268 wcmd = strtok_r (NULL, " ", &ssaveptr);
269 if (wcmd == NULL) {
270 int_value = 0;
271 endptr = "empty"; /* to report an error below */
272 } else {
florian6bd9dc12012-11-23 16:17:43 +0000273 HChar *the_end;
274 int_value = strtol (wcmd, &the_end, 10);
275 endptr = the_end;
sewardj3b290482011-05-06 21:02:55 +0000276 }
277 if (*endptr != '\0') {
278 VG_(gdb_printf) ("missing or malformed integer value\n");
279 } else if (kwdid == 0) {
philippe02ea4132013-09-04 21:42:43 +0000280 VG_(printf) ("vgdb-error value changed from %d to %d\n",
sewardj3b290482011-05-06 21:02:55 +0000281 VG_(dyn_vgdb_error), int_value);
282 VG_(dyn_vgdb_error) = int_value;
283 } else if (kwdid == 1) {
philippe02ea4132013-09-04 21:42:43 +0000284 VG_(printf) ("debuglog value changed from %d to %d\n",
sewardj3b290482011-05-06 21:02:55 +0000285 VG_(debugLog_getLevel)(), int_value);
286 VG_(debugLog_startup) (int_value, "gdbsrv");
philippe46207652013-01-20 17:11:58 +0000287 } else if (kwdid == 2) {
philippe02ea4132013-09-04 21:42:43 +0000288 VG_(printf)
philippe46207652013-01-20 17:11:58 +0000289 ("merge-recursive-frames value changed from %d to %d\n",
290 VG_(clo_merge_recursive_frames), int_value);
291 VG_(clo_merge_recursive_frames) = int_value;
sewardj3b290482011-05-06 21:02:55 +0000292 } else {
293 vg_assert (0);
294 }
295 break;
philippe46207652013-01-20 17:11:58 +0000296 case 3: /* gdb_output */
sewardj3b290482011-05-06 21:02:55 +0000297 (*sink_wanted_at_return).fd = -2;
298 command_output_to_log = False;
299 VG_(gdb_printf) ("valgrind output will go to gdb\n");
300 break;
philippe46207652013-01-20 17:11:58 +0000301 case 4: /* log_output */
sewardj3b290482011-05-06 21:02:55 +0000302 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
303 command_output_to_log = True;
304 VG_(gdb_printf) ("valgrind output will go to log\n");
305 break;
philippe46207652013-01-20 17:11:58 +0000306 case 5: /* mixed output */
sewardj3b290482011-05-06 21:02:55 +0000307 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
308 command_output_to_log = False;
309 VG_(gdb_printf)
310 ("valgrind output will go to log, interactive output will go to gdb\n");
311 break;
philippe180a7502014-04-20 13:41:10 +0000312 case 6: /* hostvisibility */
313 wcmd = strtok_r (NULL, " ", &ssaveptr);
314 if (wcmd != NULL) {
315 switch (VG_(keyword_id) ("yes no", wcmd, kwd_report_all)) {
316 case -2:
317 case -1: break;
318 case 0:
319 hostvisibility = True;
320 break;
321 case 1:
322 hostvisibility = False;
323 break;
324 default: tl_assert (0);
325 }
326 } else {
327 hostvisibility = True;
328 }
philippe3a73b392014-06-09 15:47:46 +0000329 if (hostvisibility) {
philippec23ae602014-06-09 22:08:45 +0000330 const DebugInfo *tooldi = VG_(find_DebugInfo) ((Addr)handle_gdb_valgrind_command);
philippe3a73b392014-06-09 15:47:46 +0000331 vg_assert(tooldi);
332 const NSegment *toolseg = VG_(am_find_nsegment)
333 (VG_(DebugInfo_get_text_avma) (tooldi));
334 vg_assert(toolseg);
philippe180a7502014-04-20 13:41:10 +0000335 VG_(gdb_printf)
336 ("Enabled access to Valgrind memory/status by GDB\n"
philippe3a73b392014-06-09 15:47:46 +0000337 "If not yet done, tell GDB which valgrind file(s) to use, "
338 "typically:\n"
philippec23ae602014-06-09 22:08:45 +0000339 "add-symbol-file %s %p\n", VG_(am_get_filename)(toolseg), (void*) toolseg->start);
philippe3a73b392014-06-09 15:47:46 +0000340 } else
philippe180a7502014-04-20 13:41:10 +0000341 VG_(gdb_printf)
342 ("Disabled access to Valgrind memory/status by GDB\n");
343 break;
sewardj3b290482011-05-06 21:02:55 +0000344 default:
345 vg_assert (0);
346 }
347 break;
sewardj30b3eca2011-06-28 08:20:39 +0000348 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000349 ret = 1;
350 wcmd = strtok_r (NULL, " ", &ssaveptr);
351 switch (kwdid = VG_(keyword_id)
sewardjd6e13d82011-10-22 20:23:30 +0000352 ("all_errors n_errs_found last_error gdbserver_status memory"
philippe07c08522014-05-14 20:39:27 +0000353 " scheduler stats open_fds exectxt location",
sewardj3b290482011-05-06 21:02:55 +0000354 wcmd, kwd_report_all)) {
355 case -2:
356 case -1:
357 break;
358 case 0: // all_errors
359 // A verbosity of minimum 2 is needed to show the errors.
360 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
361 break;
362 case 1: // n_errs_found
philippe02ea4132013-09-04 21:42:43 +0000363 VG_(printf) ("n_errs_found %d n_errs_shown %d (vgdb-error %d) %s\n",
364 VG_(get_n_errs_found) (),
365 VG_(get_n_errs_shown) (),
366 VG_(dyn_vgdb_error),
367 wordn (mon, 3));
sewardj3b290482011-05-06 21:02:55 +0000368 break;
369 case 2: // last_error
370 VG_(show_last_error)();
371 break;
372 case 3: // gdbserver_status
373 VG_(gdbserver_status_output)();
374 break;
375 case 4: /* memory */
philippe8587b542013-12-15 20:24:43 +0000376 VG_(printf) ("%llu bytes have already been allocated.\n",
377 VG_(am_get_anonsize_total)());
sewardj3b290482011-05-06 21:02:55 +0000378 VG_(print_all_arena_stats) ();
379 if (VG_(clo_profile_heap))
380 VG_(print_arena_cc_analysis) ();
philippe93a6a8d2012-04-27 22:59:43 +0000381 wcmd = strtok_r (NULL, " ", &ssaveptr);
382 if (wcmd != NULL) {
383 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) {
384 case -2:
385 case -1: break;
386 case 0:
387 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr");
388 break;
389 default: tl_assert (0);
390 }
391 }
392
sewardj3b290482011-05-06 21:02:55 +0000393 ret = 1;
394 break;
sewardjd6e13d82011-10-22 20:23:30 +0000395 case 5: /* scheduler */
philippe4f6f3362014-04-19 00:25:54 +0000396 VG_(show_sched_status) (True, // host_stacktrace
397 True, // valgrind_stack_usage
398 True); // exited_threads
sewardjd6e13d82011-10-22 20:23:30 +0000399 ret = 1;
400 break;
philippe8587b542013-12-15 20:24:43 +0000401 case 6: /* stats */
philippe4f6f3362014-04-19 00:25:54 +0000402 VG_(print_all_stats)(False, /* Memory stats */
403 True /* Tool stats */);
philippe8587b542013-12-15 20:24:43 +0000404 ret = 1;
405 break;
406 case 7: /* open_fds */
philippec3360382012-10-21 14:37:14 +0000407 if (VG_(clo_track_fds))
408 VG_(show_open_fds) ("");
409 else
410 VG_(gdb_printf)
411 ("Valgrind must be started with --track-fds=yes"
412 " to show open fds\n");
413 ret = 1;
414 break;
philippe8587b542013-12-15 20:24:43 +0000415 case 8: /* exectxt */
philippef3a6e932013-01-10 20:42:51 +0000416 VG_(print_ExeContext_stats) (True /* with_stacktraces */);
417 ret = 1;
418 break;
philippe07c08522014-05-14 20:39:27 +0000419 case 9: { /* location */
420 /* Note: we prefer 'v.info location' and not 'v.info address' as
421 v.info address is inconsistent with the GDB (native)
422 command 'info address' which gives the address for a symbol.
423 GDB equivalent command of 'v.info location' is 'info symbol'. */
424 Addr address;
425 SizeT dummy_sz = 0x1234;
426 if (VG_(strtok_get_address_and_size) (&address, &dummy_sz, &ssaveptr)) {
427 // If tool provides location information, use that.
428 if (VG_(needs).info_location) {
429 VG_TDICT_CALL(tool_info_location, address);
430 }
431 // If tool does not provide location information, use the common one.
432 // Also use the common to compare with tool when debug log is set.
433 if (!VG_(needs).info_location || VG_(debugLog_getLevel)() > 0 ) {
434 AddrInfo ai;
435 ai.tag = Addr_Undescribed;
436 VG_(describe_addr) (address, &ai);
437 VG_(pp_addrinfo) (address, &ai);
438 VG_(clear_addrinfo) (&ai);
439 }
440 }
441 ret = 1;
442 break;
443 }
sewardj3b290482011-05-06 21:02:55 +0000444 default:
445 vg_assert(0);
446 }
447 break;
448 }
sewardj30b3eca2011-06-28 08:20:39 +0000449 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000450 wcmd = strtok_r (NULL, " ", &ssaveptr);
451 if (wcmd != NULL) {
florian6bd9dc12012-11-23 16:17:43 +0000452 int_value = strtol (wcmd, NULL, 10);
philippe02ea4132013-09-04 21:42:43 +0000453 VG_(printf) ("gdbserver: continuing in %d ms ...\n", int_value);
sewardj3b290482011-05-06 21:02:55 +0000454 VG_(poll)(NULL, 0, int_value);
455 }
philippe02ea4132013-09-04 21:42:43 +0000456 VG_(printf) ("gdbserver: continuing after wait ...\n");
sewardj3b290482011-05-06 21:02:55 +0000457 ret = 1;
458 break;
sewardj30b3eca2011-06-28 08:20:39 +0000459 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000460 kill_request ("monitor command request to kill this process\n");
461 break;
sewardj30b3eca2011-06-28 08:20:39 +0000462 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000463 Addr address;
464 SizeT verbosity = 0x20;
465
466 ret = 1;
467
philippe07c08522014-05-14 20:39:27 +0000468 if (VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr)) {
sewardj3b290482011-05-06 21:02:55 +0000469 /* we need to force the output to log for the translation trace,
470 as low level VEX tracing cannot be redirected to gdb. */
471 int saved_command_output_to_log = command_output_to_log;
472 int saved_fd = VG_(log_output_sink).fd;
473 Bool single_stepping_on_entry = valgrind_single_stepping();
474 int vex_verbosity = verbosity & 0xff;
475 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
476 if ((verbosity & 0x100) && !single_stepping_on_entry) {
477 valgrind_set_single_stepping(True);
478 // to force gdbserver instrumentation.
479 }
sewardj99d61342011-05-17 16:35:11 +0000480# if defined(VGA_arm)
481 // on arm, we need to (potentially) convert this address
482 // to the thumb form.
483 address = thumb_pc (address);
484# endif
485
sewardj3b290482011-05-06 21:02:55 +0000486 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
487 address,
488 /*debugging*/True,
489 (Int) vex_verbosity,
490 /*bbs_done*/0,
491 /*allow redir?*/True);
492 if ((verbosity & 0x100) && !single_stepping_on_entry) {
493 valgrind_set_single_stepping(False);
494 // reset single stepping.
495 }
496 command_output_to_log = saved_command_output_to_log;
497 VG_(log_output_sink).fd = saved_fd;
498 }
499 break;
500 }
501
philippe6ec8d632013-01-23 22:10:28 +0000502 case 6: /* v.do */
503 ret = 1;
504 wcmd = strtok_r (NULL, " ", &ssaveptr);
505 switch (VG_(keyword_id) ("expensive_sanity_check_general",
506 wcmd, kwd_report_all)) {
507 case -2:
508 case -1: break;
509 case 0: { /* expensive_sanity_check_general */
510 // Temporarily bump up sanity level to check e.g. the malloc arenas.
511 const Int save_clo_sanity_level = VG_(clo_sanity_level);
512 if (VG_(clo_sanity_level) < 4) VG_(clo_sanity_level) = 4;
513 VG_(sanity_check_general) (/* force_expensive */ True);
514 VG_(clo_sanity_level) = save_clo_sanity_level;
515 break;
516 }
517 default: tl_assert (0);
518 }
519 break;
520
sewardj3b290482011-05-06 21:02:55 +0000521 default:
522 vg_assert (0);
523 }
524 return ret;
525}
526
527/* handle_gdb_monitor_command handles the provided mon string command,
528 which can be either a "standard" valgrind monitor command
529 or a tool specific monitor command.
530 If command recognised, return 1 else return 0.
531 Note that in case of ambiguous command, 1 is returned.
532*/
533static
philippe02ea4132013-09-04 21:42:43 +0000534int handle_gdb_monitor_command (char *mon)
sewardj3b290482011-05-06 21:02:55 +0000535{
536 UWord ret = 0;
537 UWord tool_ret = 0;
538 // initially, we assume that when returning, the desired sink is the
539 // one we have when entering. It can however be changed by the standard
540 // valgrind command handling.
541 OutputSink sink_wanted_at_return = VG_(log_output_sink);
542
543 if (!initial_valgrind_sink_saved) {
544 /* first time we enter here, we save the valgrind default log sink */
545 initial_valgrind_sink = sink_wanted_at_return;
546 initial_valgrind_sink_saved = True;
547 }
548
549 if (!command_output_to_log)
550 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
551
552 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
553
554 /* Even if command was recognised by valgrind core, we call the
555 tool command handler : this is needed to handle help command
556 and/or to let the tool do some additional processing of a
557 valgrind standard command. Note however that if valgrind
558 recognised the command, we will always return success. */
559 if (VG_(needs).client_requests) {
560 /* If the tool reports an error when handling a monitor command,
561 we need to avoid calling gdbserver during this command
562 handling. So, we temporarily set VG_(dyn_vgdb_error) to
563 a huge value to ensure m_errormgr.c does not call gdbserver. */
564 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
565 UWord arg[2];
566 VG_(dyn_vgdb_error) = 999999999;
567 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
568 arg[1] = (UWord) mon;
569 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
570 &tool_ret);
571 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
572 }
573
philippe02ea4132013-09-04 21:42:43 +0000574 VG_(message_flush) ();
575
sewardj3b290482011-05-06 21:02:55 +0000576 /* restore or set the desired output */
577 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
578 if (ret | tool_ret)
579 return 1;
580 else
581 return 0;
582}
583
584
585/* Handle all of the extended 'Q' packets. */
586static
587void handle_set (char *arg_own_buf, int *new_packet_len_p)
588{
589 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
590 noack_mode = True;
591 write_ok (arg_own_buf);
592 return;
593 }
594
595 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
596 int i;
597 char *from, *to;
598 char *end = arg_own_buf + strlen(arg_own_buf);
599 CORE_ADDR sig;
600 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
601 pass_signals[i] = 0;
602
603 from = arg_own_buf + 13;
604 while (from < end) {
605 to = strchr(from, ';');
606 if (to == NULL) to = end;
607 decode_address (&sig, from, to - from);
608 pass_signals[(int)sig] = 1;
philippe886fde32012-03-29 21:56:47 +0000609 dlog(1, "pass_signal gdb_nr %d %s\n",
610 (int)sig, target_signal_to_name(sig));
sewardj3b290482011-05-06 21:02:55 +0000611 from = to;
612 if (*from == ';') from++;
613 }
614 write_ok (arg_own_buf);
615 return;
616 }
617 /* Otherwise we didn't know what packet it was. Say we didn't
618 understand it. */
619 arg_own_buf[0] = 0;
620}
621
philippe02ea4132013-09-04 21:42:43 +0000622Bool VG_(client_monitor_command) (HChar *cmd)
philippe46207652013-01-20 17:11:58 +0000623{
624 const Bool connected = remote_connected();
625 const int saved_command_output_to_log = command_output_to_log;
626 Bool handled;
627
628 if (!connected)
629 command_output_to_log = True;
630 handled = handle_gdb_monitor_command (cmd);
631 if (!connected) {
632 // reset the log output unless cmd changed it.
633 if (command_output_to_log)
634 command_output_to_log = saved_command_output_to_log;
635 }
636 if (handled)
637 return False; // recognised
638 else
639 return True; // not recognised
640}
641
sewardj3b290482011-05-06 21:02:55 +0000642/* Handle all of the extended 'q' packets. */
643static
644void handle_query (char *arg_own_buf, int *new_packet_len_p)
645{
646 static struct inferior_list_entry *thread_ptr;
647
648 /* qRcmd, monitor command handling. */
649 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
650 char *p = arg_own_buf + 6;
651 int cmdlen = strlen(p)/2;
652 char cmd[cmdlen+1];
653
654 if (unhexify (cmd, p, cmdlen) != cmdlen) {
655 write_enn (arg_own_buf);
656 return;
657 }
658 cmd[cmdlen] = '\0';
659
660 if (handle_gdb_monitor_command (cmd)) {
sewardj3b290482011-05-06 21:02:55 +0000661 write_ok (arg_own_buf);
662 return;
663 } else {
664 /* cmd not recognised */
665 VG_(gdb_printf)
666 ("command '%s' not recognised\n"
667 "In gdb, try 'monitor help'\n"
668 "In a shell, try 'vgdb help'\n",
669 cmd);
670 write_ok (arg_own_buf);
671 return;
672 }
673 }
674
675 /* provide some valgrind specific info in return to qThreadExtraInfo. */
676 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
677 unsigned long gdb_id;
678 struct thread_info *ti;
679 ThreadState *tst;
680 char status[100];
681
682 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
683 ti = gdb_id_to_thread (gdb_id);
684 if (ti != NULL) {
685 tst = (ThreadState *) inferior_target_data (ti);
florian49789512013-09-16 17:08:50 +0000686 /* Additional info is the tid, the thread status and the thread's
687 name, if any. */
688 if (tst->thread_name) {
689 VG_(snprintf) (status, sizeof(status), "tid %d %s %s",
690 tst->tid,
691 VG_(name_of_ThreadStatus)(tst->status),
692 tst->thread_name);
693 } else {
694 VG_(snprintf) (status, sizeof(status), "tid %d %s",
695 tst->tid,
696 VG_(name_of_ThreadStatus)(tst->status));
697 }
sewardj3b290482011-05-06 21:02:55 +0000698 hexify (arg_own_buf, status, strlen(status));
699 return;
700 } else {
701 write_enn (arg_own_buf);
702 return;
703 }
704 }
705
706 if (strcmp ("qAttached", arg_own_buf) == 0) {
707 /* tell gdb to always detach, never kill the process */
708 arg_own_buf[0] = '1';
709 arg_own_buf[1] = 0;
710 return;
711 }
712
713 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
714 /* We have no symbol to read. */
715 write_ok (arg_own_buf);
716 return;
717 }
718
719 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
720 thread_ptr = all_threads.head;
721 VG_(sprintf) (arg_own_buf, "m%x",
722 thread_to_gdb_id ((struct thread_info *)thread_ptr));
723 thread_ptr = thread_ptr->next;
724 return;
725 }
726
727 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
728 if (thread_ptr != NULL) {
729 VG_(sprintf) (arg_own_buf, "m%x",
730 thread_to_gdb_id ((struct thread_info *)thread_ptr));
731 thread_ptr = thread_ptr->next;
732 return;
733 } else {
734 VG_(sprintf) (arg_own_buf, "l");
735 return;
736 }
737 }
738
philippe419d5f22012-05-24 21:33:17 +0000739 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
sewardj3b290482011-05-06 21:02:55 +0000740 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
741 CORE_ADDR ofs;
742 unsigned int len, doc_len;
florian6bd9dc12012-11-23 16:17:43 +0000743 const char *annex = NULL;
sewardj3b290482011-05-06 21:02:55 +0000744 // First, the annex is extracted from the packet received.
745 // Then, it is replaced by the corresponding file name.
746 int fd;
747
748 /* Grab the annex, offset, and length. */
749 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
750 strcpy (arg_own_buf, "E00");
751 return;
752 }
753
754 if (strcmp (annex, "target.xml") == 0) {
philippe419d5f22012-05-24 21:33:17 +0000755 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers));
756 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) {
757 /* Ensure the shadow registers are initialized. */
758 initialize_shadow_low(True);
sewardj3b290482011-05-06 21:02:55 +0000759 }
sewardj3b290482011-05-06 21:02:55 +0000760 if (annex == NULL) {
761 strcpy (arg_own_buf, "E00");
762 return;
763 }
764 }
765
766 {
philippe75a5f782012-02-24 11:25:58 +0000767 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
sewardj3b290482011-05-06 21:02:55 +0000768 struct vg_stat stat_doc;
769 char toread[len];
770 int len_read;
771
772 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
773 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
774 if (fd == -1) {
775 strcpy (arg_own_buf, "E00");
776 return;
777 }
778 if (VG_(fstat) (fd, &stat_doc) != 0) {
779 VG_(close) (fd);
780 strcpy (arg_own_buf, "E00");
781 return;
782 }
783 doc_len = stat_doc.size;
784
785 if (len > PBUFSIZ - POVERHSIZ)
786 len = PBUFSIZ - POVERHSIZ;
787
788 if (ofs > doc_len) {
789 write_enn (arg_own_buf);
790 VG_(close) (fd);
791 return;
792 }
793 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
794 len_read = VG_(read) (fd, toread, len);
florian1636d332012-11-15 04:27:04 +0000795 *new_packet_len_p = write_qxfer_response (arg_own_buf, (unsigned char *)toread,
sewardj3b290482011-05-06 21:02:55 +0000796 len_read, ofs + len_read < doc_len);
797 VG_(close) (fd);
798 return;
799 }
800 }
801
sewardj2a312392011-06-26 09:26:48 +0000802 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
803 unsigned char *data;
804 int n;
805 CORE_ADDR ofs;
806 unsigned int len;
florian6bd9dc12012-11-23 16:17:43 +0000807 const char *annex;
sewardj2a312392011-06-26 09:26:48 +0000808
809 /* Reject any annex; grab the offset and length. */
810 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
811 || annex[0] != '\0') {
812 strcpy (arg_own_buf, "E00");
813 return;
814 }
815
816 if (len > PBUFSIZ - 2)
817 len = PBUFSIZ - 2;
818 data = malloc (len);
819
820 {
821 UWord *client_auxv = VG_(client_auxv);
822 unsigned int client_auxv_len = 0;
823 while (*client_auxv != 0) {
824 dlog(4, "auxv %lld %llx\n",
825 (ULong)*client_auxv,
826 (ULong)*(client_auxv+1));
827 client_auxv++;
828 client_auxv++;
829 client_auxv_len += 2 * sizeof(UWord);
830 }
831 client_auxv_len += 2 * sizeof(UWord);
832 dlog(4, "auxv len %d\n", client_auxv_len);
833
834 if (ofs >= client_auxv_len)
835 n = -1;
836 else {
837 n = client_auxv_len - ofs;
838 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
839 }
840 }
841
842 if (n < 0)
843 write_enn (arg_own_buf);
844 else if (n > len)
845 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
846 else
847 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
848
849 free (data);
850
851 return;
852 }
853
854
sewardj3b290482011-05-06 21:02:55 +0000855 /* Protocol features query. */
856 if (strncmp ("qSupported", arg_own_buf, 10) == 0
857 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
858 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
859 /* Note: max packet size including frame and checksum, but without
860 trailing null byte, which is not sent/received. */
861
862 strcat (arg_own_buf, ";QStartNoAckMode+");
863 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +0000864 if (VG_(client_auxv))
865 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +0000866
philippe419d5f22012-05-24 21:33:17 +0000867 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
sewardj3b290482011-05-06 21:02:55 +0000868 strcat (arg_own_buf, ";qXfer:features:read+");
869 /* if a new gdb connects to us, we have to reset the register
870 set to the normal register sets to allow this new gdb to
871 decide to use or not the shadow registers.
872
873 Note that the reset is only done for gdb that are sending
874 qSupported packets. If a user first connected with a recent
875 gdb using shadow registers and then with a very old gdb
876 that does not use qSupported packet, then the old gdb will
877 not properly connect. */
878 initialize_shadow_low(False);
879 }
880 return;
881 }
882
883 /* Otherwise we didn't know what packet it was. Say we didn't
884 understand it. */
885 arg_own_buf[0] = 0;
886}
887
888/* Handle all of the extended 'v' packets. */
889static
sewardj0bb3c672011-07-26 23:29:25 +0000890void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +0000891{
892 /* vcont packet code from gdb 6.6 removed */
893
894 /* Otherwise we didn't know what packet it was. Say we didn't
895 understand it. */
896 arg_own_buf[0] = 0;
897 return;
898}
899
900static
901void myresume (int step, int sig)
902{
903 struct thread_resume resume_info[2];
904 int n = 0;
905
philippe349a3912012-05-23 21:50:36 +0000906 if (step || sig) {
sewardj3b290482011-05-06 21:02:55 +0000907 resume_info[0].step = step;
908 resume_info[0].sig = sig;
sewardj3b290482011-05-06 21:02:55 +0000909 n++;
910 }
sewardj3b290482011-05-06 21:02:55 +0000911 resume_info[n].step = 0;
912 resume_info[n].sig = 0;
sewardj3b290482011-05-06 21:02:55 +0000913
philippe349a3912012-05-23 21:50:36 +0000914 resume_reply_packet_needed = True;
915 valgrind_resume (resume_info);
sewardj3b290482011-05-06 21:02:55 +0000916}
917
918/* server_main global variables */
919static char *own_buf;
920static unsigned char *mem_buf;
921
922void gdbserver_init (void)
923{
924 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
925 noack_mode = False;
philippe349a3912012-05-23 21:50:36 +0000926 valgrind_initialize_target ();
philippe0e1cac92012-02-28 22:37:44 +0000927 // After a fork, gdbserver_init can be called again.
928 // We do not have to re-malloc the buffers in such a case.
929 if (own_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +0000930 own_buf = malloc (PBUFSIZ+POVERHSIZ);
philippe0e1cac92012-02-28 22:37:44 +0000931 if (mem_buf == NULL)
philippe03ffc6e2013-07-25 22:37:02 +0000932 mem_buf = malloc (PBUFSIZ+POVERHSIZ);
933 // Note: normally, we should only malloc PBUFSIZ. However,
934 // GDB has a bug, and in some cases, sends e.g. 'm' packets
935 // asking for slightly more than the PacketSize given at
936 // connection initialisation. So, we bypass the GDB bug
937 // by allocating slightly more.
sewardj3b290482011-05-06 21:02:55 +0000938}
939
940void gdbserver_terminate (void)
941{
942 /* last call to gdbserver is cleanup call */
943 if (VG_MINIMAL_SETJMP(toplevel)) {
944 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
945 return;
946 }
947 remote_close();
948}
949
950void server_main (void)
951{
952 static char status;
sewardj0bb3c672011-07-26 23:29:25 +0000953 static int zignal;
sewardj3b290482011-05-06 21:02:55 +0000954
955 char ch;
956 int i = 0;
957 unsigned int len;
958 CORE_ADDR mem_addr;
959
philippe349a3912012-05-23 21:50:36 +0000960 zignal = valgrind_wait (&status);
sewardj3b290482011-05-06 21:02:55 +0000961 if (VG_MINIMAL_SETJMP(toplevel)) {
962 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
963 }
964 while (1) {
965 unsigned char sig;
966 int packet_len;
967 int new_packet_len = -1;
968
philippe349a3912012-05-23 21:50:36 +0000969 if (resume_reply_packet_needed) {
970 /* Send the resume reply to reply to last GDB resume
971 request. */
972 resume_reply_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +0000973 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000974 putpkt (own_buf);
975 }
976
philippe0447bbd2012-10-17 21:32:03 +0000977 /* If we our status is terminal (exit or fatal signal) get out
978 as quickly as we can. We won't be able to handle any request
979 anymore. */
980 if (status == 'W' || status == 'X') {
981 return;
982 }
983
sewardj3b290482011-05-06 21:02:55 +0000984 packet_len = getpkt (own_buf);
985 if (packet_len <= 0)
986 break;
987
988 i = 0;
989 ch = own_buf[i++];
990 switch (ch) {
991 case 'Q':
992 handle_set (own_buf, &new_packet_len);
993 break;
994 case 'q':
995 handle_query (own_buf, &new_packet_len);
996 break;
997 case 'd':
998 /* set/unset debugging is done through valgrind debug level. */
999 own_buf[0] = '\0';
1000 break;
1001 case 'D':
1002 reset_valgrind_sink("gdb detaching from process");
1003
1004 /* When detaching or kill the process, gdb expects to get
1005 an packet OK back. Any other output will make gdb
1006 believes detach did not work. */
1007 write_ok (own_buf);
1008 putpkt (own_buf);
1009 remote_finish (reset_after_error);
1010 remote_open (VG_(clo_vgdb_prefix));
1011 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001012 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001013 return;
1014 case '!':
1015 /* We can not use the extended protocol with valgrind,
1016 because we can not restart the running
1017 program. So return unrecognized. */
1018 own_buf[0] = '\0';
1019 break;
1020 case '?':
sewardj0bb3c672011-07-26 23:29:25 +00001021 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +00001022 break;
1023 case 'H':
1024 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
1025 unsigned long gdb_id, thread_id;
1026
1027 gdb_id = strtoul (&own_buf[2], NULL, 16);
1028 thread_id = gdb_id_to_thread_id (gdb_id);
1029 if (thread_id == 0) {
1030 write_enn (own_buf);
1031 break;
1032 }
1033
1034 if (own_buf[1] == 'g') {
1035 general_thread = thread_id;
1036 set_desired_inferior (1);
1037 } else if (own_buf[1] == 'c') {
1038 cont_thread = thread_id;
1039 } else if (own_buf[1] == 's') {
1040 step_thread = thread_id;
1041 }
1042
1043 write_ok (own_buf);
1044 } else {
1045 /* Silently ignore it so that gdb can extend the protocol
1046 without compatibility headaches. */
1047 own_buf[0] = '\0';
1048 }
1049 break;
1050 case 'g':
1051 set_desired_inferior (1);
1052 registers_to_string (own_buf);
1053 break;
1054 case 'G':
1055 set_desired_inferior (1);
1056 registers_from_string (&own_buf[1]);
1057 write_ok (own_buf);
1058 break;
1059 case 'P': {
1060 int regno;
1061 char *regbytes;
1062 Bool mod;
1063 ThreadState *tst;
1064 regno = strtol(&own_buf[1], NULL, 16);
1065 regbytes = strchr(&own_buf[0], '=') + 1;
1066 set_desired_inferior (1);
1067 tst = (ThreadState *) inferior_target_data (current_inferior);
1068 /* Only accept changing registers in "runnable state3.
1069 In fact, it would be ok to change most of the registers
1070 except a few "sensitive" registers such as the PC, SP, BP.
1071 We assume we do not need to very specific here, and that we
1072 can just refuse all of these. */
1073 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
1074 supply_register_from_string (regno, regbytes, &mod);
1075 write_ok (own_buf);
1076 } else {
1077 /* at least from gdb 6.6 onwards, an E. error
1078 reply is shown to the user. So, we do an error
1079 msg which both is accepted by gdb as an error msg
1080 and is readable by the user. */
1081 VG_(sprintf)
1082 (own_buf,
1083"E.\n"
1084"ERROR changing register %s regno %d\n"
1085"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
1086"set pc, calling from gdb a function in the debugged process, ...)\n"
1087"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
1088"Thread status is %s\n",
1089 find_register_by_number (regno)->name, regno,
1090 VG_(name_of_ThreadStatus)(tst->status));
1091 if (VG_(clo_verbosity) > 1)
1092 VG_(umsg) ("%s\n", own_buf);
1093 }
1094 break;
1095 }
1096 case 'm':
1097 decode_m_packet (&own_buf[1], &mem_addr, &len);
philippe349a3912012-05-23 21:50:36 +00001098 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001099 convert_int_to_ascii (mem_buf, own_buf, len);
1100 else
1101 write_enn (own_buf);
1102 break;
1103 case 'M':
1104 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
philippe349a3912012-05-23 21:50:36 +00001105 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +00001106 write_ok (own_buf);
1107 else
1108 write_enn (own_buf);
1109 break;
1110 case 'X':
1111 if (decode_X_packet (&own_buf[1], packet_len - 1,
1112 &mem_addr, &len, mem_buf) < 0
philippe349a3912012-05-23 21:50:36 +00001113 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
sewardj3b290482011-05-06 21:02:55 +00001114 write_enn (own_buf);
1115 else
1116 write_ok (own_buf);
1117 break;
1118 case 'C':
1119 convert_ascii_to_int (own_buf + 1, &sig, 1);
1120 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +00001121 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001122 else
sewardj0bb3c672011-07-26 23:29:25 +00001123 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001124 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001125 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +00001126 return; // return control to valgrind
1127 case 'S':
1128 convert_ascii_to_int (own_buf + 1, &sig, 1);
1129 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +00001130 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +00001131 else
sewardj0bb3c672011-07-26 23:29:25 +00001132 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +00001133 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +00001134 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +00001135 return; // return control to valgrind
1136 case 'c':
1137 set_desired_inferior (0);
1138 myresume (0, 0);
1139 return; // return control to valgrind
1140 case 's':
1141 set_desired_inferior (0);
1142 myresume (1, 0);
1143 return; // return control to valgrind
1144 case 'Z': {
1145 char *lenptr;
1146 char *dataptr;
1147 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1148 int zlen = strtol (lenptr + 1, &dataptr, 16);
1149 char type = own_buf[1];
1150
philippe349a3912012-05-23 21:50:36 +00001151 if (type < '0' || type > '4') {
1152 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001153 own_buf[0] = '\0';
1154 } else {
1155 int res;
1156
philippe349a3912012-05-23 21:50:36 +00001157 res = valgrind_insert_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001158 if (res == 0)
1159 write_ok (own_buf);
1160 else if (res == 1)
1161 /* Unsupported. */
1162 own_buf[0] = '\0';
1163 else
1164 write_enn (own_buf);
1165 }
1166 break;
1167 }
1168 case 'z': {
1169 char *lenptr;
1170 char *dataptr;
1171 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
1172 int zlen = strtol (lenptr + 1, &dataptr, 16);
1173 char type = own_buf[1];
1174
philippe349a3912012-05-23 21:50:36 +00001175 if (type < '0' || type > '4') {
1176 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +00001177 own_buf[0] = '\0';
1178 } else {
1179 int res;
1180
philippe349a3912012-05-23 21:50:36 +00001181 res = valgrind_remove_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +00001182 if (res == 0)
1183 write_ok (own_buf);
1184 else if (res == 1)
1185 /* Unsupported. */
1186 own_buf[0] = '\0';
1187 else
1188 write_enn (own_buf);
1189 }
1190 break;
1191 }
1192 case 'k':
1193 kill_request("Gdb request to kill this process\n");
1194 break;
1195 case 'T': {
1196 unsigned long gdb_id, thread_id;
1197
1198 gdb_id = strtoul (&own_buf[1], NULL, 16);
1199 thread_id = gdb_id_to_thread_id (gdb_id);
1200 if (thread_id == 0) {
1201 write_enn (own_buf);
1202 break;
1203 }
1204
philippe349a3912012-05-23 21:50:36 +00001205 if (valgrind_thread_alive (thread_id))
sewardj3b290482011-05-06 21:02:55 +00001206 write_ok (own_buf);
1207 else
1208 write_enn (own_buf);
1209 break;
1210 }
1211 case 'R':
1212 /* Restarting the inferior is only supported in the
1213 extended protocol.
1214 => It is a request we don't understand. Respond with an
1215 empty packet so that gdb knows that we don't support this
1216 request. */
1217 own_buf[0] = '\0';
1218 break;
1219 case 'v':
1220 /* Extended (long) request. */
sewardj0bb3c672011-07-26 23:29:25 +00001221 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001222 break;
1223 default:
1224 /* It is a request we don't understand. Respond with an
1225 empty packet so that gdb knows that we don't support this
1226 request. */
1227 own_buf[0] = '\0';
1228 break;
1229 }
1230
1231 if (new_packet_len != -1)
1232 putpkt_binary (own_buf, new_packet_len);
1233 else
1234 putpkt (own_buf);
1235
1236 if (status == 'W')
sewardj0bb3c672011-07-26 23:29:25 +00001237 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001238 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001239 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
sewardj0bb3c672011-07-26 23:29:25 +00001240 target_signal_to_host (zignal),
1241 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001242 if (status == 'W' || status == 'X') {
1243 VG_(umsg) ("Process exiting\n");
1244 VG_(exit) (0);
1245 }
1246 }
1247
1248 /* We come here when getpkt fails => close the connection,
1249 and re-open. Then return control to valgrind.
1250 We return the control to valgrind as we assume that
1251 the connection was closed due to vgdb having finished
1252 to execute a command. */
1253 if (VG_(clo_verbosity) > 1)
1254 VG_(umsg) ("Remote side has terminated connection. "
1255 "GDBserver will reopen the connection.\n");
1256 remote_finish (reset_after_error);
1257 remote_open (VG_(clo_vgdb_prefix));
1258 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001259 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001260 return;
1261}