1 /* Memory-access and commands for remote VxWorks processes, for GDB.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000,
3 2001 Free Software Foundation, Inc.
4 Contributed by Wind River Systems and Cygnus Support.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
30 #include "complaints.h"
32 #include "bfd.h" /* Required by objfiles.h. */
35 #include "gdb-stabs.h"
38 #include "gdb_string.h"
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #define malloc bogon_malloc /* Sun claims "char *malloc()" not void * */
45 #define free bogon_free /* Sun claims "int free()" not void */
46 #define realloc bogon_realloc /* Sun claims "char *realloc()", not void * */
51 #include <sys/time.h> /* UTek's <rpc/rpc.h> doesn't #incl this */
53 #include "vx-share/ptrace.h"
54 #include "vx-share/xdr_ptrace.h"
55 #include "vx-share/xdr_ld.h"
56 #include "vx-share/xdr_rdb.h"
57 #include "vx-share/dbgRpcLib.h"
61 /* Maximum number of bytes to transfer in a single
62 PTRACE_{READ,WRITE}DATA request. */
63 #define VX_MEMXFER_MAX 4096
65 extern void vx_read_register ();
66 extern void vx_write_register ();
67 extern void symbol_file_command ();
68 extern int stop_soon_quietly; /* for wait_for_inferior */
70 static int net_step ();
71 static int net_ptrace_clnt_call (); /* Forward decl */
72 static enum clnt_stat net_clnt_call (); /* Forward decl */
74 /* Target ops structure for accessing memory and such over the net */
76 static struct target_ops vx_ops;
78 /* Target ops structure for accessing VxWorks child processes over the net */
80 static struct target_ops vx_run_ops;
82 /* Saved name of target host and called function for "info files".
86 static char *vx_running; /* Called function */
88 /* Nonzero means target that is being debugged remotely has a floating
93 /* Default error message when the network is forking up. */
95 static const char rpcerr[] = "network target debugging: rpc error";
97 CLIENT *pClient; /* client used in net debugging */
98 static int ptraceSock = RPC_ANYSOCK;
100 enum clnt_stat net_clnt_call ();
101 static void parse_args ();
103 static struct timeval rpcTimeout =
106 static char *skip_white_space ();
107 static char *find_white_space ();
109 /* Tell the VxWorks target system to download a file.
110 The load addresses of the text, data, and bss segments are
111 stored in *pTextAddr, *pDataAddr, and *pBssAddr (respectively).
112 Returns 0 for success, -1 for failure. */
115 net_load (char *filename, CORE_ADDR *pTextAddr, CORE_ADDR *pDataAddr,
118 enum clnt_stat status;
119 struct ldfile ldstruct;
120 struct timeval load_timeout;
122 memset ((char *) &ldstruct, '\0', sizeof (ldstruct));
124 /* We invoke clnt_call () here directly, instead of through
125 net_clnt_call (), because we need to set a large timeout value.
126 The load on the target side can take quite a while, easily
127 more than 10 seconds. The user can kill this call by typing
128 CTRL-C if there really is a problem with the load.
130 Do not change the tv_sec value without checking -- select() imposes
131 a limit of 10**8 on it for no good reason that I can see... */
133 load_timeout.tv_sec = 99999999; /* A large number, effectively inf. */
134 load_timeout.tv_usec = 0;
136 status = clnt_call (pClient, VX_LOAD, xdr_wrapstring, &filename, xdr_ldfile,
137 &ldstruct, load_timeout);
139 if (status == RPC_SUCCESS)
141 if (*ldstruct.name == 0) /* load failed on VxWorks side */
143 *pTextAddr = ldstruct.txt_addr;
144 *pDataAddr = ldstruct.data_addr;
145 *pBssAddr = ldstruct.bss_addr;
152 /* returns 0 if successful, errno if RPC failed or VxWorks complains. */
155 net_break (int addr, u_long procnum)
157 enum clnt_stat status;
159 Rptrace ptrace_in; /* XXX This is stupid. It doesn't need to be a ptrace
160 structure. How about something smaller? */
162 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
165 ptrace_in.addr = addr;
166 ptrace_in.pid = PIDGET (inferior_ptid);
168 status = net_clnt_call (procnum, xdr_rptrace, &ptrace_in, xdr_int,
171 if (status != RPC_SUCCESS)
174 if (break_status == -1)
176 return break_status; /* probably (FIXME) zero */
179 /* returns 0 if successful, errno otherwise */
182 vx_insert_breakpoint (int addr)
184 return net_break (addr, VX_BREAK_ADD);
187 /* returns 0 if successful, errno otherwise */
190 vx_remove_breakpoint (int addr)
192 return net_break (addr, VX_BREAK_DELETE);
195 /* Start an inferior process and sets inferior_ptid to its pid.
196 EXEC_FILE is the file to run.
197 ALLARGS is a string containing the arguments to the program.
198 ENV is the environment vector to pass.
199 Returns process id. Errors reported with error().
200 On VxWorks, we ignore exec_file. */
203 vx_create_inferior (char *exec_file, char *args, char **env)
205 enum clnt_stat status;
207 TASK_START taskStart;
209 memset ((char *) &passArgs, '\0', sizeof (passArgs));
210 memset ((char *) &taskStart, '\0', sizeof (taskStart));
212 /* parse arguments, put them in passArgs */
214 parse_args (args, &passArgs);
216 if (passArgs.arg_array_len == 0)
217 error ("You must specify a function name to run, and arguments if any");
219 status = net_clnt_call (PROCESS_START, xdr_arg_array, &passArgs,
220 xdr_TASK_START, &taskStart);
222 if ((status != RPC_SUCCESS) || (taskStart.status == -1))
223 error ("Can't create process on remote target machine");
225 /* Save the name of the running function */
226 vx_running = savestring (passArgs.arg_array_val[0],
227 strlen (passArgs.arg_array_val[0]));
229 push_target (&vx_run_ops);
230 inferior_ptid = pid_to_ptid (taskStart.pid);
232 /* We will get a trace trap after one instruction.
233 Insert breakpoints and continue. */
235 init_wait_for_inferior ();
237 /* Set up the "saved terminal modes" of the inferior
238 based on what modes we are starting it with. */
239 target_terminal_init ();
241 /* Install inferior's terminal modes. */
242 target_terminal_inferior ();
244 stop_soon_quietly = 1;
245 wait_for_inferior (); /* Get the task spawn event */
246 stop_soon_quietly = 0;
248 /* insert_step_breakpoint (); FIXME, do we need this? */
249 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
252 /* Fill ARGSTRUCT in argc/argv form with the arguments from the
253 argument string ARGSTRING. */
256 parse_args (register char *arg_string, arg_array *arg_struct)
258 register int arg_count = 0; /* number of arguments */
259 register int arg_index = 0;
262 memset ((char *) arg_struct, '\0', sizeof (arg_array));
264 /* first count how many arguments there are */
269 if (*(p0 = skip_white_space (p0)) == '\0')
271 p0 = find_white_space (p0);
275 arg_struct->arg_array_len = arg_count;
276 arg_struct->arg_array_val = (char **) xmalloc ((arg_count + 1)
279 /* now copy argument strings into arg_struct. */
281 while (*(arg_string = skip_white_space (arg_string)))
283 p0 = find_white_space (arg_string);
284 arg_struct->arg_array_val[arg_index++] = savestring (arg_string,
289 arg_struct->arg_array_val[arg_count] = NULL;
292 /* Advance a string pointer across whitespace and return a pointer
293 to the first non-white character. */
296 skip_white_space (register char *p)
298 while (*p == ' ' || *p == '\t')
303 /* Search for the first unquoted whitespace character in a string.
304 Returns a pointer to the character, or to the null terminator
305 if no whitespace is found. */
308 find_white_space (register char *p)
312 while ((c = *p) != ' ' && c != '\t' && c)
314 if (c == '\'' || c == '"')
316 while (*++p != c && *p)
329 /* Poll the VxWorks target system for an event related
330 to the debugged task.
331 Returns -1 if remote wait failed, task status otherwise. */
334 net_wait (RDB_EVENT *pEvent)
337 enum clnt_stat status;
339 memset ((char *) pEvent, '\0', sizeof (RDB_EVENT));
341 pid = PIDGET inferior_ptid);
342 status = net_clnt_call (PROCESS_WAIT, xdr_int, &pid, xdr_RDB_EVENT,
345 /* return (status == RPC_SUCCESS)? pEvent->status: -1; */
346 if (status == RPC_SUCCESS)
347 return ((pEvent->status) ? 1 : 0);
348 else if (status == RPC_TIMEDOUT)
354 /* Suspend the remote task.
355 Returns -1 if suspend fails on target system, 0 otherwise. */
362 enum clnt_stat status;
366 /* don't let rdbTask suspend itself by passing a pid of 0 */
368 if ((pid = PIDGET (inferior_ptid)) == 0)
371 status = net_clnt_call (VX_TASK_SUSPEND, xdr_int, &pid, xdr_int,
374 return (status == RPC_SUCCESS) ? quit_status : -1;
377 /* Read a register or registers from the remote system. */
380 net_read_registers (char *reg_buf, int len, u_long procnum)
384 Ptrace_return ptrace_out;
388 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
389 memset ((char *) &ptrace_out, '\0', sizeof (ptrace_out));
391 /* Initialize RPC input argument structure. */
393 ptrace_in.pid = PIDGET (inferior_ptid);
394 ptrace_in.info.ttype = NOINFO;
396 /* Initialize RPC return value structure. */
398 out_data.bytes = reg_buf;
400 ptrace_out.info.more_data = (caddr_t) & out_data;
402 /* Call RPC; take an error exit if appropriate. */
404 status = net_ptrace_clnt_call (procnum, &ptrace_in, &ptrace_out);
407 if (ptrace_out.status == -1)
409 errno = ptrace_out.errno_num;
410 sprintf (message, "reading %s registers", (procnum == PTRACE_GETREGS)
413 perror_with_name (message);
417 /* Write register values to a VxWorks target. REG_BUF points to a buffer
418 containing the raw register values, LEN is the length of REG_BUF in
419 bytes, and PROCNUM is the RPC procedure number (PTRACE_SETREGS or
420 PTRACE_SETFPREGS). An error exit is taken if the RPC call fails or
421 if an error status is returned by the remote debug server. This is
422 a utility routine used by vx_write_register (). */
425 net_write_registers (char *reg_buf, int len, u_long procnum)
429 Ptrace_return ptrace_out;
433 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
434 memset ((char *) &ptrace_out, '\0', sizeof (ptrace_out));
436 /* Initialize RPC input argument structure. */
438 in_data.bytes = reg_buf;
441 ptrace_in.pid = PIDGET (inferior_ptid);
442 ptrace_in.info.ttype = DATA;
443 ptrace_in.info.more_data = (caddr_t) & in_data;
445 /* Call RPC; take an error exit if appropriate. */
447 status = net_ptrace_clnt_call (procnum, &ptrace_in, &ptrace_out);
450 if (ptrace_out.status == -1)
452 errno = ptrace_out.errno_num;
453 sprintf (message, "writing %s registers", (procnum == PTRACE_SETREGS)
456 perror_with_name (message);
460 /* Prepare to store registers. Since we will store all of them,
461 read out their current values now. */
464 vx_prepare_to_store (void)
466 /* Fetch all registers, if any of them are not yet fetched. */
467 read_register_bytes (0, NULL, REGISTER_BYTES);
470 /* Copy LEN bytes to or from remote inferior's memory starting at MEMADDR
471 to debugger memory starting at MYADDR. WRITE is true if writing to the
472 inferior. TARGET is unused.
473 Result is the number of bytes written or read (zero if error). The
474 protocol allows us to return a negative count, indicating that we can't
475 handle the current address but can handle one N bytes further, but
476 vxworks doesn't give us that information. */
479 vx_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
480 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
481 struct target_ops *target ATTRIBUTE_UNUSED)
485 Ptrace_return ptrace_out;
487 enum ptracereq request;
490 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
491 memset ((char *) &ptrace_out, '\0', sizeof (ptrace_out));
493 ptrace_in.pid = PIDGET (inferior_ptid); /* XXX pid unnecessary for READDATA */
494 ptrace_in.addr = (int) memaddr; /* Where from */
495 ptrace_in.data = len; /* How many bytes */
499 ptrace_in.info.ttype = DATA;
500 ptrace_in.info.more_data = (caddr_t) & data;
502 data.bytes = (caddr_t) myaddr; /* Where from */
503 data.len = len; /* How many bytes (again, for XDR) */
504 request = PTRACE_WRITEDATA;
508 ptrace_out.info.more_data = (caddr_t) & data;
509 request = PTRACE_READDATA;
511 /* Loop until the entire request has been satisfied, transferring
512 at most VX_MEMXFER_MAX bytes per iteration. Break from the loop
513 if an error status is returned by the remote debug server. */
518 while (nleft > 0 && status == 0)
520 nxfer = min (nleft, VX_MEMXFER_MAX);
522 ptrace_in.addr = (int) memaddr;
523 ptrace_in.data = nxfer;
524 data.bytes = (caddr_t) myaddr;
527 /* Request a block from the remote debug server; if RPC fails,
528 report an error and return to debugger command level. */
530 if (net_ptrace_clnt_call (request, &ptrace_in, &ptrace_out))
533 status = ptrace_out.status;
542 /* A target-side error has ocurred. Set errno to the error
543 code chosen by the target so that a later perror () will
544 say something meaningful. */
546 errno = ptrace_out.errno_num;
550 /* Return the number of bytes transferred. */
552 return (len - nleft);
558 printf_unfiltered ("\tAttached to host `%s'", vx_host);
559 printf_unfiltered (", which has %sfloating point", target_has_fp ? "" : "no ");
560 printf_unfiltered (".\n");
564 vx_run_files_info (void)
566 printf_unfiltered ("\tRunning %s VxWorks process %s",
567 vx_running ? "child" : "attached",
568 local_hex_string (PIDGET (inferior_ptid)));
570 printf_unfiltered (", function `%s'", vx_running);
571 printf_unfiltered (".\n");
575 vx_resume (ptid_t ptid, int step, enum target_signal siggnal)
579 Ptrace_return ptrace_out;
582 if (ptid_equal (ptid, minus_one_ptid))
583 ptid = inferior_ptid;
585 if (siggnal != 0 && siggnal != stop_signal)
586 error ("Cannot send signals to VxWorks processes");
588 /* Set CONT_ADDR to the address at which we are continuing,
589 or to 1 if we are continuing from where the program stopped.
590 This conforms to traditional ptrace () usage, but at the same
591 time has special meaning for the VxWorks remote debug server.
592 If the address is not 1, the server knows that the target
593 program is jumping to a new address, which requires special
594 handling if there is a breakpoint at the new address. */
596 cont_addr = read_register (PC_REGNUM);
597 if (cont_addr == stop_pc)
600 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
601 memset ((char *) &ptrace_out, '\0', sizeof (ptrace_out));
603 ptrace_in.pid = PIDGET (ptid);
604 ptrace_in.addr = cont_addr; /* Target side insists on this, or it panics. */
607 status = net_step ();
609 status = net_ptrace_clnt_call (PTRACE_CONT, &ptrace_in, &ptrace_out);
613 if (ptrace_out.status == -1)
615 errno = ptrace_out.errno_num;
616 perror_with_name ("Resuming remote process");
621 vx_mourn_inferior (void)
623 pop_target (); /* Pop back to no-child state */
624 generic_mourn_inferior ();
628 static void vx_add_symbols (char *, int, CORE_ADDR, CORE_ADDR, CORE_ADDR);
630 struct find_sect_args
632 CORE_ADDR text_start;
633 CORE_ADDR data_start;
637 static void find_sect (bfd *, asection *, void *);
640 find_sect (bfd *abfd, asection *sect, PTR obj)
642 struct find_sect_args *args = (struct find_sect_args *) obj;
644 if (bfd_get_section_flags (abfd, sect) & (SEC_CODE & SEC_READONLY))
645 args->text_start = bfd_get_section_vma (abfd, sect);
646 else if (bfd_get_section_flags (abfd, sect) & SEC_ALLOC)
648 if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
650 /* Exclude .ctor and .dtor sections which have SEC_CODE set but not
652 if (bfd_get_section_flags (abfd, sect) & SEC_DATA)
653 args->data_start = bfd_get_section_vma (abfd, sect);
656 args->bss_start = bfd_get_section_vma (abfd, sect);
661 vx_add_symbols (char *name, int from_tty, CORE_ADDR text_addr,
662 CORE_ADDR data_addr, CORE_ADDR bss_addr)
664 struct section_offsets *offs;
665 struct objfile *objfile;
666 struct find_sect_args ss;
668 /* It might be nice to suppress the breakpoint_re_set which happens here
669 because we are going to do one again after the objfile_relocate. */
670 objfile = symbol_file_add (name, from_tty, NULL, 0, 0);
672 /* This is a (slightly cheesy) way of superceding the old symbols. A less
673 cheesy way would be to find the objfile with the same name and
675 objfile_to_front (objfile);
677 offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
678 memcpy (offs, objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
683 bfd_map_over_sections (objfile->obfd, find_sect, &ss);
685 /* Both COFF and b.out frontends use these SECT_OFF_* values. */
686 offs->offsets[SECT_OFF_TEXT (objfile)] = text_addr - ss.text_start;
687 offs->offsets[SECT_OFF_DATA (objfile)] = data_addr - ss.data_start;
688 offs->offsets[SECT_OFF_BSS (objfile)] = bss_addr - ss.bss_start;
689 objfile_relocate (objfile, offs);
692 /* This function allows the addition of incrementally linked object files. */
695 vx_load_command (char *arg_string, int from_tty)
702 error ("The load command takes a file name");
704 arg_string = tilde_expand (arg_string);
705 make_cleanup (xfree, arg_string);
709 /* Refuse to load the module if a debugged task is running. Doing so
710 can have a number of unpleasant consequences to the running task. */
712 if (PIDGET (inferior_ptid) != 0 && target_has_execution)
714 if (query ("You may not load a module while the target task is running.\n\
715 Kill the target task? "))
718 error ("Load canceled.");
723 if (net_load (arg_string, &text_addr, &data_addr, &bss_addr) == -1)
724 error ("Load failed on target machine");
727 vx_add_symbols (arg_string, from_tty, text_addr, data_addr, bss_addr);
729 /* Getting new symbols may change our opinion about what is
731 reinit_frame_cache ();
734 /* Single step the target program at the source or machine level.
735 Takes an error exit if rpc fails.
736 Returns -1 if remote single-step operation fails, else 0. */
741 enum clnt_stat status;
743 SOURCE_STEP source_step;
745 source_step.taskId = PIDGET (inferior_ptid);
749 source_step.startAddr = step_range_start;
750 source_step.endAddr = step_range_end;
754 source_step.startAddr = 0;
755 source_step.endAddr = 0;
758 status = net_clnt_call (VX_SOURCE_STEP, xdr_SOURCE_STEP, &source_step,
759 xdr_int, &step_status);
761 if (status == RPC_SUCCESS)
767 /* Emulate ptrace using RPC calls to the VxWorks target system.
768 Returns nonzero (-1) if RPC status to VxWorks is bad, 0 otherwise. */
771 net_ptrace_clnt_call (enum ptracereq request, Rptrace *pPtraceIn,
772 Ptrace_return *pPtraceOut)
774 enum clnt_stat status;
776 status = net_clnt_call (request, xdr_rptrace, pPtraceIn, xdr_ptrace_return,
779 if (status != RPC_SUCCESS)
785 /* Query the target for the name of the file from which VxWorks was
786 booted. pBootFile is the address of a pointer to the buffer to
787 receive the file name; if the pointer pointed to by pBootFile is
788 NULL, memory for the buffer will be allocated by XDR.
789 Returns -1 if rpc failed, 0 otherwise. */
792 net_get_boot_file (char **pBootFile)
794 enum clnt_stat status;
796 status = net_clnt_call (VX_BOOT_FILE_INQ, xdr_void, (char *) 0,
797 xdr_wrapstring, pBootFile);
798 return (status == RPC_SUCCESS) ? 0 : -1;
801 /* Fetch a list of loaded object modules from the VxWorks target
802 and store in PLOADTABLE.
803 Returns -1 if rpc failed, 0 otherwise
804 There's no way to check if the returned loadTable is correct.
805 VxWorks doesn't check it. */
808 net_get_symbols (ldtabl *pLoadTable)
810 enum clnt_stat status;
812 memset ((char *) pLoadTable, '\0', sizeof (struct ldtabl));
814 status = net_clnt_call (VX_STATE_INQ, xdr_void, 0, xdr_ldtabl, pLoadTable);
815 return (status == RPC_SUCCESS) ? 0 : -1;
818 /* Look up a symbol in the VxWorks target's symbol table.
819 Returns status of symbol read on target side (0=success, -1=fail)
820 Returns -1 and complain()s if rpc fails. */
822 struct complaint cant_contact_target =
823 {"Lost contact with VxWorks target", 0, 0};
826 vx_lookup_symbol (char *name, /* symbol name */
829 enum clnt_stat status;
830 SYMBOL_ADDR symbolAddr;
833 memset ((char *) &symbolAddr, '\0', sizeof (symbolAddr));
835 status = net_clnt_call (VX_SYMBOL_INQ, xdr_wrapstring, &name,
836 xdr_SYMBOL_ADDR, &symbolAddr);
837 if (status != RPC_SUCCESS)
839 complain (&cant_contact_target);
843 *pAddr = symbolAddr.addr;
844 return symbolAddr.status;
847 /* Check to see if the VxWorks target has a floating point coprocessor.
848 Returns 1 if target has floating point processor, 0 otherwise.
849 Calls error() if rpc fails. */
852 net_check_for_fp (void)
854 enum clnt_stat status;
855 bool_t fp = 0; /* true if fp processor is present on target board */
857 status = net_clnt_call (VX_FP_INQUIRE, xdr_void, 0, xdr_bool, &fp);
858 if (status != RPC_SUCCESS)
864 /* Establish an RPC connection with the VxWorks target system.
865 Calls error () if unable to establish connection. */
868 net_connect (char *host)
870 struct sockaddr_in destAddr;
871 struct hostent *destHost;
874 /* Get the internet address for the given host. Allow a numeric
875 IP address or a hostname. */
877 addr = inet_addr (host);
880 destHost = (struct hostent *) gethostbyname (host);
881 if (destHost == NULL)
882 /* FIXME: Probably should include hostname here in quotes.
883 For example if the user types "target vxworks vx960 " it should
884 say "Invalid host `vx960 '." not just "Invalid hostname". */
885 error ("Invalid hostname. Couldn't find remote host address.");
886 addr = *(unsigned long *) destHost->h_addr;
889 memset (&destAddr, '\0', sizeof (destAddr));
891 destAddr.sin_addr.s_addr = addr;
892 destAddr.sin_family = AF_INET;
893 destAddr.sin_port = 0; /* set to actual port that remote
894 ptrace is listening on. */
896 /* Create a tcp client transport on which to issue
897 calls to the remote ptrace server. */
899 ptraceSock = RPC_ANYSOCK;
900 pClient = clnttcp_create (&destAddr, RDBPROG, RDBVERS, &ptraceSock, 0, 0);
901 /* FIXME, here is where we deal with different version numbers of the
906 clnt_pcreateerror ("\tnet_connect");
907 error ("Couldn't connect to remote target.");
911 /* Sleep for the specified number of milliseconds
912 * (assumed to be less than 1000).
913 * If select () is interrupted, returns immediately;
914 * takes an error exit if select () fails for some other reason.
920 struct timeval select_timeout;
923 select_timeout.tv_sec = 0;
924 select_timeout.tv_usec = ms * 1000;
926 status = select (0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0,
929 if (status < 0 && errno != EINTR)
930 perror_with_name ("select");
934 vx_wait (ptid_t ptid_to_wait_for, struct target_waitstatus *status)
942 /* If CTRL-C is hit during this loop,
943 suspend the inferior process. */
948 quit_failed = (net_quit () == -1);
952 /* If a net_quit () or net_wait () call has failed,
953 allow the user to break the connection with the target.
954 We can't simply error () out of this loop, since the
955 data structures representing the state of the inferior
956 are in an inconsistent state. */
958 if (quit_failed || net_wait (&rdbEvent) == -1)
961 if (query ("Can't %s. Disconnect from target system? ",
962 (quit_failed) ? "suspend remote task"
963 : "get status of remote task"))
965 target_mourn_inferior ();
966 error ("Use the \"target\" command to reconnect.");
970 terminal_inferior ();
975 pid = rdbEvent.taskId;
978 sleep_ms (200); /* FIXME Don't kill the network too badly */
980 else if (pid != PIDGET (inferior_ptid))
981 internal_error (__FILE__, __LINE__,
982 "Bad pid for debugged task: %s\n",
983 local_hex_string ((unsigned long) pid));
987 /* The mostly likely kind. */
988 status->kind = TARGET_WAITKIND_STOPPED;
990 switch (rdbEvent.eventType)
993 status->kind = TARGET_WAITKIND_EXITED;
994 /* FIXME is it possible to distinguish between a
995 normal vs abnormal exit in VxWorks? */
996 status->value.integer = 0;
1000 /* Task was just started. */
1001 status->value.sig = TARGET_SIGNAL_TRAP;
1005 status->value.sig = TARGET_SIGNAL_TRAP;
1006 /* XXX was it stopped by a signal? act accordingly */
1009 case EVENT_BREAK: /* Breakpoint was hit. */
1010 status->value.sig = TARGET_SIGNAL_TRAP;
1013 case EVENT_SUSPEND: /* Task was suspended, probably by ^C. */
1014 status->value.sig = TARGET_SIGNAL_INT;
1017 case EVENT_BUS_ERR: /* Task made evil nasty reference. */
1018 status->value.sig = TARGET_SIGNAL_BUS;
1021 case EVENT_ZERO_DIV: /* Division by zero */
1022 status->value.sig = TARGET_SIGNAL_FPE;
1027 status->value.sig = i960_fault_to_signal (rdbEvent.sigType);
1029 /* Back in the old days, before enum target_signal, this code used
1030 to add NSIG to the signal number and claim that PRINT_RANDOM_SIGNAL
1031 would take care of it. But PRINT_RANDOM_SIGNAL has never been
1032 defined except on the i960, so I don't really know what we are
1033 supposed to do on other architectures. */
1034 status->value.sig = TARGET_SIGNAL_UNKNOWN;
1038 return pid_to_ptid (pid);
1042 symbol_stub (char *arg)
1044 symbol_file_add_main (arg, 0);
1049 add_symbol_stub (char *arg)
1051 struct ldfile *pLoadFile = (struct ldfile *) arg;
1053 printf_unfiltered ("\t%s: ", pLoadFile->name);
1054 vx_add_symbols (pLoadFile->name, 0, pLoadFile->txt_addr,
1055 pLoadFile->data_addr, pLoadFile->bss_addr);
1056 printf_unfiltered ("ok\n");
1059 /* Target command for VxWorks target systems.
1061 Used in vxgdb. Takes the name of a remote target machine
1062 running vxWorks and connects to it to initialize remote network
1066 vx_open (char *args, int from_tty)
1068 extern int close ();
1070 extern char *source_path;
1071 struct ldtabl loadTable;
1072 struct ldfile *pLoadFile;
1074 extern CLIENT *pClient;
1075 int symbols_added = 0;
1078 error_no_arg ("target machine name");
1080 target_preopen (from_tty);
1082 unpush_target (&vx_ops);
1083 printf_unfiltered ("Attaching remote machine across net...\n");
1084 gdb_flush (gdb_stdout);
1086 /* Allow the user to kill the connect attempt by typing ^C.
1087 Wait until the call to target_has_fp () completes before
1088 disallowing an immediate quit, since even if net_connect ()
1089 is successful, the remote debug server might be hung. */
1094 target_has_fp = net_check_for_fp ();
1095 printf_filtered ("Connected to %s.\n", args);
1099 push_target (&vx_ops);
1101 /* Save a copy of the target host's name. */
1102 vx_host = savestring (args, strlen (args));
1104 /* Find out the name of the file from which the target was booted
1105 and load its symbol table. */
1107 printf_filtered ("Looking in Unix path for all loaded modules:\n");
1109 if (!net_get_boot_file (&bootFile))
1113 printf_filtered ("\t%s: ", bootFile);
1114 /* This assumes that the kernel is never relocated. Hope that is an
1115 accurate assumption. */
1119 "Error while reading symbols from boot file:\n",
1121 puts_filtered ("ok\n");
1124 printf_unfiltered ("VxWorks kernel symbols not loaded.\n");
1127 error ("Can't retrieve boot file name from target machine.");
1129 clnt_freeres (pClient, xdr_wrapstring, &bootFile);
1131 if (net_get_symbols (&loadTable) != 0)
1132 error ("Can't read loaded modules from target machine");
1135 while (++i < loadTable.tbl_size)
1137 QUIT; /* FIXME, avoids clnt_freeres below: mem leak */
1138 pLoadFile = &loadTable.tbl_ent[i];
1142 struct cleanup *old_chain;
1143 char *fullname = NULL;
1145 desc = openp (source_path, 0, pLoadFile->name, O_RDONLY, 0, &fullname);
1147 perror_with_name (pLoadFile->name);
1148 old_chain = make_cleanup (close, desc);
1149 add_file_at_addr (fullname, desc, pLoadFile->txt_addr, pLoadFile->data_addr,
1150 pLoadFile->bss_addr);
1151 do_cleanups (old_chain);
1154 /* FIXME: Is there something better to search than the PATH? (probably
1155 not the source path, since source might be in different directories
1158 if (catch_errors (add_symbol_stub, (char *) pLoadFile, (char *) 0,
1163 printf_filtered ("Done.\n");
1165 clnt_freeres (pClient, xdr_ldtabl, &loadTable);
1167 /* Getting new symbols may change our opinion about what is
1170 reinit_frame_cache ();
1173 /* Takes a task started up outside of gdb and ``attaches'' to it.
1174 This stops it cold in its tracks and allows us to start tracing it. */
1177 vx_attach (char *args, int from_tty)
1182 Ptrace_return ptrace_out;
1186 error_no_arg ("process-id to attach");
1188 pid = strtoul (args, &cptr, 0);
1189 if ((cptr == args) || (*cptr != '\0'))
1190 error ("Invalid process-id -- give a single number in decimal or 0xhex");
1193 printf_unfiltered ("Attaching pid %s.\n",
1194 local_hex_string ((unsigned long) pid));
1196 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
1197 memset ((char *) &ptrace_out, '\0', sizeof (ptrace_out));
1198 ptrace_in.pid = pid;
1200 status = net_ptrace_clnt_call (PTRACE_ATTACH, &ptrace_in, &ptrace_out);
1203 if (ptrace_out.status == -1)
1205 errno = ptrace_out.errno_num;
1206 perror_with_name ("Attaching remote process");
1211 inferior_ptid = pid_to_ptid (pid);
1212 push_target (&vx_run_ops);
1219 /* detach_command --
1220 takes a program previously attached to and detaches it.
1221 The program resumes execution and will no longer stop
1222 on signals, etc. We better not have left any breakpoints
1223 in the program or it'll die when it hits one. For this
1224 to work, it may be necessary for the process to have been
1225 previously attached. It *might* work if the program was
1226 started via the normal ptrace (PTRACE_TRACEME). */
1229 vx_detach (char *args, int from_tty)
1232 Ptrace_return ptrace_out;
1237 error ("Argument given to VxWorks \"detach\".");
1240 printf_unfiltered ("Detaching pid %s.\n",
1242 (unsigned long) PIDGET (inferior_ptid)));
1244 if (args) /* FIXME, should be possible to leave suspended */
1245 signal = atoi (args);
1247 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
1248 memset ((char *) &ptrace_out, '\0', sizeof (ptrace_out));
1249 ptrace_in.pid = PIDGET (inferior_ptid);
1251 status = net_ptrace_clnt_call (PTRACE_DETACH, &ptrace_in, &ptrace_out);
1254 if (ptrace_out.status == -1)
1256 errno = ptrace_out.errno_num;
1257 perror_with_name ("Detaching VxWorks process");
1260 inferior_ptid = null_ptid;
1261 pop_target (); /* go back to non-executing VxWorks connection */
1264 /* vx_kill -- takes a running task and wipes it out. */
1270 Ptrace_return ptrace_out;
1273 printf_unfiltered ("Killing pid %s.\n", local_hex_string ((unsigned long) PIDGET (inferior_ptid)));
1275 memset ((char *) &ptrace_in, '\0', sizeof (ptrace_in));
1276 memset ((char *) &ptrace_out, '\0', sizeof (ptrace_out));
1277 ptrace_in.pid = PIDGET (inferior_ptid);
1279 status = net_ptrace_clnt_call (PTRACE_KILL, &ptrace_in, &ptrace_out);
1282 else if (ptrace_out.status == -1)
1284 errno = ptrace_out.errno_num;
1285 perror_with_name ("Killing VxWorks process");
1288 /* If it gives good status, the process is *gone*, no events remain.
1289 If the kill failed, assume the process is gone anyhow. */
1290 inferior_ptid = null_ptid;
1291 pop_target (); /* go back to non-executing VxWorks connection */
1294 /* Clean up from the VxWorks process target as it goes away. */
1297 vx_proc_close (int quitting)
1299 inferior_ptid = null_ptid; /* No longer have a process. */
1305 /* Make an RPC call to the VxWorks target.
1306 Returns RPC status. */
1308 static enum clnt_stat
1309 net_clnt_call (enum ptracereq procNum, xdrproc_t inProc, char *in,
1310 xdrproc_t outProc, char *out)
1312 enum clnt_stat status;
1314 status = clnt_call (pClient, procNum, inProc, in, outProc, out, rpcTimeout);
1316 if (status != RPC_SUCCESS)
1317 clnt_perrno (status);
1322 /* Clean up before losing control. */
1325 vx_close (int quitting)
1328 clnt_destroy (pClient); /* The net connection */
1332 xfree (vx_host); /* The hostname */
1336 /* A vxprocess target should be started via "run" not "target". */
1339 vx_proc_open (char *name, int from_tty)
1341 error ("Use the \"run\" command to start a VxWorks process.");
1347 vx_ops.to_shortname = "vxworks";
1348 vx_ops.to_longname = "VxWorks target memory via RPC over TCP/IP";
1349 vx_ops.to_doc = "Use VxWorks target memory. \n\
1350 Specify the name of the machine to connect to.";
1351 vx_ops.to_open = vx_open;
1352 vx_ops.to_close = vx_close;
1353 vx_ops.to_attach = vx_attach;
1354 vx_ops.to_xfer_memory = vx_xfer_memory;
1355 vx_ops.to_files_info = vx_files_info;
1356 vx_ops.to_load = vx_load_command;
1357 vx_ops.to_lookup_symbol = vx_lookup_symbol;
1358 vx_ops.to_create_inferior = vx_create_inferior;
1359 vx_ops.to_stratum = core_stratum;
1360 vx_ops.to_has_all_memory = 1;
1361 vx_ops.to_has_memory = 1;
1362 vx_ops.to_magic = OPS_MAGIC; /* Always the last thing */
1366 init_vx_run_ops (void)
1368 vx_run_ops.to_shortname = "vxprocess";
1369 vx_run_ops.to_longname = "VxWorks process";
1370 vx_run_ops.to_doc = "VxWorks process; started by the \"run\" command.";
1371 vx_run_ops.to_open = vx_proc_open;
1372 vx_run_ops.to_close = vx_proc_close;
1373 vx_run_ops.to_detach = vx_detach;
1374 vx_run_ops.to_resume = vx_resume;
1375 vx_run_ops.to_wait = vx_wait;
1376 vx_run_ops.to_fetch_registers = vx_read_register;
1377 vx_run_ops.to_store_registers = vx_write_register;
1378 vx_run_ops.to_prepare_to_store = vx_prepare_to_store;
1379 vx_run_ops.to_xfer_memory = vx_xfer_memory;
1380 vx_run_ops.to_files_info = vx_run_files_info;
1381 vx_run_ops.to_insert_breakpoint = vx_insert_breakpoint;
1382 vx_run_ops.to_remove_breakpoint = vx_remove_breakpoint;
1383 vx_run_ops.to_kill = vx_kill;
1384 vx_run_ops.to_load = vx_load_command;
1385 vx_run_ops.to_lookup_symbol = vx_lookup_symbol;
1386 vx_run_ops.to_mourn_inferior = vx_mourn_inferior;
1387 vx_run_ops.to_stratum = process_stratum;
1388 vx_run_ops.to_has_memory = 1;
1389 vx_run_ops.to_has_stack = 1;
1390 vx_run_ops.to_has_registers = 1;
1391 vx_run_ops.to_has_execution = 1;
1392 vx_run_ops.to_magic = OPS_MAGIC;
1396 _initialize_vx (void)
1399 add_target (&vx_ops);
1401 add_target (&vx_run_ops);
1404 (add_set_cmd ("vxworks-timeout", class_support, var_uinteger,
1405 (char *) &rpcTimeout.tv_sec,
1406 "Set seconds to wait for rpc calls to return.\n\
1407 Set the number of seconds to wait for rpc calls to return.", &setlist),