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