Move putchar_filtered() to utils.c.
[platform/upstream/binutils.git] / gdb / arch-utils.c
1 /* Dynamic architecture support for GDB, the GNU debugger.
2    Copyright 1998-1999, 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
23 #if GDB_MULTI_ARCH
24 #include "gdbcmd.h"
25 #include "inferior.h"           /* enum CALL_DUMMY_LOCATION et.al. */
26 #else
27 /* Just include everything in sight so that the every old definition
28    of macro is visible. */
29 #include "gdb_string.h"
30 #include <ctype.h>
31 #include "symtab.h"
32 #include "frame.h"
33 #include "inferior.h"
34 #include "breakpoint.h"
35 #include "gdb_wait.h"
36 #include "gdbcore.h"
37 #include "gdbcmd.h"
38 #include "target.h"
39 #include "gdbthread.h"
40 #include "annotate.h"
41 #include "symfile.h"            /* for overlay functions */
42 #endif
43
44 #include "version.h"
45
46 #include "floatformat.h"
47
48 /* Convenience macro for allocting typesafe memory. */
49
50 #ifndef XMALLOC
51 #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
52 #endif
53
54
55 /* Use the program counter to determine the contents and size
56    of a breakpoint instruction.  If no target-dependent macro
57    BREAKPOINT_FROM_PC has been defined to implement this function,
58    assume that the breakpoint doesn't depend on the PC, and
59    use the values of the BIG_BREAKPOINT and LITTLE_BREAKPOINT macros.
60    Return a pointer to a string of bytes that encode a breakpoint
61    instruction, stores the length of the string to *lenptr,
62    and optionally adjust the pc to point to the correct memory location
63    for inserting the breakpoint.  */
64
65 unsigned char *
66 legacy_breakpoint_from_pc (CORE_ADDR * pcptr, int *lenptr)
67 {
68   /* {BIG_,LITTLE_}BREAKPOINT is the sequence of bytes we insert for a
69      breakpoint.  On some machines, breakpoints are handled by the
70      target environment and we don't have to worry about them here.  */
71 #ifdef BIG_BREAKPOINT
72   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
73     {
74       static unsigned char big_break_insn[] = BIG_BREAKPOINT;
75       *lenptr = sizeof (big_break_insn);
76       return big_break_insn;
77     }
78 #endif
79 #ifdef LITTLE_BREAKPOINT
80   if (TARGET_BYTE_ORDER != BIG_ENDIAN)
81     {
82       static unsigned char little_break_insn[] = LITTLE_BREAKPOINT;
83       *lenptr = sizeof (little_break_insn);
84       return little_break_insn;
85     }
86 #endif
87 #ifdef BREAKPOINT
88   {
89     static unsigned char break_insn[] = BREAKPOINT;
90     *lenptr = sizeof (break_insn);
91     return break_insn;
92   }
93 #endif
94   *lenptr = 0;
95   return NULL;
96 }
97
98 int
99 generic_frameless_function_invocation_not (struct frame_info *fi)
100 {
101   return 0;
102 }
103
104 int
105 generic_return_value_on_stack_not (struct type *type)
106 {
107   return 0;
108 }
109
110 char *
111 legacy_register_name (int i)
112 {
113 #ifdef REGISTER_NAMES
114   static char *names[] = REGISTER_NAMES;
115   if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
116     return NULL;
117   else
118     return names[i];
119 #else
120   internal_error ("legacy_register_name: called.");
121   return NULL;
122 #endif
123 }
124
125 #if defined (CALL_DUMMY)
126 LONGEST legacy_call_dummy_words[] = CALL_DUMMY;
127 #else
128 LONGEST legacy_call_dummy_words[1];
129 #endif
130 int legacy_sizeof_call_dummy_words = sizeof (legacy_call_dummy_words);
131
132 void
133 generic_remote_translate_xfer_address (CORE_ADDR gdb_addr, int gdb_len,
134                                        CORE_ADDR * rem_addr, int *rem_len)
135 {
136   *rem_addr = gdb_addr;
137   *rem_len = gdb_len;
138 }
139
140 int
141 generic_prologue_frameless_p (CORE_ADDR ip)
142 {
143 #ifdef SKIP_PROLOGUE_FRAMELESS_P
144   return ip == SKIP_PROLOGUE_FRAMELESS_P (ip);
145 #else
146   return ip == SKIP_PROLOGUE (ip);
147 #endif
148 }
149
150
151 /* Helper functions for INNER_THAN */
152
153 int
154 core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
155 {
156   return (lhs < rhs);
157 }
158
159 int
160 core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
161 {
162   return (lhs > rhs);
163 }
164
165
166 /* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
167
168 const struct floatformat *
169 default_float_format (struct gdbarch *gdbarch)
170 {
171 #if GDB_MULTI_ARCH
172   int byte_order = gdbarch_byte_order (gdbarch);
173 #else
174   int byte_order = TARGET_BYTE_ORDER;
175 #endif
176   switch (byte_order)
177     {
178     case BIG_ENDIAN:
179       return &floatformat_ieee_single_big;
180     case LITTLE_ENDIAN:
181       return &floatformat_ieee_single_little;
182     default:
183       internal_error ("default_float_format: bad byte order");
184     }
185 }
186
187
188 const struct floatformat *
189 default_double_format (struct gdbarch *gdbarch)
190 {
191 #if GDB_MULTI_ARCH
192   int byte_order = gdbarch_byte_order (gdbarch);
193 #else
194   int byte_order = TARGET_BYTE_ORDER;
195 #endif
196   switch (byte_order)
197     {
198     case BIG_ENDIAN:
199       return &floatformat_ieee_double_big;
200     case LITTLE_ENDIAN:
201       return &floatformat_ieee_double_little;
202     default:
203       internal_error ("default_double_format: bad byte order");
204     }
205 }
206
207 /* Misc helper functions for targets. */
208
209 int
210 frame_num_args_unknown (struct frame_info *fi)
211 {
212   return -1;
213 }
214
215
216 int
217 generic_register_convertible_not (int num)
218 {
219   return 0;
220 }
221   
222
223 int
224 default_register_sim_regno (int num)
225 {
226   return num;
227 }
228
229
230 CORE_ADDR
231 default_convert_from_func_ptr_addr (CORE_ADDR addr)
232 {
233   return addr;
234 }
235
236 int
237 no_op_reg_to_regnum (int reg)
238 {
239   return reg;
240 }
241
242 /* For use by frame_args_address and frame_locals_address.  */
243 CORE_ADDR
244 default_frame_address (struct frame_info *fi)
245 {
246   return fi->frame;
247 }
248
249 /* Functions to manipulate the endianness of the target.  */
250
251 #ifdef TARGET_BYTE_ORDER_SELECTABLE
252 /* compat - Catch old targets that expect a selectable byte-order to
253    default to BIG_ENDIAN */
254 #ifndef TARGET_BYTE_ORDER_DEFAULT
255 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN
256 #endif
257 #endif
258 #if !TARGET_BYTE_ORDER_SELECTABLE_P
259 #ifndef TARGET_BYTE_ORDER_DEFAULT
260 /* compat - Catch old non byte-order selectable targets that do not
261    define TARGET_BYTE_ORDER_DEFAULT and instead expect
262    TARGET_BYTE_ORDER to be used as the default.  For targets that
263    defined neither TARGET_BYTE_ORDER nor TARGET_BYTE_ORDER_DEFAULT the
264    below will get a strange compiler warning. */
265 #define TARGET_BYTE_ORDER_DEFAULT TARGET_BYTE_ORDER
266 #endif
267 #endif
268 #ifndef TARGET_BYTE_ORDER_DEFAULT
269 #define TARGET_BYTE_ORDER_DEFAULT BIG_ENDIAN /* arbitrary */
270 #endif
271 /* ``target_byte_order'' is only used when non- multi-arch.
272    Multi-arch targets obtain the current byte order using
273    TARGET_BYTE_ORDER which is controlled by gdbarch.*. */
274 int target_byte_order = TARGET_BYTE_ORDER_DEFAULT;
275 int target_byte_order_auto = 1;
276
277 static const char endian_big[] = "big";
278 static const char endian_little[] = "little";
279 static const char endian_auto[] = "auto";
280 static const char *endian_enum[] =
281 {
282   endian_big,
283   endian_little,
284   endian_auto,
285   NULL,
286 };
287 static const char *set_endian_string;
288
289 /* Called by ``show endian''.  */
290
291 static void
292 show_endian (char *args, int from_tty)
293 {
294   if (TARGET_BYTE_ORDER_AUTO)
295     printf_unfiltered ("The target endianness is set automatically (currently %s endian)\n",
296                        (TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little"));
297   else
298     printf_unfiltered ("The target is assumed to be %s endian\n",
299                        (TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little"));
300 }
301
302 static void
303 set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
304 {
305   if (!TARGET_BYTE_ORDER_SELECTABLE_P)
306     {
307       printf_unfiltered ("Byte order is not selectable.");
308     }
309   else if (set_endian_string == endian_auto)
310     {
311       target_byte_order_auto = 1;
312     }
313   else if (set_endian_string == endian_little)
314     {
315       target_byte_order_auto = 0;
316       if (GDB_MULTI_ARCH)
317         {
318           struct gdbarch_info info;
319           memset (&info, 0, sizeof info);
320           info.byte_order = LITTLE_ENDIAN;
321           if (! gdbarch_update_p (info))
322             {
323               printf_unfiltered ("Little endian target not supported by GDB\n");
324             }
325         }
326       else
327         {
328           target_byte_order = LITTLE_ENDIAN;
329         }
330     }
331   else if (set_endian_string == endian_big)
332     {
333       target_byte_order_auto = 0;
334       if (GDB_MULTI_ARCH)
335         {
336           struct gdbarch_info info;
337           memset (&info, 0, sizeof info);
338           info.byte_order = BIG_ENDIAN;
339           if (! gdbarch_update_p (info))
340             {
341               printf_unfiltered ("Big endian target not supported by GDB\n");
342             }
343         }
344       else
345         {
346           target_byte_order = BIG_ENDIAN;
347         }
348     }
349   else
350     internal_error ("set_endian: bad value");
351   show_endian (NULL, from_tty);
352 }
353
354 /* Set the endianness from a BFD.  */
355
356 static void
357 set_endian_from_file (bfd *abfd)
358 {
359   if (GDB_MULTI_ARCH)
360     internal_error ("set_endian_from_file: not for multi-arch");
361   if (TARGET_BYTE_ORDER_SELECTABLE_P)
362     {
363       int want;
364       
365       if (bfd_big_endian (abfd))
366         want = BIG_ENDIAN;
367       else
368         want = LITTLE_ENDIAN;
369       if (TARGET_BYTE_ORDER_AUTO)
370         target_byte_order = want;
371       else if (TARGET_BYTE_ORDER != want)
372         warning ("%s endian file does not match %s endian target.",
373                  want == BIG_ENDIAN ? "big" : "little",
374                  TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
375     }
376   else
377     {
378       if (bfd_big_endian (abfd)
379           ? TARGET_BYTE_ORDER != BIG_ENDIAN
380           : TARGET_BYTE_ORDER == BIG_ENDIAN)
381         warning ("%s endian file does not match %s endian target.",
382                  bfd_big_endian (abfd) ? "big" : "little",
383                  TARGET_BYTE_ORDER == BIG_ENDIAN ? "big" : "little");
384     }
385 }
386
387
388 /* Functions to manipulate the architecture of the target */
389
390 enum set_arch { set_arch_auto, set_arch_manual };
391
392 int target_architecture_auto = 1;
393
394 const char *set_architecture_string;
395
396 /* Old way of changing the current architecture. */
397
398 extern const struct bfd_arch_info bfd_default_arch_struct;
399 const struct bfd_arch_info *target_architecture = &bfd_default_arch_struct;
400 int (*target_architecture_hook) (const struct bfd_arch_info *ap);
401
402 static int
403 arch_ok (const struct bfd_arch_info *arch)
404 {
405   if (GDB_MULTI_ARCH)
406     internal_error ("arch_ok: not multi-arched");
407   /* Should be performing the more basic check that the binary is
408      compatible with GDB. */
409   /* Check with the target that the architecture is valid. */
410   return (target_architecture_hook == NULL
411           || target_architecture_hook (arch));
412 }
413
414 static void
415 set_arch (const struct bfd_arch_info *arch,
416           enum set_arch type)
417 {
418   if (GDB_MULTI_ARCH)
419     internal_error ("set_arch: not multi-arched");
420   switch (type)
421     {
422     case set_arch_auto:
423       if (!arch_ok (arch))
424         warning ("Target may not support %s architecture",
425                  arch->printable_name);
426       target_architecture = arch;
427       break;
428     case set_arch_manual:
429       if (!arch_ok (arch))
430         {
431           printf_unfiltered ("Target does not support `%s' architecture.\n",
432                              arch->printable_name);
433         }
434       else
435         {
436           target_architecture_auto = 0;
437           target_architecture = arch;
438         }
439       break;
440     }
441   if (gdbarch_debug)
442     gdbarch_dump (current_gdbarch, gdb_stdlog);
443 }
444
445 /* Set the architecture from arch/machine (deprecated) */
446
447 void
448 set_architecture_from_arch_mach (enum bfd_architecture arch,
449                                  unsigned long mach)
450 {
451   const struct bfd_arch_info *wanted = bfd_lookup_arch (arch, mach);
452   if (GDB_MULTI_ARCH)
453     internal_error ("set_architecture_from_arch_mach: not multi-arched");
454   if (wanted != NULL)
455     set_arch (wanted, set_arch_manual);
456   else
457     internal_error ("gdbarch: hardwired architecture/machine not recognized");
458 }
459
460 /* Set the architecture from a BFD (deprecated) */
461
462 static void
463 set_architecture_from_file (bfd *abfd)
464 {
465   const struct bfd_arch_info *wanted = bfd_get_arch_info (abfd);
466   if (GDB_MULTI_ARCH)
467     internal_error ("set_architecture_from_file: not multi-arched");
468   if (target_architecture_auto)
469     {
470       set_arch (wanted, set_arch_auto);
471     }
472   else if (wanted != target_architecture)
473     {
474       warning ("%s architecture file may be incompatible with %s target.",
475                wanted->printable_name,
476                target_architecture->printable_name);
477     }
478 }
479
480
481 /* Called if the user enters ``show architecture'' without an
482    argument. */
483
484 static void
485 show_architecture (char *args, int from_tty)
486 {
487   const char *arch;
488   arch = TARGET_ARCHITECTURE->printable_name;
489   if (target_architecture_auto)
490     printf_filtered ("The target architecture is set automatically (currently %s)\n", arch);
491   else
492     printf_filtered ("The target architecture is assumed to be %s\n", arch);
493 }
494
495
496 /* Called if the user enters ``set architecture'' with or without an
497    argument. */
498
499 static void
500 set_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
501 {
502   if (strcmp (set_architecture_string, "auto") == 0)
503     {
504       target_architecture_auto = 1;
505     }
506   else if (GDB_MULTI_ARCH)
507     {
508       struct gdbarch_info info;
509       memset (&info, 0, sizeof info);
510       info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
511       if (info.bfd_arch_info == NULL)
512         internal_error ("set_architecture: bfd_scan_arch failed");
513       if (gdbarch_update_p (info))
514         target_architecture_auto = 0;
515       else
516         printf_unfiltered ("Architecture `%s' not recognized.\n",
517                            set_architecture_string);
518     }
519   else
520     {
521       const struct bfd_arch_info *arch
522         = bfd_scan_arch (set_architecture_string);
523       if (arch == NULL)
524         internal_error ("set_architecture: bfd_scan_arch failed");
525       set_arch (arch, set_arch_manual);
526     }
527   show_architecture (NULL, from_tty);
528 }
529
530 /* Called if the user enters ``info architecture'' without an argument. */
531
532 static void
533 info_architecture (char *args, int from_tty)
534 {
535   printf_filtered ("Available architectures are:\n");
536   if (GDB_MULTI_ARCH)
537     {
538       const char **arches = gdbarch_printable_names ();
539       const char **arch;
540       for (arch = arches; *arch != NULL; arch++)
541         {
542           printf_filtered (" %s", *arch);
543         }
544       xfree (arches);
545     }
546   else
547     {
548       enum bfd_architecture a;
549       for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
550         {
551           const struct bfd_arch_info *ap;
552           for (ap = bfd_lookup_arch (a, 0);
553                ap != NULL;
554                ap = ap->next)
555             {
556               printf_filtered (" %s", ap->printable_name);
557               ap = ap->next;
558             }
559         }
560     }
561   printf_filtered ("\n");
562 }
563
564 /* Set the dynamic target-system-dependent parameters (architecture,
565    byte-order) using information found in the BFD */
566
567 void
568 set_gdbarch_from_file (bfd *abfd)
569 {
570   if (GDB_MULTI_ARCH)
571     {
572       struct gdbarch_info info;
573       memset (&info, 0, sizeof info);
574       info.abfd = abfd;
575       if (! gdbarch_update_p (info))
576         error ("Architecture of file not recognized.\n");
577     }
578   else
579     {
580       set_architecture_from_file (abfd);
581       set_endian_from_file (abfd);
582     }
583 }
584
585 /* Initialize the current architecture.  Update the ``set
586    architecture'' command so that it specifies a list of valid
587    architectures.  */
588
589 #ifdef DEFAULT_BFD_ARCH
590 extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
591 static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
592 #else
593 static const bfd_arch_info_type *default_bfd_arch;
594 #endif
595
596 #ifdef DEFAULT_BFD_VEC
597 extern const bfd_target DEFAULT_BFD_VEC;
598 static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
599 #else
600 static const bfd_target *default_bfd_vec;
601 #endif
602
603 void
604 initialize_current_architecture (void)
605 {
606   const char **arches = gdbarch_printable_names ();
607
608   /* determine a default architecture and byte order. */
609   struct gdbarch_info info;
610   memset (&info, 0, sizeof (info));
611   
612   /* Find a default architecture. */
613   if (info.bfd_arch_info == NULL
614       && default_bfd_arch != NULL)
615     info.bfd_arch_info = default_bfd_arch;
616   if (info.bfd_arch_info == NULL)
617     {
618       /* Choose the architecture by taking the first one
619          alphabetically. */
620       const char *chosen = arches[0];
621       const char **arch;
622       for (arch = arches; *arch != NULL; arch++)
623         {
624           if (strcmp (*arch, chosen) < 0)
625             chosen = *arch;
626         }
627       if (chosen == NULL)
628         internal_error ("initialize_current_architecture: No arch");
629       info.bfd_arch_info = bfd_scan_arch (chosen);
630       if (info.bfd_arch_info == NULL)
631         internal_error ("initialize_current_architecture: Arch not found");
632     }
633
634   /* take several guesses at a byte order. */
635   /* NB: can't use TARGET_BYTE_ORDER_DEFAULT as its definition is
636      forced above. */
637   if (info.byte_order == 0
638       && default_bfd_vec != NULL)
639     {
640       /* Extract BFD's default vector's byte order. */
641       switch (default_bfd_vec->byteorder)
642         {
643         case BFD_ENDIAN_BIG:
644           info.byte_order = BIG_ENDIAN;
645           break;
646         case BFD_ENDIAN_LITTLE:
647           info.byte_order = LITTLE_ENDIAN;
648           break;
649         default:
650           break;
651         }
652     }
653   if (info.byte_order == 0)
654     {
655       /* look for ``*el-*'' in the target name. */
656       const char *chp;
657       chp = strchr (target_name, '-');
658       if (chp != NULL
659           && chp - 2 >= target_name
660           && strncmp (chp - 2, "el", 2) == 0)
661         info.byte_order = LITTLE_ENDIAN;
662     }
663   if (info.byte_order == 0)
664     {
665       /* Wire it to big-endian!!! */
666       info.byte_order = BIG_ENDIAN;
667     }
668
669   if (GDB_MULTI_ARCH)
670     {
671       if (! gdbarch_update_p (info))
672         {
673           internal_error ("initialize_current_architecture: Selection of initial architecture failed");
674         }
675     }
676
677   /* Create the ``set architecture'' command appending ``auto'' to the
678      list of architectures. */
679   {
680     struct cmd_list_element *c;
681     /* Append ``auto''. */
682     int nr;
683     for (nr = 0; arches[nr] != NULL; nr++);
684     arches = xrealloc (arches, sizeof (char*) * (nr + 2));
685     arches[nr + 0] = "auto";
686     arches[nr + 1] = NULL;
687     /* FIXME: add_set_enum_cmd() uses an array of ``char *'' instead
688        of ``const char *''.  We just happen to know that the casts are
689        safe. */
690     c = add_set_enum_cmd ("architecture", class_support,
691                           arches, &set_architecture_string,
692                           "Set architecture of target.",
693                           &setlist);
694     c->function.sfunc = set_architecture;
695     add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
696     /* Don't use set_from_show - need to print both auto/manual and
697        current setting. */
698     add_cmd ("architecture", class_support, show_architecture,
699              "Show the current target architecture", &showlist);
700     c = add_cmd ("architecture", class_support, info_architecture,
701                  "List supported target architectures", &infolist);
702     deprecate_cmd (c, "set architecture");
703   }
704 }
705
706
707 /* */
708
709 extern initialize_file_ftype _initialize_gdbarch_utils;
710
711 void
712 _initialize_gdbarch_utils (void)
713 {
714   struct cmd_list_element *c;
715   c = add_set_enum_cmd ("endian", class_support,
716                         endian_enum, &set_endian_string,
717                         "Set endianness of target.",
718                         &setlist);
719   c->function.sfunc = set_endian;
720   /* Don't use set_from_show - need to print both auto/manual and
721      current setting. */
722   add_cmd ("endian", class_support, show_endian,
723            "Show the current byte-order", &showlist);
724 }