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