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