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