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