* expression.h (OP_LABELED): New operator, for Chill
[deliverable/binutils-gdb.git] / gdb / remote-vx.c
1 /* Memory-access and commands for remote VxWorks processes, for GDB.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3 Contributed by Wind River Systems and 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 "frame.h"
23 #include "inferior.h"
24 #include "wait.h"
25 #include "target.h"
26 #include "gdbcore.h"
27 #include "command.h"
28 #include "symtab.h"
29 #include "complaints.h"
30 #include "gdbcmd.h"
31 #include "bfd.h" /* Required by objfiles.h. */
32 #include "symfile.h" /* Required by objfiles.h. */
33 #include "objfiles.h"
34 #include "gdb-stabs.h"
35
36 #include <string.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <fcntl.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #define malloc bogon_malloc /* Sun claims "char *malloc()" not void * */
43 #define free bogon_free /* Sun claims "int free()" not void */
44 #define realloc bogon_realloc /* Sun claims "char *realloc()", not void * */
45 #include <rpc/rpc.h>
46 #undef malloc
47 #undef free
48 #undef realloc
49 #include <sys/time.h> /* UTek's <rpc/rpc.h> doesn't #incl this */
50 #include <netdb.h>
51 #include "vx-share/ptrace.h"
52 #include "vx-share/xdr_ptrace.h"
53 #include "vx-share/xdr_ld.h"
54 #include "vx-share/xdr_rdb.h"
55 #include "vx-share/dbgRpcLib.h"
56
57 #include <symtab.h>
58
59 /* Maximum number of bytes to transfer in a single
60 PTRACE_{READ,WRITE}DATA request. */
61 #define VX_MEMXFER_MAX 4096
62
63 extern void vx_read_register ();
64 extern void vx_write_register ();
65 extern void symbol_file_command ();
66 extern int stop_soon_quietly; /* for wait_for_inferior */
67
68 static int net_step ();
69 static int net_ptrace_clnt_call (); /* Forward decl */
70 static enum clnt_stat net_clnt_call (); /* Forward decl */
71 extern struct target_ops vx_ops, vx_run_ops; /* Forward declaration */
72
73 /* Saved name of target host and called function for "info files".
74 Both malloc'd. */
75
76 static char *vx_host;
77 static char *vx_running; /* Called function */
78
79 /* Nonzero means target that is being debugged remotely has a floating
80 point processor. */
81
82 int target_has_fp;
83
84 /* Default error message when the network is forking up. */
85
86 static const char rpcerr[] = "network target debugging: rpc error";
87
88 CLIENT *pClient; /* client used in net debugging */
89 static int ptraceSock = RPC_ANYSOCK;
90
91 enum clnt_stat net_clnt_call();
92 static void parse_args ();
93
94 static struct timeval rpcTimeout = { 10, 0 };
95
96 static char *skip_white_space ();
97 static char *find_white_space ();
98
99 /* Tell the VxWorks target system to download a file.
100 The load addresses of the text, data, and bss segments are
101 stored in *pTextAddr, *pDataAddr, and *pBssAddr (respectively).
102 Returns 0 for success, -1 for failure. */
103
104 static int
105 net_load (filename, pTextAddr, pDataAddr, pBssAddr)
106 char *filename;
107 CORE_ADDR *pTextAddr;
108 CORE_ADDR *pDataAddr;
109 CORE_ADDR *pBssAddr;
110 {
111 enum clnt_stat status;
112 struct ldfile ldstruct;
113 struct timeval load_timeout;
114
115 memset ((char *) &ldstruct, '\0', sizeof (ldstruct));
116
117 /* We invoke clnt_call () here directly, instead of through
118 net_clnt_call (), because we need to set a large timeout value.
119 The load on the target side can take quite a while, easily
120 more than 10 seconds. The user can kill this call by typing
121 CTRL-C if there really is a problem with the load.
122
123 Do not change the tv_sec value without checking -- select() imposes
124 a limit of 10**8 on it for no good reason that I can see... */
125
126 load_timeout.tv_sec = 99999999; /* A large number, effectively inf. */
127 load_timeout.tv_usec = 0;
128
129 status = clnt_call (pClient, VX_LOAD, xdr_wrapstring, &filename, xdr_ldfile,
130 &ldstruct, load_timeout);
131
132 if (status == RPC_SUCCESS)
133 {
134 if (*ldstruct.name == 0) /* load failed on VxWorks side */
135 return -1;
136 *pTextAddr = ldstruct.txt_addr;
137 *pDataAddr = ldstruct.data_addr;
138 *pBssAddr = ldstruct.bss_addr;
139 return 0;
140 }
141 else
142 return -1;
143 }
144
145 /* returns 0 if successful, errno if RPC failed or VxWorks complains. */
146
147 static int
148 net_break (addr, procnum)
149 int addr;
150 u_long procnum;
151 {
152 enum clnt_stat status;
153 int break_status;
154 Rptrace ptrace_in; /* XXX This is stupid. It doesn't need to be a ptrace
155 structure. How about something smaller? */
156
157 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
158 break_status = 0;
159
160 ptrace_in.addr = addr;
161 ptrace_in.pid = inferior_pid;
162
163 status = net_clnt_call (procnum, xdr_rptrace, &ptrace_in, xdr_int,
164 &break_status);
165
166 if (status != RPC_SUCCESS)
167 return errno;
168
169 if (break_status == -1)
170 return ENOMEM;
171 return break_status; /* probably (FIXME) zero */
172 }
173
174 /* returns 0 if successful, errno otherwise */
175
176 static int
177 vx_insert_breakpoint (addr)
178 int addr;
179 {
180 return net_break (addr, VX_BREAK_ADD);
181 }
182
183 /* returns 0 if successful, errno otherwise */
184
185 static int
186 vx_remove_breakpoint (addr)
187 int addr;
188 {
189 return net_break (addr, VX_BREAK_DELETE);
190 }
191
192 /* Start an inferior process and sets inferior_pid to its pid.
193 EXEC_FILE is the file to run.
194 ALLARGS is a string containing the arguments to the program.
195 ENV is the environment vector to pass.
196 Returns process id. Errors reported with error().
197 On VxWorks, we ignore exec_file. */
198
199 static void
200 vx_create_inferior (exec_file, args, env)
201 char *exec_file;
202 char *args;
203 char **env;
204 {
205 enum clnt_stat status;
206 arg_array passArgs;
207 TASK_START taskStart;
208
209 memset ((char *) &passArgs, '\0', sizeof (passArgs));
210 memset ((char *) &taskStart, '\0', sizeof (taskStart));
211
212 /* parse arguments, put them in passArgs */
213
214 parse_args (args, &passArgs);
215
216 if (passArgs.arg_array_len == 0)
217 error ("You must specify a function name to run, and arguments if any");
218
219 status = net_clnt_call (PROCESS_START, xdr_arg_array, &passArgs,
220 xdr_TASK_START, &taskStart);
221
222 if ((status != RPC_SUCCESS) || (taskStart.status == -1))
223 error ("Can't create process on remote target machine");
224
225 /* Save the name of the running function */
226 vx_running = savestring (passArgs.arg_array_val[0],
227 strlen (passArgs.arg_array_val[0]));
228
229 push_target (&vx_run_ops);
230 inferior_pid = taskStart.pid;
231
232 /* We will get a trace trap after one instruction.
233 Insert breakpoints and continue. */
234
235 init_wait_for_inferior ();
236
237 /* Set up the "saved terminal modes" of the inferior
238 based on what modes we are starting it with. */
239 target_terminal_init ();
240
241 /* Install inferior's terminal modes. */
242 target_terminal_inferior ();
243
244 stop_soon_quietly = 1;
245 wait_for_inferior (); /* Get the task spawn event */
246 stop_soon_quietly = 0;
247
248 /* insert_step_breakpoint (); FIXME, do we need this? */
249 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
250 }
251
252 /* Fill ARGSTRUCT in argc/argv form with the arguments from the
253 argument string ARGSTRING. */
254
255 static void
256 parse_args (arg_string, arg_struct)
257 register char *arg_string;
258 arg_array *arg_struct;
259 {
260 register int arg_count = 0; /* number of arguments */
261 register int arg_index = 0;
262 register char *p0;
263
264 memset ((char *) arg_struct, '\0', sizeof (arg_array));
265
266 /* first count how many arguments there are */
267
268 p0 = arg_string;
269 while (*p0 != '\0')
270 {
271 if (*(p0 = skip_white_space (p0)) == '\0')
272 break;
273 p0 = find_white_space (p0);
274 arg_count++;
275 }
276
277 arg_struct->arg_array_len = arg_count;
278 arg_struct->arg_array_val = (char **) xmalloc ((arg_count + 1)
279 * sizeof (char *));
280
281 /* now copy argument strings into arg_struct. */
282
283 while (*(arg_string = skip_white_space (arg_string)))
284 {
285 p0 = find_white_space (arg_string);
286 arg_struct->arg_array_val[arg_index++] = savestring (arg_string,
287 p0 - arg_string);
288 arg_string = p0;
289 }
290
291 arg_struct->arg_array_val[arg_count] = NULL;
292 }
293
294 /* Advance a string pointer across whitespace and return a pointer
295 to the first non-white character. */
296
297 static char *
298 skip_white_space (p)
299 register char *p;
300 {
301 while (*p == ' ' || *p == '\t')
302 p++;
303 return p;
304 }
305
306 /* Search for the first unquoted whitespace character in a string.
307 Returns a pointer to the character, or to the null terminator
308 if no whitespace is found. */
309
310 static char *
311 find_white_space (p)
312 register char *p;
313 {
314 register int c;
315
316 while ((c = *p) != ' ' && c != '\t' && c)
317 {
318 if (c == '\'' || c == '"')
319 {
320 while (*++p != c && *p)
321 {
322 if (*p == '\\')
323 p++;
324 }
325 if (!*p)
326 break;
327 }
328 p++;
329 }
330 return p;
331 }
332
333 /* Poll the VxWorks target system for an event related
334 to the debugged task.
335 Returns -1 if remote wait failed, task status otherwise. */
336
337 static int
338 net_wait (pEvent)
339 RDB_EVENT *pEvent;
340 {
341 int pid;
342 enum clnt_stat status;
343
344 memset ((char *) pEvent, '\0', sizeof (RDB_EVENT));
345
346 pid = inferior_pid;
347 status = net_clnt_call (PROCESS_WAIT, xdr_int, &pid, xdr_RDB_EVENT,
348 pEvent);
349
350 /* return (status == RPC_SUCCESS)? pEvent->status: -1; */
351 if (status == RPC_SUCCESS)
352 return ((pEvent->status) ? 1 : 0);
353 else if (status == RPC_TIMEDOUT)
354 return (1);
355 else
356 return (-1);
357 }
358
359 /* Suspend the remote task.
360 Returns -1 if suspend fails on target system, 0 otherwise. */
361
362 static int
363 net_quit ()
364 {
365 int pid;
366 int quit_status;
367 enum clnt_stat status;
368
369 quit_status = 0;
370
371 /* don't let rdbTask suspend itself by passing a pid of 0 */
372
373 if ((pid = inferior_pid) == 0)
374 return -1;
375
376 status = net_clnt_call (VX_TASK_SUSPEND, xdr_int, &pid, xdr_int,
377 &quit_status);
378
379 return (status == RPC_SUCCESS)? quit_status: -1;
380 }
381
382 /* Read a register or registers from the remote system. */
383
384 static void
385 net_read_registers (reg_buf, len, procnum)
386 char *reg_buf;
387 int len;
388 u_long procnum;
389 {
390 int status;
391 Rptrace ptrace_in;
392 Ptrace_return ptrace_out;
393 C_bytes out_data;
394 char message[100];
395
396 bzero ((char *) &ptrace_in, sizeof (ptrace_in));
397 bzero ((char *) &ptrace_out, sizeof (ptrace_out));
398
399 /* Initialize RPC input argument structure. */
400
401 ptrace_in.pid = inferior_pid;
402 ptrace_in.info.ttype = NOINFO;
403
404 /* Initialize RPC return value structure. */
405
406 out_data.bytes = reg_buf;
407 out_data.len = len;
408 ptrace_out.info.more_data = (caddr_t) &out_data;
409
410 /* Call RPC; take an error exit if appropriate. */
411
412 status = net_ptrace_clnt_call (procnum, &ptrace_in, &ptrace_out);
413 if (status)
414 error (rpcerr);
415 if (ptrace_out.status == -1)
416 {
417 errno = ptrace_out.errno_val;
418 sprintf (message, "reading %s registers", (procnum == PTRACE_GETREGS)
419 ? "general-purpose"
420 : "floating-point");
421 perror_with_name (message);
422 }
423 }
424
425 /* Write register values to a VxWorks target. REG_BUF points to a buffer
426 containing the raw register values, LEN is the length of REG_BUF in
427 bytes, and PROCNUM is the RPC procedure number (PTRACE_SETREGS or
428 PTRACE_SETFPREGS). An error exit is taken if the RPC call fails or
429 if an error status is returned by the remote debug server. This is
430 a utility routine used by vx_write_register (). */
431
432 void
433 net_write_registers (reg_buf, len, procnum)
434 char *reg_buf;
435 int len;
436 u_long procnum;
437 {
438 int status;
439 Rptrace ptrace_in;
440 Ptrace_return ptrace_out;
441 C_bytes in_data;
442 char message[100];
443
444 bzero ((char *) &ptrace_in, sizeof (ptrace_in));
445 bzero ((char *) &ptrace_out, sizeof (ptrace_out));
446
447 /* Initialize RPC input argument structure. */
448
449 in_data.bytes = reg_buf;
450 in_data.len = len;
451
452 ptrace_in.pid = inferior_pid;
453 ptrace_in.info.ttype = DATA;
454 ptrace_in.info.more_data = (caddr_t) &in_data;
455
456 /* Call RPC; take an error exit if appropriate. */
457
458 status = net_ptrace_clnt_call (procnum, &ptrace_in, &ptrace_out);
459 if (status)
460 error (rpcerr);
461 if (ptrace_out.status == -1)
462 {
463 errno = ptrace_out.errno_val;
464 sprintf (message, "writing %s registers", (procnum == PTRACE_SETREGS)
465 ? "general-purpose"
466 : "floating-point");
467 perror_with_name (message);
468 }
469 }
470
471 /* Prepare to store registers. Since we will store all of them,
472 read out their current values now. */
473
474 static void
475 vx_prepare_to_store ()
476 {
477 /* Fetch all registers, if any of them are not yet fetched. */
478 read_register_bytes (0, NULL, REGISTER_BYTES);
479 }
480
481 /* Copy LEN bytes to or from remote inferior's memory starting at MEMADDR
482 to debugger memory starting at MYADDR. WRITE is true if writing to the
483 inferior.
484 Result is the number of bytes written or read (zero if error). The
485 protocol allows us to return a negative count, indicating that we can't
486 handle the current address but can handle one N bytes further, but
487 vxworks doesn't give us that information. */
488
489 static int
490 vx_xfer_memory (memaddr, myaddr, len, write, target)
491 CORE_ADDR memaddr;
492 char *myaddr;
493 int len;
494 int write;
495 struct target_ops *target; /* ignored */
496 {
497 int status;
498 Rptrace ptrace_in;
499 Ptrace_return ptrace_out;
500 C_bytes data;
501 enum ptracereq request;
502
503 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
504 memset ((char *) &ptrace_out, '\0', sizeof (ptrace_out));
505
506 ptrace_in.pid = inferior_pid; /* XXX pid unnecessary for READDATA */
507 ptrace_in.addr = (int) memaddr; /* Where from */
508 ptrace_in.data = len; /* How many bytes */
509
510 if (write)
511 {
512 ptrace_in.info.ttype = DATA;
513 ptrace_in.info.more_data = (caddr_t) &data;
514
515 data.bytes = (caddr_t) myaddr; /* Where from */
516 data.len = len; /* How many bytes (again, for XDR) */
517 request = PTRACE_WRITEDATA;
518 }
519 else
520 {
521 ptrace_out.info.more_data = (caddr_t) &data;
522 request = PTRACE_READDATA;
523 }
524 /* Loop until the entire request has been satisfied, transferring
525 at most VX_MEMXFER_MAX bytes per iteration. Break from the loop
526 if an error status is returned by the remote debug server. */
527
528 nleft = len;
529 status = 0;
530
531 while (nleft > 0 && status == 0)
532 {
533 nxfer = min (nleft, VX_MEMXFER_MAX);
534
535 ptrace_in.addr = (int) memaddr;
536 ptrace_in.data = nxfer;
537 data.bytes = (caddr_t) myaddr;
538 data.len = nxfer;
539
540 /* Request a block from the remote debug server; if RPC fails,
541 report an error and return to debugger command level. */
542
543 if (net_ptrace_clnt_call (request, &ptrace_in, &ptrace_out))
544 error (rpcerr);
545
546 status = ptrace_out.status;
547 if (status == 0)
548 {
549 memaddr += nxfer;
550 myaddr += nxfer;
551 nleft -= nxfer;
552 }
553 else
554 {
555 /* A target-side error has ocurred. Set errno to the error
556 code chosen by the target so that a later perror () will
557 say something meaningful. */
558
559 errno = ptrace_out.errno_val;
560 }
561 }
562
563 /* Return the number of bytes transferred. */
564
565 return (len - nleft);
566 }
567
568 static void
569 vx_files_info ()
570 {
571 printf_unfiltered ("\tAttached to host `%s'", vx_host);
572 printf_unfiltered (", which has %sfloating point", target_has_fp? "": "no ");
573 printf_unfiltered (".\n");
574 }
575
576 static void
577 vx_run_files_info ()
578 {
579 printf_unfiltered ("\tRunning %s VxWorks process %s",
580 vx_running ? "child" : "attached",
581 local_hex_string (inferior_pid));
582 if (vx_running)
583 printf_unfiltered (", function `%s'", vx_running);
584 printf_unfiltered(".\n");
585 }
586
587 static void
588 vx_resume (pid, step, siggnal)
589 int pid;
590 int step;
591 enum target_signal siggnal;
592 {
593 int status;
594 Rptrace ptrace_in;
595 Ptrace_return ptrace_out;
596 CORE_ADDR cont_addr;
597
598 if (pid == -1)
599 pid = inferior_pid;
600
601 if (siggnal != 0 && siggnal != stop_signal)
602 error ("Cannot send signals to VxWorks processes");
603
604 /* Set CONT_ADDR to the address at which we are continuing,
605 or to 1 if we are continuing from where the program stopped.
606 This conforms to traditional ptrace () usage, but at the same
607 time has special meaning for the VxWorks remote debug server.
608 If the address is not 1, the server knows that the target
609 program is jumping to a new address, which requires special
610 handling if there is a breakpoint at the new address. */
611
612 cont_addr = read_register (PC_REGNUM);
613 if (cont_addr == stop_pc)
614 cont_addr = 1;
615
616 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
617 memset ((char *) &ptrace_out, '\0', sizeof (ptrace_out));
618
619 ptrace_in.pid = pid;
620 ptrace_in.addr = cont_addr; /* Target side insists on this, or it panics. */
621
622 if (step)
623 status = net_step();
624 else
625 status = net_ptrace_clnt_call (PTRACE_CONT, &ptrace_in, &ptrace_out);
626
627 if (status)
628 error (rpcerr);
629 if (ptrace_out.status == -1)
630 {
631 errno = ptrace_out.errno;
632 perror_with_name ("Resuming remote process");
633 }
634 }
635
636 static void
637 vx_mourn_inferior ()
638 {
639 pop_target (); /* Pop back to no-child state */
640 generic_mourn_inferior ();
641 }
642
643 \f
644 static void vx_add_symbols PARAMS ((char *, int, CORE_ADDR, CORE_ADDR,
645 CORE_ADDR));
646
647 struct find_sect_args {
648 CORE_ADDR text_start;
649 CORE_ADDR data_start;
650 CORE_ADDR bss_start;
651 };
652
653 static void find_sect PARAMS ((bfd *, asection *, void *));
654
655 static void
656 find_sect (abfd, sect, obj)
657 bfd *abfd;
658 asection *sect;
659 PTR obj;
660 {
661 struct find_sect_args *args = (struct find_sect_args *)obj;
662
663 if (bfd_get_section_flags (abfd, sect) & (SEC_CODE & SEC_READONLY))
664 args->text_start = bfd_get_section_vma (abfd, sect);
665 else if (bfd_get_section_flags (abfd, sect) & SEC_ALLOC)
666 {
667 if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
668 {
669 /* Exclude .ctor and .dtor sections which have SEC_CODE set but not
670 SEC_DATA. */
671 if (bfd_get_section_flags (abfd, sect) & SEC_DATA)
672 args->data_start = bfd_get_section_vma (abfd, sect);
673 }
674 else
675 args->bss_start = bfd_get_section_vma (abfd, sect);
676 }
677 }
678
679 static void
680 vx_add_symbols (name, from_tty, text_addr, data_addr, bss_addr)
681 char *name;
682 int from_tty;
683 CORE_ADDR text_addr;
684 CORE_ADDR data_addr;
685 CORE_ADDR bss_addr;
686 {
687 struct section_offsets *offs;
688 struct objfile *objfile;
689 struct find_sect_args ss;
690
691 /* It might be nice to suppress the breakpoint_re_set which happens here
692 because we are going to do one again after the objfile_relocate. */
693 objfile = symbol_file_add (name, from_tty, 0, 0, 0, 0);
694
695 /* This is a (slightly cheesy) way of superceding the old symbols. A less
696 cheesy way would be to find the objfile with the same name and
697 free_objfile it. */
698 objfile_to_front (objfile);
699
700 offs = (struct section_offsets *)
701 alloca (sizeof (struct section_offsets)
702 + objfile->num_sections * sizeof (offs->offsets));
703 memcpy (offs, objfile->section_offsets,
704 sizeof (struct section_offsets)
705 + objfile->num_sections * sizeof (offs->offsets));
706
707 ss.text_start = 0;
708 ss.data_start = 0;
709 ss.bss_start = 0;
710 bfd_map_over_sections (objfile->obfd, find_sect, &ss);
711
712 /* Both COFF and b.out frontends use these SECT_OFF_* values. */
713 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr - ss.text_start;
714 ANOFFSET (offs, SECT_OFF_DATA) = data_addr - ss.data_start;
715 ANOFFSET (offs, SECT_OFF_BSS) = bss_addr - ss.bss_start;
716 objfile_relocate (objfile, offs);
717
718 /* Need to do this *after* things are relocated. */
719 breakpoint_re_set ();
720 }
721
722 /* This function allows the addition of incrementally linked object files. */
723
724 static void
725 vx_load_command (arg_string, from_tty)
726 char *arg_string;
727 int from_tty;
728 {
729 CORE_ADDR text_addr;
730 CORE_ADDR data_addr;
731 CORE_ADDR bss_addr;
732
733 if (arg_string == 0)
734 error ("The load command takes a file name");
735
736 arg_string = tilde_expand (arg_string);
737 make_cleanup (free, arg_string);
738
739 dont_repeat ();
740
741 /* Refuse to load the module if a debugged task is running. Doing so
742 can have a number of unpleasant consequences to the running task. */
743
744 if (inferior_pid != 0 && target_has_execution)
745 {
746 if (query ("You may not load a module while the target task is running.\n\
747 Kill the target task? "))
748 target_kill ();
749 else
750 error ("Load cancelled.");
751 }
752
753 QUIT;
754 immediate_quit++;
755 if (net_load (arg_string, &text_addr, &data_addr, &bss_addr) == -1)
756 error ("Load failed on target machine");
757 immediate_quit--;
758
759 vx_add_symbols (arg_string, from_tty, text_addr, data_addr, bss_addr);
760
761 /* Getting new symbols may change our opinion about what is
762 frameless. */
763 reinit_frame_cache ();
764 }
765
766 /* Single step the target program at the source or machine level.
767 Takes an error exit if rpc fails.
768 Returns -1 if remote single-step operation fails, else 0. */
769
770 static int
771 net_step ()
772 {
773 enum clnt_stat status;
774 int step_status;
775 SOURCE_STEP source_step;
776
777 source_step.taskId = inferior_pid;
778
779 if (step_range_end)
780 {
781 source_step.startAddr = step_range_start;
782 source_step.endAddr = step_range_end;
783 }
784 else
785 {
786 source_step.startAddr = 0;
787 source_step.endAddr = 0;
788 }
789
790 status = net_clnt_call (VX_SOURCE_STEP, xdr_SOURCE_STEP, &source_step,
791 xdr_int, &step_status);
792
793 if (status == RPC_SUCCESS)
794 return step_status;
795 else
796 error (rpcerr);
797 }
798
799 /* Emulate ptrace using RPC calls to the VxWorks target system.
800 Returns nonzero (-1) if RPC status to VxWorks is bad, 0 otherwise. */
801
802 static int
803 net_ptrace_clnt_call (request, pPtraceIn, pPtraceOut)
804 enum ptracereq request;
805 Rptrace *pPtraceIn;
806 Ptrace_return *pPtraceOut;
807 {
808 enum clnt_stat status;
809
810 status = net_clnt_call (request, xdr_rptrace, pPtraceIn, xdr_ptrace_return,
811 pPtraceOut);
812
813 if (status != RPC_SUCCESS)
814 return -1;
815
816 return 0;
817 }
818
819 /* Query the target for the name of the file from which VxWorks was
820 booted. pBootFile is the address of a pointer to the buffer to
821 receive the file name; if the pointer pointed to by pBootFile is
822 NULL, memory for the buffer will be allocated by XDR.
823 Returns -1 if rpc failed, 0 otherwise. */
824
825 static int
826 net_get_boot_file (pBootFile)
827 char **pBootFile;
828 {
829 enum clnt_stat status;
830
831 status = net_clnt_call (VX_BOOT_FILE_INQ, xdr_void, (char *) 0,
832 xdr_wrapstring, pBootFile);
833 return (status == RPC_SUCCESS) ? 0 : -1;
834 }
835
836 /* Fetch a list of loaded object modules from the VxWorks target.
837 Returns -1 if rpc failed, 0 otherwise
838 There's no way to check if the returned loadTable is correct.
839 VxWorks doesn't check it. */
840
841 static int
842 net_get_symbols (pLoadTable)
843 ldtabl *pLoadTable; /* return pointer to ldtabl here */
844 {
845 enum clnt_stat status;
846
847 memset ((char *) pLoadTable, '\0', sizeof (struct ldtabl));
848
849 status = net_clnt_call (VX_STATE_INQ, xdr_void, 0, xdr_ldtabl, pLoadTable);
850 return (status == RPC_SUCCESS) ? 0 : -1;
851 }
852
853 /* Look up a symbol in the VxWorks target's symbol table.
854 Returns status of symbol read on target side (0=success, -1=fail)
855 Returns -1 and complain()s if rpc fails. */
856
857 struct complaint cant_contact_target =
858 {"Lost contact with VxWorks target", 0, 0};
859
860 static int
861 vx_lookup_symbol (name, pAddr)
862 char *name; /* symbol name */
863 CORE_ADDR *pAddr;
864 {
865 enum clnt_stat status;
866 SYMBOL_ADDR symbolAddr;
867
868 *pAddr = 0;
869 memset ((char *) &symbolAddr, '\0', sizeof (symbolAddr));
870
871 status = net_clnt_call (VX_SYMBOL_INQ, xdr_wrapstring, &name,
872 xdr_SYMBOL_ADDR, &symbolAddr);
873 if (status != RPC_SUCCESS)
874 {
875 complain (&cant_contact_target);
876 return -1;
877 }
878
879 *pAddr = symbolAddr.addr;
880 return symbolAddr.status;
881 }
882
883 /* Check to see if the VxWorks target has a floating point coprocessor.
884 Returns 1 if target has floating point processor, 0 otherwise.
885 Calls error() if rpc fails. */
886
887 static int
888 net_check_for_fp ()
889 {
890 enum clnt_stat status;
891 bool_t fp = 0; /* true if fp processor is present on target board */
892
893 status = net_clnt_call (VX_FP_INQUIRE, xdr_void, 0, xdr_bool, &fp);
894 if (status != RPC_SUCCESS)
895 error (rpcerr);
896
897 return (int) fp;
898 }
899
900 /* Establish an RPC connection with the VxWorks target system.
901 Calls error () if unable to establish connection. */
902
903 static void
904 net_connect (host)
905 char *host;
906 {
907 struct sockaddr_in destAddr;
908 struct hostent *destHost;
909 unsigned long addr;
910
911 /* Get the internet address for the given host. Allow a numeric
912 IP address or a hostname. */
913
914 addr = inet_addr (host);
915 if (addr == -1)
916 {
917 destHost = (struct hostent *) gethostbyname (host);
918 if (destHost == NULL)
919 /* FIXME: Probably should include hostname here in quotes.
920 For example if the user types "target vxworks vx960 " it should
921 say "Invalid host `vx960 '." not just "Invalid hostname". */
922 error ("Invalid hostname. Couldn't find remote host address.");
923 addr = * (unsigned long *) destHost->h_addr;
924 }
925
926 memset (&destAddr, '\0', sizeof (destAddr));
927
928 destAddr.sin_addr.s_addr = addr;
929 destAddr.sin_family = AF_INET;
930 destAddr.sin_port = 0; /* set to actual port that remote
931 ptrace is listening on. */
932
933 /* Create a tcp client transport on which to issue
934 calls to the remote ptrace server. */
935
936 ptraceSock = RPC_ANYSOCK;
937 pClient = clnttcp_create (&destAddr, RDBPROG, RDBVERS, &ptraceSock, 0, 0);
938 /* FIXME, here is where we deal with different version numbers of the
939 proto */
940
941 if (pClient == NULL)
942 {
943 clnt_pcreateerror ("\tnet_connect");
944 error ("Couldn't connect to remote target.");
945 }
946 }
947 \f
948 /* Sleep for the specified number of milliseconds
949 * (assumed to be less than 1000).
950 * If select () is interrupted, returns immediately;
951 * takes an error exit if select () fails for some other reason.
952 */
953
954 static void
955 sleep_ms (ms)
956 long ms;
957 {
958 struct timeval select_timeout;
959 int status;
960
961 select_timeout.tv_sec = 0;
962 select_timeout.tv_usec = ms * 1000;
963
964 status = select (0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0,
965 &select_timeout);
966
967 if (status < 0 && errno != EINTR)
968 perror_with_name ("select");
969 }
970
971 static int
972 vx_wait (pid_to_wait_for, status)
973 int pid_to_wait_for;
974 struct target_waitstatus *status;
975 {
976 register int pid;
977 RDB_EVENT rdbEvent;
978 int quit_failed;
979
980 do
981 {
982 /* If CTRL-C is hit during this loop,
983 suspend the inferior process. */
984
985 quit_failed = 0;
986 if (quit_flag)
987 {
988 quit_failed = (net_quit () == -1);
989 quit_flag = 0;
990 }
991
992 /* If a net_quit () or net_wait () call has failed,
993 allow the user to break the connection with the target.
994 We can't simply error () out of this loop, since the
995 data structures representing the state of the inferior
996 are in an inconsistent state. */
997
998 if (quit_failed || net_wait (&rdbEvent) == -1)
999 {
1000 terminal_ours ();
1001 if (query ("Can't %s. Disconnect from target system? ",
1002 (quit_failed) ? "suspend remote task"
1003 : "get status of remote task"))
1004 {
1005 target_mourn_inferior();
1006 error ("Use the \"target\" command to reconnect.");
1007 }
1008 else
1009 {
1010 terminal_inferior ();
1011 continue;
1012 }
1013 }
1014
1015 pid = rdbEvent.taskId;
1016 if (pid == 0)
1017 {
1018 sleep_ms (200); /* FIXME Don't kill the network too badly */
1019 }
1020 else if (pid != inferior_pid)
1021 fatal ("Bad pid for debugged task: %s\n",
1022 local_hex_string((unsigned long) pid));
1023 } while (pid == 0);
1024
1025 /* The mostly likely kind. */
1026 status->kind = TARGET_WAITKIND_STOPPED;
1027
1028 switch (rdbEvent.eventType)
1029 {
1030 case EVENT_EXIT:
1031 status->kind = TARGET_WAITKIND_EXITED;
1032 /* FIXME is it possible to distinguish between a
1033 normal vs abnormal exit in VxWorks? */
1034 status->value.integer = 0;
1035 break;
1036
1037 case EVENT_START:
1038 /* Task was just started. */
1039 status->value.sig = TARGET_SIGNAL_TRAP;
1040 break;
1041
1042 case EVENT_STOP:
1043 status->value.sig = TARGET_SIGNAL_TRAP;
1044 /* XXX was it stopped by a signal? act accordingly */
1045 break;
1046
1047 case EVENT_BREAK: /* Breakpoint was hit. */
1048 status->value.sig = TARGET_SIGNAL_TRAP;
1049 break;
1050
1051 case EVENT_SUSPEND: /* Task was suspended, probably by ^C. */
1052 status->value.sig = TARGET_SIGNAL_INT;
1053 break;
1054
1055 case EVENT_BUS_ERR: /* Task made evil nasty reference. */
1056 status->value.sig = TARGET_SIGNAL_BUS;
1057 break;
1058
1059 case EVENT_ZERO_DIV: /* Division by zero */
1060 status->value.sig = TARGET_SIGNAL_FPE;
1061 break;
1062
1063 case EVENT_SIGNAL:
1064 #ifdef I80960
1065 status->value.sig = i960_fault_to_signal (rdbEvent.sigType);
1066 #else
1067 /* Back in the old days, before enum target_signal, this code used
1068 to add NSIG to the signal number and claim that PRINT_RANDOM_SIGNAL
1069 would take care of it. But PRINT_RANDOM_SIGNAL has never been
1070 defined except on the i960, so I don't really know what we are
1071 supposed to do on other architectures. */
1072 status->value.sig = TARGET_SIGNAL_UNKNOWN;
1073 #endif
1074 break;
1075 } /* switch */
1076 return pid;
1077 }
1078 \f
1079 static int
1080 symbol_stub (arg)
1081 char *arg;
1082 {
1083 symbol_file_command (arg, 0);
1084 return 1;
1085 }
1086
1087 static int
1088 add_symbol_stub (arg)
1089 char *arg;
1090 {
1091 struct ldfile *pLoadFile = (struct ldfile *)arg;
1092
1093 printf_unfiltered("\t%s: ", pLoadFile->name);
1094 vx_add_symbols (pLoadFile->name, 0, pLoadFile->txt_addr,
1095 pLoadFile->data_addr, pLoadFile->bss_addr);
1096 printf_unfiltered ("ok\n");
1097 return 1;
1098 }
1099 /* Target command for VxWorks target systems.
1100
1101 Used in vxgdb. Takes the name of a remote target machine
1102 running vxWorks and connects to it to initialize remote network
1103 debugging. */
1104
1105 static void
1106 vx_open (args, from_tty)
1107 char *args;
1108 int from_tty;
1109 {
1110 extern int close ();
1111 char *bootFile;
1112 extern char *source_path;
1113 struct ldtabl loadTable;
1114 struct ldfile *pLoadFile;
1115 int i;
1116 extern CLIENT *pClient;
1117 int symbols_added = 0;
1118
1119 if (!args)
1120 error_no_arg ("target machine name");
1121
1122 target_preopen (from_tty);
1123
1124 unpush_target (&vx_ops);
1125 printf_unfiltered ("Attaching remote machine across net...\n");
1126 gdb_flush (gdb_stdout);
1127
1128 /* Allow the user to kill the connect attempt by typing ^C.
1129 Wait until the call to target_has_fp () completes before
1130 disallowing an immediate quit, since even if net_connect ()
1131 is successful, the remote debug server might be hung. */
1132
1133 immediate_quit++;
1134
1135 net_connect (args);
1136 target_has_fp = net_check_for_fp ();
1137 printf_filtered ("Connected to %s.\n", args);
1138
1139 immediate_quit--;
1140
1141 push_target (&vx_ops);
1142
1143 /* Save a copy of the target host's name. */
1144 vx_host = savestring (args, strlen (args));
1145
1146 /* Find out the name of the file from which the target was booted
1147 and load its symbol table. */
1148
1149 printf_filtered ("Looking in Unix path for all loaded modules:\n");
1150 bootFile = NULL;
1151 if (!net_get_boot_file (&bootFile))
1152 {
1153 if (*bootFile)
1154 {
1155 printf_filtered ("\t%s: ", bootFile);
1156 /* This assumes that the kernel is never relocated. Hope that is an
1157 accurate assumption. */
1158 if (catch_errors
1159 (symbol_stub,
1160 bootFile,
1161 "Error while reading symbols from boot file:\n",
1162 RETURN_MASK_ALL))
1163 puts_filtered ("ok\n");
1164 }
1165 else if (from_tty)
1166 printf_unfiltered ("VxWorks kernel symbols not loaded.\n");
1167 }
1168 else
1169 error ("Can't retrieve boot file name from target machine.");
1170
1171 clnt_freeres (pClient, xdr_wrapstring, &bootFile);
1172
1173 if (net_get_symbols (&loadTable) != 0)
1174 error ("Can't read loaded modules from target machine");
1175
1176 i = 0-1;
1177 while (++i < loadTable.tbl_size)
1178 {
1179 QUIT; /* FIXME, avoids clnt_freeres below: mem leak */
1180 pLoadFile = &loadTable.tbl_ent [i];
1181 #ifdef WRS_ORIG
1182 {
1183 register int desc;
1184 struct cleanup *old_chain;
1185 char *fullname = NULL;
1186
1187 desc = openp (source_path, 0, pLoadFile->name, O_RDONLY, 0, &fullname);
1188 if (desc < 0)
1189 perror_with_name (pLoadFile->name);
1190 old_chain = make_cleanup (close, desc);
1191 add_file_at_addr (fullname, desc, pLoadFile->txt_addr, pLoadFile->data_addr,
1192 pLoadFile->bss_addr);
1193 do_cleanups (old_chain);
1194 }
1195 #else
1196 /* FIXME: Is there something better to search than the PATH? (probably
1197 not the source path, since source might be in different directories
1198 than objects. */
1199
1200 if (catch_errors (add_symbol_stub, (char *)pLoadFile, (char *)0,
1201 RETURN_MASK_ALL))
1202 symbols_added = 1;
1203 #endif
1204 }
1205 printf_filtered ("Done.\n");
1206
1207 clnt_freeres (pClient, xdr_ldtabl, &loadTable);
1208
1209 /* Getting new symbols may change our opinion about what is
1210 frameless. */
1211 if (symbols_added)
1212 reinit_frame_cache ();
1213 }
1214 \f
1215 /* Takes a task started up outside of gdb and ``attaches'' to it.
1216 This stops it cold in its tracks and allows us to start tracing it. */
1217
1218 static void
1219 vx_attach (args, from_tty)
1220 char *args;
1221 int from_tty;
1222 {
1223 unsigned long pid;
1224 char *cptr = 0;
1225 Rptrace ptrace_in;
1226 Ptrace_return ptrace_out;
1227 int status;
1228
1229 if (!args)
1230 error_no_arg ("process-id to attach");
1231
1232 pid = strtoul (args, &cptr, 0);
1233 if ((cptr == args) || (*cptr != '\0'))
1234 error ("Invalid process-id -- give a single number in decimal or 0xhex");
1235
1236 if (from_tty)
1237 printf_unfiltered ("Attaching pid %s.\n",
1238 local_hex_string((unsigned long) pid));
1239
1240 memset ((char *)&ptrace_in, '\0', sizeof (ptrace_in));
1241 memset ((char *)&ptrace_out, '\0', sizeof (ptrace_out));
1242 ptrace_in.pid = pid;
1243
1244 status = net_ptrace_clnt_call (PTRACE_ATTACH, &ptrace_in, &ptrace_out);
1245 if (status == -1)
1246 error (rpcerr);
1247 if (ptrace_out.status == -1)
1248 {
1249 errno = ptrace_out.errno;
1250 perror_with_name ("Attaching remote process");
1251 }
1252
1253 /* It worked... */
1254 push_target (&vx_run_ops);
1255 /* The unsigned long pid will get turned into a signed int here,
1256 but it doesn't seem to matter. inferior_pid must be signed
1257 in order for other parts of GDB to work correctly. */
1258 inferior_pid = pid;
1259 vx_running = 0;
1260 #if defined (START_INFERIOR_HOOK)
1261 START_INFERIOR_HOOK ();
1262 #endif
1263
1264 mark_breakpoints_out ();
1265
1266 /* Set up the "saved terminal modes" of the inferior
1267 based on what modes we are starting it with. */
1268
1269 target_terminal_init ();
1270
1271 /* Install inferior's terminal modes. */
1272
1273 target_terminal_inferior ();
1274
1275 /* We will get a task spawn event immediately. */
1276
1277 init_wait_for_inferior ();
1278 clear_proceed_status ();
1279 stop_soon_quietly = 1;
1280 wait_for_inferior ();
1281 stop_soon_quietly = 0;
1282 normal_stop ();
1283 }
1284
1285
1286 /* detach_command --
1287 takes a program previously attached to and detaches it.
1288 The program resumes execution and will no longer stop
1289 on signals, etc. We better not have left any breakpoints
1290 in the program or it'll die when it hits one. For this
1291 to work, it may be necessary for the process to have been
1292 previously attached. It *might* work if the program was
1293 started via the normal ptrace (PTRACE_TRACEME). */
1294
1295 static void
1296 vx_detach (args, from_tty)
1297 char *args;
1298 int from_tty;
1299 {
1300 Rptrace ptrace_in;
1301 Ptrace_return ptrace_out;
1302 int signal = 0;
1303 int status;
1304
1305 if (args)
1306 error ("Argument given to VxWorks \"detach\".");
1307
1308 if (from_tty)
1309 printf_unfiltered ("Detaching pid %s.\n",
1310 local_hex_string((unsigned long) inferior_pid));
1311
1312 if (args) /* FIXME, should be possible to leave suspended */
1313 signal = atoi (args);
1314
1315 memset ((char *)&ptrace_in, '\0', sizeof (ptrace_in));
1316 memset ((char *)&ptrace_out, '\0', sizeof (ptrace_out));
1317 ptrace_in.pid = inferior_pid;
1318
1319 status = net_ptrace_clnt_call (PTRACE_DETACH, &ptrace_in, &ptrace_out);
1320 if (status == -1)
1321 error (rpcerr);
1322 if (ptrace_out.status == -1)
1323 {
1324 errno = ptrace_out.errno;
1325 perror_with_name ("Detaching VxWorks process");
1326 }
1327
1328 inferior_pid = 0;
1329 pop_target (); /* go back to non-executing VxWorks connection */
1330 }
1331
1332 /* vx_kill -- takes a running task and wipes it out. */
1333
1334 static void
1335 vx_kill ()
1336 {
1337 Rptrace ptrace_in;
1338 Ptrace_return ptrace_out;
1339 int status;
1340
1341 printf_unfiltered ("Killing pid %s.\n", local_hex_string((unsigned long) inferior_pid));
1342
1343 memset ((char *)&ptrace_in, '\0', sizeof (ptrace_in));
1344 memset ((char *)&ptrace_out, '\0', sizeof (ptrace_out));
1345 ptrace_in.pid = inferior_pid;
1346
1347 status = net_ptrace_clnt_call (PTRACE_KILL, &ptrace_in, &ptrace_out);
1348 if (status == -1)
1349 warning (rpcerr);
1350 else if (ptrace_out.status == -1)
1351 {
1352 errno = ptrace_out.errno;
1353 perror_with_name ("Killing VxWorks process");
1354 }
1355
1356 /* If it gives good status, the process is *gone*, no events remain.
1357 If the kill failed, assume the process is gone anyhow. */
1358 inferior_pid = 0;
1359 pop_target (); /* go back to non-executing VxWorks connection */
1360 }
1361
1362 /* Clean up from the VxWorks process target as it goes away. */
1363
1364 static void
1365 vx_proc_close (quitting)
1366 int quitting;
1367 {
1368 inferior_pid = 0; /* No longer have a process. */
1369 if (vx_running)
1370 free (vx_running);
1371 vx_running = 0;
1372 }
1373 \f
1374 /* Make an RPC call to the VxWorks target.
1375 Returns RPC status. */
1376
1377 static enum clnt_stat
1378 net_clnt_call (procNum, inProc, in, outProc, out)
1379 enum ptracereq procNum;
1380 xdrproc_t inProc;
1381 char *in;
1382 xdrproc_t outProc;
1383 char *out;
1384 {
1385 enum clnt_stat status;
1386
1387 status = clnt_call (pClient, procNum, inProc, in, outProc, out, rpcTimeout);
1388
1389 if (status != RPC_SUCCESS)
1390 clnt_perrno (status);
1391
1392 return status;
1393 }
1394
1395 /* Clean up before losing control. */
1396
1397 static void
1398 vx_close (quitting)
1399 int quitting;
1400 {
1401 if (pClient)
1402 clnt_destroy (pClient); /* The net connection */
1403 pClient = 0;
1404
1405 if (vx_host)
1406 free (vx_host); /* The hostname */
1407 vx_host = 0;
1408 }
1409
1410 /* A vxprocess target should be started via "run" not "target". */
1411 /*ARGSUSED*/
1412 static void
1413 vx_proc_open (name, from_tty)
1414 char *name;
1415 int from_tty;
1416 {
1417 error ("Use the \"run\" command to start a VxWorks process.");
1418 }
1419
1420 /* Target ops structure for accessing memory and such over the net */
1421
1422 struct target_ops vx_ops = {
1423 "vxworks", "VxWorks target memory via RPC over TCP/IP",
1424 "Use VxWorks target memory. \n\
1425 Specify the name of the machine to connect to.",
1426 vx_open, vx_close, vx_attach, 0, /* vx_detach, */
1427 0, 0, /* resume, wait */
1428 0, 0, /* read_reg, write_reg */
1429 0, /* prep_to_store, */
1430 vx_xfer_memory, vx_files_info,
1431 0, 0, /* insert_breakpoint, remove_breakpoint */
1432 0, 0, 0, 0, 0, /* terminal stuff */
1433 0, /* vx_kill, */
1434 vx_load_command,
1435 vx_lookup_symbol,
1436 vx_create_inferior, 0, /* mourn_inferior */
1437 0, /* can_run */
1438 0, /* notice_signals */
1439 0, /* to_stop */
1440 core_stratum, 0, /* next */
1441 1, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
1442 0, 0, /* Section pointers */
1443 OPS_MAGIC, /* Always the last thing */
1444 };
1445
1446 /* Target ops structure for accessing VxWorks child processes over the net */
1447
1448 struct target_ops vx_run_ops = {
1449 "vxprocess", "VxWorks process",
1450 "VxWorks process, started by the \"run\" command.",
1451 vx_proc_open, vx_proc_close, 0, vx_detach, /* vx_attach */
1452 vx_resume, vx_wait,
1453 vx_read_register, vx_write_register,
1454 vx_prepare_to_store,
1455 vx_xfer_memory, vx_run_files_info,
1456 vx_insert_breakpoint, vx_remove_breakpoint,
1457 0, 0, 0, 0, 0, /* terminal stuff */
1458 vx_kill,
1459 vx_load_command,
1460 vx_lookup_symbol,
1461 0, vx_mourn_inferior,
1462 0, /* can_run */
1463 0, /* notice_signals */
1464 0, /* to_stop */
1465 process_stratum, 0, /* next */
1466 0, /* all_mem--off to avoid spurious msg in "i files" */
1467 1, 1, 1, 1, /* mem, stack, regs, exec */
1468 0, 0, /* Section pointers */
1469 OPS_MAGIC, /* Always the last thing */
1470 };
1471 /* ==> Remember when reading at end of file, there are two "ops" structs here. */
1472 \f
1473 void
1474 _initialize_vx ()
1475 {
1476 add_show_from_set
1477 (add_set_cmd ("vxworks-timeout", class_support, var_uinteger,
1478 (char *) &rpcTimeout.tv_sec,
1479 "Set seconds to wait for rpc calls to return.\n\
1480 Set the number of seconds to wait for rpc calls to return.", &setlist),
1481 &showlist);
1482
1483 add_target (&vx_ops);
1484 add_target (&vx_run_ops);
1485 }
This page took 0.060041 seconds and 4 git commands to generate.