OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [gdb/] [python/] [py-lazy-string.c] - Blame information for rev 853

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 227 jeremybenn
/* Python interface to lazy strings.
2
 
3
   Copyright (C) 2010 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.  */
19
 
20
#include "defs.h"
21
#include "python-internal.h"
22
#include "charset.h"
23
#include "value.h"
24
#include "exceptions.h"
25
#include "valprint.h"
26
#include "language.h"
27
 
28
typedef struct {
29
  PyObject_HEAD
30
  /*  Holds the address of the lazy string.  */
31
  CORE_ADDR address;
32
 
33
  /*  Holds the encoding that will be applied to the string
34
      when the string is printed by GDB.  If the encoding is set
35
      to None then GDB will select the most appropriate
36
      encoding when the sting is printed.  */
37
  char *encoding;
38
 
39
  /* Holds the length of the string in characters.  If the
40
     length is -1, then the string will be fetched and encoded up to
41
     the first null of appropriate width.  */
42
  long length;
43
 
44
  /*  This attribute holds the type that is represented by the lazy
45
      string's type.  */
46
  struct type *type;
47
} lazy_string_object;
48
 
49
static PyTypeObject lazy_string_object_type;
50
 
51
static PyObject *
52
stpy_get_address (PyObject *self, void *closure)
53
{
54
  lazy_string_object *self_string = (lazy_string_object *) self;
55
  return PyLong_FromUnsignedLongLong (self_string->address);
56
}
57
 
58
static PyObject *
59
stpy_get_encoding (PyObject *self, void *closure)
60
{
61
  lazy_string_object *self_string = (lazy_string_object *) self;
62
  PyObject *result;
63
 
64
  /* An encoding can be set to NULL by the user, so check before
65
     attempting a Python FromString call.  If NULL return Py_None.  */
66
  if (self_string->encoding)
67
    result = PyString_FromString (self_string->encoding);
68
  else
69
    {
70
      result = Py_None;
71
      Py_INCREF (result);
72
    }
73
 
74
  return result;
75
}
76
 
77
static PyObject *
78
stpy_get_length (PyObject *self, void *closure)
79
{
80
  lazy_string_object *self_string = (lazy_string_object *) self;
81
  return PyLong_FromLong (self_string->length);
82
}
83
 
84
PyObject *
85
stpy_get_type (PyObject *self, void *closure)
86
{
87
  lazy_string_object *str_obj = (lazy_string_object *) self;
88
  return type_to_type_object (str_obj->type);
89
}
90
 
91
static PyObject *
92
stpy_convert_to_value  (PyObject *self, PyObject *args)
93
{
94
  lazy_string_object *self_string = (lazy_string_object *) self;
95
  struct value *val;
96
 
97
  val = value_at_lazy (self_string->type, self_string->address);
98
  return value_to_value_object (val);
99
}
100
 
101
static void
102
stpy_dealloc (PyObject *self)
103
{
104
  lazy_string_object *self_string = (lazy_string_object *) self;
105
  xfree (self_string->encoding);
106
}
107
 
108
PyObject *
109
gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
110
                           const char *encoding, struct type *type)
111
{
112
  lazy_string_object *str_obj = NULL;
113
 
114
  if (address == 0)
115
    {
116
      PyErr_SetString (PyExc_MemoryError,
117
                       "Cannot create a lazy string from a GDB-side string.");
118
      return NULL;
119
    }
120
 
121
  if (!type)
122
    {
123
      PyErr_SetString (PyExc_RuntimeError,
124
                       "A lazy string's type cannot be NULL.");
125
      return NULL;
126
    }
127
 
128
  str_obj = PyObject_New (lazy_string_object, &lazy_string_object_type);
129
  if (!str_obj)
130
    return NULL;
131
 
132
  str_obj->address = address;
133
  str_obj->length = length;
134
  if (encoding == NULL || !strcmp (encoding, ""))
135
    str_obj->encoding = NULL;
136
  else
137
    str_obj->encoding = xstrdup (encoding);
138
  str_obj->type = type;
139
 
140
  return (PyObject *) str_obj;
141
}
142
 
143
void
144
gdbpy_initialize_lazy_string (void)
145
{
146
  if (PyType_Ready (&lazy_string_object_type) < 0)
147
    return;
148
 
149
  Py_INCREF (&lazy_string_object_type);
150
}
151
 
152
/* Determine whether the printer object pointed to by OBJ is a
153
   Python lazy string.  */
154
int
155
gdbpy_is_lazy_string (PyObject *result)
156
{
157
  return PyObject_TypeCheck (result, &lazy_string_object_type);
158
}
159
 
160
/* Extract and return the actual string from the lazy string object
161
   STRING.  Addtionally, the string type is written to *STR_TYPE, the
162
   string length is written to *LENGTH, and the string encoding is
163
   written to *ENCODING.  On error, NULL is returned.  The caller is
164
   responsible for freeing the returned buffer.  */
165
gdb_byte *
166
gdbpy_extract_lazy_string (PyObject *string, struct type **str_type,
167
                     long *length, char **encoding)
