* breakpoint.c (enable_longjmp_breakpoint,
[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 free bogon_free /* Sun claims "int free()" not void */
39 #include <rpc/rpc.h>
40 #undef free
41 #include <sys/time.h> /* UTek's <rpc/rpc.h> doesn't #incl this */
42 #include <netdb.h>
43 #include "vx-share/ptrace.h"
44 #include "vx-share/xdr_ptrace.h"
45 #include "vx-share/xdr_ld.h"
46 #include "vx-share/xdr_rdb.h"
47 #include "vx-share/dbgRpcLib.h"
48 #include "vx-share/reg.h"
49
50 #include <symtab.h>
51
52 extern void symbol_file_command ();
53 extern int stop_soon_quietly; /* for wait_for_inferior */
54 extern void host_convert_to_virtual ();
55 extern void host_convert_from_virtual ();
56
57 static int net_ptrace_clnt_call (); /* Forward decl */
58 static enum clnt_stat net_clnt_call (); /* Forward decl */
59 extern 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
64 static char *vx_host;
65 static char *vx_running; /* Called function */
66
67 /* Nonzero means target that is being debugged remotely has a floating
68 point processor. */
69
70 static int target_has_fp;
71
72 /* Default error message when the network is forking up. */
73
74 static const char rpcerr[] = "network target debugging: rpc error";
75
76 CLIENT *pClient; /* client used in net debugging */
77 static int ptraceSock = RPC_ANYSOCK;
78
79 enum clnt_stat net_clnt_call();
80 static void parse_args ();
81
82 static struct timeval rpcTimeout = { 10, 0 };
83
84 static char *skip_white_space ();
85 static 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
89 stored in *pTextAddr, *pDataAddr, and *pBssAddr (respectively).
90 Returns 0 for success, -1 for failure. */
91
92 static int
93 net_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.
110
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 {
122 if (*ldstruct.name == 0) /* load failed on VxWorks side */
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
135 static int
136 net_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
164 static int
165 vx_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
173 static int
174 vx_remove_breakpoint (addr)
175 int addr;
176 {
177 return net_break (addr, VX_BREAK_DELETE);
178 }
179
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
187 static void
188 vx_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
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
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
247 static void
248 parse_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
289 static char *
290 skip_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
302 static char *
303 find_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
329 static int
330 net_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
347 static int
348 net_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
369 static void
370 vx_read_register (regno)
371 int regno;
372 {
373 int status;
374 Rptrace ptrace_in;
375 Ptrace_return ptrace_out;
376 C_bytes in_data;
377 C_bytes out_data;
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;
387 ptrace_out.info.more_data = (caddr_t) &out_data;
388 #ifndef I80960
389 out_data.len = 18 * REGISTER_RAW_SIZE (0); /* FIXME 68k hack */
390 #else
391 out_data.len = (16 + 16 + 3) * REGISTER_RAW_SIZE (0);
392 #endif
393 out_data.bytes = (caddr_t) registers;
394
395 status = net_ptrace_clnt_call (PTRACE_GETREGS, &ptrace_in, &ptrace_out);
396 if (status)
397 error (rpcerr);
398 if (ptrace_out.status == -1)
399 {
400 errno = ptrace_out.errno;
401 perror_with_name ("net_ptrace_clnt_call(PTRACE_GETREGS)");
402 }
403
404 #ifdef I80960
405
406 {
407 /* If the target has floating point registers, fetch them.
408 Otherwise, zero the floating point register values in
409 registers[] for good measure, even though we might not
410 need to. */
411 /* @@ Can't use this -- the rdb library for the 960 target
412 doesn't support setting or retrieving FP regs. KR */
413 #if 0
414 struct fp_status inferior_fp_registers;
415
416 if (target_has_fp)
417 {
418 ptrace_in.pid = inferior_pid;
419 ptrace_out.info.more_data = (caddr_t) &inferior_fp_registers;
420 status = net_ptrace_clnt_call (PTRACE_GETFPREGS,
421 &ptrace_in, &ptrace_out);
422 if (status)
423 error (rpcerr);
424 if (ptrace_out.status == -1)
425 {
426 errno = ptrace_out.errno;
427 perror_with_name ("net_ptrace_clnt_call(PTRACE_GETFPREGS)");
428 }
429
430 bcopy (&inferior_fp_registers, &registers[REGISTER_BYTE (FP0_REGNUM)],
431 REGISTER_RAW_SIZE (FP0_REGNUM) * 4);
432 }
433 else
434 {
435 bzero ((char *) &registers[REGISTER_BYTE (FP0_REGNUM)],
436 REGISTER_RAW_SIZE (FP0_REGNUM) * 4);
437 }
438 #endif
439 }
440 #else /* not 960, thus must be 68000: FIXME! */
441
442 if (target_has_fp)
443 {
444 ptrace_in.pid = inferior_pid;
445 ptrace_out.info.more_data = (caddr_t) &out_data;
446 out_data.len = 8 * REGISTER_RAW_SIZE (FP0_REGNUM) /* FIXME */
447 + (3 * sizeof (REGISTER_TYPE));
448 out_data.bytes = (caddr_t) &registers[REGISTER_BYTE (FP0_REGNUM)];
449
450 status = net_ptrace_clnt_call (PTRACE_GETFPREGS, &ptrace_in, &ptrace_out);
451 if (status)
452 error (rpcerr);
453 if (ptrace_out.status == -1)
454 {
455 errno = ptrace_out.errno;
456 perror_with_name ("net_ptrace_clnt_call(PTRACE_GETFPREGS)");
457 }
458 }
459 else
460 {
461 bzero (&registers[REGISTER_BYTE (FP0_REGNUM)],
462 8 * REGISTER_RAW_SIZE (FP0_REGNUM));
463 bzero (&registers[REGISTER_BYTE (FPC_REGNUM)],
464 3 * sizeof (REGISTER_TYPE));
465 }
466 #endif /* various architectures */
467 }
468
469 /* Prepare to store registers. Since we will store all of them,
470 read out their current values now. */
471
472 static void
473 vx_prepare_to_store ()
474 {
475 vx_read_register (-1);
476 }
477
478
479 /* Store our register values back into the inferior.
480 If REGNO is -1, do this for all registers.
481 Otherwise, REGNO specifies which register (so we can save time). */
482 /* FIXME, look at REGNO to save time here */
483
484 static void
485 vx_write_register (regno)
486 int regno;
487 {
488 C_bytes in_data;
489 C_bytes out_data;
490 extern char registers[];
491 int status;
492 Rptrace ptrace_in;
493 Ptrace_return ptrace_out;
494
495 bzero ((char *) &ptrace_in, sizeof (ptrace_in));
496 bzero ((char *) &ptrace_out, sizeof (ptrace_out));
497
498 ptrace_in.pid = inferior_pid;
499 ptrace_in.info.ttype = DATA;
500 ptrace_in.info.more_data = (caddr_t) &in_data;
501
502 in_data.bytes = registers;
503
504 #ifdef I80960
505
506 in_data.len = (16 + 16 + 3) * sizeof (REGISTER_TYPE);
507
508 #else /* not 960 -- assume 68k -- FIXME */
509
510 in_data.len = 18 * sizeof (REGISTER_TYPE);
511
512 #endif /* Different register sets */
513
514 /* XXX change second param to be a proc number */
515 status = net_ptrace_clnt_call (PTRACE_SETREGS, &ptrace_in, &ptrace_out);
516 if (status)
517 error (rpcerr);
518 if (ptrace_out.status == -1)
519 {
520 errno = ptrace_out.errno;
521 perror_with_name ("net_ptrace_clnt_call(PTRACE_SETREGS)");
522 }
523
524 /* Store floating point registers if the target has them. */
525
526 if (target_has_fp)
527 {
528 ptrace_in.pid = inferior_pid;
529 ptrace_in.info.ttype = DATA;
530 ptrace_in.info.more_data = (caddr_t) &in_data;
531
532
533 #ifdef I80960
534 #if 0 /* @@ Not supported by target. */
535 in_data.bytes = &registers[REGISTER_BYTE (FP0_REGNUM)];
536 in_data.len = 4 * REGISTER_RAW_SIZE (FP0_REGNUM);
537 #endif
538 #else /* not 960 -- assume 68k -- FIXME */
539
540 in_data.bytes = &registers[REGISTER_BYTE (FP0_REGNUM)];
541 in_data.len = (8 * REGISTER_RAW_SIZE (FP0_REGNUM)
542 + (3 * sizeof (REGISTER_TYPE)));
543
544 #endif /* Different register sets */
545
546 status = net_ptrace_clnt_call (PTRACE_SETFPREGS, &ptrace_in, &ptrace_out);
547 if (status)
548 error (rpcerr);
549 if (ptrace_out.status == -1)
550 {
551 errno = ptrace_out.errno;
552 perror_with_name ("net_ptrace_clnt_call(PTRACE_SETFPREGS)");
553 }
554 }
555 }
556
557 /* Copy LEN bytes to or from remote inferior's memory starting at MEMADDR
558 to debugger memory starting at MYADDR. WRITE is true if writing to the
559 inferior.
560 Result is the number of bytes written or read (zero if error). The
561 protocol allows us to return a negative count, indicating that we can't
562 handle the current address but can handle one N bytes further, but
563 vxworks doesn't give us that information. */
564
565 static int
566 vx_xfer_memory (memaddr, myaddr, len, write, target)
567 CORE_ADDR memaddr;
568 char *myaddr;
569 int len;
570 int write;
571 struct target_ops *target; /* ignored */
572 {
573 int status;
574 Rptrace ptrace_in;
575 Ptrace_return ptrace_out;
576 C_bytes data;
577
578 bzero ((char *) &ptrace_in, sizeof (ptrace_in));
579 bzero ((char *) &ptrace_out, sizeof (ptrace_out));
580
581 ptrace_in.pid = inferior_pid; /* XXX pid unnecessary for READDATA */
582 ptrace_in.addr = (int) memaddr; /* Where from */
583 ptrace_in.data = len; /* How many bytes */
584
585 if (write)
586 {
587 ptrace_in.info.ttype = DATA;
588 ptrace_in.info.more_data = (caddr_t) &data;
589
590 data.bytes = (caddr_t) myaddr; /* Where from */
591 data.len = len; /* How many bytes (again, for XDR) */
592
593 /* XXX change second param to be a proc number */
594 status = net_ptrace_clnt_call (PTRACE_WRITEDATA, &ptrace_in, &ptrace_out);
595 }
596 else
597 {
598 ptrace_out.info.more_data = (caddr_t) &data;
599 data.bytes = myaddr; /* Where to */
600 data.len = len; /* How many (again, for XDR) */
601
602 /* XXX change second param to be a proc number */
603 status = net_ptrace_clnt_call (PTRACE_READDATA, &ptrace_in, &ptrace_out);
604 }
605
606 if (status)
607 error (rpcerr);
608 if (ptrace_out.status == -1)
609 {
610 return 0; /* No bytes moved */
611 }
612 return len; /* Moved *all* the bytes */
613 }
614
615 static void
616 vx_files_info ()
617 {
618 printf ("\tAttached to host `%s'", vx_host);
619 printf (", which has %sfloating point", target_has_fp? "": "no ");
620 printf (".\n");
621 }
622
623 static void
624 vx_run_files_info ()
625 {
626 printf ("\tRunning %s VxWorks process %s",
627 vx_running? "child": "attached",
628 local_hex_string(inferior_pid));
629 if (vx_running)
630 printf (", function `%s'", vx_running);
631 printf(".\n");
632 }
633
634 static void
635 vx_resume (step, siggnal)
636 int step;
637 int siggnal;
638 {
639 int status;
640 Rptrace ptrace_in;
641 Ptrace_return ptrace_out;
642
643 if (siggnal != 0 && siggnal != stop_signal)
644 error ("Cannot send signals to VxWorks processes");
645
646 bzero ((char *) &ptrace_in, sizeof (ptrace_in));
647 bzero ((char *) &ptrace_out, sizeof (ptrace_out));
648
649 ptrace_in.pid = inferior_pid;
650 ptrace_in.addr = 1; /* Target side insists on this, or it panics. */
651
652 /* XXX change second param to be a proc number */
653 status = net_ptrace_clnt_call (step? PTRACE_SINGLESTEP: PTRACE_CONT,
654 &ptrace_in, &ptrace_out);
655 if (status)
656 error (rpcerr);
657 if (ptrace_out.status == -1)
658 {
659 errno = ptrace_out.errno;
660 perror_with_name ("Resuming remote process");
661 }
662 }
663
664 static void
665 vx_mourn_inferior ()
666 {
667 pop_target (); /* Pop back to no-child state */
668 generic_mourn_inferior ();
669 }
670
671 \f
672 /* This function allows the addition of incrementally linked object files. */
673
674 static void
675 vx_load_command (arg_string, from_tty)
676 char* arg_string;
677 int from_tty;
678 {
679 CORE_ADDR text_addr;
680 CORE_ADDR data_addr;
681 CORE_ADDR bss_addr;
682
683 if (arg_string == 0)
684 error ("The load command takes a file name");
685
686 arg_string = tilde_expand (arg_string);
687 make_cleanup (free, arg_string);
688
689 dont_repeat ();
690
691 QUIT;
692 immediate_quit++;
693 if (net_load (arg_string, &text_addr, &data_addr, &bss_addr) == -1)
694 error ("Load failed on target machine");
695 immediate_quit--;
696
697 /* FIXME, for now we ignore data_addr and bss_addr. */
698 symbol_file_add (arg_string, from_tty, text_addr, 0, 0, 0);
699 }
700
701 #ifdef FIXME /* Not ready for prime time */
702 /* Single step the target program at the source or machine level.
703 Takes an error exit if rpc fails.
704 Returns -1 if remote single-step operation fails, else 0. */
705
706 static int
707 net_step ()
708 {
709 enum clnt_stat status;
710 int step_status;
711 SOURCE_STEP source_step;
712
713 source_step.taskId = inferior_pid;
714
715 if (step_range_end)
716 {
717 source_step.startAddr = step_range_start;
718 source_step.endAddr = step_range_end;
719 }
720 else
721 {
722 source_step.startAddr = 0;
723 source_step.endAddr = 0;
724 }
725
726 status = net_clnt_call (VX_SOURCE_STEP, xdr_SOURCE_STEP, &source_step,
727 xdr_int, &step_status);
728
729 if (status == RPC_SUCCESS)
730 return step_status;
731 else
732 error (rpcerr);
733 }
734 #endif
735
736 /* Emulate ptrace using RPC calls to the VxWorks target system.
737 Returns nonzero (-1) if RPC status to VxWorks is bad, 0 otherwise. */
738
739 static int
740 net_ptrace_clnt_call (request, pPtraceIn, pPtraceOut)
741 enum ptracereq request;
742 Rptrace *pPtraceIn;
743 Ptrace_return *pPtraceOut;
744 {
745 enum clnt_stat status;
746
747 status = net_clnt_call (request, xdr_rptrace, pPtraceIn, xdr_ptrace_return,
748 pPtraceOut);
749
750 if (status != RPC_SUCCESS)
751 return -1;
752
753 return 0;
754 }
755
756 /* Query the target for the name of the file from which VxWorks was
757 booted. pBootFile is the address of a pointer to the buffer to
758 receive the file name; if the pointer pointed to by pBootFile is
759 NULL, memory for the buffer will be allocated by XDR.
760 Returns -1 if rpc failed, 0 otherwise. */
761
762 static int
763 net_get_boot_file (pBootFile)
764 char **pBootFile;
765 {
766 enum clnt_stat status;
767
768 status = net_clnt_call (VX_BOOT_FILE_INQ, xdr_void, (char *) 0,
769 xdr_wrapstring, pBootFile);
770 return (status == RPC_SUCCESS) ? 0 : -1;
771 }
772
773 /* Fetch a list of loaded object modules from the VxWorks target.
774 Returns -1 if rpc failed, 0 otherwise
775 There's no way to check if the returned loadTable is correct.
776 VxWorks doesn't check it. */
777
778 static int
779 net_get_symbols (pLoadTable)
780 ldtabl *pLoadTable; /* return pointer to ldtabl here */
781 {
782 enum clnt_stat status;
783
784 bzero ((char *) pLoadTable, sizeof (struct ldtabl));
785
786 status = net_clnt_call (VX_STATE_INQ, xdr_void, 0, xdr_ldtabl, pLoadTable);
787 return (status == RPC_SUCCESS) ? 0 : -1;
788 }
789
790 /* Look up a symbol in the VxWorks target's symbol table.
791 Returns status of symbol read on target side (0=success, -1=fail)
792 Returns -1 and complain()s if rpc fails. */
793
794 struct complaint cant_contact_target =
795 {"Lost contact with VxWorks target", 0, 0};
796
797 static int
798 vx_lookup_symbol (name, pAddr)
799 char *name; /* symbol name */
800 CORE_ADDR *pAddr;
801 {
802 enum clnt_stat status;
803 SYMBOL_ADDR symbolAddr;
804
805 *pAddr = 0;
806 bzero ((char *) &symbolAddr, sizeof (symbolAddr));
807
808 status = net_clnt_call (VX_SYMBOL_INQ, xdr_wrapstring, &name,
809 xdr_SYMBOL_ADDR, &symbolAddr);
810 if (status != RPC_SUCCESS) {
811 complain (&cant_contact_target, 0);
812 return -1;
813 }
814
815 *pAddr = symbolAddr.addr;
816 return symbolAddr.status;
817 }
818
819 /* Check to see if the VxWorks target has a floating point coprocessor.
820 Returns 1 if target has floating point processor, 0 otherwise.
821 Calls error() if rpc fails. */
822
823 static int
824 net_check_for_fp ()
825 {
826 enum clnt_stat status;
827 bool_t fp = 0; /* true if fp processor is present on target board */
828
829 status = net_clnt_call (VX_FP_INQUIRE, xdr_void, 0, xdr_bool, &fp);
830 if (status != RPC_SUCCESS)
831 error (rpcerr);
832
833 return (int) fp;
834 }
835
836 /* Establish an RPC connection with the VxWorks target system.
837 Calls error () if unable to establish connection. */
838
839 static void
840 net_connect (host)
841 char *host;
842 {
843 struct sockaddr_in destAddr;
844 struct hostent *destHost;
845
846 /* get the internet address for the given host */
847
848 if ((destHost = (struct hostent *) gethostbyname (host)) == NULL)
849 error ("Invalid hostname. Couldn't find remote host address.");
850
851 bzero (&destAddr, sizeof (destAddr));
852
853 destAddr.sin_addr.s_addr = * (u_long *) destHost->h_addr;
854 destAddr.sin_family = AF_INET;
855 destAddr.sin_port = 0; /* set to actual port that remote
856 ptrace is listening on. */
857
858 /* Create a tcp client transport on which to issue
859 calls to the remote ptrace server. */
860
861 ptraceSock = RPC_ANYSOCK;
862 pClient = clnttcp_create (&destAddr, RDBPROG, RDBVERS, &ptraceSock, 0, 0);
863 /* FIXME, here is where we deal with different version numbers of the proto */
864
865 if (pClient == NULL)
866 {
867 clnt_pcreateerror ("\tnet_connect");
868 error ("Couldn't connect to remote target.");
869 }
870 }
871 \f
872 /* Sleep for the specified number of milliseconds
873 * (assumed to be less than 1000).
874 * If select () is interrupted, returns immediately;
875 * takes an error exit if select () fails for some other reason.
876 */
877
878 static void
879 sleep_ms (ms)
880 long ms;
881 {
882 struct timeval select_timeout;
883 int status;
884
885 select_timeout.tv_sec = 0;
886 select_timeout.tv_usec = ms * 1000;
887
888 status = select (0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &select_timeout);
889
890 if (status < 0 && errno != EINTR)
891 perror_with_name ("select");
892 }
893
894 /* Wait for control to return from inferior to debugger.
895 If inferior gets a signal, we may decide to start it up again
896 instead of returning. That is why there is a loop in this function.
897 When this function actually returns it means the inferior
898 should be left stopped and GDB should read more commands. */
899
900 /* For network debugging with VxWorks.
901 * VxWorks knows when tasks hit breakpoints, receive signals, exit, etc,
902 * so vx_wait() receives this information directly from
903 * VxWorks instead of trying to figure out what happenned via a wait() call.
904 */
905
906 static int
907 vx_wait (status)
908 int *status;
909 {
910 register int pid;
911 WAITTYPE w;
912 RDB_EVENT rdbEvent;
913 int quit_failed;
914
915 do
916 {
917 /* If CTRL-C is hit during this loop,
918 suspend the inferior process. */
919
920 quit_failed = 0;
921 if (quit_flag)
922 {
923 quit_failed = (net_quit () == -1);
924 quit_flag = 0;
925 }
926
927 /* If a net_quit () or net_wait () call has failed,
928 allow the user to break the connection with the target.
929 We can't simply error () out of this loop, since the
930 data structures representing the state of the inferior
931 are in an inconsistent state. */
932
933 if (quit_failed || net_wait (&rdbEvent) == -1)
934 {
935 terminal_ours ();
936 if (query ("Can't %s. Disconnect from target system? ",
937 (quit_failed) ? "suspend remote task"
938 : "get status of remote task"))
939 {
940 target_mourn_inferior();
941 error ("Use the \"target\" command to reconnect.");
942 }
943 else
944 {
945 terminal_inferior ();
946 continue;
947 }
948 }
949
950 pid = rdbEvent.taskId;
951 if (pid == 0)
952 {
953 sleep_ms (200); /* FIXME Don't kill the network too badly */
954 }
955 else if (pid != inferior_pid)
956 fatal ("Bad pid for debugged task: %s\n", local_hex_string(pid));
957 } while (pid == 0);
958
959 /* FIXME, eventually do more then SIGTRAP on everything... */
960 switch (rdbEvent.eventType)
961 {
962 case EVENT_EXIT:
963 WSETEXIT (w, 0);
964 /* FIXME is it possible to distinguish between a
965 XXX normal vs abnormal exit in VxWorks? */
966 break;
967
968 case EVENT_START: /* Task was just started. */
969 WSETSTOP (w, SIGTRAP);
970 break;
971
972 case EVENT_STOP:
973 WSETSTOP (w, SIGTRAP);
974 /* XXX was it stopped by a signal? act accordingly */
975 break;
976
977 case EVENT_BREAK: /* Breakpoint was hit. */
978 WSETSTOP (w, SIGTRAP);
979 break;
980
981 case EVENT_SUSPEND: /* Task was suspended, probably by ^C. */
982 WSETSTOP (w, SIGINT);
983 break;
984
985 case EVENT_BUS_ERR: /* Task made evil nasty reference. */
986 WSETSTOP (w, SIGBUS);
987 break;
988
989 case EVENT_ZERO_DIV: /* Division by zero */
990 WSETSTOP (w, SIGFPE); /* Like Unix, call it a float exception. */
991 break;
992
993 case EVENT_SIGNAL:
994 /* The target is not running Unix, and its
995 faults/traces do not map nicely into Unix signals.
996 Make sure they do not get confused with Unix signals
997 by numbering them with values higher than the highest
998 legal Unix signal. code in the arch-dependent PRINT_RANDOM_SIGNAL
999 routine will interpret the value for wait_for_inferior. */
1000 WSETSTOP (w, rdbEvent.sigType + NSIG);
1001 break;
1002 } /* switch */
1003 *status = *(int *)&w; /* Grumble union wait crap Grumble */
1004 return pid;
1005 }
1006 \f
1007 static int
1008 symbol_stub (arg)
1009 char *arg;
1010 {
1011 symbol_file_command (arg, 0);
1012 return 1;
1013 }
1014
1015 static int
1016 add_symbol_stub (arg)
1017 char *arg;
1018 {
1019 struct ldfile *pLoadFile = (struct ldfile *)arg;
1020
1021 printf("\t%s: ", pLoadFile->name);
1022 symbol_file_add (pLoadFile->name, 0, pLoadFile->txt_addr, 0, 0, 0);
1023 printf ("ok\n");
1024 return 1;
1025 }
1026 /* Target command for VxWorks target systems.
1027
1028 Used in vxgdb. Takes the name of a remote target machine
1029 running vxWorks and connects to it to initialize remote network
1030 debugging. */
1031
1032 static void
1033 vx_open (args, from_tty)
1034 char *args;
1035 int from_tty;
1036 {
1037 extern int close ();
1038 char *bootFile;
1039 extern char *source_path;
1040 struct ldtabl loadTable;
1041 struct ldfile *pLoadFile;
1042 int i;
1043 extern CLIENT *pClient;
1044
1045 if (!args)
1046 error_no_arg ("target machine name");
1047
1048 target_preopen (from_tty);
1049
1050 unpush_target (&vx_ops);
1051 printf ("Attaching remote machine across net...\n");
1052 fflush (stdout);
1053
1054 /* Allow the user to kill the connect attempt by typing ^C.
1055 Wait until the call to target_has_fp () completes before
1056 disallowing an immediate quit, since even if net_connect ()
1057 is successful, the remote debug server might be hung. */
1058
1059 immediate_quit++;
1060
1061 net_connect (args);
1062 target_has_fp = net_check_for_fp ();
1063 printf_filtered ("Connected to %s.\n", args);
1064
1065 immediate_quit--;
1066
1067 push_target (&vx_ops);
1068
1069 /* Save a copy of the target host's name. */
1070 vx_host = savestring (args, strlen (args));
1071
1072 /* Find out the name of the file from which the target was booted
1073 and load its symbol table. */
1074
1075 printf_filtered ("Looking in Unix path for all loaded modules:\n");
1076 bootFile = NULL;
1077 if (!net_get_boot_file (&bootFile))
1078 {
1079 if (*bootFile) {
1080 printf_filtered ("\t%s: ", bootFile);
1081 if (catch_errors (symbol_stub, bootFile,
1082 "Error while reading symbols from boot file:\n"))
1083 puts_filtered ("ok\n");
1084 } else if (from_tty)
1085 printf ("VxWorks kernel symbols not loaded.\n");
1086 }
1087 else
1088 error ("Can't retrieve boot file name from target machine.");
1089
1090 clnt_freeres (pClient, xdr_wrapstring, &bootFile);
1091
1092 if (net_get_symbols (&loadTable) != 0)
1093 error ("Can't read loaded modules from target machine");
1094
1095 i = 0-1;
1096 while (++i < loadTable.tbl_size)
1097 {
1098 QUIT; /* FIXME, avoids clnt_freeres below: mem leak */
1099 pLoadFile = &loadTable.tbl_ent [i];
1100 #ifdef WRS_ORIG
1101 {
1102 register int desc;
1103 struct cleanup *old_chain;
1104 char *fullname = NULL;
1105
1106 desc = openp (source_path, 0, pLoadFile->name, O_RDONLY, 0, &fullname);
1107 if (desc < 0)
1108 perror_with_name (pLoadFile->name);
1109 old_chain = make_cleanup (close, desc);
1110 add_file_at_addr (fullname, desc, pLoadFile->txt_addr, pLoadFile->data_addr,
1111 pLoadFile->bss_addr);
1112 do_cleanups (old_chain);
1113 }
1114 #else
1115 /* Botches, FIXME:
1116 (1) Searches the PATH, not the source path.
1117 (2) data and bss are assumed to be at the usual offsets from text. */
1118 catch_errors (add_symbol_stub, (char *)pLoadFile, (char *)0);
1119 #endif
1120 }
1121 printf_filtered ("Done.\n");
1122
1123 clnt_freeres (pClient, xdr_ldtabl, &loadTable);
1124 }
1125 \f
1126 /* attach_command --
1127 takes a task started up outside of gdb and ``attaches'' to it.
1128 This stops it cold in its tracks and allows us to start tracing it. */
1129
1130 static void
1131 vx_attach (args, from_tty)
1132 char *args;
1133 int from_tty;
1134 {
1135 int pid;
1136 char *cptr = 0;
1137 Rptrace ptrace_in;
1138 Ptrace_return ptrace_out;
1139 int status;
1140
1141 dont_repeat();
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 mark_breakpoints_out ();
1172
1173 /* Set up the "saved terminal modes" of the inferior
1174 based on what modes we are starting it with. */
1175 target_terminal_init ();
1176
1177 /* Install inferior's terminal modes. */
1178 target_terminal_inferior ();
1179
1180 /* We will get a task spawn event immediately. */
1181 init_wait_for_inferior ();
1182 clear_proceed_status ();
1183 stop_soon_quietly = 1;
1184 wait_for_inferior ();
1185 stop_soon_quietly = 0;
1186 normal_stop ();
1187 }
1188
1189
1190 /* detach_command --
1191 takes a program previously attached to and detaches it.
1192 The program resumes execution and will no longer stop
1193 on signals, etc. We better not have left any breakpoints
1194 in the program or it'll die when it hits one. For this
1195 to work, it may be necessary for the process to have been
1196 previously attached. It *might* work if the program was
1197 started via the normal ptrace (PTRACE_TRACEME). */
1198
1199 static void
1200 vx_detach (args, from_tty)
1201 char *args;
1202 int from_tty;
1203 {
1204 Rptrace ptrace_in;
1205 Ptrace_return ptrace_out;
1206 int signal = 0;
1207 int status;
1208
1209 if (args)
1210 error ("Argument given to VxWorks \"detach\".");
1211
1212 if (from_tty)
1213 printf ("Detaching pid %s.\n", local_hex_string(inferior_pid));
1214
1215 if (args) /* FIXME, should be possible to leave suspended */
1216 signal = atoi (args);
1217
1218 bzero ((char *)&ptrace_in, sizeof (ptrace_in));
1219 bzero ((char *)&ptrace_out, sizeof (ptrace_out));
1220 ptrace_in.pid = inferior_pid;
1221
1222 status = net_ptrace_clnt_call (PTRACE_DETACH, &ptrace_in, &ptrace_out);
1223 if (status == -1)
1224 error (rpcerr);
1225 if (ptrace_out.status == -1)
1226 {
1227 errno = ptrace_out.errno;
1228 perror_with_name ("Detaching VxWorks process");
1229 }
1230
1231 inferior_pid = 0;
1232 pop_target (); /* go back to non-executing VxWorks connection */
1233 }
1234
1235 /* vx_kill -- takes a running task and wipes it out. */
1236
1237 static void
1238 vx_kill ()
1239 {
1240 Rptrace ptrace_in;
1241 Ptrace_return ptrace_out;
1242 int status;
1243
1244 printf ("Killing pid %s.\n", local_hex_string(inferior_pid));
1245
1246 bzero ((char *)&ptrace_in, sizeof (ptrace_in));
1247 bzero ((char *)&ptrace_out, sizeof (ptrace_out));
1248 ptrace_in.pid = inferior_pid;
1249
1250 status = net_ptrace_clnt_call (PTRACE_KILL, &ptrace_in, &ptrace_out);
1251 if (status == -1)
1252 error (rpcerr);
1253 if (ptrace_out.status == -1)
1254 {
1255 errno = ptrace_out.errno;
1256 perror_with_name ("Killing VxWorks process");
1257 }
1258
1259 /* If it gives good status, the process is *gone*, no events remain. */
1260 inferior_pid = 0;
1261 pop_target (); /* go back to non-executing VxWorks connection */
1262 }
1263
1264 /* Clean up from the VxWorks process target as it goes away. */
1265
1266 static void
1267 vx_proc_close (quitting)
1268 int quitting;
1269 {
1270 inferior_pid = 0; /* No longer have a process. */
1271 if (vx_running)
1272 free (vx_running);
1273 vx_running = 0;
1274 }
1275 \f
1276 /* Make an RPC call to the VxWorks target.
1277 Returns RPC status. */
1278
1279 static enum clnt_stat
1280 net_clnt_call (procNum, inProc, in, outProc, out)
1281 enum ptracereq procNum;
1282 xdrproc_t inProc;
1283 char *in;
1284 xdrproc_t outProc;
1285 char *out;
1286 {
1287 enum clnt_stat status;
1288
1289 status = clnt_call (pClient, procNum, inProc, in, outProc, out, rpcTimeout);
1290
1291 if (status != RPC_SUCCESS)
1292 clnt_perrno (status);
1293
1294 return status;
1295 }
1296
1297 /* Clean up before losing control. */
1298
1299 static void
1300 vx_close (quitting)
1301 int quitting;
1302 {
1303 if (pClient)
1304 clnt_destroy (pClient); /* The net connection */
1305 pClient = 0;
1306
1307 if (vx_host)
1308 free (vx_host); /* The hostname */
1309 vx_host = 0;
1310 }
1311
1312 /* A vxprocess target should be started via "run" not "target". */
1313 /*ARGSUSED*/
1314 static void
1315 vx_proc_open (name, from_tty)
1316 char *name;
1317 int from_tty;
1318 {
1319 error ("Use the \"run\" command to start a VxWorks process.");
1320 }
1321
1322 /* Target ops structure for accessing memory and such over the net */
1323
1324 struct target_ops vx_ops = {
1325 "vxworks", "VxWorks target memory via RPC over TCP/IP",
1326 "Use VxWorks target memory. \n\
1327 Specify the name of the machine to connect to.",
1328 vx_open, vx_close, vx_attach, 0, /* vx_detach, */
1329 0, 0, /* resume, wait */
1330 0, 0, /* read_reg, write_reg */
1331 0, host_convert_to_virtual, host_convert_from_virtual, /* prep_to_store, */
1332 vx_xfer_memory, vx_files_info,
1333 0, 0, /* insert_breakpoint, remove_breakpoint */
1334 0, 0, 0, 0, 0, /* terminal stuff */
1335 0, /* vx_kill, */
1336 vx_load_command,
1337 vx_lookup_symbol,
1338 vx_create_inferior, 0, /* mourn_inferior */
1339 core_stratum, 0, /* next */
1340 1, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
1341 0, 0, /* Section pointers */
1342 OPS_MAGIC, /* Always the last thing */
1343 };
1344
1345 /* Target ops structure for accessing VxWorks child processes over the net */
1346
1347 struct target_ops vx_run_ops = {
1348 "vxprocess", "VxWorks process",
1349 "VxWorks process, started by the \"run\" command.",
1350 vx_proc_open, vx_proc_close, 0, vx_detach, /* vx_attach */
1351 vx_resume, vx_wait,
1352 vx_read_register, vx_write_register,
1353 vx_prepare_to_store, host_convert_to_virtual, host_convert_from_virtual,
1354 vx_xfer_memory, vx_run_files_info,
1355 vx_insert_breakpoint, vx_remove_breakpoint,
1356 0, 0, 0, 0, 0, /* terminal stuff */
1357 vx_kill,
1358 vx_load_command,
1359 vx_lookup_symbol,
1360 0, vx_mourn_inferior,
1361 process_stratum, 0, /* next */
1362 0, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
1363 /* all_mem is off to avoid spurious msg in "i files" */
1364 0, 0, /* Section pointers */
1365 OPS_MAGIC, /* Always the last thing */
1366 };
1367 /* ==> Remember when reading at end of file, there are two "ops" structs here. */
1368 \f
1369 void
1370 _initialize_vx ()
1371 {
1372 add_target (&vx_ops);
1373 add_target (&vx_run_ops);
1374 }
This page took 0.056537 seconds and 4 git commands to generate.