Copyright updates for 2007.
[external/binutils.git] / gdb / target-descriptions.c
1 /* Target description support for GDB.
2
3    Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4
5    Contributed by CodeSourcery.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor,
22    Boston, MA 02110-1301, USA.  */
23
24 #include "defs.h"
25 #include "arch-utils.h"
26 #include "target.h"
27 #include "target-descriptions.h"
28 #include "vec.h"
29
30 #include "gdb_assert.h"
31
32 /* Types.  */
33
34 typedef struct property
35 {
36   const char *key;
37   const char *value;
38 } property_s;
39 DEF_VEC_O(property_s);
40
41 struct target_desc
42 {
43   /* Any architecture-specific properties specified by the target.  */
44   VEC(property_s) *properties;
45 };
46
47 /* Global state.  These variables are associated with the current
48    target; if GDB adds support for multiple simultaneous targets, then
49    these variables should become target-specific data.  */
50
51 /* A flag indicating that a description has already been fetched from
52    the current target, so it should not be queried again.  */
53
54 static int target_desc_fetched;
55
56 /* The description fetched from the current target, or NULL if the
57    current target did not supply any description.  Only valid when
58    target_desc_fetched is set.  Only the description initialization
59    code should access this; normally, the description should be
60    accessed through the gdbarch object.  */
61
62 static const struct target_desc *current_target_desc;
63
64 /* Fetch the current target's description, and switch the current
65    architecture to one which incorporates that description.  */
66
67 void
68 target_find_description (void)
69 {
70   /* If we've already fetched a description from the target, don't do
71      it again.  This allows a target to fetch the description early,
72      during its to_open or to_create_inferior, if it needs extra
73      information about the target to initialize.  */
74   if (target_desc_fetched)
75     return;
76
77   /* The current architecture should not have any target description
78      specified.  It should have been cleared, e.g. when we
79      disconnected from the previous target.  */
80   gdb_assert (gdbarch_target_desc (current_gdbarch) == NULL);
81
82   current_target_desc = target_read_description (&current_target);
83
84   /* If a non-NULL description was returned, then update the current
85      architecture.  */
86   if (current_target_desc)
87     {
88       struct gdbarch_info info;
89
90       gdbarch_info_init (&info);
91       info.target_desc = current_target_desc;
92       if (!gdbarch_update_p (info))
93         warning (_("Could not use target-supplied description"));
94     }
95
96   /* Now that we know this description is usable, record that we
97      fetched it.  */
98   target_desc_fetched = 1;
99 }
100
101 /* Discard any description fetched from the current target, and switch
102    the current architecture to one with no target description.  */
103
104 void
105 target_clear_description (void)
106 {
107   struct gdbarch_info info;
108
109   if (!target_desc_fetched)
110     return;
111
112   target_desc_fetched = 0;
113   current_target_desc = NULL;
114
115   gdbarch_info_init (&info);
116   if (!gdbarch_update_p (info))
117     internal_error (__FILE__, __LINE__,
118                     _("Could not remove target-supplied description"));
119 }
120
121 /* Return the global current target description.  This should only be
122    used by gdbarch initialization code; most access should be through
123    an existing gdbarch.  */
124
125 const struct target_desc *
126 target_current_description (void)
127 {
128   if (target_desc_fetched)
129     return current_target_desc;
130
131   return NULL;
132 }
133
134 /* Return the string value of a property named KEY, or NULL if the
135    property was not specified.  */
136
137 const char *
138 tdesc_property (const struct target_desc *target_desc, const char *key)
139 {
140   struct property *prop;
141   int ix;
142
143   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
144        ix++)
145     if (strcmp (prop->key, key) == 0)
146       return prop->value;
147
148   return NULL;
149 }
150
151 /* Methods for constructing a target description.  */
152
153 struct target_desc *
154 allocate_target_description (void)
155 {
156   return XZALLOC (struct target_desc);
157 }
158
159 void
160 set_tdesc_property (struct target_desc *target_desc,
161                     const char *key, const char *value)
162 {
163   struct property *prop, new_prop;
164   int ix;
165
166   gdb_assert (key != NULL && value != NULL);
167
168   for (ix = 0; VEC_iterate (property_s, target_desc->properties, ix, prop);
169        ix++)
170     if (strcmp (prop->key, key) == 0)
171       internal_error (__FILE__, __LINE__,
172                       _("Attempted to add duplicate property \"%s\""), key);
173
174   new_prop.key = key;
175   new_prop.value = value;
176   VEC_safe_push (property_s, target_desc->properties, &new_prop);
177 }