| 1 | /* Remote debugging interface for generalized simulator |
| 2 | Copyright 1992 Free Software Foundation, Inc. |
| 3 | Contributed by Cygnus Support. Written by Steve Chamberlain |
| 4 | (sac@cygnus.com). |
| 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 2 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, write to the Free Software |
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 21 | |
| 22 | #include "defs.h" |
| 23 | #include "inferior.h" |
| 24 | #include "wait.h" |
| 25 | #include "value.h" |
| 26 | #include <string.h> |
| 27 | #include <ctype.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <signal.h> |
| 30 | #include <setjmp.h> |
| 31 | #include <errno.h> |
| 32 | #include "terminal.h" |
| 33 | #include "target.h" |
| 34 | #include "gdbcore.h" |
| 35 | |
| 36 | /* Forward data declarations */ |
| 37 | extern struct target_ops sim_ops; /* Forward declaration */ |
| 38 | |
| 39 | int |
| 40 | sim_write_inferior_memory (memaddr, myaddr, len) |
| 41 | CORE_ADDR memaddr; |
| 42 | unsigned char *myaddr; |
| 43 | int len; |
| 44 | { |
| 45 | return sim_write(memaddr, myaddr, len); |
| 46 | } |
| 47 | |
| 48 | static int |
| 49 | store_register(regno) |
| 50 | int regno; |
| 51 | { |
| 52 | if (regno == -1) |
| 53 | { |
| 54 | for (regno = 0; regno < NUM_REGS; regno++) |
| 55 | store_register(regno); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | sim_store_register(regno, read_register(regno)); |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /* |
| 66 | * Download a file specified in 'args', to the sim. |
| 67 | */ |
| 68 | static void |
| 69 | sim_load(args,fromtty) |
| 70 | char *args; |
| 71 | int fromtty; |
| 72 | { |
| 73 | bfd *abfd; |
| 74 | asection *s; |
| 75 | |
| 76 | inferior_pid = 0; |
| 77 | abfd = bfd_openr(args, (char*)0); |
| 78 | |
| 79 | if (!abfd) |
| 80 | { |
| 81 | printf_filtered("Unable to open file %s\n", args); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if (bfd_check_format(abfd, bfd_object) ==0) |
| 86 | { |
| 87 | printf_filtered("File is not an object file\n"); |
| 88 | return ; |
| 89 | } |
| 90 | |
| 91 | s = abfd->sections; |
| 92 | while (s != (asection *)NULL) |
| 93 | { |
| 94 | if (s->flags & SEC_LOAD) |
| 95 | { |
| 96 | int i; |
| 97 | int delta = 4096; |
| 98 | char *buffer = xmalloc(delta); |
| 99 | printf_filtered("%s\t: 0x%4x .. 0x%4x ", |
| 100 | s->name, s->vma, s->vma + s->_raw_size); |
| 101 | for (i = 0; i < s->_raw_size; i+= delta) |
| 102 | { |
| 103 | int sub_delta = delta; |
| 104 | if (sub_delta > s->_raw_size - i) |
| 105 | sub_delta = s->_raw_size - i ; |
| 106 | |
| 107 | bfd_get_section_contents(abfd, s, buffer, i, sub_delta); |
| 108 | sim_write_inferior_memory(s->vma + i, buffer, sub_delta); |
| 109 | printf_filtered("*"); |
| 110 | fflush(stdout); |
| 111 | } |
| 112 | printf_filtered( "\n"); |
| 113 | free(buffer); |
| 114 | } |
| 115 | s = s->next; |
| 116 | } |
| 117 | |
| 118 | sim_store_register(PC_REGNUM, abfd->start_address); |
| 119 | } |
| 120 | |
| 121 | /* This is called not only when we first attach, but also when the |
| 122 | user types "run" after having attached. */ |
| 123 | void |
| 124 | sim_create_inferior (execfile, args, env) |
| 125 | char *execfile; |
| 126 | char *args; |
| 127 | char **env; |
| 128 | { |
| 129 | int entry_pt; |
| 130 | |
| 131 | if (args && *args) |
| 132 | error ("Can't pass arguments to remote sim process."); |
| 133 | |
| 134 | if (execfile == 0 || exec_bfd == 0) |
| 135 | error ("No exec file specified"); |
| 136 | |
| 137 | entry_pt = (int) bfd_get_start_address (exec_bfd); |
| 138 | init_wait_for_inferior (); |
| 139 | insert_breakpoints (); |
| 140 | proceed(entry_pt, -1, 0); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | static int |
| 146 | sim_open (name, from_tty) |
| 147 | char *name; |
| 148 | int from_tty; |
| 149 | { |
| 150 | if(name == 0) |
| 151 | { |
| 152 | name = ""; |
| 153 | } |
| 154 | |
| 155 | push_target (&sim_ops); |
| 156 | target_fetch_registers(-1); |
| 157 | printf_filtered("Connected to the simulator.\n"); |
| 158 | } |
| 159 | |
| 160 | /* Close out all files and local state before this target loses control. */ |
| 161 | |
| 162 | static int |
| 163 | sim_close (quitting) |
| 164 | int quitting; |
| 165 | { |
| 166 | } |
| 167 | |
| 168 | /* Terminate the open connection to the remote debugger. |
| 169 | Use this when you want to detach and do something else |
| 170 | with your gdb. */ |
| 171 | int |
| 172 | sim_detach (args,from_tty) |
| 173 | char *args; |
| 174 | int from_tty; |
| 175 | { |
| 176 | pop_target(); /* calls sim_close to do the real work */ |
| 177 | if (from_tty) |
| 178 | printf_filtered ("Ending remote %s debugging\n", target_shortname); |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /* Tell the remote machine to resume. */ |
| 183 | |
| 184 | |
| 185 | /* Wait until the remote machine stops, then return, |
| 186 | storing status in STATUS just as `wait' would. */ |
| 187 | |
| 188 | int |
| 189 | sim_wait (status) |
| 190 | WAITTYPE *status; |
| 191 | { |
| 192 | WSETSTOP(*status, sim_stop_signal()); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | static void |
| 199 | fetch_register(regno) |
| 200 | int regno; |
| 201 | { |
| 202 | if (regno == -1) |
| 203 | { |
| 204 | for (regno = 0; regno < NUM_REGS; regno++) |
| 205 | fetch_register(regno); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | char buf[MAX_REGISTER_RAW_SIZE]; |
| 210 | sim_fetch_register(regno, buf); |
| 211 | supply_register(regno, buf); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | int |
| 217 | sim_xfer_inferior_memory(memaddr, myaddr, len, write, target) |
| 218 | CORE_ADDR memaddr; |
| 219 | char *myaddr; |
| 220 | int len; |
| 221 | int write; |
| 222 | struct target_ops *target; /* ignored */ |
| 223 | { |
| 224 | if (write) |
| 225 | { |
| 226 | sim_write(memaddr, myaddr, len); |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | sim_read(memaddr, myaddr, len); |
| 231 | } |
| 232 | return len; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | /* This routine is run as a hook, just before the main command loop is |
| 237 | entered. If gdb is configured for the H8, but has not had its |
| 238 | target specified yet, this will loop prompting the user to do so. |
| 239 | */ |
| 240 | |
| 241 | void |
| 242 | sim_before_main_loop () |
| 243 | { |
| 244 | push_target (&sim_ops); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | static int rem_resume(a,b) |
| 249 | { |
| 250 | sim_resume(a,b); |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | pstore() |
| 255 | { |
| 256 | return 1; |
| 257 | } |
| 258 | /* Define the target subroutine names */ |
| 259 | |
| 260 | struct target_ops sim_ops = |
| 261 | { |
| 262 | "sim", "simulator", |
| 263 | "Use the simulator", |
| 264 | sim_open, sim_close, |
| 265 | 0, sim_detach, rem_resume, sim_wait, /* attach */ |
| 266 | fetch_register, store_register, |
| 267 | pstore, |
| 268 | sim_xfer_inferior_memory, |
| 269 | 0, |
| 270 | 0, 0, /* Breakpoints */ |
| 271 | 0, 0, 0, 0, 0, /* Terminal handling */ |
| 272 | pstore, |
| 273 | sim_load, |
| 274 | 0, /* lookup_symbol */ |
| 275 | sim_create_inferior, /* create_inferior */ |
| 276 | pstore, /* mourn_inferior FIXME */ |
| 277 | 0, /* can_run */ |
| 278 | 0, /* notice_signals */ |
| 279 | process_stratum, 0, /* next */ |
| 280 | 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */ |
| 281 | 0,0, /* Section pointers */ |
| 282 | OPS_MAGIC, /* Always the last thing */ |
| 283 | }; |
| 284 | |
| 285 | /***********************************************************************/ |
| 286 | |
| 287 | void |
| 288 | _initialize_remote_sim () |
| 289 | { |
| 290 | add_target (&sim_ops); |
| 291 | } |
| 292 | |
| 293 | |