| 1 | /* Remote debugging with the XLNT Designs, Inc (XDI) NetROM. |
| 2 | Copyright 1990, 1991, 1992, 1995, 1998, 1999, 2000 |
| 3 | Free Software Foundation, Inc. |
| 4 | Contributed by: |
| 5 | Roger Moyers |
| 6 | XLNT Designs, Inc. |
| 7 | 15050 Avenue of Science, Suite 106 |
| 8 | San Diego, CA 92128 |
| 9 | (619)487-9320 |
| 10 | roger@xlnt.com |
| 11 | Adapted from work done at Cygnus Support in remote-nindy.c, |
| 12 | later merged in by Stan Shebs at Cygnus. |
| 13 | |
| 14 | This file is part of GDB. |
| 15 | |
| 16 | This program is free software; you can redistribute it and/or modify |
| 17 | it under the terms of the GNU General Public License as published by |
| 18 | the Free Software Foundation; either version 2 of the License, or |
| 19 | (at your option) any later version. |
| 20 | |
| 21 | This program is distributed in the hope that it will be useful, |
| 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | GNU General Public License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU General Public License |
| 27 | along with this program; if not, write to the Free Software |
| 28 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 29 | Boston, MA 02111-1307, USA. */ |
| 30 | |
| 31 | #include "defs.h" |
| 32 | #include "gdbcmd.h" |
| 33 | #include "serial.h" |
| 34 | #include "target.h" |
| 35 | |
| 36 | /* Default ports used to talk with the NetROM. */ |
| 37 | |
| 38 | #define DEFAULT_NETROM_LOAD_PORT 1236 |
| 39 | #define DEFAULT_NETROM_CONTROL_PORT 1237 |
| 40 | |
| 41 | static void nrom_close (int quitting); |
| 42 | |
| 43 | /* New commands. */ |
| 44 | |
| 45 | static void nrom_passthru (char *, int); |
| 46 | |
| 47 | /* We talk to the NetROM over these sockets. */ |
| 48 | |
| 49 | static serial_t load_desc = NULL; |
| 50 | static serial_t ctrl_desc = NULL; |
| 51 | |
| 52 | static int load_port = DEFAULT_NETROM_LOAD_PORT; |
| 53 | static int control_port = DEFAULT_NETROM_CONTROL_PORT; |
| 54 | |
| 55 | static char nrom_hostname[100]; |
| 56 | |
| 57 | /* Forward data declaration. */ |
| 58 | |
| 59 | extern struct target_ops nrom_ops; |
| 60 | |
| 61 | /* Scan input from the remote system, until STRING is found. Print chars that |
| 62 | don't match. */ |
| 63 | |
| 64 | static int |
| 65 | expect (char *string) |
| 66 | { |
| 67 | char *p = string; |
| 68 | int c; |
| 69 | |
| 70 | immediate_quit++; |
| 71 | |
| 72 | while (1) |
| 73 | { |
| 74 | c = SERIAL_READCHAR (ctrl_desc, 5); |
| 75 | |
| 76 | if (c == *p++) |
| 77 | { |
| 78 | if (*p == '\0') |
| 79 | { |
| 80 | immediate_quit--; |
| 81 | return 0; |
| 82 | } |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | fputc_unfiltered (c, gdb_stdout); |
| 87 | p = string; |
| 88 | if (c == *p) |
| 89 | p++; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void |
| 95 | nrom_kill (void) |
| 96 | { |
| 97 | nrom_close (0); |
| 98 | } |
| 99 | |
| 100 | static serial_t |
| 101 | open_socket (char *name, int port) |
| 102 | { |
| 103 | char sockname[100]; |
| 104 | serial_t desc; |
| 105 | |
| 106 | sprintf (sockname, "%s:%d", name, port); |
| 107 | desc = SERIAL_OPEN (sockname); |
| 108 | if (!desc) |
| 109 | perror_with_name (sockname); |
| 110 | |
| 111 | return desc; |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | load_cleanup (void) |
| 116 | { |
| 117 | SERIAL_CLOSE (load_desc); |
| 118 | load_desc = NULL; |
| 119 | } |
| 120 | |
| 121 | /* Download a file specified in ARGS to the netROM. */ |
| 122 | |
| 123 | static void |
| 124 | nrom_load (char *args, int fromtty) |
| 125 | { |
| 126 | int fd, rd_amt, fsize; |
| 127 | bfd *pbfd; |
| 128 | asection *section; |
| 129 | char *downloadstring = "download 0\n"; |
| 130 | struct cleanup *old_chain; |
| 131 | |
| 132 | /* Tell the netrom to get ready to download. */ |
| 133 | if (SERIAL_WRITE (ctrl_desc, downloadstring, strlen (downloadstring))) |
| 134 | error ("nrom_load: control_send() of `%s' failed", downloadstring); |
| 135 | |
| 136 | expect ("Waiting for a connection...\n"); |
| 137 | |
| 138 | load_desc = open_socket (nrom_hostname, load_port); |
| 139 | |
| 140 | old_chain = make_cleanup (load_cleanup, 0); |
| 141 | |
| 142 | pbfd = bfd_openr (args, 0); |
| 143 | |
| 144 | if (pbfd) |
| 145 | { |
| 146 | make_cleanup (bfd_close, pbfd); |
| 147 | |
| 148 | if (!bfd_check_format (pbfd, bfd_object)) |
| 149 | error ("\"%s\": not in executable format: %s", |
| 150 | args, bfd_errmsg (bfd_get_error ())); |
| 151 | |
| 152 | for (section = pbfd->sections; section; section = section->next) |
| 153 | { |
| 154 | if (bfd_get_section_flags (pbfd, section) & SEC_ALLOC) |
| 155 | { |
| 156 | bfd_vma section_address; |
| 157 | unsigned long section_size; |
| 158 | const char *section_name; |
| 159 | |
| 160 | section_name = bfd_get_section_name (pbfd, section); |
| 161 | section_address = bfd_get_section_vma (pbfd, section); |
| 162 | section_size = bfd_section_size (pbfd, section); |
| 163 | |
| 164 | if (bfd_get_section_flags (pbfd, section) & SEC_LOAD) |
| 165 | { |
| 166 | file_ptr fptr; |
| 167 | |
| 168 | printf_filtered ("[Loading section %s at %x (%d bytes)]\n", |
| 169 | section_name, section_address, |
| 170 | section_size); |
| 171 | |
| 172 | fptr = 0; |
| 173 | |
| 174 | while (section_size > 0) |
| 175 | { |
| 176 | char buffer[1024]; |
| 177 | int count; |
| 178 | |
| 179 | count = min (section_size, 1024); |
| 180 | |
| 181 | bfd_get_section_contents (pbfd, section, buffer, fptr, |
| 182 | count); |
| 183 | |
| 184 | SERIAL_WRITE (load_desc, buffer, count); |
| 185 | section_address += count; |
| 186 | fptr += count; |
| 187 | section_size -= count; |
| 188 | } |
| 189 | } |
| 190 | else |
| 191 | /* BSS and such */ |
| 192 | { |
| 193 | printf_filtered ("[section %s: not loading]\n", |
| 194 | section_name); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | else |
| 200 | error ("\"%s\": Could not open", args); |
| 201 | |
| 202 | do_cleanups (old_chain); |
| 203 | } |
| 204 | |
| 205 | /* Open a connection to the remote NetROM devices. */ |
| 206 | |
| 207 | static void |
| 208 | nrom_open (char *name, int from_tty) |
| 209 | { |
| 210 | int errn; |
| 211 | |
| 212 | if (!name || strchr (name, '/') || strchr (name, ':')) |
| 213 | error ( |
| 214 | "To open a NetROM connection, you must specify the hostname\n\ |
| 215 | or IP address of the NetROM device you wish to use."); |
| 216 | |
| 217 | strcpy (nrom_hostname, name); |
| 218 | |
| 219 | target_preopen (from_tty); |
| 220 | |
| 221 | unpush_target (&nrom_ops); |
| 222 | |
| 223 | ctrl_desc = open_socket (nrom_hostname, control_port); |
| 224 | |
| 225 | push_target (&nrom_ops); |
| 226 | |
| 227 | if (from_tty) |
| 228 | printf_filtered ("Connected to NetROM device \"%s\"\n", nrom_hostname); |
| 229 | } |
| 230 | |
| 231 | /* Close out all files and local state before this target loses control. */ |
| 232 | |
| 233 | static void |
| 234 | nrom_close (int quitting) |
| 235 | { |
| 236 | if (load_desc) |
| 237 | SERIAL_CLOSE (load_desc); |
| 238 | if (ctrl_desc) |
| 239 | SERIAL_CLOSE (ctrl_desc); |
| 240 | } |
| 241 | |
| 242 | /* Pass arguments directly to the NetROM. */ |
| 243 | |
| 244 | static void |
| 245 | nrom_passthru (char *args, int fromtty) |
| 246 | { |
| 247 | char buf[1024]; |
| 248 | |
| 249 | sprintf (buf, "%s\n", args); |
| 250 | if (SERIAL_WRITE (ctrl_desc, buf, strlen (buf))) |
| 251 | error ("nrom_reset: control_send() of `%s'failed", args); |
| 252 | } |
| 253 | |
| 254 | static void |
| 255 | nrom_mourn (void) |
| 256 | { |
| 257 | unpush_target (&nrom_ops); |
| 258 | generic_mourn_inferior (); |
| 259 | } |
| 260 | |
| 261 | /* Define the target vector. */ |
| 262 | |
| 263 | struct target_ops nrom_ops; |
| 264 | |
| 265 | static void |
| 266 | init_nrom_ops (void) |
| 267 | { |
| 268 | nrom_ops.to_shortname = "nrom"; |
| 269 | nrom_ops.to_longname = "Remote XDI `NetROM' target"; |
| 270 | nrom_ops.to_doc = "Remote debug using a NetROM over Ethernet"; |
| 271 | nrom_ops.to_open = nrom_open; |
| 272 | nrom_ops.to_close = nrom_close; |
| 273 | nrom_ops.to_attach = NULL; |
| 274 | nrom_ops.to_post_attach = NULL; |
| 275 | nrom_ops.to_require_attach = NULL; |
| 276 | nrom_ops.to_detach = NULL; |
| 277 | nrom_ops.to_require_detach = NULL; |
| 278 | nrom_ops.to_resume = NULL; |
| 279 | nrom_ops.to_wait = NULL; |
| 280 | nrom_ops.to_post_wait = NULL; |
| 281 | nrom_ops.to_fetch_registers = NULL; |
| 282 | nrom_ops.to_store_registers = NULL; |
| 283 | nrom_ops.to_prepare_to_store = NULL; |
| 284 | nrom_ops.to_xfer_memory = NULL; |
| 285 | nrom_ops.to_files_info = NULL; |
| 286 | nrom_ops.to_insert_breakpoint = NULL; |
| 287 | nrom_ops.to_remove_breakpoint = NULL; |
| 288 | nrom_ops.to_terminal_init = NULL; |
| 289 | nrom_ops.to_terminal_inferior = NULL; |
| 290 | nrom_ops.to_terminal_ours_for_output = NULL; |
| 291 | nrom_ops.to_terminal_ours = NULL; |
| 292 | nrom_ops.to_terminal_info = NULL; |
| 293 | nrom_ops.to_kill = nrom_kill; |
| 294 | nrom_ops.to_load = nrom_load; |
| 295 | nrom_ops.to_lookup_symbol = NULL; |
| 296 | nrom_ops.to_create_inferior = NULL; |
| 297 | nrom_ops.to_post_startup_inferior = NULL; |
| 298 | nrom_ops.to_acknowledge_created_inferior = NULL; |
| 299 | nrom_ops.to_clone_and_follow_inferior = NULL; |
| 300 | nrom_ops.to_post_follow_inferior_by_clone = NULL; |
| 301 | nrom_ops.to_insert_fork_catchpoint = NULL; |
| 302 | nrom_ops.to_remove_fork_catchpoint = NULL; |
| 303 | nrom_ops.to_insert_vfork_catchpoint = NULL; |
| 304 | nrom_ops.to_remove_vfork_catchpoint = NULL; |
| 305 | nrom_ops.to_has_forked = NULL; |
| 306 | nrom_ops.to_has_vforked = NULL; |
| 307 | nrom_ops.to_can_follow_vfork_prior_to_exec = NULL; |
| 308 | nrom_ops.to_post_follow_vfork = NULL; |
| 309 | nrom_ops.to_insert_exec_catchpoint = NULL; |
| 310 | nrom_ops.to_remove_exec_catchpoint = NULL; |
| 311 | nrom_ops.to_has_execd = NULL; |
| 312 | nrom_ops.to_reported_exec_events_per_exec_call = NULL; |
| 313 | nrom_ops.to_has_exited = NULL; |
| 314 | nrom_ops.to_mourn_inferior = nrom_mourn; |
| 315 | nrom_ops.to_can_run = NULL; |
| 316 | nrom_ops.to_notice_signals = 0; |
| 317 | nrom_ops.to_thread_alive = 0; |
| 318 | nrom_ops.to_stop = 0; |
| 319 | nrom_ops.to_pid_to_exec_file = NULL; |
| 320 | nrom_ops.to_stratum = download_stratum; |
| 321 | nrom_ops.DONT_USE = NULL; |
| 322 | nrom_ops.to_has_all_memory = 1; |
| 323 | nrom_ops.to_has_memory = 1; |
| 324 | nrom_ops.to_has_stack = 1; |
| 325 | nrom_ops.to_has_registers = 1; |
| 326 | nrom_ops.to_has_execution = 0; |
| 327 | nrom_ops.to_sections = NULL; |
| 328 | nrom_ops.to_sections_end = NULL; |
| 329 | nrom_ops.to_magic = OPS_MAGIC; |
| 330 | } |
| 331 | |
| 332 | void |
| 333 | _initialize_remote_nrom (void) |
| 334 | { |
| 335 | init_nrom_ops (); |
| 336 | add_target (&nrom_ops); |
| 337 | |
| 338 | add_show_from_set ( |
| 339 | add_set_cmd ("nrom_load_port", no_class, var_zinteger, (char *) &load_port, |
| 340 | "Set the port to use for NetROM downloads\n", &setlist), |
| 341 | &showlist); |
| 342 | |
| 343 | add_show_from_set ( |
| 344 | add_set_cmd ("nrom_control_port", no_class, var_zinteger, (char *) &control_port, |
| 345 | "Set the port to use for NetROM debugger services\n", &setlist), |
| 346 | &showlist); |
| 347 | |
| 348 | add_cmd ("nrom", no_class, nrom_passthru, |
| 349 | "Pass arguments as command to NetROM", |
| 350 | &cmdlist); |
| 351 | } |