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