| 1 | /* Target operations for the remote server for GDB. |
| 2 | Copyright (C) 2002-2015 Free Software Foundation, Inc. |
| 3 | |
| 4 | Contributed by MontaVista Software. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "server.h" |
| 22 | #include "tracepoint.h" |
| 23 | |
| 24 | struct target_ops *the_target; |
| 25 | |
| 26 | int |
| 27 | set_desired_thread (int use_general) |
| 28 | { |
| 29 | struct thread_info *found; |
| 30 | |
| 31 | if (use_general == 1) |
| 32 | found = find_thread_ptid (general_thread); |
| 33 | else |
| 34 | found = find_thread_ptid (cont_thread); |
| 35 | |
| 36 | current_thread = found; |
| 37 | return (current_thread != NULL); |
| 38 | } |
| 39 | |
| 40 | int |
| 41 | read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
| 42 | { |
| 43 | int res; |
| 44 | res = (*the_target->read_memory) (memaddr, myaddr, len); |
| 45 | check_mem_read (memaddr, myaddr, len); |
| 46 | return res; |
| 47 | } |
| 48 | |
| 49 | /* See target/target.h. */ |
| 50 | |
| 51 | int |
| 52 | target_read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len) |
| 53 | { |
| 54 | return read_inferior_memory (memaddr, myaddr, len); |
| 55 | } |
| 56 | |
| 57 | /* See target/target.h. */ |
| 58 | |
| 59 | int |
| 60 | target_read_uint32 (CORE_ADDR memaddr, uint32_t *result) |
| 61 | { |
| 62 | return read_inferior_memory (memaddr, (gdb_byte *) result, sizeof (*result)); |
| 63 | } |
| 64 | |
| 65 | int |
| 66 | write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr, |
| 67 | int len) |
| 68 | { |
| 69 | /* Lacking cleanups, there is some potential for a memory leak if the |
| 70 | write fails and we go through error(). Make sure that no more than |
| 71 | one buffer is ever pending by making BUFFER static. */ |
| 72 | static unsigned char *buffer = 0; |
| 73 | int res; |
| 74 | |
| 75 | if (buffer != NULL) |
| 76 | free (buffer); |
| 77 | |
| 78 | buffer = (unsigned char *) xmalloc (len); |
| 79 | memcpy (buffer, myaddr, len); |
| 80 | check_mem_write (memaddr, buffer, myaddr, len); |
| 81 | res = (*the_target->write_memory) (memaddr, buffer, len); |
| 82 | free (buffer); |
| 83 | buffer = NULL; |
| 84 | |
| 85 | return res; |
| 86 | } |
| 87 | |
| 88 | /* See target/target.h. */ |
| 89 | |
| 90 | int |
| 91 | target_write_memory (CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len) |
| 92 | { |
| 93 | return write_inferior_memory (memaddr, myaddr, len); |
| 94 | } |
| 95 | |
| 96 | ptid_t |
| 97 | mywait (ptid_t ptid, struct target_waitstatus *ourstatus, int options, |
| 98 | int connected_wait) |
| 99 | { |
| 100 | ptid_t ret; |
| 101 | |
| 102 | if (connected_wait) |
| 103 | server_waiting = 1; |
| 104 | |
| 105 | ret = (*the_target->wait) (ptid, ourstatus, options); |
| 106 | |
| 107 | /* We don't expose _LOADED events to gdbserver core. See the |
| 108 | `dlls_changed' global. */ |
| 109 | if (ourstatus->kind == TARGET_WAITKIND_LOADED) |
| 110 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
| 111 | |
| 112 | /* If GDB is connected through TCP/serial, then GDBserver will most |
| 113 | probably be running on its own terminal/console, so it's nice to |
| 114 | print there why is GDBserver exiting. If however, GDB is |
| 115 | connected through stdio, then there's no need to spam the GDB |
| 116 | console with this -- the user will already see the exit through |
| 117 | regular GDB output, in that same terminal. */ |
| 118 | if (!remote_connection_is_stdio ()) |
| 119 | { |
| 120 | if (ourstatus->kind == TARGET_WAITKIND_EXITED) |
| 121 | fprintf (stderr, |
| 122 | "\nChild exited with status %d\n", ourstatus->value.integer); |
| 123 | else if (ourstatus->kind == TARGET_WAITKIND_SIGNALLED) |
| 124 | fprintf (stderr, "\nChild terminated with signal = 0x%x (%s)\n", |
| 125 | gdb_signal_to_host (ourstatus->value.sig), |
| 126 | gdb_signal_to_name (ourstatus->value.sig)); |
| 127 | } |
| 128 | |
| 129 | if (connected_wait) |
| 130 | server_waiting = 0; |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | /* See target/target.h. */ |
| 136 | |
| 137 | void |
| 138 | target_stop_and_wait (ptid_t ptid) |
| 139 | { |
| 140 | struct target_waitstatus status; |
| 141 | int was_non_stop = non_stop; |
| 142 | struct thread_resume resume_info; |
| 143 | |
| 144 | resume_info.thread = ptid; |
| 145 | resume_info.kind = resume_stop; |
| 146 | resume_info.sig = GDB_SIGNAL_0; |
| 147 | (*the_target->resume) (&resume_info, 1); |
| 148 | |
| 149 | non_stop = 1; |
| 150 | mywait (ptid, &status, 0, 0); |
| 151 | non_stop = was_non_stop; |
| 152 | } |
| 153 | |
| 154 | /* See target/target.h. */ |
| 155 | |
| 156 | void |
| 157 | target_continue_no_signal (ptid_t ptid) |
| 158 | { |
| 159 | struct thread_resume resume_info; |
| 160 | |
| 161 | resume_info.thread = ptid; |
| 162 | resume_info.kind = resume_continue; |
| 163 | resume_info.sig = GDB_SIGNAL_0; |
| 164 | (*the_target->resume) (&resume_info, 1); |
| 165 | } |
| 166 | |
| 167 | int |
| 168 | start_non_stop (int nonstop) |
| 169 | { |
| 170 | if (the_target->start_non_stop == NULL) |
| 171 | { |
| 172 | if (nonstop) |
| 173 | return -1; |
| 174 | else |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | return (*the_target->start_non_stop) (nonstop); |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | set_target_ops (struct target_ops *target) |
| 183 | { |
| 184 | the_target = XNEW (struct target_ops); |
| 185 | memcpy (the_target, target, sizeof (*the_target)); |
| 186 | } |
| 187 | |
| 188 | /* Convert pid to printable format. */ |
| 189 | |
| 190 | const char * |
| 191 | target_pid_to_str (ptid_t ptid) |
| 192 | { |
| 193 | static char buf[80]; |
| 194 | |
| 195 | if (ptid_equal (ptid, minus_one_ptid)) |
| 196 | xsnprintf (buf, sizeof (buf), "<all threads>"); |
| 197 | else if (ptid_equal (ptid, null_ptid)) |
| 198 | xsnprintf (buf, sizeof (buf), "<null thread>"); |
| 199 | else if (ptid_get_tid (ptid) != 0) |
| 200 | xsnprintf (buf, sizeof (buf), "Thread %d.0x%lx", |
| 201 | ptid_get_pid (ptid), ptid_get_tid (ptid)); |
| 202 | else if (ptid_get_lwp (ptid) != 0) |
| 203 | xsnprintf (buf, sizeof (buf), "LWP %d.%ld", |
| 204 | ptid_get_pid (ptid), ptid_get_lwp (ptid)); |
| 205 | else |
| 206 | xsnprintf (buf, sizeof (buf), "Process %d", |
| 207 | ptid_get_pid (ptid)); |
| 208 | |
| 209 | return buf; |
| 210 | } |
| 211 | |
| 212 | int |
| 213 | kill_inferior (int pid) |
| 214 | { |
| 215 | gdb_agent_about_to_close (pid); |
| 216 | |
| 217 | return (*the_target->kill) (pid); |
| 218 | } |
| 219 | |
| 220 | /* Target can do hardware single step. */ |
| 221 | |
| 222 | int |
| 223 | target_can_do_hardware_single_step (void) |
| 224 | { |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | /* Default implementation for breakpoint_kind_for_pc. |
| 229 | |
| 230 | The default behavior for targets that don't implement breakpoint_kind_for_pc |
| 231 | is to use the size of a breakpoint as the kind. */ |
| 232 | |
| 233 | int |
| 234 | default_breakpoint_kind_from_pc (CORE_ADDR *pcptr) |
| 235 | { |
| 236 | int size = 0; |
| 237 | |
| 238 | gdb_assert (the_target->sw_breakpoint_from_kind != NULL); |
| 239 | |
| 240 | (*the_target->sw_breakpoint_from_kind) (0, &size); |
| 241 | return size; |
| 242 | } |