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