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