* breakpoint.c, core.c, exec.c, language.c, main.c, printcmd.c,
[external/binutils.git] / gdb / target.c
1 /* Select target systems and architectures at runtime for GDB.
2    Copyright 1990, 1992 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include "defs.h"
22 #include <errno.h>
23 #include <ctype.h>
24 #include "target.h"
25 #include "gdbcmd.h"
26 #include "symtab.h"
27 #include "inferior.h"
28 #include "bfd.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31
32 extern int errno;
33
34 static void
35 target_info PARAMS ((char *, int));
36
37 static void
38 cleanup_target PARAMS ((struct target_ops *));
39
40 static void
41 maybe_kill_then_create_inferior PARAMS ((char *, char *, char **));
42
43 static void
44 maybe_kill_then_attach PARAMS ((char *, int));
45
46 static void
47 kill_or_be_killed PARAMS ((int));
48
49 static void
50 default_terminal_info PARAMS ((char *, int));
51
52 static int
53 nosymbol PARAMS ((char *, CORE_ADDR *));
54
55 static void
56 tcomplain PARAMS ((void));
57
58 static int
59 nomemory PARAMS ((CORE_ADDR, char *, int, int));
60
61 static void
62 ignore PARAMS ((void));
63 static void
64 target_command PARAMS ((char *, int));
65
66 /* Pointer to array of target architecture structures; the size of the
67    array; the current index into the array; the allocated size of the 
68    array.  */
69 struct target_ops **target_structs;
70 unsigned target_struct_size;
71 unsigned target_struct_index;
72 unsigned target_struct_allocsize;
73 #define DEFAULT_ALLOCSIZE       10
74
75 /* The initial current target, so that there is always a semi-valid
76    current target.  */
77
78 struct target_ops dummy_target = {"None", "None", "",
79     0, 0, 0, 0,         /* open, close, attach, detach */
80     0, 0,               /* resume, wait */
81     0, 0, 0, 0, 0,      /* registers */
82     0, 0,               /* memory */
83     0, 0,               /* bkpts */
84     0, 0, 0, 0, 0,      /* terminal */
85     0, 0,               /* kill, load */
86     0,                  /* lookup_symbol */
87     0, 0,               /* create_inferior, mourn_inferior */
88     dummy_stratum, 0,   /* stratum, next */
89     0, 0, 0, 0, 0,      /* all mem, mem, stack, regs, exec */
90     0, 0,               /* section pointers */
91     OPS_MAGIC,
92 };
93
94 /* The target structure we are currently using to talk to a process
95    or file or whatever "inferior" we have.  */
96
97 struct target_ops *current_target;
98
99 /* The stack of target structures that have been pushed.  */
100
101 struct target_ops **current_target_stack;
102
103 /* Command list for target.  */
104
105 static struct cmd_list_element *targetlist = NULL;
106
107 /* The user just typed 'target' without the name of a target.  */
108
109 /* ARGSUSED */
110 static void
111 target_command (arg, from_tty)
112      char *arg;
113      int from_tty;
114 {
115   fputs_filtered ("Argument required (target name).\n", stdout);
116 }
117
118 /* Add a possible target architecture to the list.  */
119
120 void
121 add_target (t)
122      struct target_ops *t;
123 {
124   if (t->to_magic != OPS_MAGIC)
125     {
126       fprintf_filtered(stderr, "Magic number of %s target struct wrong\n", 
127         t->to_shortname);
128       abort();
129     }
130
131   if (!target_structs)
132     {
133       target_struct_allocsize = DEFAULT_ALLOCSIZE;
134       target_structs = (struct target_ops **) xmalloc
135         (target_struct_allocsize * sizeof (*target_structs));
136     }
137   if (target_struct_size >= target_struct_allocsize)
138     {
139       target_struct_allocsize *= 2;
140       target_structs = (struct target_ops **)
141           xrealloc ((char *) target_structs, 
142                     target_struct_allocsize * sizeof (*target_structs));
143     }
144   target_structs[target_struct_size++] = t;
145   cleanup_target (t);
146
147   if (targetlist == NULL)
148     add_prefix_cmd ("target", class_run, target_command,
149                     "Connect to a target machine or process.\n\
150 The first argument is the type or protocol of the target machine.\n\
151 Remaining arguments are interpreted by the target protocol.  For more\n\
152 information on the arguments for a particular protocol, type\n\
153 `help target ' followed by the protocol name.",
154                     &targetlist, "target ", 0, &cmdlist);
155   add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
156 }
157
158 /* Stub functions */
159
160 static void
161 ignore ()
162 {
163 }
164
165 /* ARGSUSED */
166 static int
167 nomemory (memaddr, myaddr, len, write)
168      CORE_ADDR memaddr;
169      char *myaddr;
170      int len;
171      int write;
172 {
173   errno = EIO;          /* Can't read/write this location */
174   return 0;             /* No bytes handled */
175 }
176
177 static void
178 tcomplain ()
179 {
180   error ("You can't do that when your target is `%s'",
181          current_target->to_shortname);
182 }
183
184 void
185 noprocess ()
186 {
187   error ("You can't do that without a process to debug");
188 }
189
190 /* ARGSUSED */
191 static int
192 nosymbol (name, addrp)
193      char *name;
194      CORE_ADDR *addrp;
195 {
196   return 1;             /* Symbol does not exist in target env */
197 }
198
199 /* ARGSUSED */
200 static void
201 default_terminal_info (args, from_tty)
202      char *args;
203      int from_tty;
204 {
205   printf_filtered("No saved terminal information.\n");
206 }
207
208 #if 0
209 /* With strata, this function is no longer needed.  FIXME.  */
210 /* This is the default target_create_inferior function.  It looks up
211    the stack for some target that cares to create inferiors, then
212    calls it -- or complains if not found.  */
213
214 static void
215 upstack_create_inferior (exec, args, env)
216      char *exec;
217      char *args;
218      char **env;
219 {
220   struct target_ops *t;
221
222   for (t = current_target;
223        t;
224        t = t->to_next)
225     {
226       if (t->to_create_inferior != upstack_create_inferior)
227         {
228           t->to_create_inferior (exec, args, env);
229           return;
230         }
231
232     }
233   tcomplain();
234 }
235 #endif
236
237 /* This is the default target_create_inferior and target_attach function.
238    If the current target is executing, it asks whether to kill it off.
239    If this function returns without calling error(), it has killed off
240    the target, and the operation should be attempted.  */
241
242 static void
243 kill_or_be_killed (from_tty)
244      int from_tty;
245 {
246   if (target_has_execution)
247     {
248       printf_filtered ("You are already running a program:\n");
249       target_files_info ();
250       if (query ("Kill it? ")) {
251         target_kill ();
252         if (target_has_execution)
253           error ("Killing the program did not help.");
254         return;
255       } else {
256         error ("Program not killed.");
257       }
258     }
259   tcomplain();
260 }
261
262 static void
263 maybe_kill_then_attach (args, from_tty)
264      char *args;
265      int from_tty;
266 {
267   kill_or_be_killed (from_tty);
268   target_attach (args, from_tty);
269 }
270
271 static void
272 maybe_kill_then_create_inferior (exec, args, env)
273      char *exec;
274      char *args;
275      char **env;
276 {
277   kill_or_be_killed (0);
278   target_create_inferior (exec, args, env);
279 }
280
281 /* Clean up a target struct so it no longer has any zero pointers in it.
282    We default entries, at least to stubs that print error messages.  */
283
284 static void
285 cleanup_target (t)
286      struct target_ops *t;
287 {
288
289   /* Check magic number.  If wrong, it probably means someone changed
290      the struct definition, but not all the places that initialize one.  */
291   if (t->to_magic != OPS_MAGIC)
292     {
293       fprintf_filtered(stderr, "Magic number of %s target struct wrong\n", 
294         t->to_shortname);
295       abort();
296     }
297
298 #define de_fault(field, value) \
299   if (!t->field)        t->field = value
300
301   /*        FIELD                       DEFAULT VALUE        */
302
303   de_fault (to_open,                    (void (*)())tcomplain);
304   de_fault (to_close,                   (void (*)())ignore);
305   de_fault (to_attach,                  maybe_kill_then_attach);
306   de_fault (to_detach,                  (void (*)())ignore);
307   de_fault (to_resume,                  (void (*)())noprocess);
308   de_fault (to_wait,                    (int (*)())noprocess);
309   de_fault (to_fetch_registers,         (void (*)())ignore);
310   de_fault (to_store_registers,         (void (*)())noprocess);
311   de_fault (to_prepare_to_store,        (void (*)())noprocess);
312   de_fault (to_convert_to_virtual,      host_convert_to_virtual);
313   de_fault (to_convert_from_virtual,    host_convert_from_virtual);
314   de_fault (to_xfer_memory,             (int (*)())nomemory);
315   de_fault (to_files_info,              (void (*)())ignore);
316   de_fault (to_insert_breakpoint,       memory_insert_breakpoint);
317   de_fault (to_remove_breakpoint,       memory_remove_breakpoint);
318   de_fault (to_terminal_init,           ignore);
319   de_fault (to_terminal_inferior,       ignore);
320   de_fault (to_terminal_ours_for_output,ignore);
321   de_fault (to_terminal_ours,           ignore);
322   de_fault (to_terminal_info,           default_terminal_info);
323   de_fault (to_kill,                    (void (*)())noprocess);
324   de_fault (to_load,                    (void (*)())tcomplain);
325   de_fault (to_lookup_symbol,           nosymbol);
326   de_fault (to_create_inferior,         maybe_kill_then_create_inferior);
327   de_fault (to_mourn_inferior,          (void (*)())noprocess);
328   de_fault (to_next,                    0);
329   de_fault (to_has_all_memory,          0);
330   de_fault (to_has_memory,              0);
331   de_fault (to_has_stack,               0);
332   de_fault (to_has_registers,           0);
333   de_fault (to_has_execution,           0);
334
335 #undef de_fault
336 }
337
338 /* Push a new target type into the stack of the existing target accessors,
339    possibly superseding some of the existing accessors.
340
341    Result is zero if the pushed target ended up on top of the stack,
342    nonzero if at least one target is on top of it.
343
344    Rather than allow an empty stack, we always have the dummy target at
345    the bottom stratum, so we can call the function vectors without
346    checking them.  */
347
348 int
349 push_target (t)
350      struct target_ops *t;
351 {
352   struct target_ops *st, *prev;
353
354   for (prev = 0, st = current_target;
355        st;
356        prev = st, st = st->to_next) {
357     if ((int)(t->to_stratum) >= (int)(st->to_stratum))
358       break;
359   }
360
361   while (t->to_stratum == st->to_stratum) {
362     /* There's already something on this stratum.  Close it off.  */
363     (st->to_close) (0);
364     if (prev)
365       prev->to_next = st->to_next;      /* Unchain old target_ops */
366     else
367       current_target = st->to_next;     /* Unchain first on list */
368     st = st->to_next;
369   }
370
371   /* We have removed all targets in our stratum, now add ourself.  */
372   t->to_next = st;
373   if (prev)
374     prev->to_next = t;
375   else
376     current_target = t;
377
378   cleanup_target (current_target);
379   return prev != 0;
380 }
381
382 /* Remove a target_ops vector from the stack, wherever it may be. 
383    Return how many times it was removed (0 or 1 unless bug).  */
384
385 int
386 unpush_target (t)
387      struct target_ops *t;
388 {
389   struct target_ops *u, *v;
390   int result = 0;
391
392   for (u = current_target, v = 0;
393        u;
394        v = u, u = u->to_next)
395     if (u == t)
396       {
397         if (v == 0)
398           pop_target();                 /* unchain top copy */
399         else {
400           (t->to_close)(0);             /* Let it clean up */
401           v->to_next = t->to_next;      /* unchain middle copy */
402         }
403         result++;
404       }
405   return result;
406 }
407
408 void
409 pop_target ()
410 {
411   (current_target->to_close)(0);        /* Let it clean up */
412   current_target = current_target->to_next;
413   if (!current_target)          /* At bottom, push dummy.  */
414     push_target (&dummy_target);
415 }
416
417 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
418
419 /* target_read_string -- read a null terminated string from MEMADDR in target.
420    The read may also be terminated early by getting an error from target_xfer_
421    memory.
422    LEN is the size of the buffer pointed to by MYADDR.  Note that a terminating
423    null will only be written if there is sufficient room.  The return value is
424    is the number of bytes (including the null) actually transferred.
425 */
426
427 int
428 target_read_string (memaddr, myaddr, len)
429      CORE_ADDR memaddr;
430      char *myaddr;
431      int len;
432 {
433   int tlen, origlen, offset, i;
434   char buf[4];
435
436   origlen = len;
437
438   while (len > 0)
439     {
440       tlen = MIN (len, 4 - (memaddr & 3));
441       offset = memaddr & 3;
442
443       if (target_xfer_memory (memaddr & ~3, buf, 4, 0))
444         return origlen - len;
445
446       for (i = 0; i < tlen; i++)
447         {
448           *myaddr++ = buf[i + offset];
449           if (buf[i + offset] == '\000')
450             return (origlen - len) + i + 1;
451         }
452
453       memaddr += tlen;
454       len -= tlen;
455     }
456   return origlen;
457 }
458
459 /* Move memory to or from the targets.  Iterate until all of it has
460    been moved, if necessary.  The top target gets priority; anything
461    it doesn't want, is offered to the next one down, etc.  Note the
462    business with curlen:  if an early target says "no, but I have a
463    boundary overlapping this xfer" then we shorten what we offer to
464    the subsequent targets so the early guy will get a chance at the
465    tail before the subsequent ones do. 
466
467    Result is 0 or errno value.  */
468
469 int
470 target_read_memory (memaddr, myaddr, len)
471      CORE_ADDR memaddr;
472      char *myaddr;
473      int len;
474 {
475   return target_xfer_memory (memaddr, myaddr, len, 0);
476 }
477
478 int
479 target_write_memory (memaddr, myaddr, len)
480      CORE_ADDR memaddr;
481      char *myaddr;
482      int len;
483 {
484   return target_xfer_memory (memaddr, myaddr, len, 1);
485 }
486  
487 int
488 target_xfer_memory (memaddr, myaddr, len, write)
489      CORE_ADDR memaddr;
490      char *myaddr;
491      int len;
492      int write;
493 {
494   int curlen;
495   int res;
496   struct target_ops *t;
497   
498   /* The quick case is that the top target does it all.  */
499   res = current_target->to_xfer_memory
500                         (memaddr, myaddr, len, write, current_target);
501   if (res == len)
502     return 0;
503
504   if (res > 0)
505     goto bump;
506   /* If res <= 0 then we call it again in the loop.  Ah well.  */
507
508   for (; len > 0;)
509     {
510       curlen = len;             /* Want to do it all */
511       for (t = current_target;
512            t;
513            t = t->to_has_all_memory? 0: t->to_next)
514         {
515           res = t->to_xfer_memory(memaddr, myaddr, curlen, write, t);
516           if (res > 0) break;   /* Handled all or part of xfer */
517           if (res == 0) continue;       /* Handled none */
518           curlen = -res;        /* Could handle once we get past res bytes */
519         }
520       if (res <= 0)
521         {
522           /* If this address is for nonexistent memory,
523              read zeros if reading, or do nothing if writing.  Return error. */
524           if (!write)
525             memset (myaddr, 0, len);
526           if (errno == 0)
527             return EIO;
528           else
529             return errno;
530         }
531 bump:
532       memaddr += res;
533       myaddr  += res;
534       len     -= res;
535     }
536   return 0;                     /* We managed to cover it all somehow. */
537 }
538
539
540 /* ARGSUSED */
541 static void
542 target_info (args, from_tty)
543      char *args;
544      int from_tty;
545 {
546   struct target_ops *t;
547   int has_all_mem = 0;
548   
549   if (symfile_objfile != NULL)
550     printf_filtered ("Symbols from \"%s\".\n", symfile_objfile->name);
551
552 #ifdef FILES_INFO_HOOK
553   if (FILES_INFO_HOOK ())
554     return;
555 #endif
556
557   for (t = current_target;
558        t;
559        t = t->to_next)
560     {
561       if ((int)(t->to_stratum) <= (int)dummy_stratum)
562         continue;
563       if (has_all_mem)
564         printf_filtered("\tWhile running this, gdb does not access memory from...\n");
565       printf_filtered("%s:\n", t->to_longname);
566       (t->to_files_info)(t);
567       has_all_mem = t->to_has_all_memory;
568     }
569 }
570
571 /* This is to be called by the open routine before it does
572    anything.  */
573
574 void
575 target_preopen (from_tty)
576      int from_tty;
577 {
578   dont_repeat();
579
580   if (target_has_execution)
581     {   
582       if (query ("A program is being debugged already.  Kill it? "))
583         target_kill ();
584       else
585         error ("Program not killed.");
586     }
587 }
588
589 static char targ_desc[] = 
590     "Names of targets and files being debugged.\n\
591 Shows the entire stack of targets currently in use (including the exec-file,\n\
592 core-file, and process, if any), as well as the symbol file name.";
593
594 void
595 _initialize_targets ()
596 {
597   current_target = &dummy_target;
598   cleanup_target (current_target);
599
600   add_info ("target", target_info, targ_desc);
601   add_info ("files", target_info, targ_desc);
602 }