168
{
169
  int width;
170
  int bytes_read;
171
  gdb_byte *buffer = NULL;
172
  int errcode = 0;
173
  CORE_ADDR addr;
174
  struct gdbarch *gdbarch;
175
  enum bfd_endian byte_order;
176
  PyObject *py_len = NULL, *py_encoding = NULL;
177
  PyObject *py_addr = NULL, *py_type = NULL;
178
  volatile struct gdb_exception except;
179
 
180
  py_len = PyObject_GetAttrString (string, "length");
181
  py_encoding = PyObject_GetAttrString (string, "encoding");
182
  py_addr = PyObject_GetAttrString (string, "address");
183
  py_type = PyObject_GetAttrString (string, "type");
184
 
185
  /* A NULL encoding, length, address or type is not ok.  */
186
  if (!py_len || !py_encoding || !py_addr || !py_type)
187
    goto error;
188
 
189
  *length = PyLong_AsLong (py_len);
190
  addr = PyLong_AsUnsignedLongLong (py_addr);
191
 
192
  /* If the user supplies Py_None an encoding, set encoding to NULL.
193
     This will trigger the resulting LA_PRINT_CALL to automatically
194
     select an encoding.  */
195
  if (py_encoding == Py_None)
196
    *encoding = NULL;
197
  else
198
    *encoding = xstrdup (PyString_AsString (py_encoding));
199
 
200
  *str_type = type_object_to_type (py_type);
201
  gdbarch = get_type_arch (*str_type);
202
  byte_order = gdbarch_byte_order (gdbarch);
203
  width = TYPE_LENGTH (*str_type);
204
 
205
  TRY_CATCH (except, RETURN_MASK_ALL)
206
    {
207
      errcode = read_string (addr, *length, width,
208
                             *length, byte_order, &buffer,
209
                             &bytes_read);
210
    }
211
  if (except.reason < 0)
212
    {
213
      PyErr_Format (except.reason == RETURN_QUIT                        \
214
                    ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,     \
215
                    "%s", except.message);                              \
216
      goto error;
217
 
218
    }
219
 
220
  if (errcode)
221
    goto error;
222
 
223
  *length = bytes_read / width;
224
 
225
  Py_DECREF (py_encoding);
226
  Py_DECREF (py_len);
227
  Py_DECREF (py_addr);
228
  Py_DECREF (py_type);
229
  return buffer;
230
 
231
 error:
232
  Py_XDECREF (py_encoding);
233
  Py_XDECREF (py_len);
234
  Py_XDECREF (py_addr);
235
  Py_XDECREF (py_type);
236
  xfree (buffer);
237
  *length = 0;
238
  *str_type = NULL;
239
  return NULL;
240
}
241
 
242
 
243
 
244
static PyMethodDef lazy_string_object_methods[] = {
245
  { "value", stpy_convert_to_value, METH_NOARGS,
246
    "Create a (lazy) value that contains a pointer to the string." },
247
  {NULL}  /* Sentinel */
248
};
249
 
250
 
251
static PyGetSetDef lazy_string_object_getset[] = {
252
  { "address", stpy_get_address, NULL, "Address of the string.", NULL },
253
  { "encoding", stpy_get_encoding, NULL, "Encoding of the string.", NULL },
254
  { "length", stpy_get_length, NULL, "Length of the string.", NULL },
255
  { "type", stpy_get_type, NULL, "Type associated with the string.", NULL },
256
  { NULL }  /* Sentinel */
257
};
258
 
259
static PyTypeObject lazy_string_object_type = {
260
  PyObject_HEAD_INIT (NULL)
261
  0,                               /*ob_size*/
262
  "gdb.LazyString",               /*tp_name*/
263
  sizeof (lazy_string_object),    /*tp_basicsize*/
264
  0,                               /*tp_itemsize*/
265
  stpy_dealloc,                   /*tp_dealloc*/
266
  0,                               /*tp_print*/
267
  0,                               /*tp_getattr*/
268
  0,                               /*tp_setattr*/
269
  0,                               /*tp_compare*/
270
  0,                               /*tp_repr*/
271
  0,                               /*tp_as_number*/
272
  0,                               /*tp_as_sequence*/
273
  0,                               /*tp_as_mapping*/
274
  0,                               /*tp_hash */
275
  0,                               /*tp_call*/
276
  0,                               /*tp_str*/
277
  0,                               /*tp_getattro*/
278
  0,                               /*tp_setattro*/
279
  0,                               /*tp_as_buffer*/
280
  Py_TPFLAGS_DEFAULT,             /*tp_flags*/
281
  "GDB lazy string object",       /* tp_doc */
282
  0,                               /* tp_traverse */
283
  0,                               /* tp_clear */
284
  0,                               /* tp_richcompare */
285
  0,                               /* tp_weaklistoffset */
286
  0,                               /* tp_iter */
287
  0,                               /* tp_iternext */
288
  lazy_string_object_methods,     /* tp_methods */
289
  0,                               /* tp_members */
290
  lazy_string_object_getset       /* tp_getset */
291
};

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.