blob: 90340a65efbd2aabf142215c53cadc0ae24a7744 [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)
sewardj3b290482011-05-06 21:02:55 +000032
33unsigned long cont_thread;
34unsigned long general_thread;
35unsigned long step_thread;
36unsigned long thread_from_wait;
37unsigned long old_thread_from_wait;
38
philippe886fde32012-03-29 21:56:47 +000039int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
bart2dafc542011-05-18 16:08:28 +000040
sewardj3b290482011-05-06 21:02:55 +000041/* for a gdbserver integrated in valgrind, resuming the process consists
42 in returning the control to valgrind.
philippe349a3912012-05-23 21:50:36 +000043 The guess process resumes its execution.
sewardj3b290482011-05-06 21:02:55 +000044 Then at the next error or break or ..., valgrind calls gdbserver again.
philippe349a3912012-05-23 21:50:36 +000045 A resume reply packet must then be built to inform GDB that the
46 resume request is finished.
47 resume_reply_packet_needed records the fact that the next call to gdbserver
sewardj3b290482011-05-06 21:02:55 +000048 must send a resume packet to gdb. */
philippe349a3912012-05-23 21:50:36 +000049static Bool resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +000050
51VG_MINIMAL_JMP_BUF(toplevel);
52
53/* Decode a qXfer read request. Return 0 if everything looks OK,
54 or -1 otherwise. */
55
56static
florian6bd9dc12012-11-23 16:17:43 +000057int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len)
sewardj3b290482011-05-06 21:02:55 +000058{
59 /* Extract and NUL-terminate the annex. */
60 *annex = buf;
61 while (*buf && *buf != ':')
62 buf++;
63 if (*buf == '\0')
64 return -1;
65 *buf++ = 0;
66
67 /* After the read/write marker and annex, qXfer looks like a
68 traditional 'm' packet. */
69 decode_m_packet (buf, ofs, len);
70
71 return 0;
72}
73
74/* Write the response to a successful qXfer read. Returns the
75 length of the (binary) data stored in BUF, corresponding
76 to as much of DATA/LEN as we could fit. IS_MORE controls
77 the first character of the response. */
78static
79int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
80{
81 int out_len;
82
83 if (is_more)
84 buf[0] = 'm';
85 else
86 buf[0] = 'l';
87
88 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
89 PBUFSIZ - POVERHSIZ - 1) + 1;
90}
91
92static Bool initial_valgrind_sink_saved = False;
93/* True <=> valgrind log sink saved in initial_valgrind_sink */
94static OutputSink initial_valgrind_sink;
95
96static Bool command_output_to_log = False;
97/* True <=> command output goes to log instead of gdb */
98
florian6bd9dc12012-11-23 16:17:43 +000099void reset_valgrind_sink(const char *info)
sewardj3b290482011-05-06 21:02:55 +0000100{
101 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
102 && initial_valgrind_sink_saved) {
103 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
104 VG_(umsg) ("Reset valgrind output to log (%s)\n",
105 (info = NULL ? "" : info));
106 }
107}
108
109static
florian6bd9dc12012-11-23 16:17:43 +0000110void kill_request (const char *msg)
sewardj3b290482011-05-06 21:02:55 +0000111{
112 VG_(umsg) ("%s", msg);
113 remote_close();
114 VG_(exit) (0);
115}
116
117/* handle_gdb_valgrind_command handles the provided mon string command.
118 If command is recognised, return 1 else return 0.
119 Note that in case of ambiguous command, 1 is returned.
120
121 *sink_wanted_at_return is modified if one of the commands
sewardj30b3eca2011-06-28 08:20:39 +0000122 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000123*/
124static
125int handle_gdb_valgrind_command (char* mon, OutputSink* sink_wanted_at_return)
126{
127 UWord ret = 0;
128 char s[strlen(mon)+1]; /* copy for strtok_r */
129 char* wcmd;
florian19f91bb2012-11-10 22:29:54 +0000130 HChar* ssaveptr;
florian6bd9dc12012-11-23 16:17:43 +0000131 const char* endptr;
sewardj3b290482011-05-06 21:02:55 +0000132 int kwdid;
133 int int_value;
134
135 vg_assert (initial_valgrind_sink_saved);
136
137 strcpy (s, mon);
138 wcmd = strtok_r (s, " ", &ssaveptr);
139 /* NB: if possible, avoid introducing a new command below which
sewardj30b3eca2011-06-28 08:20:39 +0000140 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000141 command. This ensures a shorter abbreviation for the user. */
sewardj30b3eca2011-06-28 08:20:39 +0000142 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate",
sewardj3b290482011-05-06 21:02:55 +0000143 wcmd, kwd_report_duplicated_matches)) {
144 case -2:
145 ret = 1;
146 break;
147 case -1:
148 break;
149 case 0: /* help */
150 ret = 1;
151 wcmd = strtok_r (NULL, " ", &ssaveptr);
152 if (wcmd == NULL) {
153 int_value = 0;
154 } else {
155 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
156 case -2: int_value = 0; break;
157 case -1: int_value = 0; break;
158 case 0: int_value = 1; break;
159 default: tl_assert (0);
160 }
161 }
162
163 VG_(gdb_printf) (
164"general valgrind monitor commands:\n"
philippec3360382012-10-21 14:37:14 +0000165" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000166" v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
167" v.info all_errors : show all errors found so far\n"
168" v.info last_error : show last error found\n"
169" v.info n_errs_found : show the nr of errors found so far\n"
philippec3360382012-10-21 14:37:14 +0000170" v.info open_fds : show open file descriptors (only if --track-fds=yes)\n"
sewardj30b3eca2011-06-28 08:20:39 +0000171" v.kill : kill the Valgrind process\n"
172" v.set gdb_output : set valgrind output to gdb\n"
173" v.set log_output : set valgrind output to log\n"
174" v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
175" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000176 if (int_value) { VG_(gdb_printf) (
177"debugging valgrind internals monitor commands:\n"
sewardj30b3eca2011-06-28 08:20:39 +0000178" v.info gdbserver_status : show gdbserver status\n"
philippe93a6a8d2012-04-27 22:59:43 +0000179" v.info memory [aspacemgr] : show valgrind heap memory stats\n"
180" (with aspacemgr arg, also shows valgrind segments on log ouput)\n"
philippef3a6e932013-01-10 20:42:51 +0000181" v.info exectxt : show stacktraces and stats of all execontexts\n"
sewardjd6e13d82011-10-22 20:23:30 +0000182" v.info scheduler : show valgrind thread state and stacktrace\n"
sewardj30b3eca2011-06-28 08:20:39 +0000183" v.set debuglog <level> : set valgrind debug log level to <level>\n"
184" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000185" (default traceflags 0b00100000 : show after instrumentation)\n"
186" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
187 }
188 break;
sewardj30b3eca2011-06-28 08:20:39 +0000189 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000190 ret = 1;
191 wcmd = strtok_r (NULL, " ", &ssaveptr);
192 switch (kwdid = VG_(keyword_id)
193 ("vgdb-error debuglog gdb_output log_output mixed_output",
194 wcmd, kwd_report_all)) {
195 case -2:
196 case -1:
197 break;
198 case 0: /* vgdb-error */
199 case 1: /* debuglog */
200 wcmd = strtok_r (NULL, " ", &ssaveptr);
201 if (wcmd == NULL) {
202 int_value = 0;
203 endptr = "empty"; /* to report an error below */
204 } else {
florian6bd9dc12012-11-23 16:17:43 +0000205 HChar *the_end;
206 int_value = strtol (wcmd, &the_end, 10);
207 endptr = the_end;
sewardj3b290482011-05-06 21:02:55 +0000208 }
209 if (*endptr != '\0') {
210 VG_(gdb_printf) ("missing or malformed integer value\n");
211 } else if (kwdid == 0) {
212 VG_(gdb_printf) ("vgdb-error value changed from %d to %d\n",
213 VG_(dyn_vgdb_error), int_value);
214 VG_(dyn_vgdb_error) = int_value;
215 } else if (kwdid == 1) {
216 VG_(gdb_printf) ("debuglog value changed from %d to %d\n",
217 VG_(debugLog_getLevel)(), int_value);
218 VG_(debugLog_startup) (int_value, "gdbsrv");
219 } else {
220 vg_assert (0);
221 }
222 break;
223 case 2: /* gdb_output */
224 (*sink_wanted_at_return).fd = -2;
225 command_output_to_log = False;
226 VG_(gdb_printf) ("valgrind output will go to gdb\n");
227 break;
228 case 3: /* log_output */
229 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
230 command_output_to_log = True;
231 VG_(gdb_printf) ("valgrind output will go to log\n");
232 break;
233 case 4: /* mixed output */
234 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
235 command_output_to_log = False;
236 VG_(gdb_printf)
237 ("valgrind output will go to log, interactive output will go to gdb\n");
238 break;
239 default:
240 vg_assert (0);
241 }
242 break;
sewardj30b3eca2011-06-28 08:20:39 +0000243 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000244 ret = 1;
245 wcmd = strtok_r (NULL, " ", &ssaveptr);
246 switch (kwdid = VG_(keyword_id)
sewardjd6e13d82011-10-22 20:23:30 +0000247 ("all_errors n_errs_found last_error gdbserver_status memory"
philippef3a6e932013-01-10 20:42:51 +0000248 " scheduler open_fds exectxt",
sewardj3b290482011-05-06 21:02:55 +0000249 wcmd, kwd_report_all)) {
250 case -2:
251 case -1:
252 break;
253 case 0: // all_errors
254 // A verbosity of minimum 2 is needed to show the errors.
255 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
256 break;
257 case 1: // n_errs_found
philippebaf69642012-02-15 22:29:30 +0000258 VG_(gdb_printf) ("n_errs_found %d n_errs_shown %d (vgdb-error %d)\n",
sewardj3b290482011-05-06 21:02:55 +0000259 VG_(get_n_errs_found) (),
philippebaf69642012-02-15 22:29:30 +0000260 VG_(get_n_errs_shown) (),
sewardj3b290482011-05-06 21:02:55 +0000261 VG_(dyn_vgdb_error));
262 break;
263 case 2: // last_error
264 VG_(show_last_error)();
265 break;
266 case 3: // gdbserver_status
267 VG_(gdbserver_status_output)();
268 break;
269 case 4: /* memory */
270 VG_(print_all_arena_stats) ();
271 if (VG_(clo_profile_heap))
272 VG_(print_arena_cc_analysis) ();
philippe93a6a8d2012-04-27 22:59:43 +0000273 wcmd = strtok_r (NULL, " ", &ssaveptr);
274 if (wcmd != NULL) {
275 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) {
276 case -2:
277 case -1: break;
278 case 0:
279 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr");
280 break;
281 default: tl_assert (0);
282 }
283 }
284
sewardj3b290482011-05-06 21:02:55 +0000285 ret = 1;
286 break;
sewardjd6e13d82011-10-22 20:23:30 +0000287 case 5: /* scheduler */
288 VG_(show_sched_status) ();
289 ret = 1;
290 break;
philippec3360382012-10-21 14:37:14 +0000291 case 6: /* open_fds */
292 if (VG_(clo_track_fds))
293 VG_(show_open_fds) ("");
294 else
295 VG_(gdb_printf)
296 ("Valgrind must be started with --track-fds=yes"
297 " to show open fds\n");
298 ret = 1;
299 break;
philippef3a6e932013-01-10 20:42:51 +0000300 case 7: /* exectxt */
301 VG_(print_ExeContext_stats) (True /* with_stacktraces */);
302 ret = 1;
303 break;
sewardj3b290482011-05-06 21:02:55 +0000304 default:
305 vg_assert(0);
306 }
307 break;
308 }
sewardj30b3eca2011-06-28 08:20:39 +0000309 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000310 wcmd = strtok_r (NULL, " ", &ssaveptr);
311 if (wcmd != NULL) {
florian6bd9dc12012-11-23 16:17:43 +0000312 int_value = strtol (wcmd, NULL, 10);
sewardj3b290482011-05-06 21:02:55 +0000313 VG_(gdb_printf) ("gdbserver: continuing in %d ms ...\n", int_value);
314 VG_(poll)(NULL, 0, int_value);
315 }
316 VG_(gdb_printf) ("gdbserver: continuing after wait ...\n");
317 ret = 1;
318 break;
sewardj30b3eca2011-06-28 08:20:39 +0000319 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000320 kill_request ("monitor command request to kill this process\n");
321 break;
sewardj30b3eca2011-06-28 08:20:39 +0000322 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000323 Addr address;
324 SizeT verbosity = 0x20;
325
326 ret = 1;
327
328 VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr);
329 if (address != (Addr) 0 || verbosity != 0) {
330 /* we need to force the output to log for the translation trace,
331 as low level VEX tracing cannot be redirected to gdb. */
332 int saved_command_output_to_log = command_output_to_log;
333 int saved_fd = VG_(log_output_sink).fd;
334 Bool single_stepping_on_entry = valgrind_single_stepping();
335 int vex_verbosity = verbosity & 0xff;
336 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
337 if ((verbosity & 0x100) && !single_stepping_on_entry) {
338 valgrind_set_single_stepping(True);
339 // to force gdbserver instrumentation.
340 }
sewardj99d61342011-05-17 16:35:11 +0000341# if defined(VGA_arm)
342 // on arm, we need to (potentially) convert this address
343 // to the thumb form.
344 address = thumb_pc (address);
345# endif
346
sewardj3b290482011-05-06 21:02:55 +0000347 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
348 address,
349 /*debugging*/True,
350 (Int) vex_verbosity,
351 /*bbs_done*/0,
352 /*allow redir?*/True);
353 if ((verbosity & 0x100) && !single_stepping_on_entry) {
354 valgrind_set_single_stepping(False);
355 // reset single stepping.
356 }
357 command_output_to_log = saved_command_output_to_log;
358 VG_(log_output_sink).fd = saved_fd;
359 }
360 break;
361 }
362
363 default:
364 vg_assert (0);
365 }
366 return ret;
367}
368
369/* handle_gdb_monitor_command handles the provided mon string command,
370 which can be either a "standard" valgrind monitor command
371 or a tool specific monitor command.
372 If command recognised, return 1 else return 0.
373 Note that in case of ambiguous command, 1 is returned.
374*/
375static
376int handle_gdb_monitor_command (char* mon)
377{
378 UWord ret = 0;
379 UWord tool_ret = 0;
380 // initially, we assume that when returning, the desired sink is the
381 // one we have when entering. It can however be changed by the standard
382 // valgrind command handling.
383 OutputSink sink_wanted_at_return = VG_(log_output_sink);
384
385 if (!initial_valgrind_sink_saved) {
386 /* first time we enter here, we save the valgrind default log sink */
387 initial_valgrind_sink = sink_wanted_at_return;
388 initial_valgrind_sink_saved = True;
389 }
390
391 if (!command_output_to_log)
392 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
393
394 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
395
396 /* Even if command was recognised by valgrind core, we call the
397 tool command handler : this is needed to handle help command
398 and/or to let the tool do some additional processing of a
399 valgrind standard command. Note however that if valgrind
400 recognised the command, we will always return success. */
401 if (VG_(needs).client_requests) {
402 /* If the tool reports an error when handling a monitor command,
403 we need to avoid calling gdbserver during this command
404 handling. So, we temporarily set VG_(dyn_vgdb_error) to
405 a huge value to ensure m_errormgr.c does not call gdbserver. */
406 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
407 UWord arg[2];
408 VG_(dyn_vgdb_error) = 999999999;
409 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
410 arg[1] = (UWord) mon;
411 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
412 &tool_ret);
413 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
414 }
415
416 /* restore or set the desired output */
417 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
418 if (ret | tool_ret)
419 return 1;
420 else
421 return 0;
422}
423
424
425/* Handle all of the extended 'Q' packets. */
426static
427void handle_set (char *arg_own_buf, int *new_packet_len_p)
428{
429 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
430 noack_mode = True;
431 write_ok (arg_own_buf);
432 return;
433 }
434
435 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
436 int i;
437 char *from, *to;
438 char *end = arg_own_buf + strlen(arg_own_buf);
439 CORE_ADDR sig;
440 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
441 pass_signals[i] = 0;
442
443 from = arg_own_buf + 13;
444 while (from < end) {
445 to = strchr(from, ';');
446 if (to == NULL) to = end;
447 decode_address (&sig, from, to - from);
448 pass_signals[(int)sig] = 1;
philippe886fde32012-03-29 21:56:47 +0000449 dlog(1, "pass_signal gdb_nr %d %s\n",
450 (int)sig, target_signal_to_name(sig));
sewardj3b290482011-05-06 21:02:55 +0000451 from = to;
452 if (*from == ';') from++;
453 }
454 write_ok (arg_own_buf);
455 return;
456 }
457 /* Otherwise we didn't know what packet it was. Say we didn't
458 understand it. */
459 arg_own_buf[0] = 0;
460}
461
462/* Handle all of the extended 'q' packets. */
463static
464void handle_query (char *arg_own_buf, int *new_packet_len_p)
465{
466 static struct inferior_list_entry *thread_ptr;
467
468 /* qRcmd, monitor command handling. */
469 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
470 char *p = arg_own_buf + 6;
471 int cmdlen = strlen(p)/2;
472 char cmd[cmdlen+1];
473
474 if (unhexify (cmd, p, cmdlen) != cmdlen) {
475 write_enn (arg_own_buf);
476 return;
477 }
478 cmd[cmdlen] = '\0';
479
480 if (handle_gdb_monitor_command (cmd)) {
481 /* In case the command is from a standalone vgdb,
482 connection will be closed soon => flush the output. */
483 VG_(message_flush) ();
484 write_ok (arg_own_buf);
485 return;
486 } else {
487 /* cmd not recognised */
488 VG_(gdb_printf)
489 ("command '%s' not recognised\n"
490 "In gdb, try 'monitor help'\n"
491 "In a shell, try 'vgdb help'\n",
492 cmd);
493 write_ok (arg_own_buf);
494 return;
495 }
496 }
497
498 /* provide some valgrind specific info in return to qThreadExtraInfo. */
499 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
500 unsigned long gdb_id;
501 struct thread_info *ti;
502 ThreadState *tst;
503 char status[100];
504
505 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
506 ti = gdb_id_to_thread (gdb_id);
507 if (ti != NULL) {
508 tst = (ThreadState *) inferior_target_data (ti);
509 /* Additional info is the tid and the thread status. */
510 VG_(snprintf) (status, sizeof(status), "tid %d %s",
511 tst->tid,
512 VG_(name_of_ThreadStatus)(tst->status));
513 hexify (arg_own_buf, status, strlen(status));
514 return;
515 } else {
516 write_enn (arg_own_buf);
517 return;
518 }
519 }
520
521 if (strcmp ("qAttached", arg_own_buf) == 0) {
522 /* tell gdb to always detach, never kill the process */
523 arg_own_buf[0] = '1';
524 arg_own_buf[1] = 0;
525 return;
526 }
527
528 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
529 /* We have no symbol to read. */
530 write_ok (arg_own_buf);
531 return;
532 }
533
534 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
535 thread_ptr = all_threads.head;
536 VG_(sprintf) (arg_own_buf, "m%x",
537 thread_to_gdb_id ((struct thread_info *)thread_ptr));
538 thread_ptr = thread_ptr->next;
539 return;
540 }
541
542 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
543 if (thread_ptr != NULL) {
544 VG_(sprintf) (arg_own_buf, "m%x",
545 thread_to_gdb_id ((struct thread_info *)thread_ptr));
546 thread_ptr = thread_ptr->next;
547 return;
548 } else {
549 VG_(sprintf) (arg_own_buf, "l");
550 return;
551 }
552 }
553
philippe419d5f22012-05-24 21:33:17 +0000554 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL
sewardj3b290482011-05-06 21:02:55 +0000555 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
556 CORE_ADDR ofs;
557 unsigned int len, doc_len;
florian6bd9dc12012-11-23 16:17:43 +0000558 const char *annex = NULL;
sewardj3b290482011-05-06 21:02:55 +0000559 // First, the annex is extracted from the packet received.
560 // Then, it is replaced by the corresponding file name.
561 int fd;
562
563 /* Grab the annex, offset, and length. */
564 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
565 strcpy (arg_own_buf, "E00");
566 return;
567 }
568
569 if (strcmp (annex, "target.xml") == 0) {
philippe419d5f22012-05-24 21:33:17 +0000570 annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers));
571 if (annex != NULL && VG_(clo_vgdb_shadow_registers)) {
572 /* Ensure the shadow registers are initialized. */
573 initialize_shadow_low(True);
sewardj3b290482011-05-06 21:02:55 +0000574 }
sewardj3b290482011-05-06 21:02:55 +0000575 if (annex == NULL) {
576 strcpy (arg_own_buf, "E00");
577 return;
578 }
579 }
580
581 {
philippe75a5f782012-02-24 11:25:58 +0000582 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
sewardj3b290482011-05-06 21:02:55 +0000583 struct vg_stat stat_doc;
584 char toread[len];
585 int len_read;
586
587 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
588 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
589 if (fd == -1) {
590 strcpy (arg_own_buf, "E00");
591 return;
592 }
593 if (VG_(fstat) (fd, &stat_doc) != 0) {
594 VG_(close) (fd);
595 strcpy (arg_own_buf, "E00");
596 return;
597 }
598 doc_len = stat_doc.size;
599
600 if (len > PBUFSIZ - POVERHSIZ)
601 len = PBUFSIZ - POVERHSIZ;
602
603 if (ofs > doc_len) {
604 write_enn (arg_own_buf);
605 VG_(close) (fd);
606 return;
607 }
608 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
609 len_read = VG_(read) (fd, toread, len);
florian1636d332012-11-15 04:27:04 +0000610 *new_packet_len_p = write_qxfer_response (arg_own_buf, (unsigned char *)toread,
sewardj3b290482011-05-06 21:02:55 +0000611 len_read, ofs + len_read < doc_len);
612 VG_(close) (fd);
613 return;
614 }
615 }
616
sewardj2a312392011-06-26 09:26:48 +0000617 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
618 unsigned char *data;
619 int n;
620 CORE_ADDR ofs;
621 unsigned int len;
florian6bd9dc12012-11-23 16:17:43 +0000622 const char *annex;
sewardj2a312392011-06-26 09:26:48 +0000623
624 /* Reject any annex; grab the offset and length. */
625 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
626 || annex[0] != '\0') {
627 strcpy (arg_own_buf, "E00");
628 return;
629 }
630
631 if (len > PBUFSIZ - 2)
632 len = PBUFSIZ - 2;
633 data = malloc (len);
634
635 {
636 UWord *client_auxv = VG_(client_auxv);
637 unsigned int client_auxv_len = 0;
638 while (*client_auxv != 0) {
639 dlog(4, "auxv %lld %llx\n",
640 (ULong)*client_auxv,
641 (ULong)*(client_auxv+1));
642 client_auxv++;
643 client_auxv++;
644 client_auxv_len += 2 * sizeof(UWord);
645 }
646 client_auxv_len += 2 * sizeof(UWord);
647 dlog(4, "auxv len %d\n", client_auxv_len);
648
649 if (ofs >= client_auxv_len)
650 n = -1;
651 else {
652 n = client_auxv_len - ofs;
653 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
654 }
655 }
656
657 if (n < 0)
658 write_enn (arg_own_buf);
659 else if (n > len)
660 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
661 else
662 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
663
664 free (data);
665
666 return;
667 }
668
669
sewardj3b290482011-05-06 21:02:55 +0000670 /* Protocol features query. */
671 if (strncmp ("qSupported", arg_own_buf, 10) == 0
672 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
673 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
674 /* Note: max packet size including frame and checksum, but without
675 trailing null byte, which is not sent/received. */
676
677 strcat (arg_own_buf, ";QStartNoAckMode+");
678 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +0000679 if (VG_(client_auxv))
680 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +0000681
philippe419d5f22012-05-24 21:33:17 +0000682 if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) {
sewardj3b290482011-05-06 21:02:55 +0000683 strcat (arg_own_buf, ";qXfer:features:read+");
684 /* if a new gdb connects to us, we have to reset the register
685 set to the normal register sets to allow this new gdb to
686 decide to use or not the shadow registers.
687
688 Note that the reset is only done for gdb that are sending
689 qSupported packets. If a user first connected with a recent
690 gdb using shadow registers and then with a very old gdb
691 that does not use qSupported packet, then the old gdb will
692 not properly connect. */
693 initialize_shadow_low(False);
694 }
695 return;
696 }
697
698 /* Otherwise we didn't know what packet it was. Say we didn't
699 understand it. */
700 arg_own_buf[0] = 0;
701}
702
703/* Handle all of the extended 'v' packets. */
704static
sewardj0bb3c672011-07-26 23:29:25 +0000705void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +0000706{
707 /* vcont packet code from gdb 6.6 removed */
708
709 /* Otherwise we didn't know what packet it was. Say we didn't
710 understand it. */
711 arg_own_buf[0] = 0;
712 return;
713}
714
715static
716void myresume (int step, int sig)
717{
718 struct thread_resume resume_info[2];
719 int n = 0;
720
philippe349a3912012-05-23 21:50:36 +0000721 if (step || sig) {
sewardj3b290482011-05-06 21:02:55 +0000722 resume_info[0].step = step;
723 resume_info[0].sig = sig;
sewardj3b290482011-05-06 21:02:55 +0000724 n++;
725 }
sewardj3b290482011-05-06 21:02:55 +0000726 resume_info[n].step = 0;
727 resume_info[n].sig = 0;
sewardj3b290482011-05-06 21:02:55 +0000728
philippe349a3912012-05-23 21:50:36 +0000729 resume_reply_packet_needed = True;
730 valgrind_resume (resume_info);
sewardj3b290482011-05-06 21:02:55 +0000731}
732
733/* server_main global variables */
734static char *own_buf;
735static unsigned char *mem_buf;
736
737void gdbserver_init (void)
738{
739 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
740 noack_mode = False;
philippe349a3912012-05-23 21:50:36 +0000741 valgrind_initialize_target ();
philippe0e1cac92012-02-28 22:37:44 +0000742 // After a fork, gdbserver_init can be called again.
743 // We do not have to re-malloc the buffers in such a case.
744 if (own_buf == NULL)
745 own_buf = malloc (PBUFSIZ);
746 if (mem_buf == NULL)
747 mem_buf = malloc (PBUFSIZ);
sewardj3b290482011-05-06 21:02:55 +0000748}
749
750void gdbserver_terminate (void)
751{
752 /* last call to gdbserver is cleanup call */
753 if (VG_MINIMAL_SETJMP(toplevel)) {
754 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
755 return;
756 }
757 remote_close();
758}
759
760void server_main (void)
761{
762 static char status;
sewardj0bb3c672011-07-26 23:29:25 +0000763 static int zignal;
sewardj3b290482011-05-06 21:02:55 +0000764
765 char ch;
766 int i = 0;
767 unsigned int len;
768 CORE_ADDR mem_addr;
769
philippe349a3912012-05-23 21:50:36 +0000770 zignal = valgrind_wait (&status);
sewardj3b290482011-05-06 21:02:55 +0000771 if (VG_MINIMAL_SETJMP(toplevel)) {
772 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
773 }
774 while (1) {
775 unsigned char sig;
776 int packet_len;
777 int new_packet_len = -1;
778
philippe349a3912012-05-23 21:50:36 +0000779 if (resume_reply_packet_needed) {
780 /* Send the resume reply to reply to last GDB resume
781 request. */
782 resume_reply_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +0000783 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000784 putpkt (own_buf);
785 }
786
philippe0447bbd2012-10-17 21:32:03 +0000787 /* If we our status is terminal (exit or fatal signal) get out
788 as quickly as we can. We won't be able to handle any request
789 anymore. */
790 if (status == 'W' || status == 'X') {
791 return;
792 }
793
sewardj3b290482011-05-06 21:02:55 +0000794 packet_len = getpkt (own_buf);
795 if (packet_len <= 0)
796 break;
797
798 i = 0;
799 ch = own_buf[i++];
800 switch (ch) {
801 case 'Q':
802 handle_set (own_buf, &new_packet_len);
803 break;
804 case 'q':
805 handle_query (own_buf, &new_packet_len);
806 break;
807 case 'd':
808 /* set/unset debugging is done through valgrind debug level. */
809 own_buf[0] = '\0';
810 break;
811 case 'D':
812 reset_valgrind_sink("gdb detaching from process");
813
814 /* When detaching or kill the process, gdb expects to get
815 an packet OK back. Any other output will make gdb
816 believes detach did not work. */
817 write_ok (own_buf);
818 putpkt (own_buf);
819 remote_finish (reset_after_error);
820 remote_open (VG_(clo_vgdb_prefix));
821 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +0000822 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +0000823 return;
824 case '!':
825 /* We can not use the extended protocol with valgrind,
826 because we can not restart the running
827 program. So return unrecognized. */
828 own_buf[0] = '\0';
829 break;
830 case '?':
sewardj0bb3c672011-07-26 23:29:25 +0000831 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000832 break;
833 case 'H':
834 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
835 unsigned long gdb_id, thread_id;
836
837 gdb_id = strtoul (&own_buf[2], NULL, 16);
838 thread_id = gdb_id_to_thread_id (gdb_id);
839 if (thread_id == 0) {
840 write_enn (own_buf);
841 break;
842 }
843
844 if (own_buf[1] == 'g') {
845 general_thread = thread_id;
846 set_desired_inferior (1);
847 } else if (own_buf[1] == 'c') {
848 cont_thread = thread_id;
849 } else if (own_buf[1] == 's') {
850 step_thread = thread_id;
851 }
852
853 write_ok (own_buf);
854 } else {
855 /* Silently ignore it so that gdb can extend the protocol
856 without compatibility headaches. */
857 own_buf[0] = '\0';
858 }
859 break;
860 case 'g':
861 set_desired_inferior (1);
862 registers_to_string (own_buf);
863 break;
864 case 'G':
865 set_desired_inferior (1);
866 registers_from_string (&own_buf[1]);
867 write_ok (own_buf);
868 break;
869 case 'P': {
870 int regno;
871 char *regbytes;
872 Bool mod;
873 ThreadState *tst;
874 regno = strtol(&own_buf[1], NULL, 16);
875 regbytes = strchr(&own_buf[0], '=') + 1;
876 set_desired_inferior (1);
877 tst = (ThreadState *) inferior_target_data (current_inferior);
878 /* Only accept changing registers in "runnable state3.
879 In fact, it would be ok to change most of the registers
880 except a few "sensitive" registers such as the PC, SP, BP.
881 We assume we do not need to very specific here, and that we
882 can just refuse all of these. */
883 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
884 supply_register_from_string (regno, regbytes, &mod);
885 write_ok (own_buf);
886 } else {
887 /* at least from gdb 6.6 onwards, an E. error
888 reply is shown to the user. So, we do an error
889 msg which both is accepted by gdb as an error msg
890 and is readable by the user. */
891 VG_(sprintf)
892 (own_buf,
893"E.\n"
894"ERROR changing register %s regno %d\n"
895"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
896"set pc, calling from gdb a function in the debugged process, ...)\n"
897"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
898"Thread status is %s\n",
899 find_register_by_number (regno)->name, regno,
900 VG_(name_of_ThreadStatus)(tst->status));
901 if (VG_(clo_verbosity) > 1)
902 VG_(umsg) ("%s\n", own_buf);
903 }
904 break;
905 }
906 case 'm':
907 decode_m_packet (&own_buf[1], &mem_addr, &len);
philippe349a3912012-05-23 21:50:36 +0000908 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +0000909 convert_int_to_ascii (mem_buf, own_buf, len);
910 else
911 write_enn (own_buf);
912 break;
913 case 'M':
914 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
philippe349a3912012-05-23 21:50:36 +0000915 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +0000916 write_ok (own_buf);
917 else
918 write_enn (own_buf);
919 break;
920 case 'X':
921 if (decode_X_packet (&own_buf[1], packet_len - 1,
922 &mem_addr, &len, mem_buf) < 0
philippe349a3912012-05-23 21:50:36 +0000923 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
sewardj3b290482011-05-06 21:02:55 +0000924 write_enn (own_buf);
925 else
926 write_ok (own_buf);
927 break;
928 case 'C':
929 convert_ascii_to_int (own_buf + 1, &sig, 1);
930 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +0000931 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +0000932 else
sewardj0bb3c672011-07-26 23:29:25 +0000933 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +0000934 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +0000935 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +0000936 return; // return control to valgrind
937 case 'S':
938 convert_ascii_to_int (own_buf + 1, &sig, 1);
939 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +0000940 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +0000941 else
sewardj0bb3c672011-07-26 23:29:25 +0000942 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +0000943 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +0000944 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +0000945 return; // return control to valgrind
946 case 'c':
947 set_desired_inferior (0);
948 myresume (0, 0);
949 return; // return control to valgrind
950 case 's':
951 set_desired_inferior (0);
952 myresume (1, 0);
953 return; // return control to valgrind
954 case 'Z': {
955 char *lenptr;
956 char *dataptr;
957 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
958 int zlen = strtol (lenptr + 1, &dataptr, 16);
959 char type = own_buf[1];
960
philippe349a3912012-05-23 21:50:36 +0000961 if (type < '0' || type > '4') {
962 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +0000963 own_buf[0] = '\0';
964 } else {
965 int res;
966
philippe349a3912012-05-23 21:50:36 +0000967 res = valgrind_insert_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +0000968 if (res == 0)
969 write_ok (own_buf);
970 else if (res == 1)
971 /* Unsupported. */
972 own_buf[0] = '\0';
973 else
974 write_enn (own_buf);
975 }
976 break;
977 }
978 case 'z': {
979 char *lenptr;
980 char *dataptr;
981 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
982 int zlen = strtol (lenptr + 1, &dataptr, 16);
983 char type = own_buf[1];
984
philippe349a3912012-05-23 21:50:36 +0000985 if (type < '0' || type > '4') {
986 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +0000987 own_buf[0] = '\0';
988 } else {
989 int res;
990
philippe349a3912012-05-23 21:50:36 +0000991 res = valgrind_remove_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +0000992 if (res == 0)
993 write_ok (own_buf);
994 else if (res == 1)
995 /* Unsupported. */
996 own_buf[0] = '\0';
997 else
998 write_enn (own_buf);
999 }
1000 break;
1001 }
1002 case 'k':
1003 kill_request("Gdb request to kill this process\n");
1004 break;
1005 case 'T': {
1006 unsigned long gdb_id, thread_id;
1007
1008 gdb_id = strtoul (&own_buf[1], NULL, 16);
1009 thread_id = gdb_id_to_thread_id (gdb_id);
1010 if (thread_id == 0) {
1011 write_enn (own_buf);
1012 break;
1013 }
1014
philippe349a3912012-05-23 21:50:36 +00001015 if (valgrind_thread_alive (thread_id))
sewardj3b290482011-05-06 21:02:55 +00001016 write_ok (own_buf);
1017 else
1018 write_enn (own_buf);
1019 break;
1020 }
1021 case 'R':
1022 /* Restarting the inferior is only supported in the
1023 extended protocol.
1024 => It is a request we don't understand. Respond with an
1025 empty packet so that gdb knows that we don't support this
1026 request. */
1027 own_buf[0] = '\0';
1028 break;
1029 case 'v':
1030 /* Extended (long) request. */
sewardj0bb3c672011-07-26 23:29:25 +00001031 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001032 break;
1033 default:
1034 /* It is a request we don't understand. Respond with an
1035 empty packet so that gdb knows that we don't support this
1036 request. */
1037 own_buf[0] = '\0';
1038 break;
1039 }
1040
1041 if (new_packet_len != -1)
1042 putpkt_binary (own_buf, new_packet_len);
1043 else
1044 putpkt (own_buf);
1045
1046 if (status == 'W')
sewardj0bb3c672011-07-26 23:29:25 +00001047 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001048 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001049 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
sewardj0bb3c672011-07-26 23:29:25 +00001050 target_signal_to_host (zignal),
1051 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001052 if (status == 'W' || status == 'X') {
1053 VG_(umsg) ("Process exiting\n");
1054 VG_(exit) (0);
1055 }
1056 }
1057
1058 /* We come here when getpkt fails => close the connection,
1059 and re-open. Then return control to valgrind.
1060 We return the control to valgrind as we assume that
1061 the connection was closed due to vgdb having finished
1062 to execute a command. */
1063 if (VG_(clo_verbosity) > 1)
1064 VG_(umsg) ("Remote side has terminated connection. "
1065 "GDBserver will reopen the connection.\n");
1066 remote_finish (reset_after_error);
1067 remote_open (VG_(clo_vgdb_prefix));
1068 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001069 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001070 return;
1071}