blob: 58fb2295ac484c21e1c093641cb8d96628d13d94 [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"
sewardj3b290482011-05-06 21:02:55 +000030
31unsigned long cont_thread;
32unsigned long general_thread;
33unsigned long step_thread;
34unsigned long thread_from_wait;
35unsigned long old_thread_from_wait;
36
philippe886fde32012-03-29 21:56:47 +000037int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */
bart2dafc542011-05-18 16:08:28 +000038
sewardj3b290482011-05-06 21:02:55 +000039/* for a gdbserver integrated in valgrind, resuming the process consists
40 in returning the control to valgrind.
philippe349a3912012-05-23 21:50:36 +000041 The guess process resumes its execution.
sewardj3b290482011-05-06 21:02:55 +000042 Then at the next error or break or ..., valgrind calls gdbserver again.
philippe349a3912012-05-23 21:50:36 +000043 A resume reply packet must then be built to inform GDB that the
44 resume request is finished.
45 resume_reply_packet_needed records the fact that the next call to gdbserver
sewardj3b290482011-05-06 21:02:55 +000046 must send a resume packet to gdb. */
philippe349a3912012-05-23 21:50:36 +000047static Bool resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +000048
49VG_MINIMAL_JMP_BUF(toplevel);
50
51/* Decode a qXfer read request. Return 0 if everything looks OK,
52 or -1 otherwise. */
53
54static
55int decode_xfer_read (char *buf, char **annex, CORE_ADDR *ofs, unsigned int *len)
56{
57 /* Extract and NUL-terminate the annex. */
58 *annex = buf;
59 while (*buf && *buf != ':')
60 buf++;
61 if (*buf == '\0')
62 return -1;
63 *buf++ = 0;
64
65 /* After the read/write marker and annex, qXfer looks like a
66 traditional 'm' packet. */
67 decode_m_packet (buf, ofs, len);
68
69 return 0;
70}
71
72/* Write the response to a successful qXfer read. Returns the
73 length of the (binary) data stored in BUF, corresponding
74 to as much of DATA/LEN as we could fit. IS_MORE controls
75 the first character of the response. */
76static
77int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more)
78{
79 int out_len;
80
81 if (is_more)
82 buf[0] = 'm';
83 else
84 buf[0] = 'l';
85
86 return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len,
87 PBUFSIZ - POVERHSIZ - 1) + 1;
88}
89
90static Bool initial_valgrind_sink_saved = False;
91/* True <=> valgrind log sink saved in initial_valgrind_sink */
92static OutputSink initial_valgrind_sink;
93
94static Bool command_output_to_log = False;
95/* True <=> command output goes to log instead of gdb */
96
97void reset_valgrind_sink(char *info)
98{
99 if (VG_(log_output_sink).fd != initial_valgrind_sink.fd
100 && initial_valgrind_sink_saved) {
101 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
102 VG_(umsg) ("Reset valgrind output to log (%s)\n",
103 (info = NULL ? "" : info));
104 }
105}
106
107static
108void kill_request (char *msg)
109{
110 VG_(umsg) ("%s", msg);
111 remote_close();
112 VG_(exit) (0);
113}
114
115/* handle_gdb_valgrind_command handles the provided mon string command.
116 If command is recognised, return 1 else return 0.
117 Note that in case of ambiguous command, 1 is returned.
118
119 *sink_wanted_at_return is modified if one of the commands
sewardj30b3eca2011-06-28 08:20:39 +0000120 'v.set *_output' is handled.
sewardj3b290482011-05-06 21:02:55 +0000121*/
122static
123int handle_gdb_valgrind_command (char* mon, OutputSink* sink_wanted_at_return)
124{
125 UWord ret = 0;
126 char s[strlen(mon)+1]; /* copy for strtok_r */
127 char* wcmd;
128 Char* ssaveptr;
129 char* endptr;
130 int kwdid;
131 int int_value;
132
133 vg_assert (initial_valgrind_sink_saved);
134
135 strcpy (s, mon);
136 wcmd = strtok_r (s, " ", &ssaveptr);
137 /* NB: if possible, avoid introducing a new command below which
sewardj30b3eca2011-06-28 08:20:39 +0000138 starts with the same 3 first letters as an already existing
sewardj3b290482011-05-06 21:02:55 +0000139 command. This ensures a shorter abbreviation for the user. */
sewardj30b3eca2011-06-28 08:20:39 +0000140 switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate",
sewardj3b290482011-05-06 21:02:55 +0000141 wcmd, kwd_report_duplicated_matches)) {
142 case -2:
143 ret = 1;
144 break;
145 case -1:
146 break;
147 case 0: /* help */
148 ret = 1;
149 wcmd = strtok_r (NULL, " ", &ssaveptr);
150 if (wcmd == NULL) {
151 int_value = 0;
152 } else {
153 switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) {
154 case -2: int_value = 0; break;
155 case -1: int_value = 0; break;
156 case 0: int_value = 1; break;
157 default: tl_assert (0);
158 }
159 }
160
161 VG_(gdb_printf) (
162"general valgrind monitor commands:\n"
163" help [debug] : monitor command help. With debug: + debugging commands\n"
sewardj30b3eca2011-06-28 08:20:39 +0000164" v.wait [<ms>] : sleep <ms> (default 0) then continue\n"
165" v.info all_errors : show all errors found so far\n"
166" v.info last_error : show last error found\n"
167" v.info n_errs_found : show the nr of errors found so far\n"
168" v.kill : kill the Valgrind process\n"
169" v.set gdb_output : set valgrind output to gdb\n"
170" v.set log_output : set valgrind output to log\n"
171" v.set mixed_output : set valgrind output to log, interactive output to gdb\n"
172" v.set vgdb-error <errornr> : debug me at error >= <errornr> \n");
sewardj3b290482011-05-06 21:02:55 +0000173 if (int_value) { VG_(gdb_printf) (
174"debugging valgrind internals monitor commands:\n"
sewardj30b3eca2011-06-28 08:20:39 +0000175" v.info gdbserver_status : show gdbserver status\n"
philippe93a6a8d2012-04-27 22:59:43 +0000176" v.info memory [aspacemgr] : show valgrind heap memory stats\n"
177" (with aspacemgr arg, also shows valgrind segments on log ouput)\n"
sewardjd6e13d82011-10-22 20:23:30 +0000178" v.info scheduler : show valgrind thread state and stacktrace\n"
sewardj30b3eca2011-06-28 08:20:39 +0000179" v.set debuglog <level> : set valgrind debug log level to <level>\n"
180" v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n"
sewardj3b290482011-05-06 21:02:55 +0000181" (default traceflags 0b00100000 : show after instrumentation)\n"
182" An additional flag 0b100000000 allows to show gdbserver instrumentation\n");
183 }
184 break;
sewardj30b3eca2011-06-28 08:20:39 +0000185 case 1: /* v.set */
sewardj3b290482011-05-06 21:02:55 +0000186 ret = 1;
187 wcmd = strtok_r (NULL, " ", &ssaveptr);
188 switch (kwdid = VG_(keyword_id)
189 ("vgdb-error debuglog gdb_output log_output mixed_output",
190 wcmd, kwd_report_all)) {
191 case -2:
192 case -1:
193 break;
194 case 0: /* vgdb-error */
195 case 1: /* debuglog */
196 wcmd = strtok_r (NULL, " ", &ssaveptr);
197 if (wcmd == NULL) {
198 int_value = 0;
199 endptr = "empty"; /* to report an error below */
200 } else {
201 int_value = strtol (wcmd, &endptr, 10);
202 }
203 if (*endptr != '\0') {
204 VG_(gdb_printf) ("missing or malformed integer value\n");
205 } else if (kwdid == 0) {
206 VG_(gdb_printf) ("vgdb-error value changed from %d to %d\n",
207 VG_(dyn_vgdb_error), int_value);
208 VG_(dyn_vgdb_error) = int_value;
209 } else if (kwdid == 1) {
210 VG_(gdb_printf) ("debuglog value changed from %d to %d\n",
211 VG_(debugLog_getLevel)(), int_value);
212 VG_(debugLog_startup) (int_value, "gdbsrv");
213 } else {
214 vg_assert (0);
215 }
216 break;
217 case 2: /* gdb_output */
218 (*sink_wanted_at_return).fd = -2;
219 command_output_to_log = False;
220 VG_(gdb_printf) ("valgrind output will go to gdb\n");
221 break;
222 case 3: /* log_output */
223 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
224 command_output_to_log = True;
225 VG_(gdb_printf) ("valgrind output will go to log\n");
226 break;
227 case 4: /* mixed output */
228 (*sink_wanted_at_return).fd = initial_valgrind_sink.fd;
229 command_output_to_log = False;
230 VG_(gdb_printf)
231 ("valgrind output will go to log, interactive output will go to gdb\n");
232 break;
233 default:
234 vg_assert (0);
235 }
236 break;
sewardj30b3eca2011-06-28 08:20:39 +0000237 case 2: /* v.info */ {
sewardj3b290482011-05-06 21:02:55 +0000238 ret = 1;
239 wcmd = strtok_r (NULL, " ", &ssaveptr);
240 switch (kwdid = VG_(keyword_id)
sewardjd6e13d82011-10-22 20:23:30 +0000241 ("all_errors n_errs_found last_error gdbserver_status memory"
242 " scheduler",
sewardj3b290482011-05-06 21:02:55 +0000243 wcmd, kwd_report_all)) {
244 case -2:
245 case -1:
246 break;
247 case 0: // all_errors
248 // A verbosity of minimum 2 is needed to show the errors.
249 VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False);
250 break;
251 case 1: // n_errs_found
philippebaf69642012-02-15 22:29:30 +0000252 VG_(gdb_printf) ("n_errs_found %d n_errs_shown %d (vgdb-error %d)\n",
sewardj3b290482011-05-06 21:02:55 +0000253 VG_(get_n_errs_found) (),
philippebaf69642012-02-15 22:29:30 +0000254 VG_(get_n_errs_shown) (),
sewardj3b290482011-05-06 21:02:55 +0000255 VG_(dyn_vgdb_error));
256 break;
257 case 2: // last_error
258 VG_(show_last_error)();
259 break;
260 case 3: // gdbserver_status
261 VG_(gdbserver_status_output)();
262 break;
263 case 4: /* memory */
264 VG_(print_all_arena_stats) ();
265 if (VG_(clo_profile_heap))
266 VG_(print_arena_cc_analysis) ();
philippe93a6a8d2012-04-27 22:59:43 +0000267 wcmd = strtok_r (NULL, " ", &ssaveptr);
268 if (wcmd != NULL) {
269 switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) {
270 case -2:
271 case -1: break;
272 case 0:
273 VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr");
274 break;
275 default: tl_assert (0);
276 }
277 }
278
sewardj3b290482011-05-06 21:02:55 +0000279 ret = 1;
280 break;
sewardjd6e13d82011-10-22 20:23:30 +0000281 case 5: /* scheduler */
282 VG_(show_sched_status) ();
283 ret = 1;
284 break;
sewardj3b290482011-05-06 21:02:55 +0000285 default:
286 vg_assert(0);
287 }
288 break;
289 }
sewardj30b3eca2011-06-28 08:20:39 +0000290 case 3: /* v.wait */
sewardj3b290482011-05-06 21:02:55 +0000291 wcmd = strtok_r (NULL, " ", &ssaveptr);
292 if (wcmd != NULL) {
293 int_value = strtol (wcmd, &endptr, 10);
294 VG_(gdb_printf) ("gdbserver: continuing in %d ms ...\n", int_value);
295 VG_(poll)(NULL, 0, int_value);
296 }
297 VG_(gdb_printf) ("gdbserver: continuing after wait ...\n");
298 ret = 1;
299 break;
sewardj30b3eca2011-06-28 08:20:39 +0000300 case 4: /* v.kill */
sewardj3b290482011-05-06 21:02:55 +0000301 kill_request ("monitor command request to kill this process\n");
302 break;
sewardj30b3eca2011-06-28 08:20:39 +0000303 case 5: { /* v.translate */
sewardj3b290482011-05-06 21:02:55 +0000304 Addr address;
305 SizeT verbosity = 0x20;
306
307 ret = 1;
308
309 VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr);
310 if (address != (Addr) 0 || verbosity != 0) {
311 /* we need to force the output to log for the translation trace,
312 as low level VEX tracing cannot be redirected to gdb. */
313 int saved_command_output_to_log = command_output_to_log;
314 int saved_fd = VG_(log_output_sink).fd;
315 Bool single_stepping_on_entry = valgrind_single_stepping();
316 int vex_verbosity = verbosity & 0xff;
317 VG_(log_output_sink).fd = initial_valgrind_sink.fd;
318 if ((verbosity & 0x100) && !single_stepping_on_entry) {
319 valgrind_set_single_stepping(True);
320 // to force gdbserver instrumentation.
321 }
sewardj99d61342011-05-17 16:35:11 +0000322# if defined(VGA_arm)
323 // on arm, we need to (potentially) convert this address
324 // to the thumb form.
325 address = thumb_pc (address);
326# endif
327
sewardj3b290482011-05-06 21:02:55 +0000328 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
329 address,
330 /*debugging*/True,
331 (Int) vex_verbosity,
332 /*bbs_done*/0,
333 /*allow redir?*/True);
334 if ((verbosity & 0x100) && !single_stepping_on_entry) {
335 valgrind_set_single_stepping(False);
336 // reset single stepping.
337 }
338 command_output_to_log = saved_command_output_to_log;
339 VG_(log_output_sink).fd = saved_fd;
340 }
341 break;
342 }
343
344 default:
345 vg_assert (0);
346 }
347 return ret;
348}
349
350/* handle_gdb_monitor_command handles the provided mon string command,
351 which can be either a "standard" valgrind monitor command
352 or a tool specific monitor command.
353 If command recognised, return 1 else return 0.
354 Note that in case of ambiguous command, 1 is returned.
355*/
356static
357int handle_gdb_monitor_command (char* mon)
358{
359 UWord ret = 0;
360 UWord tool_ret = 0;
361 // initially, we assume that when returning, the desired sink is the
362 // one we have when entering. It can however be changed by the standard
363 // valgrind command handling.
364 OutputSink sink_wanted_at_return = VG_(log_output_sink);
365
366 if (!initial_valgrind_sink_saved) {
367 /* first time we enter here, we save the valgrind default log sink */
368 initial_valgrind_sink = sink_wanted_at_return;
369 initial_valgrind_sink_saved = True;
370 }
371
372 if (!command_output_to_log)
373 VG_(log_output_sink).fd = -2; /* redirect to monitor_output */
374
375 ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return);
376
377 /* Even if command was recognised by valgrind core, we call the
378 tool command handler : this is needed to handle help command
379 and/or to let the tool do some additional processing of a
380 valgrind standard command. Note however that if valgrind
381 recognised the command, we will always return success. */
382 if (VG_(needs).client_requests) {
383 /* If the tool reports an error when handling a monitor command,
384 we need to avoid calling gdbserver during this command
385 handling. So, we temporarily set VG_(dyn_vgdb_error) to
386 a huge value to ensure m_errormgr.c does not call gdbserver. */
387 Int save_dyn_vgdb_error = VG_(dyn_vgdb_error);
388 UWord arg[2];
389 VG_(dyn_vgdb_error) = 999999999;
390 arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND;
391 arg[1] = (UWord) mon;
392 VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg,
393 &tool_ret);
394 VG_(dyn_vgdb_error) = save_dyn_vgdb_error;
395 }
396
397 /* restore or set the desired output */
398 VG_(log_output_sink).fd = sink_wanted_at_return.fd;
399 if (ret | tool_ret)
400 return 1;
401 else
402 return 0;
403}
404
405
406/* Handle all of the extended 'Q' packets. */
407static
408void handle_set (char *arg_own_buf, int *new_packet_len_p)
409{
410 if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) {
411 noack_mode = True;
412 write_ok (arg_own_buf);
413 return;
414 }
415
416 if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) {
417 int i;
418 char *from, *to;
419 char *end = arg_own_buf + strlen(arg_own_buf);
420 CORE_ADDR sig;
421 for (i = 0; i < TARGET_SIGNAL_LAST; i++)
422 pass_signals[i] = 0;
423
424 from = arg_own_buf + 13;
425 while (from < end) {
426 to = strchr(from, ';');
427 if (to == NULL) to = end;
428 decode_address (&sig, from, to - from);
429 pass_signals[(int)sig] = 1;
philippe886fde32012-03-29 21:56:47 +0000430 dlog(1, "pass_signal gdb_nr %d %s\n",
431 (int)sig, target_signal_to_name(sig));
sewardj3b290482011-05-06 21:02:55 +0000432 from = to;
433 if (*from == ';') from++;
434 }
435 write_ok (arg_own_buf);
436 return;
437 }
438 /* Otherwise we didn't know what packet it was. Say we didn't
439 understand it. */
440 arg_own_buf[0] = 0;
441}
442
443/* Handle all of the extended 'q' packets. */
444static
445void handle_query (char *arg_own_buf, int *new_packet_len_p)
446{
447 static struct inferior_list_entry *thread_ptr;
448
449 /* qRcmd, monitor command handling. */
450 if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) {
451 char *p = arg_own_buf + 6;
452 int cmdlen = strlen(p)/2;
453 char cmd[cmdlen+1];
454
455 if (unhexify (cmd, p, cmdlen) != cmdlen) {
456 write_enn (arg_own_buf);
457 return;
458 }
459 cmd[cmdlen] = '\0';
460
461 if (handle_gdb_monitor_command (cmd)) {
462 /* In case the command is from a standalone vgdb,
463 connection will be closed soon => flush the output. */
464 VG_(message_flush) ();
465 write_ok (arg_own_buf);
466 return;
467 } else {
468 /* cmd not recognised */
469 VG_(gdb_printf)
470 ("command '%s' not recognised\n"
471 "In gdb, try 'monitor help'\n"
472 "In a shell, try 'vgdb help'\n",
473 cmd);
474 write_ok (arg_own_buf);
475 return;
476 }
477 }
478
479 /* provide some valgrind specific info in return to qThreadExtraInfo. */
480 if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) {
481 unsigned long gdb_id;
482 struct thread_info *ti;
483 ThreadState *tst;
484 char status[100];
485
486 gdb_id = strtoul (&arg_own_buf[17], NULL, 16);
487 ti = gdb_id_to_thread (gdb_id);
488 if (ti != NULL) {
489 tst = (ThreadState *) inferior_target_data (ti);
490 /* Additional info is the tid and the thread status. */
491 VG_(snprintf) (status, sizeof(status), "tid %d %s",
492 tst->tid,
493 VG_(name_of_ThreadStatus)(tst->status));
494 hexify (arg_own_buf, status, strlen(status));
495 return;
496 } else {
497 write_enn (arg_own_buf);
498 return;
499 }
500 }
501
502 if (strcmp ("qAttached", arg_own_buf) == 0) {
503 /* tell gdb to always detach, never kill the process */
504 arg_own_buf[0] = '1';
505 arg_own_buf[1] = 0;
506 return;
507 }
508
509 if (strcmp ("qSymbol::", arg_own_buf) == 0) {
510 /* We have no symbol to read. */
511 write_ok (arg_own_buf);
512 return;
513 }
514
515 if (strcmp ("qfThreadInfo", arg_own_buf) == 0) {
516 thread_ptr = all_threads.head;
517 VG_(sprintf) (arg_own_buf, "m%x",
518 thread_to_gdb_id ((struct thread_info *)thread_ptr));
519 thread_ptr = thread_ptr->next;
520 return;
521 }
522
523 if (strcmp ("qsThreadInfo", arg_own_buf) == 0) {
524 if (thread_ptr != NULL) {
525 VG_(sprintf) (arg_own_buf, "m%x",
526 thread_to_gdb_id ((struct thread_info *)thread_ptr));
527 thread_ptr = thread_ptr->next;
528 return;
529 } else {
530 VG_(sprintf) (arg_own_buf, "l");
531 return;
532 }
533 }
534
philippe349a3912012-05-23 21:50:36 +0000535 if ( (valgrind_target_xml() != NULL
536 || valgrind_shadow_target_xml() != NULL)
sewardj3b290482011-05-06 21:02:55 +0000537 && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) {
538 CORE_ADDR ofs;
539 unsigned int len, doc_len;
540 char *annex = NULL;
541 // First, the annex is extracted from the packet received.
542 // Then, it is replaced by the corresponding file name.
543 int fd;
544
545 /* Grab the annex, offset, and length. */
546 if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) {
547 strcpy (arg_own_buf, "E00");
548 return;
549 }
550
551 if (strcmp (annex, "target.xml") == 0) {
552 annex = NULL; // to replace it by the corresponding filename.
553
554 /* If VG_(clo_vgdb_shadow_registers), try to use
555 shadow_target_xml. Fallback to target_xml
556 if not defined. */
557 if (VG_(clo_vgdb_shadow_registers)) {
philippe349a3912012-05-23 21:50:36 +0000558 annex = valgrind_shadow_target_xml();
sewardj3b290482011-05-06 21:02:55 +0000559 if (annex != NULL)
560 /* Ensure the shadow registers are initialized. */
561 initialize_shadow_low(True);
562 }
563 if (annex == NULL)
philippe349a3912012-05-23 21:50:36 +0000564 annex = valgrind_target_xml();
sewardj3b290482011-05-06 21:02:55 +0000565 if (annex == NULL) {
566 strcpy (arg_own_buf, "E00");
567 return;
568 }
569 }
570
571 {
philippe75a5f782012-02-24 11:25:58 +0000572 char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1];
sewardj3b290482011-05-06 21:02:55 +0000573 struct vg_stat stat_doc;
574 char toread[len];
575 int len_read;
576
577 VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex);
578 fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0);
579 if (fd == -1) {
580 strcpy (arg_own_buf, "E00");
581 return;
582 }
583 if (VG_(fstat) (fd, &stat_doc) != 0) {
584 VG_(close) (fd);
585 strcpy (arg_own_buf, "E00");
586 return;
587 }
588 doc_len = stat_doc.size;
589
590 if (len > PBUFSIZ - POVERHSIZ)
591 len = PBUFSIZ - POVERHSIZ;
592
593 if (ofs > doc_len) {
594 write_enn (arg_own_buf);
595 VG_(close) (fd);
596 return;
597 }
598 VG_(lseek) (fd, ofs, VKI_SEEK_SET);
599 len_read = VG_(read) (fd, toread, len);
600 *new_packet_len_p = write_qxfer_response (arg_own_buf, toread,
601 len_read, ofs + len_read < doc_len);
602 VG_(close) (fd);
603 return;
604 }
605 }
606
sewardj2a312392011-06-26 09:26:48 +0000607 if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) {
608 unsigned char *data;
609 int n;
610 CORE_ADDR ofs;
611 unsigned int len;
612 char *annex;
613
614 /* Reject any annex; grab the offset and length. */
615 if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0
616 || annex[0] != '\0') {
617 strcpy (arg_own_buf, "E00");
618 return;
619 }
620
621 if (len > PBUFSIZ - 2)
622 len = PBUFSIZ - 2;
623 data = malloc (len);
624
625 {
626 UWord *client_auxv = VG_(client_auxv);
627 unsigned int client_auxv_len = 0;
628 while (*client_auxv != 0) {
629 dlog(4, "auxv %lld %llx\n",
630 (ULong)*client_auxv,
631 (ULong)*(client_auxv+1));
632 client_auxv++;
633 client_auxv++;
634 client_auxv_len += 2 * sizeof(UWord);
635 }
636 client_auxv_len += 2 * sizeof(UWord);
637 dlog(4, "auxv len %d\n", client_auxv_len);
638
639 if (ofs >= client_auxv_len)
640 n = -1;
641 else {
642 n = client_auxv_len - ofs;
643 VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n);
644 }
645 }
646
647 if (n < 0)
648 write_enn (arg_own_buf);
649 else if (n > len)
650 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1);
651 else
652 *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0);
653
654 free (data);
655
656 return;
657 }
658
659
sewardj3b290482011-05-06 21:02:55 +0000660 /* Protocol features query. */
661 if (strncmp ("qSupported", arg_own_buf, 10) == 0
662 && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) {
663 VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1);
664 /* Note: max packet size including frame and checksum, but without
665 trailing null byte, which is not sent/received. */
666
667 strcat (arg_own_buf, ";QStartNoAckMode+");
668 strcat (arg_own_buf, ";QPassSignals+");
sewardj2a312392011-06-26 09:26:48 +0000669 if (VG_(client_auxv))
670 strcat (arg_own_buf, ";qXfer:auxv:read+");
sewardj3b290482011-05-06 21:02:55 +0000671
philippe349a3912012-05-23 21:50:36 +0000672 if (valgrind_target_xml() != NULL
673 || valgrind_shadow_target_xml() != NULL) {
sewardj3b290482011-05-06 21:02:55 +0000674 strcat (arg_own_buf, ";qXfer:features:read+");
675 /* if a new gdb connects to us, we have to reset the register
676 set to the normal register sets to allow this new gdb to
677 decide to use or not the shadow registers.
678
679 Note that the reset is only done for gdb that are sending
680 qSupported packets. If a user first connected with a recent
681 gdb using shadow registers and then with a very old gdb
682 that does not use qSupported packet, then the old gdb will
683 not properly connect. */
684 initialize_shadow_low(False);
685 }
686 return;
687 }
688
689 /* Otherwise we didn't know what packet it was. Say we didn't
690 understand it. */
691 arg_own_buf[0] = 0;
692}
693
694/* Handle all of the extended 'v' packets. */
695static
sewardj0bb3c672011-07-26 23:29:25 +0000696void handle_v_requests (char *arg_own_buf, char *status, int *zignal)
sewardj3b290482011-05-06 21:02:55 +0000697{
698 /* vcont packet code from gdb 6.6 removed */
699
700 /* Otherwise we didn't know what packet it was. Say we didn't
701 understand it. */
702 arg_own_buf[0] = 0;
703 return;
704}
705
706static
707void myresume (int step, int sig)
708{
709 struct thread_resume resume_info[2];
710 int n = 0;
711
philippe349a3912012-05-23 21:50:36 +0000712 if (step || sig) {
sewardj3b290482011-05-06 21:02:55 +0000713 resume_info[0].step = step;
714 resume_info[0].sig = sig;
sewardj3b290482011-05-06 21:02:55 +0000715 n++;
716 }
sewardj3b290482011-05-06 21:02:55 +0000717 resume_info[n].step = 0;
718 resume_info[n].sig = 0;
sewardj3b290482011-05-06 21:02:55 +0000719
philippe349a3912012-05-23 21:50:36 +0000720 resume_reply_packet_needed = True;
721 valgrind_resume (resume_info);
sewardj3b290482011-05-06 21:02:55 +0000722}
723
724/* server_main global variables */
725static char *own_buf;
726static unsigned char *mem_buf;
727
728void gdbserver_init (void)
729{
730 dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version);
731 noack_mode = False;
philippe349a3912012-05-23 21:50:36 +0000732 valgrind_initialize_target ();
philippe0e1cac92012-02-28 22:37:44 +0000733 // After a fork, gdbserver_init can be called again.
734 // We do not have to re-malloc the buffers in such a case.
735 if (own_buf == NULL)
736 own_buf = malloc (PBUFSIZ);
737 if (mem_buf == NULL)
738 mem_buf = malloc (PBUFSIZ);
sewardj3b290482011-05-06 21:02:55 +0000739}
740
741void gdbserver_terminate (void)
742{
743 /* last call to gdbserver is cleanup call */
744 if (VG_MINIMAL_SETJMP(toplevel)) {
745 dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n");
746 return;
747 }
748 remote_close();
749}
750
751void server_main (void)
752{
753 static char status;
sewardj0bb3c672011-07-26 23:29:25 +0000754 static int zignal;
sewardj3b290482011-05-06 21:02:55 +0000755
756 char ch;
757 int i = 0;
758 unsigned int len;
759 CORE_ADDR mem_addr;
760
philippe349a3912012-05-23 21:50:36 +0000761 zignal = valgrind_wait (&status);
sewardj3b290482011-05-06 21:02:55 +0000762 if (VG_MINIMAL_SETJMP(toplevel)) {
763 dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n");
764 }
765 while (1) {
766 unsigned char sig;
767 int packet_len;
768 int new_packet_len = -1;
769
philippe349a3912012-05-23 21:50:36 +0000770 if (resume_reply_packet_needed) {
771 /* Send the resume reply to reply to last GDB resume
772 request. */
773 resume_reply_packet_needed = False;
sewardj0bb3c672011-07-26 23:29:25 +0000774 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000775 putpkt (own_buf);
776 }
777
778 packet_len = getpkt (own_buf);
779 if (packet_len <= 0)
780 break;
781
782 i = 0;
783 ch = own_buf[i++];
784 switch (ch) {
785 case 'Q':
786 handle_set (own_buf, &new_packet_len);
787 break;
788 case 'q':
789 handle_query (own_buf, &new_packet_len);
790 break;
791 case 'd':
792 /* set/unset debugging is done through valgrind debug level. */
793 own_buf[0] = '\0';
794 break;
795 case 'D':
796 reset_valgrind_sink("gdb detaching from process");
797
798 /* When detaching or kill the process, gdb expects to get
799 an packet OK back. Any other output will make gdb
800 believes detach did not work. */
801 write_ok (own_buf);
802 putpkt (own_buf);
803 remote_finish (reset_after_error);
804 remote_open (VG_(clo_vgdb_prefix));
805 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +0000806 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +0000807 return;
808 case '!':
809 /* We can not use the extended protocol with valgrind,
810 because we can not restart the running
811 program. So return unrecognized. */
812 own_buf[0] = '\0';
813 break;
814 case '?':
sewardj0bb3c672011-07-26 23:29:25 +0000815 prepare_resume_reply (own_buf, status, zignal);
sewardj3b290482011-05-06 21:02:55 +0000816 break;
817 case 'H':
818 if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') {
819 unsigned long gdb_id, thread_id;
820
821 gdb_id = strtoul (&own_buf[2], NULL, 16);
822 thread_id = gdb_id_to_thread_id (gdb_id);
823 if (thread_id == 0) {
824 write_enn (own_buf);
825 break;
826 }
827
828 if (own_buf[1] == 'g') {
829 general_thread = thread_id;
830 set_desired_inferior (1);
831 } else if (own_buf[1] == 'c') {
832 cont_thread = thread_id;
833 } else if (own_buf[1] == 's') {
834 step_thread = thread_id;
835 }
836
837 write_ok (own_buf);
838 } else {
839 /* Silently ignore it so that gdb can extend the protocol
840 without compatibility headaches. */
841 own_buf[0] = '\0';
842 }
843 break;
844 case 'g':
845 set_desired_inferior (1);
846 registers_to_string (own_buf);
847 break;
848 case 'G':
849 set_desired_inferior (1);
850 registers_from_string (&own_buf[1]);
851 write_ok (own_buf);
852 break;
853 case 'P': {
854 int regno;
855 char *regbytes;
856 Bool mod;
857 ThreadState *tst;
858 regno = strtol(&own_buf[1], NULL, 16);
859 regbytes = strchr(&own_buf[0], '=') + 1;
860 set_desired_inferior (1);
861 tst = (ThreadState *) inferior_target_data (current_inferior);
862 /* Only accept changing registers in "runnable state3.
863 In fact, it would be ok to change most of the registers
864 except a few "sensitive" registers such as the PC, SP, BP.
865 We assume we do not need to very specific here, and that we
866 can just refuse all of these. */
867 if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) {
868 supply_register_from_string (regno, regbytes, &mod);
869 write_ok (own_buf);
870 } else {
871 /* at least from gdb 6.6 onwards, an E. error
872 reply is shown to the user. So, we do an error
873 msg which both is accepted by gdb as an error msg
874 and is readable by the user. */
875 VG_(sprintf)
876 (own_buf,
877"E.\n"
878"ERROR changing register %s regno %d\n"
879"gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n"
880"set pc, calling from gdb a function in the debugged process, ...)\n"
881"can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n"
882"Thread status is %s\n",
883 find_register_by_number (regno)->name, regno,
884 VG_(name_of_ThreadStatus)(tst->status));
885 if (VG_(clo_verbosity) > 1)
886 VG_(umsg) ("%s\n", own_buf);
887 }
888 break;
889 }
890 case 'm':
891 decode_m_packet (&own_buf[1], &mem_addr, &len);
philippe349a3912012-05-23 21:50:36 +0000892 if (valgrind_read_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +0000893 convert_int_to_ascii (mem_buf, own_buf, len);
894 else
895 write_enn (own_buf);
896 break;
897 case 'M':
898 decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf);
philippe349a3912012-05-23 21:50:36 +0000899 if (valgrind_write_memory (mem_addr, mem_buf, len) == 0)
sewardj3b290482011-05-06 21:02:55 +0000900 write_ok (own_buf);
901 else
902 write_enn (own_buf);
903 break;
904 case 'X':
905 if (decode_X_packet (&own_buf[1], packet_len - 1,
906 &mem_addr, &len, mem_buf) < 0
philippe349a3912012-05-23 21:50:36 +0000907 || valgrind_write_memory (mem_addr, mem_buf, len) != 0)
sewardj3b290482011-05-06 21:02:55 +0000908 write_enn (own_buf);
909 else
910 write_ok (own_buf);
911 break;
912 case 'C':
913 convert_ascii_to_int (own_buf + 1, &sig, 1);
914 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +0000915 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +0000916 else
sewardj0bb3c672011-07-26 23:29:25 +0000917 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +0000918 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +0000919 myresume (0, zignal);
sewardj3b290482011-05-06 21:02:55 +0000920 return; // return control to valgrind
921 case 'S':
922 convert_ascii_to_int (own_buf + 1, &sig, 1);
923 if (target_signal_to_host_p (sig))
sewardj0bb3c672011-07-26 23:29:25 +0000924 zignal = target_signal_to_host (sig);
sewardj3b290482011-05-06 21:02:55 +0000925 else
sewardj0bb3c672011-07-26 23:29:25 +0000926 zignal = 0;
sewardj3b290482011-05-06 21:02:55 +0000927 set_desired_inferior (0);
sewardj0bb3c672011-07-26 23:29:25 +0000928 myresume (1, zignal);
sewardj3b290482011-05-06 21:02:55 +0000929 return; // return control to valgrind
930 case 'c':
931 set_desired_inferior (0);
932 myresume (0, 0);
933 return; // return control to valgrind
934 case 's':
935 set_desired_inferior (0);
936 myresume (1, 0);
937 return; // return control to valgrind
938 case 'Z': {
939 char *lenptr;
940 char *dataptr;
941 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
942 int zlen = strtol (lenptr + 1, &dataptr, 16);
943 char type = own_buf[1];
944
philippe349a3912012-05-23 21:50:36 +0000945 if (type < '0' || type > '4') {
946 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +0000947 own_buf[0] = '\0';
948 } else {
949 int res;
950
philippe349a3912012-05-23 21:50:36 +0000951 res = valgrind_insert_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +0000952 if (res == 0)
953 write_ok (own_buf);
954 else if (res == 1)
955 /* Unsupported. */
956 own_buf[0] = '\0';
957 else
958 write_enn (own_buf);
959 }
960 break;
961 }
962 case 'z': {
963 char *lenptr;
964 char *dataptr;
965 CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16);
966 int zlen = strtol (lenptr + 1, &dataptr, 16);
967 char type = own_buf[1];
968
philippe349a3912012-05-23 21:50:36 +0000969 if (type < '0' || type > '4') {
970 /* Watchpoint command type unrecognized. */
sewardj3b290482011-05-06 21:02:55 +0000971 own_buf[0] = '\0';
972 } else {
973 int res;
974
philippe349a3912012-05-23 21:50:36 +0000975 res = valgrind_remove_watchpoint (type, addr, zlen);
sewardj3b290482011-05-06 21:02:55 +0000976 if (res == 0)
977 write_ok (own_buf);
978 else if (res == 1)
979 /* Unsupported. */
980 own_buf[0] = '\0';
981 else
982 write_enn (own_buf);
983 }
984 break;
985 }
986 case 'k':
987 kill_request("Gdb request to kill this process\n");
988 break;
989 case 'T': {
990 unsigned long gdb_id, thread_id;
991
992 gdb_id = strtoul (&own_buf[1], NULL, 16);
993 thread_id = gdb_id_to_thread_id (gdb_id);
994 if (thread_id == 0) {
995 write_enn (own_buf);
996 break;
997 }
998
philippe349a3912012-05-23 21:50:36 +0000999 if (valgrind_thread_alive (thread_id))
sewardj3b290482011-05-06 21:02:55 +00001000 write_ok (own_buf);
1001 else
1002 write_enn (own_buf);
1003 break;
1004 }
1005 case 'R':
1006 /* Restarting the inferior is only supported in the
1007 extended protocol.
1008 => It is a request we don't understand. Respond with an
1009 empty packet so that gdb knows that we don't support this
1010 request. */
1011 own_buf[0] = '\0';
1012 break;
1013 case 'v':
1014 /* Extended (long) request. */
sewardj0bb3c672011-07-26 23:29:25 +00001015 handle_v_requests (own_buf, &status, &zignal);
sewardj3b290482011-05-06 21:02:55 +00001016 break;
1017 default:
1018 /* It is a request we don't understand. Respond with an
1019 empty packet so that gdb knows that we don't support this
1020 request. */
1021 own_buf[0] = '\0';
1022 break;
1023 }
1024
1025 if (new_packet_len != -1)
1026 putpkt_binary (own_buf, new_packet_len);
1027 else
1028 putpkt (own_buf);
1029
1030 if (status == 'W')
sewardj0bb3c672011-07-26 23:29:25 +00001031 VG_(umsg) ("\nChild exited with status %d\n", zignal);
sewardj3b290482011-05-06 21:02:55 +00001032 if (status == 'X')
sewardj47ffedb2011-10-24 07:36:57 +00001033 VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n",
sewardj0bb3c672011-07-26 23:29:25 +00001034 target_signal_to_host (zignal),
1035 target_signal_to_name (zignal));
sewardj3b290482011-05-06 21:02:55 +00001036 if (status == 'W' || status == 'X') {
1037 VG_(umsg) ("Process exiting\n");
1038 VG_(exit) (0);
1039 }
1040 }
1041
1042 /* We come here when getpkt fails => close the connection,
1043 and re-open. Then return control to valgrind.
1044 We return the control to valgrind as we assume that
1045 the connection was closed due to vgdb having finished
1046 to execute a command. */
1047 if (VG_(clo_verbosity) > 1)
1048 VG_(umsg) ("Remote side has terminated connection. "
1049 "GDBserver will reopen the connection.\n");
1050 remote_finish (reset_after_error);
1051 remote_open (VG_(clo_vgdb_prefix));
1052 myresume (0, 0);
philippe349a3912012-05-23 21:50:36 +00001053 resume_reply_packet_needed = False;
sewardj3b290482011-05-06 21:02:55 +00001054 return;
1055}