Keep COPYING.NEWLIB if keep-newlib.
[deliverable/binutils-gdb.git] / gdb / target.c
1 /* Select target systems and architectures at runtime for GDB.
2 Copyright 1990, 1992, 1993, 1994 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 #include "wait.h"
32 #include <signal.h>
33
34 extern int errno;
35
36 static void
37 target_info PARAMS ((char *, int));
38
39 static void
40 cleanup_target PARAMS ((struct target_ops *));
41
42 static void
43 maybe_kill_then_create_inferior PARAMS ((char *, char *, char **));
44
45 static void
46 maybe_kill_then_attach PARAMS ((char *, int));
47
48 static void
49 kill_or_be_killed PARAMS ((int));
50
51 static void
52 default_terminal_info PARAMS ((char *, int));
53
54 static int
55 nosymbol PARAMS ((char *, CORE_ADDR *));
56
57 static void
58 tcomplain PARAMS ((void));
59
60 static int
61 nomemory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
62
63 static int
64 return_zero PARAMS ((void));
65
66 static void
67 ignore PARAMS ((void));
68
69 static void
70 target_command PARAMS ((char *, int));
71
72 static struct target_ops *
73 find_default_run_target PARAMS ((char *));
74
75 /* Pointer to array of target architecture structures; the size of the
76 array; the current index into the array; the allocated size of the
77 array. */
78 struct target_ops **target_structs;
79 unsigned target_struct_size;
80 unsigned target_struct_index;
81 unsigned target_struct_allocsize;
82 #define DEFAULT_ALLOCSIZE 10
83
84 /* The initial current target, so that there is always a semi-valid
85 current target. */
86
87 struct target_ops dummy_target = {"None", "None", "",
88 0, 0, /* open, close */
89 find_default_attach, 0, /* attach, detach */
90 0, 0, /* resume, wait */
91 0, 0, 0, /* registers */
92 0, 0, /* memory */
93 0, 0, /* bkpts */
94 0, 0, 0, 0, 0, /* terminal */
95 0, 0, /* kill, load */
96 0, /* lookup_symbol */
97 find_default_create_inferior, /* create_inferior */
98 0, /* mourn_inferior */
99 0, /* can_run */
100 0, /* notice_signals */
101 dummy_stratum, 0, /* stratum, next */
102 0, 0, 0, 0, 0, /* all mem, mem, stack, regs, exec */
103 0, 0, /* section pointers */
104 OPS_MAGIC,
105 };
106
107 /* Top of target stack. */
108
109 struct target_stack_item *target_stack;
110
111 /* The target structure we are currently using to talk to a process
112 or file or whatever "inferior" we have. */
113
114 struct target_ops current_target;
115
116 /* Command list for target. */
117
118 static struct cmd_list_element *targetlist = NULL;
119
120 /* Nonzero if we are debugging an attached outside process
121 rather than an inferior. */
122
123 int attach_flag;
124
125 /* The user just typed 'target' without the name of a target. */
126
127 /* ARGSUSED */
128 static void
129 target_command (arg, from_tty)
130 char *arg;
131 int from_tty;
132 {
133 fputs_filtered ("Argument required (target name). Try `help target'\n",
134 gdb_stdout);
135 }
136
137 /* Add a possible target architecture to the list. */
138
139 void
140 add_target (t)
141 struct target_ops *t;
142 {
143 if (!target_structs)
144 {
145 target_struct_allocsize = DEFAULT_ALLOCSIZE;
146 target_structs = (struct target_ops **) xmalloc
147 (target_struct_allocsize * sizeof (*target_structs));
148 }
149 if (target_struct_size >= target_struct_allocsize)
150 {
151 target_struct_allocsize *= 2;
152 target_structs = (struct target_ops **)
153 xrealloc ((char *) target_structs,
154 target_struct_allocsize * sizeof (*target_structs));
155 }
156 target_structs[target_struct_size++] = t;
157 /* cleanup_target (t);*/
158
159 if (targetlist == NULL)
160 add_prefix_cmd ("target", class_run, target_command,
161 "Connect to a target machine or process.\n\
162 The first argument is the type or protocol of the target machine.\n\
163 Remaining arguments are interpreted by the target protocol. For more\n\
164 information on the arguments for a particular protocol, type\n\
165 `help target ' followed by the protocol name.",
166 &targetlist, "target ", 0, &cmdlist);
167 add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
168 }
169
170 /* Stub functions */
171
172 static void
173 ignore ()
174 {
175 }
176
177 /* ARGSUSED */
178 static int
179 nomemory (memaddr, myaddr, len, write, t)
180 CORE_ADDR memaddr;
181 char *myaddr;
182 int len;
183 int write;
184 struct target_ops *t;
185 {
186 errno = EIO; /* Can't read/write this location */
187 return 0; /* No bytes handled */
188 }
189
190 static void
191 tcomplain ()
192 {
193 error ("You can't do that when your target is `%s'",
194 current_target.to_shortname);
195 }
196
197 void
198 noprocess ()
199 {
200 error ("You can't do that without a process to debug");
201 }
202
203 /* ARGSUSED */
204 static int
205 nosymbol (name, addrp)
206 char *name;
207 CORE_ADDR *addrp;
208 {
209 return 1; /* Symbol does not exist in target env */
210 }
211
212 /* ARGSUSED */
213 static void
214 default_terminal_info (args, from_tty)
215 char *args;
216 int from_tty;
217 {
218 printf_unfiltered("No saved terminal information.\n");
219 }
220
221 /* This is the default target_create_inferior and target_attach function.
222 If the current target is executing, it asks whether to kill it off.
223 If this function returns without calling error(), it has killed off
224 the target, and the operation should be attempted. */
225
226 static void
227 kill_or_be_killed (from_tty)
228 int from_tty;
229 {
230 if (target_has_execution)
231 {
232 printf_unfiltered ("You are already running a program:\n");
233 target_files_info ();
234 if (query ("Kill it? ")) {
235 target_kill ();
236 if (target_has_execution)
237 error ("Killing the program did not help.");
238 return;
239 } else {
240 error ("Program not killed.");
241 }
242 }
243 tcomplain();
244 }
245
246 static void
247 maybe_kill_then_attach (args, from_tty)
248 char *args;
249 int from_tty;
250 {
251 kill_or_be_killed (from_tty);
252 target_attach (args, from_tty);
253 }
254
255 static void
256 maybe_kill_then_create_inferior (exec, args, env)
257 char *exec;
258 char *args;
259 char **env;
260 {
261 kill_or_be_killed (0);
262 target_create_inferior (exec, args, env);
263 }
264
265 /* Clean up a target struct so it no longer has any zero pointers in it.
266 We default entries, at least to stubs that print error messages. */
267
268 static void
269 cleanup_target (t)
270 struct target_ops *t;
271 {
272
273 #define de_fault(field, value) \
274 if (!t->field) t->field = value
275
276 /* FIELD DEFAULT VALUE */
277
278 de_fault (to_open, (void (*)())tcomplain);
279 de_fault (to_close, (void (*)())ignore);
280 de_fault (to_attach, maybe_kill_then_attach);
281 de_fault (to_detach, (void (*)())ignore);
282 de_fault (to_resume, (void (*)())noprocess);
283 de_fault (to_wait, (int (*)())noprocess);
284 de_fault (to_fetch_registers, (void (*)())ignore);
285 de_fault (to_store_registers, (void (*)())noprocess);
286 de_fault (to_prepare_to_store, (void (*)())noprocess);
287 de_fault (to_xfer_memory, (int (*)())nomemory);
288 de_fault (to_files_info, (void (*)())ignore);
289 de_fault (to_insert_breakpoint, memory_insert_breakpoint);
290 de_fault (to_remove_breakpoint, memory_remove_breakpoint);
291 de_fault (to_terminal_init, ignore);
292 de_fault (to_terminal_inferior, ignore);
293 de_fault (to_terminal_ours_for_output,ignore);
294 de_fault (to_terminal_ours, ignore);
295 de_fault (to_terminal_info, default_terminal_info);
296 de_fault (to_kill, (void (*)())noprocess);
297 de_fault (to_load, (void (*)())tcomplain);
298 de_fault (to_lookup_symbol, nosymbol);
299 de_fault (to_create_inferior, maybe_kill_then_create_inferior);
300 de_fault (to_mourn_inferior, (void (*)())noprocess);
301 de_fault (to_can_run, return_zero);
302 de_fault (to_notice_signals, (void (*)())ignore);
303
304 #undef de_fault
305 }
306
307 /* Go through the target stack from top to bottom, copying over zero entries in
308 current_target. In effect, we are doing class inheritance through the
309 pushed target vectors. */
310
311 static void
312 update_current_target ()
313 {
314 struct target_stack_item *item;
315 struct target_ops *t;
316
317 /* First, reset current_target */
318 memset (&current_target, 0, sizeof current_target);
319
320 for (item = target_stack; item; item = item->next)
321 {
322 t = item->target_ops;
323
324 #define INHERIT(FIELD, TARGET) \
325 if (!current_target.FIELD) \
326 current_target.FIELD = TARGET->FIELD
327
328 INHERIT (to_shortname, t);
329 INHERIT (to_longname, t);
330 INHERIT (to_doc, t);
331 INHERIT (to_open, t);
332 INHERIT (to_close, t);
333 INHERIT (to_attach, t);
334 INHERIT (to_detach, t);
335 INHERIT (to_resume, t);
336 INHERIT (to_wait, t);
337 INHERIT (to_fetch_registers, t);
338 INHERIT (to_store_registers, t);
339 INHERIT (to_prepare_to_store, t);
340 INHERIT (to_xfer_memory, t);
341 INHERIT (to_files_info, t);
342 INHERIT (to_insert_breakpoint, t);
343 INHERIT (to_remove_breakpoint, t);
344 INHERIT (to_terminal_init, t);
345 INHERIT (to_terminal_inferior, t);
346 INHERIT (to_terminal_ours_for_output, t);
347 INHERIT (to_terminal_ours, t);
348 INHERIT (to_terminal_info, t);
349 INHERIT (to_kill, t);
350 INHERIT (to_load, t);
351 INHERIT (to_lookup_symbol, t);
352 INHERIT (to_create_inferior, t);
353 INHERIT (to_mourn_inferior, t);
354 INHERIT (to_can_run, t);
355 INHERIT (to_notice_signals, t);
356 INHERIT (to_stratum, t);
357 INHERIT (DONT_USE, t);
358 INHERIT (to_has_all_memory, t);
359 INHERIT (to_has_memory, t);
360 INHERIT (to_has_stack, t);
361 INHERIT (to_has_registers, t);
362 INHERIT (to_has_execution, t);
363 INHERIT (to_sections, t);
364 INHERIT (to_sections_end, t);
365 INHERIT (to_magic, t);
366
367 #undef INHERIT
368 }
369 }
370
371 /* Push a new target type into the stack of the existing target accessors,
372 possibly superseding some of the existing accessors.
373
374 Result is zero if the pushed target ended up on top of the stack,
375 nonzero if at least one target is on top of it.
376
377 Rather than allow an empty stack, we always have the dummy target at
378 the bottom stratum, so we can call the function vectors without
379 checking them. */
380
381 int
382 push_target (t)
383 struct target_ops *t;
384 {
385 struct target_stack_item *cur, *prev, *tmp;
386
387 /* Check magic number. If wrong, it probably means someone changed
388 the struct definition, but not all the places that initialize one. */
389 if (t->to_magic != OPS_MAGIC)
390 {
391 fprintf_unfiltered(gdb_stderr,
392 "Magic number of %s target struct wrong\n",
393 t->to_shortname);
394 abort();
395 }
396
397 /* Find the proper stratum to install this target in. */
398
399 for (prev = NULL, cur = target_stack; cur; prev = cur, cur = cur->next)
400 {
401 if ((int)(t->to_stratum) >= (int)(cur->target_ops->to_stratum))
402 break;
403 }
404
405 /* If there's already targets at this stratum, remove them. */
406
407 if (cur)
408 while (t->to_stratum == cur->target_ops->to_stratum)
409 {
410 /* There's already something on this stratum. Close it off. */
411 (cur->target_ops->to_close) (0);
412 if (prev)
413 prev->next = cur->next; /* Unchain old target_ops */
414 else
415 target_stack = cur->next; /* Unchain first on list */
416 tmp = cur->next;
417 free (cur);
418 cur = tmp;
419 }
420
421 /* We have removed all targets in our stratum, now add the new one. */
422
423 tmp = (struct target_stack_item *)
424 xmalloc (sizeof (struct target_stack_item));
425 tmp->next = cur;
426 tmp->target_ops = t;
427
428 if (prev)
429 prev->next = tmp;
430 else
431 target_stack = tmp;
432
433 update_current_target ();
434
435 cleanup_target (&current_target); /* Fill in the gaps */
436 return prev != 0;
437 }
438
439 /* Remove a target_ops vector from the stack, wherever it may be.
440 Return how many times it was removed (0 or 1). */
441
442 int
443 unpush_target (t)
444 struct target_ops *t;
445 {
446 struct target_stack_item *cur, *prev;
447
448 if (t->to_close)
449 t->to_close (0); /* Let it clean up */
450
451 /* Look for the specified target. Note that we assume that a target
452 can only occur once in the target stack. */
453
454 for (cur = target_stack, prev = NULL; cur; prev = cur, cur = cur->next)
455 if (cur->target_ops == t)
456 break;
457
458 if (!cur)
459 return 0; /* Didn't find target_ops, quit now */
460
461 /* Unchain the target */
462
463 if (!prev)
464 target_stack = cur->next;
465 else
466 prev->next = cur->next;
467
468 free (cur); /* Release the target_stack_item */
469
470 update_current_target ();
471 cleanup_target (&current_target);
472
473 return 1;
474 }
475
476 void
477 pop_target ()
478 {
479 (current_target.to_close)(0); /* Let it clean up */
480 if (unpush_target (target_stack->target_ops) == 1)
481 return;
482
483 fprintf_unfiltered(gdb_stderr,
484 "pop_target couldn't find target %s\n",
485 current_target.to_shortname);
486 abort();
487 }
488
489 #undef MIN
490 #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
491
492 /* target_read_string -- read a null terminated string, up to LEN bytes,
493 from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
494 Set *STRING to a pointer to malloc'd memory containing the data; the caller
495 is responsible for freeing it. Return the number of bytes successfully
496 read. */
497
498 int
499 target_read_string (memaddr, string, len, errnop)
500 CORE_ADDR memaddr;
501 char **string;
502 int len;
503 int *errnop;
504 {
505 int tlen, origlen, offset, i;
506 char buf[4];
507 int errcode = 0;
508 char *buffer;
509 int buffer_allocated;
510 char *bufptr;
511 unsigned int nbytes_read = 0;
512
513 /* Small for testing. */
514 buffer_allocated = 4;
515 buffer = xmalloc (buffer_allocated);
516 bufptr = buffer;
517
518 origlen = len;
519
520 while (len > 0)
521 {
522 tlen = MIN (len, 4 - (memaddr & 3));
523 offset = memaddr & 3;
524
525 errcode = target_xfer_memory (memaddr & ~3, buf, 4, 0);
526 if (errcode != 0)
527 goto done;
528
529 if (bufptr - buffer + tlen > buffer_allocated)
530 {
531 unsigned int bytes;
532 bytes = bufptr - buffer;
533 buffer_allocated *= 2;
534 buffer = xrealloc (buffer, buffer_allocated);
535 bufptr = buffer + bytes;
536 }
537
538 for (i = 0; i < tlen; i++)
539 {
540 *bufptr++ = buf[i + offset];
541 if (buf[i + offset] == '\000')
542 {
543 nbytes_read += i + 1;
544 goto done;
545 }
546 }
547
548 memaddr += tlen;
549 len -= tlen;
550 nbytes_read += tlen;
551 }
552 done:
553 if (errnop != NULL)
554 *errnop = errcode;
555 if (string != NULL)
556 *string = buffer;
557 return nbytes_read;
558 }
559
560 /* Read LEN bytes of target memory at address MEMADDR, placing the results in
561 GDB's memory at MYADDR. Returns either 0 for success or an errno value
562 if any error occurs.
563
564 If an error occurs, no guarantee is made about the contents of the data at
565 MYADDR. In particular, the caller should not depend upon partial reads
566 filling the buffer with good data. There is no way for the caller to know
567 how much good data might have been transfered anyway. Callers that can
568 deal with partial reads should call target_read_memory_partial. */
569
570 int
571 target_read_memory (memaddr, myaddr, len)
572 CORE_ADDR memaddr;
573 char *myaddr;
574 int len;
575 {
576 return target_xfer_memory (memaddr, myaddr, len, 0);
577 }
578
579 /* Read LEN bytes of target memory at address MEMADDR, placing the results
580 in GDB's memory at MYADDR. Returns a count of the bytes actually read,
581 and optionally an errno value in the location pointed to by ERRNOPTR
582 if ERRNOPTR is non-null. */
583
584 int
585 target_read_memory_partial (memaddr, myaddr, len, errnoptr)
586 CORE_ADDR memaddr;
587 char *myaddr;
588 int len;
589 int *errnoptr;
590 {
591 int nread; /* Number of bytes actually read. */
592 int errcode; /* Error from last read. */
593
594 /* First try a complete read. */
595 errcode = target_xfer_memory (memaddr, myaddr, len, 0);
596 if (errcode == 0)
597 {
598 /* Got it all. */
599 nread = len;
600 }
601 else
602 {
603 /* Loop, reading one byte at a time until we get as much as we can. */
604 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
605 {
606 errcode = target_xfer_memory (memaddr++, myaddr++, 1, 0);
607 }
608 /* If an error, the last read was unsuccessful, so adjust count. */
609 if (errcode != 0)
610 {
611 nread--;
612 }
613 }
614 if (errnoptr != NULL)
615 {
616 *errnoptr = errcode;
617 }
618 return (nread);
619 }
620
621 int
622 target_write_memory (memaddr, myaddr, len)
623 CORE_ADDR memaddr;
624 char *myaddr;
625 int len;
626 {
627 return target_xfer_memory (memaddr, myaddr, len, 1);
628 }
629
630 /* Move memory to or from the targets. Iterate until all of it has
631 been moved, if necessary. The top target gets priority; anything
632 it doesn't want, is offered to the next one down, etc. Note the
633 business with curlen: if an early target says "no, but I have a
634 boundary overlapping this xfer" then we shorten what we offer to
635 the subsequent targets so the early guy will get a chance at the
636 tail before the subsequent ones do.
637
638 Result is 0 or errno value. */
639
640 int
641 target_xfer_memory (memaddr, myaddr, len, write)
642 CORE_ADDR memaddr;
643 char *myaddr;
644 int len;
645 int write;
646 {
647 int curlen;
648 int res;
649 struct target_ops *t;
650 struct target_stack_item *item;
651
652 /* to_xfer_memory is not guaranteed to set errno, even when it returns
653 0. */
654 errno = 0;
655
656 /* The quick case is that the top target does it all. */
657 res = current_target.to_xfer_memory
658 (memaddr, myaddr, len, write, &current_target);
659 if (res == len)
660 return 0;
661
662 if (res > 0)
663 goto bump;
664 /* If res <= 0 then we call it again in the loop. Ah well. */
665
666 for (; len > 0;)
667 {
668 curlen = len; /* Want to do it all */
669 for (item = target_stack; item; item = item->next)
670 {
671 t = item->target_ops;
672 if (!t->to_has_memory)
673 continue;
674
675 res = t->to_xfer_memory (memaddr, myaddr, curlen, write, t);
676 if (res > 0)
677 break; /* Handled all or part of xfer */
678 if (t->to_has_all_memory)
679 break;
680 }
681
682 if (res <= 0)
683 {
684 /* If this address is for nonexistent memory,
685 read zeros if reading, or do nothing if writing. Return error. */
686 if (!write)
687 memset (myaddr, 0, len);
688 if (errno == 0)
689 return EIO;
690 else
691 return errno;
692 }
693 bump:
694 memaddr += res;
695 myaddr += res;
696 len -= res;
697 }
698 return 0; /* We managed to cover it all somehow. */
699 }
700
701
702 /* ARGSUSED */
703 static void
704 target_info (args, from_tty)
705 char *args;
706 int from_tty;
707 {
708 struct target_ops *t;
709 struct target_stack_item *item;
710 int has_all_mem = 0;
711
712 if (symfile_objfile != NULL)
713 printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
714
715 #ifdef FILES_INFO_HOOK
716 if (FILES_INFO_HOOK ())
717 return;
718 #endif
719
720 for (item = target_stack; item; item = item->next)
721 {
722 t = item->target_ops;
723
724 if (!t->to_has_memory)
725 continue;
726
727 if ((int)(t->to_stratum) <= (int)dummy_stratum)
728 continue;
729 if (has_all_mem)
730 printf_unfiltered("\tWhile running this, GDB does not access memory from...\n");
731 printf_unfiltered("%s:\n", t->to_longname);
732 (t->to_files_info)(t);
733 has_all_mem = t->to_has_all_memory;
734 }
735 }
736
737 /* This is to be called by the open routine before it does
738 anything. */
739
740 void
741 target_preopen (from_tty)
742 int from_tty;
743 {
744 dont_repeat();
745
746 if (target_has_execution)
747 {
748 if (query ("A program is being debugged already. Kill it? "))
749 target_kill ();
750 else
751 error ("Program not killed.");
752 }
753
754 /* Calling target_kill may remove the target from the stack. But if
755 it doesn't (which seems like a win for UDI), remove it now. */
756
757 if (target_has_execution)
758 pop_target ();
759 }
760
761 /* Detach a target after doing deferred register stores. */
762
763 void
764 target_detach (args, from_tty)
765 char *args;
766 int from_tty;
767 {
768 /* Handle any optimized stores to the inferior. */
769 #ifdef DO_DEFERRED_STORES
770 DO_DEFERRED_STORES;
771 #endif
772 (current_target.to_detach) (args, from_tty);
773 }
774
775 void
776 target_link (modname, t_reloc)
777 char *modname;
778 CORE_ADDR *t_reloc;
779 {
780 if (STREQ(current_target.to_shortname, "rombug"))
781 {
782 (current_target.to_lookup_symbol) (modname, t_reloc);
783 if (*t_reloc == 0)
784 error("Unable to link to %s and get relocation in rombug", modname);
785 }
786 else
787 *t_reloc = (CORE_ADDR)-1;
788 }
789
790 /* Look through the list of possible targets for a target that can
791 execute a run or attach command without any other data. This is
792 used to locate the default process stratum.
793
794 Result is always valid (error() is called for errors). */
795
796 static struct target_ops *
797 find_default_run_target (do_mesg)
798 char *do_mesg;
799 {
800 struct target_ops **t;
801 struct target_ops *runable = NULL;
802 int count;
803
804 count = 0;
805
806 for (t = target_structs; t < target_structs + target_struct_size;
807 ++t)
808 {
809 if (target_can_run(*t))
810 {
811 runable = *t;
812 ++count;
813 }
814 }
815
816 if (count != 1)
817 error ("Don't know how to %s. Try \"help target\".", do_mesg);
818
819 return runable;
820 }
821
822 void
823 find_default_attach (args, from_tty)
824 char *args;
825 int from_tty;
826 {
827 struct target_ops *t;
828
829 t = find_default_run_target("attach");
830 (t->to_attach) (args, from_tty);
831 return;
832 }
833
834 void
835 find_default_create_inferior (exec_file, allargs, env)
836 char *exec_file;
837 char *allargs;
838 char **env;
839 {
840 struct target_ops *t;
841
842 t = find_default_run_target("run");
843 (t->to_create_inferior) (exec_file, allargs, env);
844 return;
845 }
846
847 static int
848 return_zero ()
849 {
850 return 0;
851 }
852
853 struct target_ops *
854 find_core_target ()
855 {
856 struct target_ops **t;
857 struct target_ops *runable = NULL;
858 int count;
859
860 count = 0;
861
862 for (t = target_structs; t < target_structs + target_struct_size;
863 ++t)
864 {
865 if ((*t)->to_stratum == core_stratum)
866 {
867 runable = *t;
868 ++count;
869 }
870 }
871
872 return(count == 1 ? runable : NULL);
873 }
874 \f
875 /* The inferior process has died. Long live the inferior! */
876
877 void
878 generic_mourn_inferior ()
879 {
880 extern int show_breakpoint_hit_counts;
881
882 inferior_pid = 0;
883 attach_flag = 0;
884 breakpoint_init_inferior ();
885 registers_changed ();
886
887 #ifdef CLEAR_DEFERRED_STORES
888 /* Delete any pending stores to the inferior... */
889 CLEAR_DEFERRED_STORES;
890 #endif
891
892 reopen_exec_file ();
893 reinit_frame_cache ();
894
895 /* It is confusing to the user for ignore counts to stick around
896 from previous runs of the inferior. So clear them. */
897 /* However, it is more confusing for the ignore counts to disappear when
898 using hit counts. So don't clear them if we're counting hits. */
899 if (!show_breakpoint_hit_counts)
900 breakpoint_clear_ignore_counts ();
901 }
902 \f
903 /* This table must match in order and size the signals in enum target_signal
904 in target.h. */
905 static struct {
906 char *name;
907 char *string;
908 } signals [] =
909 {
910 {"0", "Signal 0"},
911 {"SIGHUP", "Hangup"},
912 {"SIGINT", "Interrupt"},
913 {"SIGQUIT", "Quit"},
914 {"SIGILL", "Illegal instruction"},
915 {"SIGTRAP", "Trace/breakpoint trap"},
916 {"SIGABRT", "Aborted"},
917 {"SIGEMT", "Emulation trap"},
918 {"SIGFPE", "Arithmetic exception"},
919 {"SIGKILL", "Killed"},
920 {"SIGBUS", "Bus error"},
921 {"SIGSEGV", "Segmentation fault"},
922 {"SIGSYS", "Bad system call"},
923 {"SIGPIPE", "Broken pipe"},
924 {"SIGALRM", "Alarm clock"},
925 {"SIGTERM", "Terminated"},
926 {"SIGURG", "Urgent I/O condition"},
927 {"SIGSTOP", "Stopped (signal)"},
928 {"SIGTSTP", "Stopped (user)"},
929 {"SIGCONT", "Continued"},
930 {"SIGCHLD", "Child status changed"},
931 {"SIGTTIN", "Stopped (tty input)"},
932 {"SIGTTOU", "Stopped (tty output)"},
933 {"SIGIO", "I/O possible"},
934 {"SIGXCPU", "CPU time limit exceeded"},
935 {"SIGXFSZ", "File size limit exceeded"},
936 {"SIGVTALRM", "Virtual timer expired"},
937 {"SIGPROF", "Profiling timer expired"},
938 {"SIGWINCH", "Window size changed"},
939 {"SIGLOST", "Resource lost"},
940 {"SIGUSR1", "User defined signal 1"},
941 {"SIGUSR2", "User defined signal 2"},
942 {"SIGPWR", "Power fail/restart"},
943 {"SIGPOLL", "Pollable event occurred"},
944 {"SIGWIND", "SIGWIND"},
945 {"SIGPHONE", "SIGPHONE"},
946 {"SIGWAITING", "Process's LWPs are blocked"},
947 {"SIGLWP", "Signal LWP"},
948 {"SIGDANGER", "Swap space dangerously low"},
949 {"SIGGRANT", "Monitor mode granted"},
950 {"SIGRETRACT", "Need to relinguish monitor mode"},
951 {"SIGMSG", "Monitor mode data available"},
952 {"SIGSOUND", "Sound completed"},
953 {"SIGSAK", "Secure attention"},
954 {NULL, "Unknown signal"},
955 {NULL, "Internal error: printing TARGET_SIGNAL_DEFAULT"},
956
957 /* Last entry, used to check whether the table is the right size. */
958 {NULL, "TARGET_SIGNAL_MAGIC"}
959 };
960
961 /* Return the string for a signal. */
962 char *
963 target_signal_to_string (sig)
964 enum target_signal sig;
965 {
966 return signals[sig].string;
967 }
968
969 /* Return the name for a signal. */
970 char *
971 target_signal_to_name (sig)
972 enum target_signal sig;
973 {
974 if (sig == TARGET_SIGNAL_UNKNOWN)
975 /* I think the code which prints this will always print it along with
976 the string, so no need to be verbose. */
977 return "?";
978 return signals[sig].name;
979 }
980
981 /* Given a name, return its signal. */
982 enum target_signal
983 target_signal_from_name (name)
984 char *name;
985 {
986 enum target_signal sig;
987
988 /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
989 for TARGET_SIGNAL_SIGCHLD. SIGIOT, on the other hand, is more
990 questionable; seems like by now people should call it SIGABRT
991 instead. */
992
993 /* This ugly cast brought to you by the native VAX compiler. */
994 for (sig = TARGET_SIGNAL_HUP;
995 signals[sig].name != NULL;
996 sig = (enum target_signal)((int)sig + 1))
997 if (STREQ (name, signals[sig].name))
998 return sig;
999 return TARGET_SIGNAL_UNKNOWN;
1000 }
1001 \f
1002 /* The following functions are to help certain targets deal
1003 with the signal/waitstatus stuff. They could just as well be in
1004 a file called native-utils.c or unixwaitstatus-utils.c or whatever. */
1005
1006 /* Convert host signal to our signals. */
1007 enum target_signal
1008 target_signal_from_host (hostsig)
1009 int hostsig;
1010 {
1011 /* A switch statement would make sense but would require special kludges
1012 to deal with the cases where more than one signal has the same number. */
1013
1014 if (hostsig == 0) return TARGET_SIGNAL_0;
1015
1016 #if defined (SIGHUP)
1017 if (hostsig == SIGHUP) return TARGET_SIGNAL_HUP;
1018 #endif
1019 #if defined (SIGINT)
1020 if (hostsig == SIGINT) return TARGET_SIGNAL_INT;
1021 #endif
1022 #if defined (SIGQUIT)
1023 if (hostsig == SIGQUIT) return TARGET_SIGNAL_QUIT;
1024 #endif
1025 #if defined (SIGILL)
1026 if (hostsig == SIGILL) return TARGET_SIGNAL_ILL;
1027 #endif
1028 #if defined (SIGTRAP)
1029 if (hostsig == SIGTRAP) return TARGET_SIGNAL_TRAP;
1030 #endif
1031 #if defined (SIGABRT)
1032 if (hostsig == SIGABRT) return TARGET_SIGNAL_ABRT;
1033 #endif
1034 #if defined (SIGEMT)
1035 if (hostsig == SIGEMT) return TARGET_SIGNAL_EMT;
1036 #endif
1037 #if defined (SIGFPE)
1038 if (hostsig == SIGFPE) return TARGET_SIGNAL_FPE;
1039 #endif
1040 #if defined (SIGKILL)
1041 if (hostsig == SIGKILL) return TARGET_SIGNAL_KILL;
1042 #endif
1043 #if defined (SIGBUS)
1044 if (hostsig == SIGBUS) return TARGET_SIGNAL_BUS;
1045 #endif
1046 #if defined (SIGSEGV)
1047 if (hostsig == SIGSEGV) return TARGET_SIGNAL_SEGV;
1048 #endif
1049 #if defined (SIGSYS)
1050 if (hostsig == SIGSYS) return TARGET_SIGNAL_SYS;
1051 #endif
1052 #if defined (SIGPIPE)
1053 if (hostsig == SIGPIPE) return TARGET_SIGNAL_PIPE;
1054 #endif
1055 #if defined (SIGALRM)
1056 if (hostsig == SIGALRM) return TARGET_SIGNAL_ALRM;
1057 #endif
1058 #if defined (SIGTERM)
1059 if (hostsig == SIGTERM) return TARGET_SIGNAL_TERM;
1060 #endif
1061 #if defined (SIGUSR1)
1062 if (hostsig == SIGUSR1) return TARGET_SIGNAL_USR1;
1063 #endif
1064 #if defined (SIGUSR2)
1065 if (hostsig == SIGUSR2) return TARGET_SIGNAL_USR2;
1066 #endif
1067 #if defined (SIGCLD)
1068 if (hostsig == SIGCLD) return TARGET_SIGNAL_CHLD;
1069 #endif
1070 #if defined (SIGCHLD)
1071 if (hostsig == SIGCHLD) return TARGET_SIGNAL_CHLD;
1072 #endif
1073 #if defined (SIGPWR)
1074 if (hostsig == SIGPWR) return TARGET_SIGNAL_PWR;
1075 #endif
1076 #if defined (SIGWINCH)
1077 if (hostsig == SIGWINCH) return TARGET_SIGNAL_WINCH;
1078 #endif
1079 #if defined (SIGURG)
1080 if (hostsig == SIGURG) return TARGET_SIGNAL_URG;
1081 #endif
1082 #if defined (SIGIO)
1083 if (hostsig == SIGIO) return TARGET_SIGNAL_IO;
1084 #endif
1085 #if defined (SIGPOLL)
1086 if (hostsig == SIGPOLL) return TARGET_SIGNAL_POLL;
1087 #endif
1088 #if defined (SIGSTOP)
1089 if (hostsig == SIGSTOP) return TARGET_SIGNAL_STOP;
1090 #endif
1091 #if defined (SIGTSTP)
1092 if (hostsig == SIGTSTP) return TARGET_SIGNAL_TSTP;
1093 #endif
1094 #if defined (SIGCONT)
1095 if (hostsig == SIGCONT) return TARGET_SIGNAL_CONT;
1096 #endif
1097 #if defined (SIGTTIN)
1098 if (hostsig == SIGTTIN) return TARGET_SIGNAL_TTIN;
1099 #endif
1100 #if defined (SIGTTOU)
1101 if (hostsig == SIGTTOU) return TARGET_SIGNAL_TTOU;
1102 #endif
1103 #if defined (SIGVTALRM)
1104 if (hostsig == SIGVTALRM) return TARGET_SIGNAL_VTALRM;
1105 #endif
1106 #if defined (SIGPROF)
1107 if (hostsig == SIGPROF) return TARGET_SIGNAL_PROF;
1108 #endif
1109 #if defined (SIGXCPU)
1110 if (hostsig == SIGXCPU) return TARGET_SIGNAL_XCPU;
1111 #endif
1112 #if defined (SIGXFSZ)
1113 if (hostsig == SIGXFSZ) return TARGET_SIGNAL_XFSZ;
1114 #endif
1115 #if defined (SIGWIND)
1116 if (hostsig == SIGWIND) return TARGET_SIGNAL_WIND;
1117 #endif
1118 #if defined (SIGPHONE)
1119 if (hostsig == SIGPHONE) return TARGET_SIGNAL_PHONE;
1120 #endif
1121 #if defined (SIGLOST)
1122 if (hostsig == SIGLOST) return TARGET_SIGNAL_LOST;
1123 #endif
1124 #if defined (SIGWAITING)
1125 if (hostsig == SIGWAITING) return TARGET_SIGNAL_WAITING;
1126 #endif
1127 #if defined (SIGLWP)
1128 if (hostsig == SIGLWP) return TARGET_SIGNAL_LWP;
1129 #endif
1130 #if defined (SIGDANGER)
1131 if (hostsig == SIGDANGER) return TARGET_SIGNAL_DANGER;
1132 #endif
1133 #if defined (SIGGRANT)
1134 if (hostsig == SIGGRANT) return TARGET_SIGNAL_GRANT;
1135 #endif
1136 #if defined (SIGRETRACT)
1137 if (hostsig == SIGRETRACT) return TARGET_SIGNAL_RETRACT;
1138 #endif
1139 #if defined (SIGMSG)
1140 if (hostsig == SIGMSG) return TARGET_SIGNAL_MSG;
1141 #endif
1142 #if defined (SIGSOUND)
1143 if (hostsig == SIGSOUND) return TARGET_SIGNAL_SOUND;
1144 #endif
1145 #if defined (SIGSAK)
1146 if (hostsig == SIGSAK) return TARGET_SIGNAL_SAK;
1147 #endif
1148 return TARGET_SIGNAL_UNKNOWN;
1149 }
1150
1151 int
1152 target_signal_to_host (oursig)
1153 enum target_signal oursig;
1154 {
1155 switch (oursig)
1156 {
1157 case TARGET_SIGNAL_0: return 0;
1158
1159 #if defined (SIGHUP)
1160 case TARGET_SIGNAL_HUP: return SIGHUP;
1161 #endif
1162 #if defined (SIGINT)
1163 case TARGET_SIGNAL_INT: return SIGINT;
1164 #endif
1165 #if defined (SIGQUIT)
1166 case TARGET_SIGNAL_QUIT: return SIGQUIT;
1167 #endif
1168 #if defined (SIGILL)
1169 case TARGET_SIGNAL_ILL: return SIGILL;
1170 #endif
1171 #if defined (SIGTRAP)
1172 case TARGET_SIGNAL_TRAP: return SIGTRAP;
1173 #endif
1174 #if defined (SIGABRT)
1175 case TARGET_SIGNAL_ABRT: return SIGABRT;
1176 #endif
1177 #if defined (SIGEMT)
1178 case TARGET_SIGNAL_EMT: return SIGEMT;
1179 #endif
1180 #if defined (SIGFPE)
1181 case TARGET_SIGNAL_FPE: return SIGFPE;
1182 #endif
1183 #if defined (SIGKILL)
1184 case TARGET_SIGNAL_KILL: return SIGKILL;
1185 #endif
1186 #if defined (SIGBUS)
1187 case TARGET_SIGNAL_BUS: return SIGBUS;
1188 #endif
1189 #if defined (SIGSEGV)
1190 case TARGET_SIGNAL_SEGV: return SIGSEGV;
1191 #endif
1192 #if defined (SIGSYS)
1193 case TARGET_SIGNAL_SYS: return SIGSYS;
1194 #endif
1195 #if defined (SIGPIPE)
1196 case TARGET_SIGNAL_PIPE: return SIGPIPE;
1197 #endif
1198 #if defined (SIGALRM)
1199 case TARGET_SIGNAL_ALRM: return SIGALRM;
1200 #endif
1201 #if defined (SIGTERM)
1202 case TARGET_SIGNAL_TERM: return SIGTERM;
1203 #endif
1204 #if defined (SIGUSR1)
1205 case TARGET_SIGNAL_USR1: return SIGUSR1;
1206 #endif
1207 #if defined (SIGUSR2)
1208 case TARGET_SIGNAL_USR2: return SIGUSR2;
1209 #endif
1210 #if defined (SIGCHLD) || defined (SIGCLD)
1211 case TARGET_SIGNAL_CHLD:
1212 #if defined (SIGCHLD)
1213 return SIGCHLD;
1214 #else
1215 return SIGCLD;
1216 #endif
1217 #endif /* SIGCLD or SIGCHLD */
1218 #if defined (SIGPWR)
1219 case TARGET_SIGNAL_PWR: return SIGPWR;
1220 #endif
1221 #if defined (SIGWINCH)
1222 case TARGET_SIGNAL_WINCH: return SIGWINCH;
1223 #endif
1224 #if defined (SIGURG)
1225 case TARGET_SIGNAL_URG: return SIGURG;
1226 #endif
1227 #if defined (SIGIO)
1228 case TARGET_SIGNAL_IO: return SIGIO;
1229 #endif
1230 #if defined (SIGPOLL)
1231 case TARGET_SIGNAL_POLL: return SIGPOLL;
1232 #endif
1233 #if defined (SIGSTOP)
1234 case TARGET_SIGNAL_STOP: return SIGSTOP;
1235 #endif
1236 #if defined (SIGTSTP)
1237 case TARGET_SIGNAL_TSTP: return SIGTSTP;
1238 #endif
1239 #if defined (SIGCONT)
1240 case TARGET_SIGNAL_CONT: return SIGCONT;
1241 #endif
1242 #if defined (SIGTTIN)
1243 case TARGET_SIGNAL_TTIN: return SIGTTIN;
1244 #endif
1245 #if defined (SIGTTOU)
1246 case TARGET_SIGNAL_TTOU: return SIGTTOU;
1247 #endif
1248 #if defined (SIGVTALRM)
1249 case TARGET_SIGNAL_VTALRM: return SIGVTALRM;
1250 #endif
1251 #if defined (SIGPROF)
1252 case TARGET_SIGNAL_PROF: return SIGPROF;
1253 #endif
1254 #if defined (SIGXCPU)
1255 case TARGET_SIGNAL_XCPU: return SIGXCPU;
1256 #endif
1257 #if defined (SIGXFSZ)
1258 case TARGET_SIGNAL_XFSZ: return SIGXFSZ;
1259 #endif
1260 #if defined (SIGWIND)
1261 case TARGET_SIGNAL_WIND: return SIGWIND;
1262 #endif
1263 #if defined (SIGPHONE)
1264 case TARGET_SIGNAL_PHONE: return SIGPHONE;
1265 #endif
1266 #if defined (SIGLOST)
1267 case TARGET_SIGNAL_LOST: return SIGLOST;
1268 #endif
1269 #if defined (SIGWAITING)
1270 case TARGET_SIGNAL_WAITING: return SIGWAITING;
1271 #endif
1272 #if defined (SIGLWP)
1273 case TARGET_SIGNAL_LWP: return SIGLWP;
1274 #endif
1275 #if defined (SIGDANGER)
1276 case TARGET_SIGNAL_DANGER: return SIGDANGER;
1277 #endif
1278 #if defined (SIGGRANT)
1279 case TARGET_SIGNAL_GRANT: return SIGGRANT;
1280 #endif
1281 #if defined (SIGRETRACT)
1282 case TARGET_SIGNAL_RETRACT: return SIGRETRACT;
1283 #endif
1284 #if defined (SIGMSG)
1285 case TARGET_SIGNAL_MSG: return SIGMSG;
1286 #endif
1287 #if defined (SIGSOUND)
1288 case TARGET_SIGNAL_SOUND: return SIGSOUND;
1289 #endif
1290 #if defined (SIGSAK)
1291 case TARGET_SIGNAL_SAK: return SIGSAK;
1292 #endif
1293 default:
1294 /* The user might be trying to do "signal SIGSAK" where this system
1295 doesn't have SIGSAK. */
1296 warning ("Signal %s does not exist on this system.\n",
1297 target_signal_to_name (oursig));
1298 return 0;
1299 }
1300 }
1301
1302 /* Helper function for child_wait and the Lynx derivatives of child_wait.
1303 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
1304 translation of that in OURSTATUS. */
1305 void
1306 store_waitstatus (ourstatus, hoststatus)
1307 struct target_waitstatus *ourstatus;
1308 int hoststatus;
1309 {
1310 #ifdef CHILD_SPECIAL_WAITSTATUS
1311 /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
1312 if it wants to deal with hoststatus. */
1313 if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
1314 return;
1315 #endif
1316
1317 if (WIFEXITED (hoststatus))
1318 {
1319 ourstatus->kind = TARGET_WAITKIND_EXITED;
1320 ourstatus->value.integer = WEXITSTATUS (hoststatus);
1321 }
1322 else if (!WIFSTOPPED (hoststatus))
1323 {
1324 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1325 ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
1326 }
1327 else
1328 {
1329 ourstatus->kind = TARGET_WAITKIND_STOPPED;
1330 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
1331 }
1332 }
1333
1334 \f
1335 /* Returns zero to leave the inferior alone, one to interrupt it. */
1336 int (*target_activity_function) PARAMS ((void));
1337 int target_activity_fd;
1338 \f
1339 /* Convert a normal process ID to a string. Returns the string in a static
1340 buffer. */
1341
1342 char *
1343 normal_pid_to_str (pid)
1344 int pid;
1345 {
1346 static char buf[30];
1347
1348 sprintf (buf, "process %d", pid);
1349
1350 return buf;
1351 }
1352 \f
1353 static char targ_desc[] =
1354 "Names of targets and files being debugged.\n\
1355 Shows the entire stack of targets currently in use (including the exec-file,\n\
1356 core-file, and process, if any), as well as the symbol file name.";
1357
1358 void
1359 initialize_targets ()
1360 {
1361 push_target (&dummy_target);
1362
1363 add_info ("target", target_info, targ_desc);
1364 add_info ("files", target_info, targ_desc);
1365
1366 if (!STREQ (signals[TARGET_SIGNAL_LAST].string, "TARGET_SIGNAL_MAGIC"))
1367 abort ();
1368 }
This page took 0.058871 seconds and 4 git commands to generate.