This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gdb / remote-rdi.c
1 /* GDB interface to ARM RDI library.
2    Copyright 1997, 1998 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "gdb_string.h"
23 #include <fcntl.h>
24 #include "frame.h"
25 #include "inferior.h"
26 #include "bfd.h"
27 #include "symfile.h"
28 #include "target.h"
29 #include "gdb_wait.h"
30 #include "gdbcmd.h"
31 #include "objfiles.h"
32 #include "gdb-stabs.h"
33 #include "gdbthread.h"
34 #include "gdbcore.h"
35 #include "breakpoint.h"
36
37 #ifdef USG
38 #include <sys/types.h>
39 #endif
40
41 #include <signal.h>
42
43 #include "rdi-share/ardi.h"
44 #include "rdi-share/adp.h"
45 #include "rdi-share/hsys.h"
46
47 extern int isascii PARAMS ((int));
48
49 /* Prototypes for local functions */
50
51 static void arm_rdi_files_info PARAMS ((struct target_ops * ignore));
52
53 static int arm_rdi_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
54                                         int len, int should_write,
55                                         struct target_ops * target));
56
57 static void arm_rdi_prepare_to_store PARAMS ((void));
58
59 static void arm_rdi_fetch_registers PARAMS ((int regno));
60
61 static void arm_rdi_resume PARAMS ((int pid, int step,
62                                     enum target_signal siggnal));
63
64 static int arm_rdi_start_remote PARAMS ((char *dummy));
65
66 static void arm_rdi_open PARAMS ((char *name, int from_tty));
67
68 static void arm_rdi_create_inferior PARAMS ((char *exec_file, char *args,
69                                              char **env));
70
71 static void arm_rdi_close PARAMS ((int quitting));
72
73 static void arm_rdi_store_registers PARAMS ((int regno));
74
75 static void arm_rdi_mourn PARAMS ((void));
76
77 static void arm_rdi_send PARAMS ((char *buf));
78
79 static int arm_rdi_wait PARAMS ((int pid, struct target_waitstatus * status));
80
81 static void arm_rdi_kill PARAMS ((void));
82
83 static void arm_rdi_detach PARAMS ((char *args, int from_tty));
84
85 static void arm_rdi_interrupt PARAMS ((int signo));
86
87 static void arm_rdi_interrupt_twice PARAMS ((int signo));
88
89 static void interrupt_query PARAMS ((void));
90
91 static int arm_rdi_insert_breakpoint PARAMS ((CORE_ADDR, char *));
92
93 static int arm_rdi_remove_breakpoint PARAMS ((CORE_ADDR, char *));
94
95 static char *rdi_error_message PARAMS ((int err));
96
97 static enum target_signal rdi_error_signal PARAMS ((int err));
98
99 /* Global variables.  */
100
101 struct target_ops arm_rdi_ops;
102
103 static struct Dbg_ConfigBlock gdb_config;
104
105 static struct Dbg_HostosInterface gdb_hostif;
106
107 static int max_load_size;
108
109 static int execute_status;
110
111 /* Send heatbeat packets? */
112 static int rdi_heartbeat = 0;
113
114 /* Target has ROM at address 0. */
115 static int rom_at_zero = 0;
116
117 /* Enable logging? */
118 static int log_enable = 0;
119
120 /* Name of the log file. Default is "rdi.log". */
121 static char *log_filename;
122
123 /* A little list of breakpoints that have been set.  */
124
125 static struct local_bp_list_entry
126   {
127     CORE_ADDR addr;
128     PointHandle point;
129     struct local_bp_list_entry *next;
130   }
131  *local_bp_list;
132 \f
133
134 /* Stub for catch_errors.  */
135
136 static int
137 arm_rdi_start_remote (dummy)
138      char *dummy;
139 {
140   return 1;
141 }
142
143 /* Helper callbacks for the "host interface" structure.  RDI functions call
144    these to forward output from the target system and so forth.  */
145
146 void
147 voiddummy ()
148 {
149   fprintf_unfiltered (gdb_stdout, "void dummy\n");
150 }
151
152 static void
153 myprint (arg, format, ap)
154      PTR arg;
155      const char *format;
156      va_list ap;
157 {
158   vfprintf_unfiltered (gdb_stdout, format, ap);
159 }
160
161 static void
162 mywritec (arg, c)
163      PTR arg;
164      int c;
165 {
166   if (isascii (c))
167     fputc_unfiltered (c, gdb_stdout);
168 }
169
170 static int
171 mywrite (arg, buffer, len)
172      PTR arg;
173      char const *buffer;
174      int len;
175 {
176   int i;
177   char *e;
178
179   e = (char *) buffer;
180   for (i = 0; i < len; i++)
181     {
182       if (isascii ((int) *e))
183         {
184           fputc_unfiltered ((int) *e, gdb_stdout);
185           e++;
186         }
187     }
188
189   return len;
190 }
191
192 static void
193 mypause (arg)
194      PTR arg;
195 {
196 }
197
198 /* These last two are tricky as we have to handle the special case of
199    being interrupted more carefully */
200
201 static int
202 myreadc (arg)
203      PTR arg;
204 {
205   return fgetc (stdin);
206 }
207
208 static char *
209 mygets (arg, buffer, len)
210      PTR arg;
211      char *buffer;
212      int len;
213 {
214   return fgets (buffer, len, stdin);
215 }
216
217 /* Prevent multiple calls to angel_RDI_close().  */
218 static int closed_already = 1;
219
220 /* Open a connection to a remote debugger.  NAME is the filename used
221    for communication.  */
222
223 static void
224 arm_rdi_open (name, from_tty)
225      char *name;
226      int from_tty;
227 {
228   int rslt, i;
229   unsigned long arg1, arg2;
230   char *openArgs = NULL;
231   char *devName = NULL;
232   char *p;
233
234   if (name == NULL)
235     error ("To open an RDI connection, you need to specify what serial\n\
236 device is attached to the remote system (e.g. /dev/ttya).");
237
238   /* split name after whitespace, pass tail as arg to open command */
239
240   devName = xstrdup (name);
241   p = strchr (devName, ' ');
242   if (p)
243     {
244       *p = '\0';
245       ++p;
246
247       while (*p == ' ')
248         ++p;
249
250       openArgs = p;
251     }
252
253   /* Make the basic low-level connection.  */
254
255   arm_rdi_close (0);
256   rslt = Adp_OpenDevice (devName, openArgs, rdi_heartbeat);
257
258   if (rslt != adp_ok)
259     error ("Could not open device \"%s\"", name);
260
261   gdb_config.bytesex = 2 | (TARGET_BYTE_ORDER == BIG_ENDIAN ? 1 : 0);
262   gdb_config.fpe = 1;
263   gdb_config.rditype = 2;
264   gdb_config.heartbeat_on = 1;
265   gdb_config.flags = 2;
266
267   gdb_hostif.dbgprint = myprint;
268   gdb_hostif.dbgpause = mypause;
269   gdb_hostif.dbgarg = NULL;
270   gdb_hostif.writec = mywritec;
271   gdb_hostif.readc = myreadc;
272   gdb_hostif.write = mywrite;
273   gdb_hostif.gets = mygets;
274   gdb_hostif.hostosarg = NULL;
275   gdb_hostif.reset = voiddummy;
276
277   rslt = angel_RDI_open (10, &gdb_config, &gdb_hostif, NULL);
278   if (rslt == RDIError_BigEndian || rslt == RDIError_LittleEndian)
279     ;                           /* do nothing, this is the expected return */
280   else if (rslt)
281     {
282       printf_filtered ("RDI_open: %s\n", rdi_error_message (rslt));
283       Adp_CloseDevice ();
284       error ("RID_open failed\n");
285     }
286
287   rslt = angel_RDI_info (RDIInfo_Target, &arg1, &arg2);
288   if (rslt)
289     {
290       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
291     }
292   rslt = angel_RDI_info (RDIInfo_Points, &arg1, &arg2);
293   if (rslt)
294     {
295       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
296     }
297   rslt = angel_RDI_info (RDIInfo_Step, &arg1, &arg2);
298   if (rslt)
299     {
300       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
301     }
302   rslt = angel_RDI_info (RDIInfo_CoPro, &arg1, &arg2);
303   if (rslt)
304     {
305       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
306     }
307   rslt = angel_RDI_info (RDIInfo_SemiHosting, &arg1, &arg2);
308   if (rslt)
309     {
310       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
311     }
312
313   rslt = angel_RDI_info (RDIInfo_GetLoadSize, &arg1, &arg2);
314   if (rslt)
315     {
316       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
317     }
318   max_load_size = arg1;
319
320   push_target (&arm_rdi_ops);
321
322   target_fetch_registers (-1);
323
324   rslt = angel_RDI_open (1, &gdb_config, NULL, NULL);
325   if (rslt)
326     {
327       printf_filtered ("RDI_open: %s\n", rdi_error_message (rslt));
328     }
329
330   arg1 = rom_at_zero ? 0x0 : 0x13b;
331
332   rslt = angel_RDI_info (RDIVector_Catch, &arg1, &arg2);
333   if (rslt)
334     {
335       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
336     }
337
338   arg1 = (unsigned long) "";
339   rslt = angel_RDI_info (RDISet_Cmdline, &arg1, &arg2);
340   if (rslt)
341     {
342       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
343     }
344
345   /* Clear out any existing records of breakpoints.  */
346   {
347     struct local_bp_list_entry *entry, *preventry = NULL;
348
349     for (entry = local_bp_list; entry != NULL; entry = entry->next)
350       {
351         if (preventry)
352           free (preventry);
353       }
354   }
355
356   printf_filtered ("Connected to ARM RDI target.\n");
357   closed_already = 0;
358   inferior_pid = 42;
359 }
360
361 /* Start an inferior process and set inferior_pid to its pid.
362    EXEC_FILE is the file to run.
363    ARGS is a string containing the arguments to the program.
364    ENV is the environment vector to pass.  Errors reported with error().
365    On VxWorks and various standalone systems, we ignore exec_file.  */
366 /* This is called not only when we first attach, but also when the
367    user types "run" after having attached.  */
368
369 static void
370 arm_rdi_create_inferior (exec_file, args, env)
371      char *exec_file;
372      char *args;
373      char **env;
374 {
375   int len, rslt;
376   unsigned long arg1, arg2;
377   char *arg_buf;
378   CORE_ADDR entry_point;
379
380   if (exec_file == 0 || exec_bfd == 0)
381     error ("No executable file specified.");
382
383   entry_point = (CORE_ADDR) bfd_get_start_address (exec_bfd);
384
385   arm_rdi_kill ();
386   remove_breakpoints ();
387   init_wait_for_inferior ();
388
389   len = strlen (exec_file) + 1 + strlen (args) + 1 + /*slop */ 10;
390   arg_buf = (char *) alloca (len);
391   arg_buf[0] = '\0';
392   strcat (arg_buf, exec_file);
393   strcat (arg_buf, " ");
394   strcat (arg_buf, args);
395
396   inferior_pid = 42;
397   insert_breakpoints ();        /* Needed to get correct instruction in cache */
398
399   if (env != NULL)
400     {
401       while (*env)
402         {
403           if (strncmp (*env, "MEMSIZE=", sizeof ("MEMSIZE=") - 1) == 0)
404             {
405               unsigned long top_of_memory;
406               char *end_of_num;
407
408               /* Set up memory limit */
409               top_of_memory = strtoul (*env + sizeof ("MEMSIZE=") - 1,
410                                        &end_of_num, 0);
411               printf_filtered ("Setting top-of-memory to 0x%lx\n",
412                                top_of_memory);
413
414               rslt = angel_RDI_info (RDIInfo_SetTopMem, &top_of_memory, &arg2);
415               if (rslt)
416                 {
417                   printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
418                 }
419             }
420           env++;
421         }
422     }
423
424   arg1 = (unsigned long) arg_buf;
425   rslt = angel_RDI_info (RDISet_Cmdline, /* &arg1 */ (unsigned long *) arg_buf, &arg2);
426   if (rslt)
427     {
428       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
429     }
430
431   proceed (entry_point, TARGET_SIGNAL_DEFAULT, 0);
432 }
433
434 /* This takes a program previously attached to and detaches it.  After
435    this is done, GDB can be used to debug some other program.  We
436    better not have left any breakpoints in the target program or it'll
437    die when it hits one.  */
438
439 static void
440 arm_rdi_detach (args, from_tty)
441      char *args;
442      int from_tty;
443 {
444   pop_target ();
445 }
446
447 /* Clean up connection to a remote debugger.  */
448
449 static void
450 arm_rdi_close (quitting)
451      int quitting;
452 {
453   int rslt;
454
455   if (!closed_already)
456     {
457       rslt = angel_RDI_close ();
458       if (rslt)
459         {
460           printf_filtered ("RDI_close: %s\n", rdi_error_message (rslt));
461         }
462       closed_already = 1;
463       inferior_pid = 0;
464       Adp_CloseDevice ();
465     }
466 }
467 \f
468 /* Tell the remote machine to resume.  */
469
470 static void
471 arm_rdi_resume (pid, step, siggnal)
472      int pid, step;
473      enum target_signal siggnal;
474 {
475   int rslt;
476   PointHandle point;
477
478   if (0 /* turn on when hardware supports single-stepping */ )
479     {
480       rslt = angel_RDI_step (1, &point);
481       if (rslt)
482         {
483           printf_filtered ("RDI_step: %s\n", rdi_error_message (rslt));
484         }
485     }
486   else
487     {
488       char handle[4];
489       CORE_ADDR pc;
490
491       if (step)
492         {
493           pc = read_register (PC_REGNUM);
494           pc = arm_get_next_pc (pc);
495           arm_rdi_insert_breakpoint (pc, handle);
496         }
497       execute_status = rslt = angel_RDI_execute (&point);
498       if (rslt == RDIError_BreakpointReached)
499         ;
500       else if (rslt)
501         {
502           printf_filtered ("RDI_execute: %s\n", rdi_error_message (rslt));
503         }
504       if (step)
505         {
506           arm_rdi_remove_breakpoint (pc, handle);
507         }
508     }
509 }
510 \f
511 /* Send ^C to target to halt it.  Target will respond, and send us a
512    packet.  */
513
514 static void
515 arm_rdi_interrupt (signo)
516      int signo;
517 {
518 }
519
520 static void (*ofunc) ();
521
522 /* The user typed ^C twice.  */
523 static void
524 arm_rdi_interrupt_twice (signo)
525      int signo;
526 {
527 }
528
529 /* Ask the user what to do when an interrupt is received.  */
530
531 static void
532 interrupt_query ()
533 {
534 }
535
536 /* Wait until the remote machine stops, then return, storing status in
537    STATUS just as `wait' would.  Returns "pid" (though it's not clear
538    what, if anything, that means in the case of this target).  */
539
540 static int
541 arm_rdi_wait (pid, status)
542      int pid;
543      struct target_waitstatus *status;
544 {
545   status->kind = (execute_status == RDIError_NoError ?
546                   TARGET_WAITKIND_EXITED : TARGET_WAITKIND_STOPPED);
547
548   /* convert stopped code from target into right signal */
549   status->value.sig = rdi_error_signal (execute_status);
550
551   return inferior_pid;
552 }
553
554 /* Read the remote registers into the block REGS.  */
555
556 /* ARGSUSED */
557 static void
558 arm_rdi_fetch_registers (regno)
559      int regno;
560 {
561   int rslt, rdi_regmask;
562   unsigned long rawreg, rawregs[32];
563   char cookedreg[4];
564
565   if (regno == -1)
566     {
567       rslt = angel_RDI_CPUread (255, 0x27fff, rawregs);
568       if (rslt)
569         {
570           printf_filtered ("RDI_CPUread: %s\n", rdi_error_message (rslt));
571         }
572
573       for (regno = 0; regno < 15; regno++)
574         {
575           store_unsigned_integer (cookedreg, 4, rawregs[regno]);
576           supply_register (regno, (char *) cookedreg);
577         }
578       store_unsigned_integer (cookedreg, 4, rawregs[15]);
579       supply_register (PS_REGNUM, (char *) cookedreg);
580       arm_rdi_fetch_registers (PC_REGNUM);
581     }
582   else
583     {
584       if (regno == PC_REGNUM)
585         rdi_regmask = RDIReg_PC;
586       else if (regno == PS_REGNUM)
587         rdi_regmask = RDIReg_CPSR;
588       else if (regno < 0 || regno > 15)
589         {
590           rawreg = 0;
591           supply_register (regno, (char *) &rawreg);
592           return;
593         }
594       else
595         rdi_regmask = 1 << regno;
596
597       rslt = angel_RDI_CPUread (255, rdi_regmask, &rawreg);
598       if (rslt)
599         {
600           printf_filtered ("RDI_CPUread: %s\n", rdi_error_message (rslt));
601         }
602       store_unsigned_integer (cookedreg, 4, rawreg);
603       supply_register (regno, (char *) cookedreg);
604     }
605 }
606
607 static void
608 arm_rdi_prepare_to_store ()
609 {
610   /* Nothing to do.  */
611 }
612
613 /* Store register REGNO, or all registers if REGNO == -1, from the contents
614    of REGISTERS.  FIXME: ignores errors.  */
615
616 static void
617 arm_rdi_store_registers (regno)
618      int regno;
619 {
620   int rslt, rdi_regmask;
621
622   /* These need to be able to take 'floating point register' contents */
623   unsigned long rawreg[3], rawerreg[3];
624
625   if (regno == -1)
626     {
627       for (regno = 0; regno < NUM_REGS; regno++)
628         arm_rdi_store_registers (regno);
629     }
630   else
631     {
632       read_register_gen (regno, (char *) rawreg);
633       /* RDI manipulates data in host byte order, so convert now. */
634       store_unsigned_integer (rawerreg, 4, rawreg[0]);
635
636       if (regno == PC_REGNUM)
637         rdi_regmask = RDIReg_PC;
638       else if (regno == PS_REGNUM)
639         rdi_regmask = RDIReg_CPSR;
640       else if (regno < 0 || regno > 15)
641         return;
642       else
643         rdi_regmask = 1 << regno;
644
645       rslt = angel_RDI_CPUwrite (255, rdi_regmask, rawerreg);
646       if (rslt)
647         {
648           printf_filtered ("RDI_CPUwrite: %s\n", rdi_error_message (rslt));
649         }
650     }
651 }
652 \f
653 /* Read or write LEN bytes from inferior memory at MEMADDR,
654    transferring to or from debugger address MYADDR.  Write to inferior
655    if SHOULD_WRITE is nonzero.  Returns length of data written or
656    read; 0 for error.  */
657
658 /* ARGSUSED */
659 static int
660 arm_rdi_xfer_memory (memaddr, myaddr, len, should_write, target)
661      CORE_ADDR memaddr;
662      char *myaddr;
663      int len;
664      int should_write;
665      struct target_ops *target; /* ignored */
666 {
667   int rslt, i;
668
669   if (should_write)
670     {
671       rslt = angel_RDI_write (myaddr, memaddr, &len);
672       if (rslt)
673         {
674           printf_filtered ("RDI_write: %s\n", rdi_error_message (rslt));
675         }
676     }
677   else
678     {
679       rslt = angel_RDI_read (memaddr, myaddr, &len);
680       if (rslt)
681         {
682           printf_filtered ("RDI_read: %s\n", rdi_error_message (rslt));
683           len = 0;
684         }
685     }
686   return len;
687 }
688 \f
689 /* Display random info collected from the target.  */
690
691 static void
692 arm_rdi_files_info (ignore)
693      struct target_ops *ignore;
694 {
695   char *file = "nothing";
696   int rslt;
697   unsigned long arg1, arg2;
698
699   rslt = angel_RDI_info (RDIInfo_Target, &arg1, &arg2);
700   if (rslt)
701     {
702       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
703     }
704   if (arg1 & (1 << 15))
705     printf_filtered ("Target supports Thumb code.\n");
706   if (arg1 & (1 << 14))
707     printf_filtered ("Target can do profiling.\n");
708   if (arg1 & (1 << 4))
709     printf_filtered ("Target is real hardware.\n");
710
711   rslt = angel_RDI_info (RDIInfo_Step, &arg1, &arg2);
712   if (rslt)
713     {
714       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
715     }
716   printf_filtered ("Target can%s single-step.\n", (arg1 & 0x4 ? "" : "not"));
717
718   rslt = angel_RDI_info (RDIInfo_Icebreaker, &arg1, &arg2);
719   if (rslt)
720     {
721       printf_filtered ("RDI_info: %s\n", rdi_error_message (rslt));
722     }
723   else
724     printf_filtered ("Target includes an EmbeddedICE.\n");
725 }
726 \f
727 static void
728 arm_rdi_kill ()
729 {
730   int rslt;
731
732   rslt = angel_RDI_open (1, &gdb_config, NULL, NULL);
733   if (rslt)
734     {
735       printf_filtered ("RDI_open: %s\n", rdi_error_message (rslt));
736     }
737 }
738
739 static void
740 arm_rdi_mourn_inferior ()
741 {
742   /* We remove the inserted breakpoints in case the user wants to
743      issue another target and load commands to rerun his application;
744      This is something that wouldn't work on a native target, for instance,
745      as the process goes away when the inferior exits, but it works with
746      some remote targets like this one.  That is why this is done here. */
747   remove_breakpoints();
748   unpush_target (&arm_rdi_ops);
749   generic_mourn_inferior ();
750 }
751 \f
752 /* While the RDI library keeps track of its own breakpoints, we need
753    to remember "handles" so that we can delete them later.  Since
754    breakpoints get used for stepping, be careful not to leak memory
755    here.  */
756
757 static int
758 arm_rdi_insert_breakpoint (addr, contents_cache)
759      CORE_ADDR addr;
760      char *contents_cache;
761 {
762   int rslt;
763   PointHandle point;
764   struct local_bp_list_entry *entry;
765   int type = RDIPoint_EQ;
766
767   if (arm_pc_is_thumb (addr) || arm_pc_is_thumb_dummy (addr))
768     type |= RDIPoint_16Bit;
769   rslt = angel_RDI_setbreak (addr, type, 0, &point);
770   if (rslt)
771     {
772       printf_filtered ("RDI_setbreak: %s\n", rdi_error_message (rslt));
773     }
774   entry =
775     (struct local_bp_list_entry *) xmalloc (sizeof (struct local_bp_list_entry));
776   entry->addr = addr;
777   entry->point = point;
778   entry->next = local_bp_list;
779   local_bp_list = entry;
780   return rslt;
781 }
782
783 static int
784 arm_rdi_remove_breakpoint (addr, contents_cache)
785      CORE_ADDR addr;
786      char *contents_cache;
787 {
788   int rslt;
789   PointHandle point;
790   struct local_bp_list_entry *entry, *preventry;
791
792   for (entry = local_bp_list; entry != NULL; entry = entry->next)
793     {
794       if (entry->addr == addr)
795         {
796           break;
797         }
798       preventry = entry;
799     }
800   if (entry)
801     {
802       rslt = angel_RDI_clearbreak (entry->point);
803       if (rslt)
804         {
805           printf_filtered ("RDI_clearbreak: %s\n", rdi_error_message (rslt));
806         }
807       /* Delete the breakpoint entry locally.  */
808       if (entry == local_bp_list)
809         {
810           local_bp_list = entry->next;
811         }
812       else
813         {
814           preventry->next = entry->next;
815         }
816       free (entry);
817     }
818   return 0;
819 }
820 \f
821 static char *
822 rdi_error_message (err)
823      int err;
824 {
825   switch (err)
826     {
827     case RDIError_NoError:
828       return "no error";
829     case RDIError_Reset:
830       return "debuggee reset";
831     case RDIError_UndefinedInstruction:
832       return "undefined instruction";
833     case RDIError_SoftwareInterrupt:
834       return "SWI trapped";
835     case RDIError_PrefetchAbort:
836       return "prefetch abort, execution ran into unmapped memory?";
837     case RDIError_DataAbort:
838       return "data abort, no memory at specified address?";
839     case RDIError_AddressException:
840       return "address exception, access >26bit in 26bit mode";
841     case RDIError_IRQ:
842       return "IRQ, interrupt trapped";
843     case RDIError_FIQ:
844       return "FIQ, fast interrupt trapped";
845     case RDIError_Error:
846       return "a miscellaneous type of error";
847     case RDIError_BranchThrough0:
848       return "branch through location 0";
849     case RDIError_NotInitialised:
850       return "internal error, RDI_open not called first";
851     case RDIError_UnableToInitialise:
852       return "internal error, target world is broken";
853     case RDIError_WrongByteSex:
854       return "See Operator: WrongByteSex";
855     case RDIError_UnableToTerminate:
856       return "See Operator: Unable to Terminate";
857     case RDIError_BadInstruction:
858       return "bad instruction, illegal to execute this instruction";
859     case RDIError_IllegalInstruction:
860       return "illegal instruction, the effect of executing it is undefined";
861     case RDIError_BadCPUStateSetting:
862       return "internal error, tried to set SPSR of user mode";
863     case RDIError_UnknownCoPro:
864       return "unknown co-processor";
865     case RDIError_UnknownCoProState:
866       return "cannot execute co-processor request";
867     case RDIError_BadCoProState:
868       return "recognizably broken co-processor request";
869     case RDIError_BadPointType:
870       return "internal error, bad point yype";
871     case RDIError_UnimplementedType:
872       return "internal error, unimplemented type";
873     case RDIError_BadPointSize:
874       return "internal error, bad point size";
875     case RDIError_UnimplementedSize:
876       return "internal error, unimplemented size";
877     case RDIError_NoMorePoints:
878       return "last break/watch point was used";
879     case RDIError_BreakpointReached:
880       return "breakpoint reached";
881     case RDIError_WatchpointAccessed:
882       return "watchpoint accessed";
883     case RDIError_NoSuchPoint:
884       return "attempted to clear non-existent break/watch point";
885     case RDIError_ProgramFinishedInStep:
886       return "end of the program reached while stepping";
887     case RDIError_UserInterrupt:
888       return "you pressed Escape";
889     case RDIError_CantSetPoint:
890       return "no more break/watch points available";
891     case RDIError_IncompatibleRDILevels:
892       return "incompatible RDI levels";
893     case RDIError_LittleEndian:
894       return "debuggee is little endian";
895     case RDIError_BigEndian:
896       return "debuggee is big endian";
897     case RDIError_SoftInitialiseError:
898       return "recoverable error in RDI initialization";
899     case RDIError_InsufficientPrivilege:
900       return "internal error, supervisor state not accessible to monitor";
901     case RDIError_UnimplementedMessage:
902       return "internal error, unimplemented message";
903     case RDIError_UndefinedMessage:
904       return "internal error, undefined message";
905     default:
906       return "undefined error message, should reset target";
907     }
908 }
909
910 /* Convert the ARM error messages to signals that GDB knows about.  */
911
912 static enum target_signal
913 rdi_error_signal (err)
914      int err;
915 {
916   switch (err)
917     {
918     case RDIError_NoError:
919       return 0;
920     case RDIError_Reset:
921       return TARGET_SIGNAL_TERM;        /* ??? */
922     case RDIError_UndefinedInstruction:
923       return TARGET_SIGNAL_ILL;
924     case RDIError_SoftwareInterrupt:
925     case RDIError_PrefetchAbort:
926     case RDIError_DataAbort:
927       return TARGET_SIGNAL_TRAP;
928     case RDIError_AddressException:
929       return TARGET_SIGNAL_SEGV;
930     case RDIError_IRQ:
931     case RDIError_FIQ:
932       return TARGET_SIGNAL_TRAP;
933     case RDIError_Error:
934       return TARGET_SIGNAL_TERM;
935     case RDIError_BranchThrough0:
936       return TARGET_SIGNAL_TRAP;
937     case RDIError_NotInitialised:
938     case RDIError_UnableToInitialise:
939     case RDIError_WrongByteSex:
940     case RDIError_UnableToTerminate:
941       return TARGET_SIGNAL_UNKNOWN;
942     case RDIError_BadInstruction:
943     case RDIError_IllegalInstruction:
944       return TARGET_SIGNAL_ILL;
945     case RDIError_BadCPUStateSetting:
946     case RDIError_UnknownCoPro:
947     case RDIError_UnknownCoProState:
948     case RDIError_BadCoProState:
949     case RDIError_BadPointType:
950     case RDIError_UnimplementedType:
951     case RDIError_BadPointSize:
952     case RDIError_UnimplementedSize:
953     case RDIError_NoMorePoints:
954       return TARGET_SIGNAL_UNKNOWN;
955     case RDIError_BreakpointReached:
956     case RDIError_WatchpointAccessed:
957       return TARGET_SIGNAL_TRAP;
958     case RDIError_NoSuchPoint:
959     case RDIError_ProgramFinishedInStep:
960       return TARGET_SIGNAL_UNKNOWN;
961     case RDIError_UserInterrupt:
962       return TARGET_SIGNAL_INT;
963     case RDIError_IncompatibleRDILevels:
964     case RDIError_LittleEndian:
965     case RDIError_BigEndian:
966     case RDIError_SoftInitialiseError:
967     case RDIError_InsufficientPrivilege:
968     case RDIError_UnimplementedMessage:
969     case RDIError_UndefinedMessage:
970     default:
971       return TARGET_SIGNAL_UNKNOWN;
972     }
973 }
974 \f
975 /* Define the target operations structure.  */
976
977 static void
978 init_rdi_ops ()
979 {
980   arm_rdi_ops.to_shortname = "rdi";
981   arm_rdi_ops.to_longname = "ARM RDI";
982   arm_rdi_ops.to_doc = "Use a remote ARM-based computer; via the RDI library.\n\
983 Specify the serial device it is connected to (e.g. /dev/ttya).";
984   arm_rdi_ops.to_open = arm_rdi_open;
985   arm_rdi_ops.to_close = arm_rdi_close;
986   arm_rdi_ops.to_detach = arm_rdi_detach;
987   arm_rdi_ops.to_resume = arm_rdi_resume;
988   arm_rdi_ops.to_wait = arm_rdi_wait;
989   arm_rdi_ops.to_fetch_registers = arm_rdi_fetch_registers;
990   arm_rdi_ops.to_store_registers = arm_rdi_store_registers;
991   arm_rdi_ops.to_prepare_to_store = arm_rdi_prepare_to_store;
992   arm_rdi_ops.to_xfer_memory = arm_rdi_xfer_memory;
993   arm_rdi_ops.to_files_info = arm_rdi_files_info;
994   arm_rdi_ops.to_insert_breakpoint = arm_rdi_insert_breakpoint;
995   arm_rdi_ops.to_remove_breakpoint = arm_rdi_remove_breakpoint;
996   arm_rdi_ops.to_kill = arm_rdi_kill;
997   arm_rdi_ops.to_load = generic_load;
998   arm_rdi_ops.to_create_inferior = arm_rdi_create_inferior;
999   arm_rdi_ops.to_mourn_inferior = arm_rdi_mourn_inferior;
1000   arm_rdi_ops.to_stratum = process_stratum;
1001   arm_rdi_ops.to_has_all_memory = 1;
1002   arm_rdi_ops.to_has_memory = 1;
1003   arm_rdi_ops.to_has_stack = 1;
1004   arm_rdi_ops.to_has_registers = 1;
1005   arm_rdi_ops.to_has_execution = 1;
1006   arm_rdi_ops.to_magic = OPS_MAGIC;
1007 }
1008
1009 static void 
1010 rdilogfile_command (char *arg, int from_tty)
1011 {
1012   if (!arg || strlen (arg) == 0)
1013     {
1014       printf_filtered ("rdi log file is '%s'\n", log_filename);
1015       return;
1016     }
1017
1018   if (log_filename)
1019     free (log_filename);
1020
1021   log_filename = xstrdup (arg);
1022
1023   Adp_SetLogfile (log_filename);
1024 }
1025
1026 static void 
1027 rdilogenable_command (char *args, int from_tty)
1028 {
1029   if (!args || strlen (args) == 0)
1030     {
1031       printf_filtered ("rdi log is %s\n", log_enable ? "enabled" : "disabled");
1032       return;
1033     }
1034
1035   if (!strcasecmp (args, "1") ||
1036       !strcasecmp (args, "y") ||
1037       !strcasecmp (args, "yes") ||
1038       !strcasecmp (args, "on") ||
1039       !strcasecmp (args, "t") ||
1040       !strcasecmp (args, "true"))
1041     Adp_SetLogEnable (log_enable = 1);
1042   else if (!strcasecmp (args, "0") ||
1043            !strcasecmp (args, "n") ||
1044            !strcasecmp (args, "no") ||
1045            !strcasecmp (args, "off") ||
1046            !strcasecmp (args, "f") ||
1047            !strcasecmp (args, "false"))
1048     Adp_SetLogEnable (log_enable = 0);
1049   else
1050     printf_filtered ("rdilogenable: unrecognized argument '%s'\n"
1051                      "              try y or n\n", args);
1052 }
1053
1054 void
1055 _initialize_remote_rdi ()
1056 {
1057   init_rdi_ops ();
1058   add_target (&arm_rdi_ops);
1059
1060   log_filename = xstrdup ("rdi.log");
1061   Adp_SetLogfile (log_filename);
1062   Adp_SetLogEnable (log_enable);
1063
1064   add_cmd ("rdilogfile", class_maintenance,
1065            rdilogfile_command,
1066            "Set filename for ADP packet log.\n\
1067 This file is used to log Angel Debugger Protocol packets.\n\
1068 With a single argument, sets the logfile name to that value.\n\
1069 Without an argument, shows the current logfile name.\n\
1070 See also: rdilogenable\n",
1071            &maintenancelist);
1072
1073   add_cmd ("rdilogenable", class_maintenance,
1074            rdilogenable_command,
1075            "Set enable logging of ADP packets.\n\
1076 This will log ADP packets exchanged between gdb and the\n\
1077 rdi target device.\n\
1078 An argument of 1,t,true,y,yes will enable.\n\
1079 An argument of 0,f,false,n,no will disabled.\n\
1080 Withough an argument, it will display current state.\n",
1081            &maintenancelist);
1082
1083   add_show_from_set
1084     (add_set_cmd ("rdiromatzero", no_class,
1085                   var_boolean, (char *) &rom_at_zero,
1086                   "Set target has ROM at addr 0.\n\
1087 A true value disables vector catching, false enables vector catching.\n\
1088 This is evaluated at the time the 'target rdi' command is executed\n",
1089                   &setlist),
1090      &showlist);
1091
1092   add_show_from_set
1093     (add_set_cmd ("rdiheartbeat", no_class,
1094                   var_boolean, (char *) &rdi_heartbeat,
1095                   "Set enable for ADP heartbeat packets.\n\
1096 I don't know why you would want this. If you enable them,\n\
1097 it will confuse ARM and EPI JTAG interface boxes as well\n\
1098 as the Angel Monitor.\n",
1099                   &setlist),
1100      &showlist);
1101 }
1102
1103 /* A little dummy to make linking with the library succeed. */
1104
1105 int
1106 Fail ()
1107 {
1108   return 0;
1109 }