sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1 | /* 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" |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 29 | #include "pub_core_initimg.h" |
philippe | f3a6e93 | 2013-01-10 20:42:51 +0000 | [diff] [blame] | 30 | #include "pub_core_execontext.h" |
florian | dbb3584 | 2012-10-27 18:39:11 +0000 | [diff] [blame] | 31 | #include "pub_core_syswrap.h" // VG_(show_open_fds) |
philippe | 66e4c73 | 2013-01-26 16:45:01 +0000 | [diff] [blame] | 32 | #include "pub_core_scheduler.h" |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 33 | #include "pub_core_transtab.h" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 34 | |
| 35 | unsigned long cont_thread; |
| 36 | unsigned long general_thread; |
| 37 | unsigned long step_thread; |
| 38 | unsigned long thread_from_wait; |
| 39 | unsigned long old_thread_from_wait; |
| 40 | |
philippe | 886fde3 | 2012-03-29 21:56:47 +0000 | [diff] [blame] | 41 | int pass_signals[TARGET_SIGNAL_LAST]; /* indexed by gdb signal nr */ |
bart | 2dafc54 | 2011-05-18 16:08:28 +0000 | [diff] [blame] | 42 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 43 | /* for a gdbserver integrated in valgrind, resuming the process consists |
| 44 | in returning the control to valgrind. |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 45 | The guess process resumes its execution. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 46 | Then at the next error or break or ..., valgrind calls gdbserver again. |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 47 | A resume reply packet must then be built to inform GDB that the |
| 48 | resume request is finished. |
| 49 | resume_reply_packet_needed records the fact that the next call to gdbserver |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 50 | must send a resume packet to gdb. */ |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 51 | static Bool resume_reply_packet_needed = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 52 | |
| 53 | VG_MINIMAL_JMP_BUF(toplevel); |
| 54 | |
| 55 | /* Decode a qXfer read request. Return 0 if everything looks OK, |
| 56 | or -1 otherwise. */ |
| 57 | |
| 58 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 59 | int decode_xfer_read (char *buf, const char **annex, CORE_ADDR *ofs, unsigned int *len) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 60 | { |
| 61 | /* Extract and NUL-terminate the annex. */ |
| 62 | *annex = buf; |
| 63 | while (*buf && *buf != ':') |
| 64 | buf++; |
| 65 | if (*buf == '\0') |
| 66 | return -1; |
| 67 | *buf++ = 0; |
| 68 | |
| 69 | /* After the read/write marker and annex, qXfer looks like a |
| 70 | traditional 'm' packet. */ |
| 71 | decode_m_packet (buf, ofs, len); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* Write the response to a successful qXfer read. Returns the |
| 77 | length of the (binary) data stored in BUF, corresponding |
| 78 | to as much of DATA/LEN as we could fit. IS_MORE controls |
| 79 | the first character of the response. */ |
| 80 | static |
| 81 | int write_qxfer_response (char *buf, unsigned char *data, int len, int is_more) |
| 82 | { |
| 83 | int out_len; |
| 84 | |
| 85 | if (is_more) |
| 86 | buf[0] = 'm'; |
| 87 | else |
| 88 | buf[0] = 'l'; |
| 89 | |
| 90 | return remote_escape_output (data, len, (unsigned char *) buf + 1, &out_len, |
| 91 | PBUFSIZ - POVERHSIZ - 1) + 1; |
| 92 | } |
| 93 | |
| 94 | static Bool initial_valgrind_sink_saved = False; |
| 95 | /* True <=> valgrind log sink saved in initial_valgrind_sink */ |
| 96 | static OutputSink initial_valgrind_sink; |
| 97 | |
| 98 | static Bool command_output_to_log = False; |
| 99 | /* True <=> command output goes to log instead of gdb */ |
| 100 | |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 101 | void reset_valgrind_sink(const char *info) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 102 | { |
| 103 | if (VG_(log_output_sink).fd != initial_valgrind_sink.fd |
| 104 | && initial_valgrind_sink_saved) { |
| 105 | VG_(log_output_sink).fd = initial_valgrind_sink.fd; |
| 106 | VG_(umsg) ("Reset valgrind output to log (%s)\n", |
| 107 | (info = NULL ? "" : info)); |
| 108 | } |
| 109 | } |
| 110 | |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 111 | void print_to_initial_valgrind_sink (const char *msg) |
| 112 | { |
| 113 | vg_assert (initial_valgrind_sink_saved); |
| 114 | VG_(write) (initial_valgrind_sink.fd, msg, strlen(msg)); |
| 115 | } |
| 116 | |
| 117 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 118 | static |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 119 | void kill_request (const char *msg) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 120 | { |
| 121 | VG_(umsg) ("%s", msg); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 122 | VG_(exit) (0); |
| 123 | } |
| 124 | |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 125 | // s is a NULL terminated string made of O or more words (separated by spaces). |
| 126 | // Returns a pointer to the Nth word in s. |
| 127 | // If Nth word does not exist, return a pointer to the last (0) byte of s. |
| 128 | static |
| 129 | const char *wordn (const char *s, int n) |
| 130 | { |
| 131 | int word_seen = 0; |
| 132 | Bool searching_word = True; |
| 133 | |
| 134 | while (*s) { |
| 135 | if (*s == ' ') |
| 136 | searching_word = True; |
| 137 | else { |
| 138 | if (searching_word) { |
| 139 | searching_word = False; |
| 140 | word_seen++; |
| 141 | if (word_seen == n) |
| 142 | return s; |
| 143 | } |
| 144 | } |
| 145 | s++; |
| 146 | } |
| 147 | return s; |
| 148 | } |
| 149 | |
philippe | 4f6f336 | 2014-04-19 00:25:54 +0000 | [diff] [blame] | 150 | void VG_(print_all_stats) (Bool memory_stats, Bool tool_stats) |
| 151 | { |
| 152 | if (memory_stats) { |
| 153 | VG_(message)(Vg_DebugMsg, "\n"); |
| 154 | VG_(message)(Vg_DebugMsg, |
| 155 | "------ Valgrind's internal memory use stats follow ------\n" ); |
| 156 | VG_(sanity_check_malloc_all)(); |
| 157 | VG_(message)(Vg_DebugMsg, "------\n" ); |
| 158 | VG_(print_all_arena_stats)(); |
| 159 | if (VG_(clo_profile_heap)) |
| 160 | VG_(print_arena_cc_analysis) (); |
| 161 | VG_(message)(Vg_DebugMsg, "\n"); |
| 162 | } |
| 163 | |
| 164 | VG_(print_translation_stats)(); |
| 165 | VG_(print_tt_tc_stats)(); |
| 166 | VG_(print_scheduler_stats)(); |
| 167 | VG_(print_ExeContext_stats)( False /* with_stacktraces */ ); |
| 168 | VG_(print_errormgr_stats)(); |
| 169 | if (tool_stats && VG_(needs).print_stats) { |
| 170 | VG_TDICT_CALL(tool_print_stats); |
| 171 | } |
| 172 | } |
| 173 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 174 | /* handle_gdb_valgrind_command handles the provided mon string command. |
| 175 | If command is recognised, return 1 else return 0. |
| 176 | Note that in case of ambiguous command, 1 is returned. |
| 177 | |
| 178 | *sink_wanted_at_return is modified if one of the commands |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 179 | 'v.set *_output' is handled. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 180 | */ |
| 181 | static |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 182 | int handle_gdb_valgrind_command (char *mon, OutputSink *sink_wanted_at_return) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 183 | { |
| 184 | UWord ret = 0; |
| 185 | char s[strlen(mon)+1]; /* copy for strtok_r */ |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 186 | char *wcmd; |
| 187 | HChar *ssaveptr; |
| 188 | const char *endptr; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 189 | int kwdid; |
| 190 | int int_value; |
| 191 | |
| 192 | vg_assert (initial_valgrind_sink_saved); |
| 193 | |
| 194 | strcpy (s, mon); |
| 195 | wcmd = strtok_r (s, " ", &ssaveptr); |
| 196 | /* NB: if possible, avoid introducing a new command below which |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 197 | starts with the same 3 first letters as an already existing |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 198 | command. This ensures a shorter abbreviation for the user. */ |
philippe | 6ec8d63 | 2013-01-23 22:10:28 +0000 | [diff] [blame] | 199 | switch (VG_(keyword_id) ("help v.set v.info v.wait v.kill v.translate" |
| 200 | " v.do", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 201 | wcmd, kwd_report_duplicated_matches)) { |
| 202 | case -2: |
| 203 | ret = 1; |
| 204 | break; |
| 205 | case -1: |
| 206 | break; |
| 207 | case 0: /* help */ |
| 208 | ret = 1; |
| 209 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 210 | if (wcmd == NULL) { |
| 211 | int_value = 0; |
| 212 | } else { |
| 213 | switch (VG_(keyword_id) ("debug", wcmd, kwd_report_all)) { |
| 214 | case -2: int_value = 0; break; |
| 215 | case -1: int_value = 0; break; |
| 216 | case 0: int_value = 1; break; |
| 217 | default: tl_assert (0); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | VG_(gdb_printf) ( |
| 222 | "general valgrind monitor commands:\n" |
philippe | c336038 | 2012-10-21 14:37:14 +0000 | [diff] [blame] | 223 | " help [debug] : monitor command help. With debug: + debugging commands\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 224 | " v.wait [<ms>] : sleep <ms> (default 0) then continue\n" |
| 225 | " v.info all_errors : show all errors found so far\n" |
| 226 | " v.info last_error : show last error found\n" |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 227 | " v.info n_errs_found [msg] : show the nr of errors found so far and the given msg\n" |
philippe | c336038 | 2012-10-21 14:37:14 +0000 | [diff] [blame] | 228 | " v.info open_fds : show open file descriptors (only if --track-fds=yes)\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 229 | " v.kill : kill the Valgrind process\n" |
| 230 | " v.set gdb_output : set valgrind output to gdb\n" |
| 231 | " v.set log_output : set valgrind output to log\n" |
| 232 | " v.set mixed_output : set valgrind output to log, interactive output to gdb\n" |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 233 | " v.set merge-recursive-frames <num> : merge recursive calls in max <num> frames\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 234 | " v.set vgdb-error <errornr> : debug me at error >= <errornr> \n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 235 | if (int_value) { VG_(gdb_printf) ( |
| 236 | "debugging valgrind internals monitor commands:\n" |
philippe | 6ec8d63 | 2013-01-23 22:10:28 +0000 | [diff] [blame] | 237 | " v.do expensive_sanity_check_general : do an expensive sanity check now\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 238 | " v.info gdbserver_status : show gdbserver status\n" |
philippe | 93a6a8d | 2012-04-27 22:59:43 +0000 | [diff] [blame] | 239 | " v.info memory [aspacemgr] : show valgrind heap memory stats\n" |
| 240 | " (with aspacemgr arg, also shows valgrind segments on log ouput)\n" |
philippe | f3a6e93 | 2013-01-10 20:42:51 +0000 | [diff] [blame] | 241 | " v.info exectxt : show stacktraces and stats of all execontexts\n" |
sewardj | d6e13d8 | 2011-10-22 20:23:30 +0000 | [diff] [blame] | 242 | " v.info scheduler : show valgrind thread state and stacktrace\n" |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 243 | " v.info stats : show various valgrind and tool stats\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 244 | " v.set debuglog <level> : set valgrind debug log level to <level>\n" |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame^] | 245 | " v.set hostvisibility [yes*|no] : (en/dis)ables access by gdb/gdbserver to\n" |
| 246 | " Valgrind internal host status/memory\n" |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 247 | " v.translate <addr> [<traceflags>] : debug translation of <addr> with <traceflags>\n" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 248 | " (default traceflags 0b00100000 : show after instrumentation)\n" |
| 249 | " An additional flag 0b100000000 allows to show gdbserver instrumentation\n"); |
| 250 | } |
| 251 | break; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 252 | case 1: /* v.set */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 253 | ret = 1; |
| 254 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 255 | switch (kwdid = VG_(keyword_id) |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 256 | ("vgdb-error debuglog merge-recursive-frames" |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame^] | 257 | " gdb_output log_output mixed_output hostvisibility ", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 258 | wcmd, kwd_report_all)) { |
| 259 | case -2: |
| 260 | case -1: |
| 261 | break; |
| 262 | case 0: /* vgdb-error */ |
| 263 | case 1: /* debuglog */ |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 264 | case 2: /* merge-recursive-frames */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 265 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 266 | if (wcmd == NULL) { |
| 267 | int_value = 0; |
| 268 | endptr = "empty"; /* to report an error below */ |
| 269 | } else { |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 270 | HChar *the_end; |
| 271 | int_value = strtol (wcmd, &the_end, 10); |
| 272 | endptr = the_end; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 273 | } |
| 274 | if (*endptr != '\0') { |
| 275 | VG_(gdb_printf) ("missing or malformed integer value\n"); |
| 276 | } else if (kwdid == 0) { |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 277 | VG_(printf) ("vgdb-error value changed from %d to %d\n", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 278 | VG_(dyn_vgdb_error), int_value); |
| 279 | VG_(dyn_vgdb_error) = int_value; |
| 280 | } else if (kwdid == 1) { |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 281 | VG_(printf) ("debuglog value changed from %d to %d\n", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 282 | VG_(debugLog_getLevel)(), int_value); |
| 283 | VG_(debugLog_startup) (int_value, "gdbsrv"); |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 284 | } else if (kwdid == 2) { |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 285 | VG_(printf) |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 286 | ("merge-recursive-frames value changed from %d to %d\n", |
| 287 | VG_(clo_merge_recursive_frames), int_value); |
| 288 | VG_(clo_merge_recursive_frames) = int_value; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 289 | } else { |
| 290 | vg_assert (0); |
| 291 | } |
| 292 | break; |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 293 | case 3: /* gdb_output */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 294 | (*sink_wanted_at_return).fd = -2; |
| 295 | command_output_to_log = False; |
| 296 | VG_(gdb_printf) ("valgrind output will go to gdb\n"); |
| 297 | break; |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 298 | case 4: /* log_output */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 299 | (*sink_wanted_at_return).fd = initial_valgrind_sink.fd; |
| 300 | command_output_to_log = True; |
| 301 | VG_(gdb_printf) ("valgrind output will go to log\n"); |
| 302 | break; |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 303 | case 5: /* mixed output */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 304 | (*sink_wanted_at_return).fd = initial_valgrind_sink.fd; |
| 305 | command_output_to_log = False; |
| 306 | VG_(gdb_printf) |
| 307 | ("valgrind output will go to log, interactive output will go to gdb\n"); |
| 308 | break; |
philippe | 180a750 | 2014-04-20 13:41:10 +0000 | [diff] [blame^] | 309 | case 6: /* hostvisibility */ |
| 310 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 311 | if (wcmd != NULL) { |
| 312 | switch (VG_(keyword_id) ("yes no", wcmd, kwd_report_all)) { |
| 313 | case -2: |
| 314 | case -1: break; |
| 315 | case 0: |
| 316 | hostvisibility = True; |
| 317 | break; |
| 318 | case 1: |
| 319 | hostvisibility = False; |
| 320 | break; |
| 321 | default: tl_assert (0); |
| 322 | } |
| 323 | } else { |
| 324 | hostvisibility = True; |
| 325 | } |
| 326 | if (hostvisibility) |
| 327 | VG_(gdb_printf) |
| 328 | ("Enabled access to Valgrind memory/status by GDB\n" |
| 329 | "If not yet done, tell GDB which valgrind file(s) to use:\n" |
| 330 | "add-symbol-file <tool or preloaded file> <loadaddr>\n"); |
| 331 | else |
| 332 | VG_(gdb_printf) |
| 333 | ("Disabled access to Valgrind memory/status by GDB\n"); |
| 334 | break; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 335 | default: |
| 336 | vg_assert (0); |
| 337 | } |
| 338 | break; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 339 | case 2: /* v.info */ { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 340 | ret = 1; |
| 341 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 342 | switch (kwdid = VG_(keyword_id) |
sewardj | d6e13d8 | 2011-10-22 20:23:30 +0000 | [diff] [blame] | 343 | ("all_errors n_errs_found last_error gdbserver_status memory" |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 344 | " scheduler stats open_fds exectxt", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 345 | wcmd, kwd_report_all)) { |
| 346 | case -2: |
| 347 | case -1: |
| 348 | break; |
| 349 | case 0: // all_errors |
| 350 | // A verbosity of minimum 2 is needed to show the errors. |
| 351 | VG_(show_all_errors)(/* verbosity */ 2, /* xml */ False); |
| 352 | break; |
| 353 | case 1: // n_errs_found |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 354 | VG_(printf) ("n_errs_found %d n_errs_shown %d (vgdb-error %d) %s\n", |
| 355 | VG_(get_n_errs_found) (), |
| 356 | VG_(get_n_errs_shown) (), |
| 357 | VG_(dyn_vgdb_error), |
| 358 | wordn (mon, 3)); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 359 | break; |
| 360 | case 2: // last_error |
| 361 | VG_(show_last_error)(); |
| 362 | break; |
| 363 | case 3: // gdbserver_status |
| 364 | VG_(gdbserver_status_output)(); |
| 365 | break; |
| 366 | case 4: /* memory */ |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 367 | VG_(printf) ("%llu bytes have already been allocated.\n", |
| 368 | VG_(am_get_anonsize_total)()); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 369 | VG_(print_all_arena_stats) (); |
| 370 | if (VG_(clo_profile_heap)) |
| 371 | VG_(print_arena_cc_analysis) (); |
philippe | 93a6a8d | 2012-04-27 22:59:43 +0000 | [diff] [blame] | 372 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 373 | if (wcmd != NULL) { |
| 374 | switch (VG_(keyword_id) ("aspacemgr", wcmd, kwd_report_all)) { |
| 375 | case -2: |
| 376 | case -1: break; |
| 377 | case 0: |
| 378 | VG_(am_show_nsegments) (0, "gdbserver v.info memory aspacemgr"); |
| 379 | break; |
| 380 | default: tl_assert (0); |
| 381 | } |
| 382 | } |
| 383 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 384 | ret = 1; |
| 385 | break; |
sewardj | d6e13d8 | 2011-10-22 20:23:30 +0000 | [diff] [blame] | 386 | case 5: /* scheduler */ |
philippe | 4f6f336 | 2014-04-19 00:25:54 +0000 | [diff] [blame] | 387 | VG_(show_sched_status) (True, // host_stacktrace |
| 388 | True, // valgrind_stack_usage |
| 389 | True); // exited_threads |
sewardj | d6e13d8 | 2011-10-22 20:23:30 +0000 | [diff] [blame] | 390 | ret = 1; |
| 391 | break; |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 392 | case 6: /* stats */ |
philippe | 4f6f336 | 2014-04-19 00:25:54 +0000 | [diff] [blame] | 393 | VG_(print_all_stats)(False, /* Memory stats */ |
| 394 | True /* Tool stats */); |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 395 | ret = 1; |
| 396 | break; |
| 397 | case 7: /* open_fds */ |
philippe | c336038 | 2012-10-21 14:37:14 +0000 | [diff] [blame] | 398 | if (VG_(clo_track_fds)) |
| 399 | VG_(show_open_fds) (""); |
| 400 | else |
| 401 | VG_(gdb_printf) |
| 402 | ("Valgrind must be started with --track-fds=yes" |
| 403 | " to show open fds\n"); |
| 404 | ret = 1; |
| 405 | break; |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 406 | case 8: /* exectxt */ |
philippe | f3a6e93 | 2013-01-10 20:42:51 +0000 | [diff] [blame] | 407 | VG_(print_ExeContext_stats) (True /* with_stacktraces */); |
| 408 | ret = 1; |
| 409 | break; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 410 | default: |
| 411 | vg_assert(0); |
| 412 | } |
| 413 | break; |
| 414 | } |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 415 | case 3: /* v.wait */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 416 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 417 | if (wcmd != NULL) { |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 418 | int_value = strtol (wcmd, NULL, 10); |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 419 | VG_(printf) ("gdbserver: continuing in %d ms ...\n", int_value); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 420 | VG_(poll)(NULL, 0, int_value); |
| 421 | } |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 422 | VG_(printf) ("gdbserver: continuing after wait ...\n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 423 | ret = 1; |
| 424 | break; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 425 | case 4: /* v.kill */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 426 | kill_request ("monitor command request to kill this process\n"); |
| 427 | break; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 428 | case 5: { /* v.translate */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 429 | Addr address; |
| 430 | SizeT verbosity = 0x20; |
| 431 | |
| 432 | ret = 1; |
| 433 | |
| 434 | VG_(strtok_get_address_and_size) (&address, &verbosity, &ssaveptr); |
| 435 | if (address != (Addr) 0 || verbosity != 0) { |
| 436 | /* we need to force the output to log for the translation trace, |
| 437 | as low level VEX tracing cannot be redirected to gdb. */ |
| 438 | int saved_command_output_to_log = command_output_to_log; |
| 439 | int saved_fd = VG_(log_output_sink).fd; |
| 440 | Bool single_stepping_on_entry = valgrind_single_stepping(); |
| 441 | int vex_verbosity = verbosity & 0xff; |
| 442 | VG_(log_output_sink).fd = initial_valgrind_sink.fd; |
| 443 | if ((verbosity & 0x100) && !single_stepping_on_entry) { |
| 444 | valgrind_set_single_stepping(True); |
| 445 | // to force gdbserver instrumentation. |
| 446 | } |
sewardj | 99d6134 | 2011-05-17 16:35:11 +0000 | [diff] [blame] | 447 | # if defined(VGA_arm) |
| 448 | // on arm, we need to (potentially) convert this address |
| 449 | // to the thumb form. |
| 450 | address = thumb_pc (address); |
| 451 | # endif |
| 452 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 453 | VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/, |
| 454 | address, |
| 455 | /*debugging*/True, |
| 456 | (Int) vex_verbosity, |
| 457 | /*bbs_done*/0, |
| 458 | /*allow redir?*/True); |
| 459 | if ((verbosity & 0x100) && !single_stepping_on_entry) { |
| 460 | valgrind_set_single_stepping(False); |
| 461 | // reset single stepping. |
| 462 | } |
| 463 | command_output_to_log = saved_command_output_to_log; |
| 464 | VG_(log_output_sink).fd = saved_fd; |
| 465 | } |
| 466 | break; |
| 467 | } |
| 468 | |
philippe | 6ec8d63 | 2013-01-23 22:10:28 +0000 | [diff] [blame] | 469 | case 6: /* v.do */ |
| 470 | ret = 1; |
| 471 | wcmd = strtok_r (NULL, " ", &ssaveptr); |
| 472 | switch (VG_(keyword_id) ("expensive_sanity_check_general", |
| 473 | wcmd, kwd_report_all)) { |
| 474 | case -2: |
| 475 | case -1: break; |
| 476 | case 0: { /* expensive_sanity_check_general */ |
| 477 | // Temporarily bump up sanity level to check e.g. the malloc arenas. |
| 478 | const Int save_clo_sanity_level = VG_(clo_sanity_level); |
| 479 | if (VG_(clo_sanity_level) < 4) VG_(clo_sanity_level) = 4; |
| 480 | VG_(sanity_check_general) (/* force_expensive */ True); |
| 481 | VG_(clo_sanity_level) = save_clo_sanity_level; |
| 482 | break; |
| 483 | } |
| 484 | default: tl_assert (0); |
| 485 | } |
| 486 | break; |
| 487 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 488 | default: |
| 489 | vg_assert (0); |
| 490 | } |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | /* handle_gdb_monitor_command handles the provided mon string command, |
| 495 | which can be either a "standard" valgrind monitor command |
| 496 | or a tool specific monitor command. |
| 497 | If command recognised, return 1 else return 0. |
| 498 | Note that in case of ambiguous command, 1 is returned. |
| 499 | */ |
| 500 | static |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 501 | int handle_gdb_monitor_command (char *mon) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 502 | { |
| 503 | UWord ret = 0; |
| 504 | UWord tool_ret = 0; |
| 505 | // initially, we assume that when returning, the desired sink is the |
| 506 | // one we have when entering. It can however be changed by the standard |
| 507 | // valgrind command handling. |
| 508 | OutputSink sink_wanted_at_return = VG_(log_output_sink); |
| 509 | |
| 510 | if (!initial_valgrind_sink_saved) { |
| 511 | /* first time we enter here, we save the valgrind default log sink */ |
| 512 | initial_valgrind_sink = sink_wanted_at_return; |
| 513 | initial_valgrind_sink_saved = True; |
| 514 | } |
| 515 | |
| 516 | if (!command_output_to_log) |
| 517 | VG_(log_output_sink).fd = -2; /* redirect to monitor_output */ |
| 518 | |
| 519 | ret = handle_gdb_valgrind_command (mon, &sink_wanted_at_return); |
| 520 | |
| 521 | /* Even if command was recognised by valgrind core, we call the |
| 522 | tool command handler : this is needed to handle help command |
| 523 | and/or to let the tool do some additional processing of a |
| 524 | valgrind standard command. Note however that if valgrind |
| 525 | recognised the command, we will always return success. */ |
| 526 | if (VG_(needs).client_requests) { |
| 527 | /* If the tool reports an error when handling a monitor command, |
| 528 | we need to avoid calling gdbserver during this command |
| 529 | handling. So, we temporarily set VG_(dyn_vgdb_error) to |
| 530 | a huge value to ensure m_errormgr.c does not call gdbserver. */ |
| 531 | Int save_dyn_vgdb_error = VG_(dyn_vgdb_error); |
| 532 | UWord arg[2]; |
| 533 | VG_(dyn_vgdb_error) = 999999999; |
| 534 | arg[0] = (UWord) VG_USERREQ__GDB_MONITOR_COMMAND; |
| 535 | arg[1] = (UWord) mon; |
| 536 | VG_TDICT_CALL(tool_handle_client_request, VG_(running_tid), arg, |
| 537 | &tool_ret); |
| 538 | VG_(dyn_vgdb_error) = save_dyn_vgdb_error; |
| 539 | } |
| 540 | |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 541 | VG_(message_flush) (); |
| 542 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 543 | /* restore or set the desired output */ |
| 544 | VG_(log_output_sink).fd = sink_wanted_at_return.fd; |
| 545 | if (ret | tool_ret) |
| 546 | return 1; |
| 547 | else |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | /* Handle all of the extended 'Q' packets. */ |
| 553 | static |
| 554 | void handle_set (char *arg_own_buf, int *new_packet_len_p) |
| 555 | { |
| 556 | if (strcmp ("QStartNoAckMode", arg_own_buf) == 0) { |
| 557 | noack_mode = True; |
| 558 | write_ok (arg_own_buf); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | if (strncmp ("QPassSignals:", arg_own_buf, 13) == 0) { |
| 563 | int i; |
| 564 | char *from, *to; |
| 565 | char *end = arg_own_buf + strlen(arg_own_buf); |
| 566 | CORE_ADDR sig; |
| 567 | for (i = 0; i < TARGET_SIGNAL_LAST; i++) |
| 568 | pass_signals[i] = 0; |
| 569 | |
| 570 | from = arg_own_buf + 13; |
| 571 | while (from < end) { |
| 572 | to = strchr(from, ';'); |
| 573 | if (to == NULL) to = end; |
| 574 | decode_address (&sig, from, to - from); |
| 575 | pass_signals[(int)sig] = 1; |
philippe | 886fde3 | 2012-03-29 21:56:47 +0000 | [diff] [blame] | 576 | dlog(1, "pass_signal gdb_nr %d %s\n", |
| 577 | (int)sig, target_signal_to_name(sig)); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 578 | from = to; |
| 579 | if (*from == ';') from++; |
| 580 | } |
| 581 | write_ok (arg_own_buf); |
| 582 | return; |
| 583 | } |
| 584 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 585 | understand it. */ |
| 586 | arg_own_buf[0] = 0; |
| 587 | } |
| 588 | |
philippe | 02ea413 | 2013-09-04 21:42:43 +0000 | [diff] [blame] | 589 | Bool VG_(client_monitor_command) (HChar *cmd) |
philippe | 4620765 | 2013-01-20 17:11:58 +0000 | [diff] [blame] | 590 | { |
| 591 | const Bool connected = remote_connected(); |
| 592 | const int saved_command_output_to_log = command_output_to_log; |
| 593 | Bool handled; |
| 594 | |
| 595 | if (!connected) |
| 596 | command_output_to_log = True; |
| 597 | handled = handle_gdb_monitor_command (cmd); |
| 598 | if (!connected) { |
| 599 | // reset the log output unless cmd changed it. |
| 600 | if (command_output_to_log) |
| 601 | command_output_to_log = saved_command_output_to_log; |
| 602 | } |
| 603 | if (handled) |
| 604 | return False; // recognised |
| 605 | else |
| 606 | return True; // not recognised |
| 607 | } |
| 608 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 609 | /* Handle all of the extended 'q' packets. */ |
| 610 | static |
| 611 | void handle_query (char *arg_own_buf, int *new_packet_len_p) |
| 612 | { |
| 613 | static struct inferior_list_entry *thread_ptr; |
| 614 | |
| 615 | /* qRcmd, monitor command handling. */ |
| 616 | if (strncmp ("qRcmd,", arg_own_buf, 6) == 0) { |
| 617 | char *p = arg_own_buf + 6; |
| 618 | int cmdlen = strlen(p)/2; |
| 619 | char cmd[cmdlen+1]; |
| 620 | |
| 621 | if (unhexify (cmd, p, cmdlen) != cmdlen) { |
| 622 | write_enn (arg_own_buf); |
| 623 | return; |
| 624 | } |
| 625 | cmd[cmdlen] = '\0'; |
| 626 | |
| 627 | if (handle_gdb_monitor_command (cmd)) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 628 | write_ok (arg_own_buf); |
| 629 | return; |
| 630 | } else { |
| 631 | /* cmd not recognised */ |
| 632 | VG_(gdb_printf) |
| 633 | ("command '%s' not recognised\n" |
| 634 | "In gdb, try 'monitor help'\n" |
| 635 | "In a shell, try 'vgdb help'\n", |
| 636 | cmd); |
| 637 | write_ok (arg_own_buf); |
| 638 | return; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* provide some valgrind specific info in return to qThreadExtraInfo. */ |
| 643 | if (strncmp ("qThreadExtraInfo,", arg_own_buf, 17) == 0) { |
| 644 | unsigned long gdb_id; |
| 645 | struct thread_info *ti; |
| 646 | ThreadState *tst; |
| 647 | char status[100]; |
| 648 | |
| 649 | gdb_id = strtoul (&arg_own_buf[17], NULL, 16); |
| 650 | ti = gdb_id_to_thread (gdb_id); |
| 651 | if (ti != NULL) { |
| 652 | tst = (ThreadState *) inferior_target_data (ti); |
florian | 4978951 | 2013-09-16 17:08:50 +0000 | [diff] [blame] | 653 | /* Additional info is the tid, the thread status and the thread's |
| 654 | name, if any. */ |
| 655 | if (tst->thread_name) { |
| 656 | VG_(snprintf) (status, sizeof(status), "tid %d %s %s", |
| 657 | tst->tid, |
| 658 | VG_(name_of_ThreadStatus)(tst->status), |
| 659 | tst->thread_name); |
| 660 | } else { |
| 661 | VG_(snprintf) (status, sizeof(status), "tid %d %s", |
| 662 | tst->tid, |
| 663 | VG_(name_of_ThreadStatus)(tst->status)); |
| 664 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 665 | hexify (arg_own_buf, status, strlen(status)); |
| 666 | return; |
| 667 | } else { |
| 668 | write_enn (arg_own_buf); |
| 669 | return; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | if (strcmp ("qAttached", arg_own_buf) == 0) { |
| 674 | /* tell gdb to always detach, never kill the process */ |
| 675 | arg_own_buf[0] = '1'; |
| 676 | arg_own_buf[1] = 0; |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | if (strcmp ("qSymbol::", arg_own_buf) == 0) { |
| 681 | /* We have no symbol to read. */ |
| 682 | write_ok (arg_own_buf); |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | if (strcmp ("qfThreadInfo", arg_own_buf) == 0) { |
| 687 | thread_ptr = all_threads.head; |
| 688 | VG_(sprintf) (arg_own_buf, "m%x", |
| 689 | thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
| 690 | thread_ptr = thread_ptr->next; |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | if (strcmp ("qsThreadInfo", arg_own_buf) == 0) { |
| 695 | if (thread_ptr != NULL) { |
| 696 | VG_(sprintf) (arg_own_buf, "m%x", |
| 697 | thread_to_gdb_id ((struct thread_info *)thread_ptr)); |
| 698 | thread_ptr = thread_ptr->next; |
| 699 | return; |
| 700 | } else { |
| 701 | VG_(sprintf) (arg_own_buf, "l"); |
| 702 | return; |
| 703 | } |
| 704 | } |
| 705 | |
philippe | 419d5f2 | 2012-05-24 21:33:17 +0000 | [diff] [blame] | 706 | if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 707 | && strncmp ("qXfer:features:read:", arg_own_buf, 20) == 0) { |
| 708 | CORE_ADDR ofs; |
| 709 | unsigned int len, doc_len; |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 710 | const char *annex = NULL; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 711 | // First, the annex is extracted from the packet received. |
| 712 | // Then, it is replaced by the corresponding file name. |
| 713 | int fd; |
| 714 | |
| 715 | /* Grab the annex, offset, and length. */ |
| 716 | if (decode_xfer_read (arg_own_buf + 20, &annex, &ofs, &len) < 0) { |
| 717 | strcpy (arg_own_buf, "E00"); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | if (strcmp (annex, "target.xml") == 0) { |
philippe | 419d5f2 | 2012-05-24 21:33:17 +0000 | [diff] [blame] | 722 | annex = valgrind_target_xml(VG_(clo_vgdb_shadow_registers)); |
| 723 | if (annex != NULL && VG_(clo_vgdb_shadow_registers)) { |
| 724 | /* Ensure the shadow registers are initialized. */ |
| 725 | initialize_shadow_low(True); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 726 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 727 | if (annex == NULL) { |
| 728 | strcpy (arg_own_buf, "E00"); |
| 729 | return; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | { |
philippe | 75a5f78 | 2012-02-24 11:25:58 +0000 | [diff] [blame] | 734 | char doc[VG_(strlen)(VG_(libdir)) + 1 + VG_(strlen)(annex) + 1]; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 735 | struct vg_stat stat_doc; |
| 736 | char toread[len]; |
| 737 | int len_read; |
| 738 | |
| 739 | VG_(sprintf)(doc, "%s/%s", VG_(libdir), annex); |
| 740 | fd = VG_(fd_open) (doc, VKI_O_RDONLY, 0); |
| 741 | if (fd == -1) { |
| 742 | strcpy (arg_own_buf, "E00"); |
| 743 | return; |
| 744 | } |
| 745 | if (VG_(fstat) (fd, &stat_doc) != 0) { |
| 746 | VG_(close) (fd); |
| 747 | strcpy (arg_own_buf, "E00"); |
| 748 | return; |
| 749 | } |
| 750 | doc_len = stat_doc.size; |
| 751 | |
| 752 | if (len > PBUFSIZ - POVERHSIZ) |
| 753 | len = PBUFSIZ - POVERHSIZ; |
| 754 | |
| 755 | if (ofs > doc_len) { |
| 756 | write_enn (arg_own_buf); |
| 757 | VG_(close) (fd); |
| 758 | return; |
| 759 | } |
| 760 | VG_(lseek) (fd, ofs, VKI_SEEK_SET); |
| 761 | len_read = VG_(read) (fd, toread, len); |
florian | 1636d33 | 2012-11-15 04:27:04 +0000 | [diff] [blame] | 762 | *new_packet_len_p = write_qxfer_response (arg_own_buf, (unsigned char *)toread, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 763 | len_read, ofs + len_read < doc_len); |
| 764 | VG_(close) (fd); |
| 765 | return; |
| 766 | } |
| 767 | } |
| 768 | |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 769 | if (strncmp ("qXfer:auxv:read:", arg_own_buf, 16) == 0) { |
| 770 | unsigned char *data; |
| 771 | int n; |
| 772 | CORE_ADDR ofs; |
| 773 | unsigned int len; |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 774 | const char *annex; |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 775 | |
| 776 | /* Reject any annex; grab the offset and length. */ |
| 777 | if (decode_xfer_read (arg_own_buf + 16, &annex, &ofs, &len) < 0 |
| 778 | || annex[0] != '\0') { |
| 779 | strcpy (arg_own_buf, "E00"); |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | if (len > PBUFSIZ - 2) |
| 784 | len = PBUFSIZ - 2; |
| 785 | data = malloc (len); |
| 786 | |
| 787 | { |
| 788 | UWord *client_auxv = VG_(client_auxv); |
| 789 | unsigned int client_auxv_len = 0; |
| 790 | while (*client_auxv != 0) { |
| 791 | dlog(4, "auxv %lld %llx\n", |
| 792 | (ULong)*client_auxv, |
| 793 | (ULong)*(client_auxv+1)); |
| 794 | client_auxv++; |
| 795 | client_auxv++; |
| 796 | client_auxv_len += 2 * sizeof(UWord); |
| 797 | } |
| 798 | client_auxv_len += 2 * sizeof(UWord); |
| 799 | dlog(4, "auxv len %d\n", client_auxv_len); |
| 800 | |
| 801 | if (ofs >= client_auxv_len) |
| 802 | n = -1; |
| 803 | else { |
| 804 | n = client_auxv_len - ofs; |
| 805 | VG_(memcpy) (data, (unsigned char *) VG_(client_auxv), n); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | if (n < 0) |
| 810 | write_enn (arg_own_buf); |
| 811 | else if (n > len) |
| 812 | *new_packet_len_p = write_qxfer_response (arg_own_buf, data, len, 1); |
| 813 | else |
| 814 | *new_packet_len_p = write_qxfer_response (arg_own_buf, data, n, 0); |
| 815 | |
| 816 | free (data); |
| 817 | |
| 818 | return; |
| 819 | } |
| 820 | |
| 821 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 822 | /* Protocol features query. */ |
| 823 | if (strncmp ("qSupported", arg_own_buf, 10) == 0 |
| 824 | && (arg_own_buf[10] == ':' || arg_own_buf[10] == '\0')) { |
| 825 | VG_(sprintf) (arg_own_buf, "PacketSize=%x", PBUFSIZ - 1); |
| 826 | /* Note: max packet size including frame and checksum, but without |
| 827 | trailing null byte, which is not sent/received. */ |
| 828 | |
| 829 | strcat (arg_own_buf, ";QStartNoAckMode+"); |
| 830 | strcat (arg_own_buf, ";QPassSignals+"); |
sewardj | 2a31239 | 2011-06-26 09:26:48 +0000 | [diff] [blame] | 831 | if (VG_(client_auxv)) |
| 832 | strcat (arg_own_buf, ";qXfer:auxv:read+"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 833 | |
philippe | 419d5f2 | 2012-05-24 21:33:17 +0000 | [diff] [blame] | 834 | if (valgrind_target_xml(VG_(clo_vgdb_shadow_registers)) != NULL) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 835 | strcat (arg_own_buf, ";qXfer:features:read+"); |
| 836 | /* if a new gdb connects to us, we have to reset the register |
| 837 | set to the normal register sets to allow this new gdb to |
| 838 | decide to use or not the shadow registers. |
| 839 | |
| 840 | Note that the reset is only done for gdb that are sending |
| 841 | qSupported packets. If a user first connected with a recent |
| 842 | gdb using shadow registers and then with a very old gdb |
| 843 | that does not use qSupported packet, then the old gdb will |
| 844 | not properly connect. */ |
| 845 | initialize_shadow_low(False); |
| 846 | } |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 851 | understand it. */ |
| 852 | arg_own_buf[0] = 0; |
| 853 | } |
| 854 | |
| 855 | /* Handle all of the extended 'v' packets. */ |
| 856 | static |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 857 | void handle_v_requests (char *arg_own_buf, char *status, int *zignal) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 858 | { |
| 859 | /* vcont packet code from gdb 6.6 removed */ |
| 860 | |
| 861 | /* Otherwise we didn't know what packet it was. Say we didn't |
| 862 | understand it. */ |
| 863 | arg_own_buf[0] = 0; |
| 864 | return; |
| 865 | } |
| 866 | |
| 867 | static |
| 868 | void myresume (int step, int sig) |
| 869 | { |
| 870 | struct thread_resume resume_info[2]; |
| 871 | int n = 0; |
| 872 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 873 | if (step || sig) { |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 874 | resume_info[0].step = step; |
| 875 | resume_info[0].sig = sig; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 876 | n++; |
| 877 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 878 | resume_info[n].step = 0; |
| 879 | resume_info[n].sig = 0; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 880 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 881 | resume_reply_packet_needed = True; |
| 882 | valgrind_resume (resume_info); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /* server_main global variables */ |
| 886 | static char *own_buf; |
| 887 | static unsigned char *mem_buf; |
| 888 | |
| 889 | void gdbserver_init (void) |
| 890 | { |
| 891 | dlog(1, "gdbserver_init gdbserver embedded in valgrind: %s\n", version); |
| 892 | noack_mode = False; |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 893 | valgrind_initialize_target (); |
philippe | 0e1cac9 | 2012-02-28 22:37:44 +0000 | [diff] [blame] | 894 | // After a fork, gdbserver_init can be called again. |
| 895 | // We do not have to re-malloc the buffers in such a case. |
| 896 | if (own_buf == NULL) |
philippe | 03ffc6e | 2013-07-25 22:37:02 +0000 | [diff] [blame] | 897 | own_buf = malloc (PBUFSIZ+POVERHSIZ); |
philippe | 0e1cac9 | 2012-02-28 22:37:44 +0000 | [diff] [blame] | 898 | if (mem_buf == NULL) |
philippe | 03ffc6e | 2013-07-25 22:37:02 +0000 | [diff] [blame] | 899 | mem_buf = malloc (PBUFSIZ+POVERHSIZ); |
| 900 | // Note: normally, we should only malloc PBUFSIZ. However, |
| 901 | // GDB has a bug, and in some cases, sends e.g. 'm' packets |
| 902 | // asking for slightly more than the PacketSize given at |
| 903 | // connection initialisation. So, we bypass the GDB bug |
| 904 | // by allocating slightly more. |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | void gdbserver_terminate (void) |
| 908 | { |
| 909 | /* last call to gdbserver is cleanup call */ |
| 910 | if (VG_MINIMAL_SETJMP(toplevel)) { |
| 911 | dlog(0, "error caused VG_MINIMAL_LONGJMP to gdbserver_terminate\n"); |
| 912 | return; |
| 913 | } |
| 914 | remote_close(); |
| 915 | } |
| 916 | |
| 917 | void server_main (void) |
| 918 | { |
| 919 | static char status; |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 920 | static int zignal; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 921 | |
| 922 | char ch; |
| 923 | int i = 0; |
| 924 | unsigned int len; |
| 925 | CORE_ADDR mem_addr; |
| 926 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 927 | zignal = valgrind_wait (&status); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 928 | if (VG_MINIMAL_SETJMP(toplevel)) { |
| 929 | dlog(0, "error caused VG_MINIMAL_LONGJMP to server_main\n"); |
| 930 | } |
| 931 | while (1) { |
| 932 | unsigned char sig; |
| 933 | int packet_len; |
| 934 | int new_packet_len = -1; |
| 935 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 936 | if (resume_reply_packet_needed) { |
| 937 | /* Send the resume reply to reply to last GDB resume |
| 938 | request. */ |
| 939 | resume_reply_packet_needed = False; |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 940 | prepare_resume_reply (own_buf, status, zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 941 | putpkt (own_buf); |
| 942 | } |
| 943 | |
philippe | 0447bbd | 2012-10-17 21:32:03 +0000 | [diff] [blame] | 944 | /* If we our status is terminal (exit or fatal signal) get out |
| 945 | as quickly as we can. We won't be able to handle any request |
| 946 | anymore. */ |
| 947 | if (status == 'W' || status == 'X') { |
| 948 | return; |
| 949 | } |
| 950 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 951 | packet_len = getpkt (own_buf); |
| 952 | if (packet_len <= 0) |
| 953 | break; |
| 954 | |
| 955 | i = 0; |
| 956 | ch = own_buf[i++]; |
| 957 | switch (ch) { |
| 958 | case 'Q': |
| 959 | handle_set (own_buf, &new_packet_len); |
| 960 | break; |
| 961 | case 'q': |
| 962 | handle_query (own_buf, &new_packet_len); |
| 963 | break; |
| 964 | case 'd': |
| 965 | /* set/unset debugging is done through valgrind debug level. */ |
| 966 | own_buf[0] = '\0'; |
| 967 | break; |
| 968 | case 'D': |
| 969 | reset_valgrind_sink("gdb detaching from process"); |
| 970 | |
| 971 | /* When detaching or kill the process, gdb expects to get |
| 972 | an packet OK back. Any other output will make gdb |
| 973 | believes detach did not work. */ |
| 974 | write_ok (own_buf); |
| 975 | putpkt (own_buf); |
| 976 | remote_finish (reset_after_error); |
| 977 | remote_open (VG_(clo_vgdb_prefix)); |
| 978 | myresume (0, 0); |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 979 | resume_reply_packet_needed = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 980 | return; |
| 981 | case '!': |
| 982 | /* We can not use the extended protocol with valgrind, |
| 983 | because we can not restart the running |
| 984 | program. So return unrecognized. */ |
| 985 | own_buf[0] = '\0'; |
| 986 | break; |
| 987 | case '?': |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 988 | prepare_resume_reply (own_buf, status, zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 989 | break; |
| 990 | case 'H': |
| 991 | if (own_buf[1] == 'c' || own_buf[1] == 'g' || own_buf[1] == 's') { |
| 992 | unsigned long gdb_id, thread_id; |
| 993 | |
| 994 | gdb_id = strtoul (&own_buf[2], NULL, 16); |
| 995 | thread_id = gdb_id_to_thread_id (gdb_id); |
| 996 | if (thread_id == 0) { |
| 997 | write_enn (own_buf); |
| 998 | break; |
| 999 | } |
| 1000 | |
| 1001 | if (own_buf[1] == 'g') { |
| 1002 | general_thread = thread_id; |
| 1003 | set_desired_inferior (1); |
| 1004 | } else if (own_buf[1] == 'c') { |
| 1005 | cont_thread = thread_id; |
| 1006 | } else if (own_buf[1] == 's') { |
| 1007 | step_thread = thread_id; |
| 1008 | } |
| 1009 | |
| 1010 | write_ok (own_buf); |
| 1011 | } else { |
| 1012 | /* Silently ignore it so that gdb can extend the protocol |
| 1013 | without compatibility headaches. */ |
| 1014 | own_buf[0] = '\0'; |
| 1015 | } |
| 1016 | break; |
| 1017 | case 'g': |
| 1018 | set_desired_inferior (1); |
| 1019 | registers_to_string (own_buf); |
| 1020 | break; |
| 1021 | case 'G': |
| 1022 | set_desired_inferior (1); |
| 1023 | registers_from_string (&own_buf[1]); |
| 1024 | write_ok (own_buf); |
| 1025 | break; |
| 1026 | case 'P': { |
| 1027 | int regno; |
| 1028 | char *regbytes; |
| 1029 | Bool mod; |
| 1030 | ThreadState *tst; |
| 1031 | regno = strtol(&own_buf[1], NULL, 16); |
| 1032 | regbytes = strchr(&own_buf[0], '=') + 1; |
| 1033 | set_desired_inferior (1); |
| 1034 | tst = (ThreadState *) inferior_target_data (current_inferior); |
| 1035 | /* Only accept changing registers in "runnable state3. |
| 1036 | In fact, it would be ok to change most of the registers |
| 1037 | except a few "sensitive" registers such as the PC, SP, BP. |
| 1038 | We assume we do not need to very specific here, and that we |
| 1039 | can just refuse all of these. */ |
| 1040 | if (tst->status == VgTs_Runnable || tst->status == VgTs_Yielding) { |
| 1041 | supply_register_from_string (regno, regbytes, &mod); |
| 1042 | write_ok (own_buf); |
| 1043 | } else { |
| 1044 | /* at least from gdb 6.6 onwards, an E. error |
| 1045 | reply is shown to the user. So, we do an error |
| 1046 | msg which both is accepted by gdb as an error msg |
| 1047 | and is readable by the user. */ |
| 1048 | VG_(sprintf) |
| 1049 | (own_buf, |
| 1050 | "E.\n" |
| 1051 | "ERROR changing register %s regno %d\n" |
| 1052 | "gdb commands changing registers (pc, sp, ...) (e.g. 'jump',\n" |
| 1053 | "set pc, calling from gdb a function in the debugged process, ...)\n" |
| 1054 | "can only be accepted if the thread is VgTs_Runnable or VgTs_Yielding state\n" |
| 1055 | "Thread status is %s\n", |
| 1056 | find_register_by_number (regno)->name, regno, |
| 1057 | VG_(name_of_ThreadStatus)(tst->status)); |
| 1058 | if (VG_(clo_verbosity) > 1) |
| 1059 | VG_(umsg) ("%s\n", own_buf); |
| 1060 | } |
| 1061 | break; |
| 1062 | } |
| 1063 | case 'm': |
| 1064 | decode_m_packet (&own_buf[1], &mem_addr, &len); |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1065 | if (valgrind_read_memory (mem_addr, mem_buf, len) == 0) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1066 | convert_int_to_ascii (mem_buf, own_buf, len); |
| 1067 | else |
| 1068 | write_enn (own_buf); |
| 1069 | break; |
| 1070 | case 'M': |
| 1071 | decode_M_packet (&own_buf[1], &mem_addr, &len, mem_buf); |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1072 | if (valgrind_write_memory (mem_addr, mem_buf, len) == 0) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1073 | write_ok (own_buf); |
| 1074 | else |
| 1075 | write_enn (own_buf); |
| 1076 | break; |
| 1077 | case 'X': |
| 1078 | if (decode_X_packet (&own_buf[1], packet_len - 1, |
| 1079 | &mem_addr, &len, mem_buf) < 0 |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1080 | || valgrind_write_memory (mem_addr, mem_buf, len) != 0) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1081 | write_enn (own_buf); |
| 1082 | else |
| 1083 | write_ok (own_buf); |
| 1084 | break; |
| 1085 | case 'C': |
| 1086 | convert_ascii_to_int (own_buf + 1, &sig, 1); |
| 1087 | if (target_signal_to_host_p (sig)) |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1088 | zignal = target_signal_to_host (sig); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1089 | else |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1090 | zignal = 0; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1091 | set_desired_inferior (0); |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1092 | myresume (0, zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1093 | return; // return control to valgrind |
| 1094 | case 'S': |
| 1095 | convert_ascii_to_int (own_buf + 1, &sig, 1); |
| 1096 | if (target_signal_to_host_p (sig)) |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1097 | zignal = target_signal_to_host (sig); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1098 | else |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1099 | zignal = 0; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1100 | set_desired_inferior (0); |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1101 | myresume (1, zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1102 | return; // return control to valgrind |
| 1103 | case 'c': |
| 1104 | set_desired_inferior (0); |
| 1105 | myresume (0, 0); |
| 1106 | return; // return control to valgrind |
| 1107 | case 's': |
| 1108 | set_desired_inferior (0); |
| 1109 | myresume (1, 0); |
| 1110 | return; // return control to valgrind |
| 1111 | case 'Z': { |
| 1112 | char *lenptr; |
| 1113 | char *dataptr; |
| 1114 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); |
| 1115 | int zlen = strtol (lenptr + 1, &dataptr, 16); |
| 1116 | char type = own_buf[1]; |
| 1117 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1118 | if (type < '0' || type > '4') { |
| 1119 | /* Watchpoint command type unrecognized. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1120 | own_buf[0] = '\0'; |
| 1121 | } else { |
| 1122 | int res; |
| 1123 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1124 | res = valgrind_insert_watchpoint (type, addr, zlen); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1125 | if (res == 0) |
| 1126 | write_ok (own_buf); |
| 1127 | else if (res == 1) |
| 1128 | /* Unsupported. */ |
| 1129 | own_buf[0] = '\0'; |
| 1130 | else |
| 1131 | write_enn (own_buf); |
| 1132 | } |
| 1133 | break; |
| 1134 | } |
| 1135 | case 'z': { |
| 1136 | char *lenptr; |
| 1137 | char *dataptr; |
| 1138 | CORE_ADDR addr = strtoul (&own_buf[3], &lenptr, 16); |
| 1139 | int zlen = strtol (lenptr + 1, &dataptr, 16); |
| 1140 | char type = own_buf[1]; |
| 1141 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1142 | if (type < '0' || type > '4') { |
| 1143 | /* Watchpoint command type unrecognized. */ |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1144 | own_buf[0] = '\0'; |
| 1145 | } else { |
| 1146 | int res; |
| 1147 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1148 | res = valgrind_remove_watchpoint (type, addr, zlen); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1149 | if (res == 0) |
| 1150 | write_ok (own_buf); |
| 1151 | else if (res == 1) |
| 1152 | /* Unsupported. */ |
| 1153 | own_buf[0] = '\0'; |
| 1154 | else |
| 1155 | write_enn (own_buf); |
| 1156 | } |
| 1157 | break; |
| 1158 | } |
| 1159 | case 'k': |
| 1160 | kill_request("Gdb request to kill this process\n"); |
| 1161 | break; |
| 1162 | case 'T': { |
| 1163 | unsigned long gdb_id, thread_id; |
| 1164 | |
| 1165 | gdb_id = strtoul (&own_buf[1], NULL, 16); |
| 1166 | thread_id = gdb_id_to_thread_id (gdb_id); |
| 1167 | if (thread_id == 0) { |
| 1168 | write_enn (own_buf); |
| 1169 | break; |
| 1170 | } |
| 1171 | |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1172 | if (valgrind_thread_alive (thread_id)) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1173 | write_ok (own_buf); |
| 1174 | else |
| 1175 | write_enn (own_buf); |
| 1176 | break; |
| 1177 | } |
| 1178 | case 'R': |
| 1179 | /* Restarting the inferior is only supported in the |
| 1180 | extended protocol. |
| 1181 | => It is a request we don't understand. Respond with an |
| 1182 | empty packet so that gdb knows that we don't support this |
| 1183 | request. */ |
| 1184 | own_buf[0] = '\0'; |
| 1185 | break; |
| 1186 | case 'v': |
| 1187 | /* Extended (long) request. */ |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1188 | handle_v_requests (own_buf, &status, &zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1189 | break; |
| 1190 | default: |
| 1191 | /* It is a request we don't understand. Respond with an |
| 1192 | empty packet so that gdb knows that we don't support this |
| 1193 | request. */ |
| 1194 | own_buf[0] = '\0'; |
| 1195 | break; |
| 1196 | } |
| 1197 | |
| 1198 | if (new_packet_len != -1) |
| 1199 | putpkt_binary (own_buf, new_packet_len); |
| 1200 | else |
| 1201 | putpkt (own_buf); |
| 1202 | |
| 1203 | if (status == 'W') |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1204 | VG_(umsg) ("\nChild exited with status %d\n", zignal); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1205 | if (status == 'X') |
sewardj | 47ffedb | 2011-10-24 07:36:57 +0000 | [diff] [blame] | 1206 | VG_(umsg) ("\nChild terminated with signal = 0x%x (%s)\n", |
sewardj | 0bb3c67 | 2011-07-26 23:29:25 +0000 | [diff] [blame] | 1207 | target_signal_to_host (zignal), |
| 1208 | target_signal_to_name (zignal)); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1209 | if (status == 'W' || status == 'X') { |
| 1210 | VG_(umsg) ("Process exiting\n"); |
| 1211 | VG_(exit) (0); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | /* We come here when getpkt fails => close the connection, |
| 1216 | and re-open. Then return control to valgrind. |
| 1217 | We return the control to valgrind as we assume that |
| 1218 | the connection was closed due to vgdb having finished |
| 1219 | to execute a command. */ |
| 1220 | if (VG_(clo_verbosity) > 1) |
| 1221 | VG_(umsg) ("Remote side has terminated connection. " |
| 1222 | "GDBserver will reopen the connection.\n"); |
| 1223 | remote_finish (reset_after_error); |
| 1224 | remote_open (VG_(clo_vgdb_prefix)); |
| 1225 | myresume (0, 0); |
philippe | 349a391 | 2012-05-23 21:50:36 +0000 | [diff] [blame] | 1226 | resume_reply_packet_needed = False; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1227 | return; |
| 1228 | } |