1 /* Typemap definitions, to allow SWIG to properly handle 'char**' data types. */
4 using namespace lldb_private;
5 /* Check if is a list */
6 if (PythonList::Check($input)) {
7 PythonList list(PyRefType::Borrowed, $input);
8 int size = list.GetSize();
10 $1 = (char**)malloc((size+1)*sizeof(char*));
11 for (i = 0; i < size; i++) {
12 PythonString py_str = list.GetItemAtIndex(i).AsType<PythonString>();
13 if (!py_str.IsAllocated()) {
14 PyErr_SetString(PyExc_TypeError,"list must contain strings");
19 $1[i] = const_cast<char*>(py_str.GetString().data());
22 } else if ($input == Py_None) {
25 PyErr_SetString(PyExc_TypeError,"not a list");
30 %typemap(typecheck) char ** {
31 /* Check if is a list */
33 using namespace lldb_private;
34 if (PythonList::Check($input)) {
35 PythonList list(PyRefType::Borrowed, $input);
36 int size = list.GetSize();
38 for (i = 0; i < size; i++) {
39 PythonString s = list.GetItemAtIndex(i).AsType<PythonString>();
40 if (!s.IsAllocated()) { $1 = 0; }
45 $1 = ( ($input == Py_None) ? 1 : 0);
49 %typemap(freearg) char** {
53 %typemap(out) char** {
57 while ($1[len]) len++;
58 using namespace lldb_private;
60 for (i = 0; i < len; i++)
61 list.SetItemAtIndex(i, PythonString($1[i]));
62 $result = list.release();
66 %typemap(in) lldb::tid_t {
67 using namespace lldb_private;
68 if (PythonInteger::Check($input))
70 PythonInteger py_int(PyRefType::Borrowed, $input);
71 $1 = static_cast<lldb::tid_t>(py_int.GetInteger());
75 PyErr_SetString(PyExc_ValueError, "Expecting an integer");
80 /* Typemap definitions to allow SWIG to properly handle char buffer. */
82 // typemap for a char buffer
83 // See also SBThread::GetStopDescription.
84 %typemap(in) (char *dst, size_t dst_len) {
85 if (!PyInt_Check($input)) {
86 PyErr_SetString(PyExc_ValueError, "Expecting an integer");
89 $2 = PyInt_AsLong($input);
91 PyErr_SetString(PyExc_ValueError, "Positive integer expected");
94 $1 = (char *) malloc($2);
96 // SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
97 // as char data instead of byte data.
98 %typemap(in) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
100 // Return the char buffer. Discarding any previous return result
101 // See also SBThread::GetStopDescription.
102 %typemap(argout) (char *dst, size_t dst_len) {
103 Py_XDECREF($result); /* Blow away any previous result */
108 llvm::StringRef ref(static_cast<const char*>($1), result);
109 lldb_private::PythonString string(ref);
110 $result = string.release();
114 // SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
115 // as char data instead of byte data.
116 %typemap(argout) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
119 // typemap for an outgoing buffer
120 // See also SBEvent::SBEvent(uint32_t event, const char *cstr, uint32_t cstr_len).
121 %typemap(in) (const char *cstr, uint32_t cstr_len) {
122 using namespace lldb_private;
123 if (PythonString::Check($input)) {
124 PythonString str(PyRefType::Borrowed, $input);
125 $1 = (char*)str.GetString().data();
128 else if(PythonByteArray::Check($input)) {
129 PythonByteArray bytearray(PyRefType::Borrowed, $input);
130 $1 = (char*)bytearray.GetBytes().data();
131 $2 = bytearray.GetSize();
133 else if (PythonBytes::Check($input)) {
134 PythonBytes bytes(PyRefType::Borrowed, $input);
135 $1 = (char*)bytes.GetBytes().data();
136 $2 = bytes.GetSize();
139 PyErr_SetString(PyExc_ValueError, "Expecting a string");
143 // Ditto for SBProcess::PutSTDIN(const char *src, size_t src_len).
144 %typemap(in) (const char *src, size_t src_len) {
145 using namespace lldb_private;
146 if (PythonString::Check($input)) {
147 PythonString str(PyRefType::Borrowed, $input);
148 $1 = (char*)str.GetString().data();
151 else if(PythonByteArray::Check($input)) {
152 PythonByteArray bytearray(PyRefType::Borrowed, $input);
153 $1 = (char*)bytearray.GetBytes().data();
154 $2 = bytearray.GetSize();
156 else if (PythonBytes::Check($input)) {
157 PythonBytes bytes(PyRefType::Borrowed, $input);
158 $1 = (char*)bytes.GetBytes().data();
159 $2 = bytes.GetSize();
162 PyErr_SetString(PyExc_ValueError, "Expecting a string");
166 // And SBProcess::WriteMemory.
167 %typemap(in) (const void *buf, size_t size) {
168 using namespace lldb_private;
169 if (PythonString::Check($input)) {
170 PythonString str(PyRefType::Borrowed, $input);
171 $1 = (void*)str.GetString().data();
174 else if(PythonByteArray::Check($input)) {
175 PythonByteArray bytearray(PyRefType::Borrowed, $input);
176 $1 = (void*)bytearray.GetBytes().data();
177 $2 = bytearray.GetSize();
179 else if (PythonBytes::Check($input)) {
180 PythonBytes bytes(PyRefType::Borrowed, $input);
181 $1 = (void*)bytes.GetBytes().data();
182 $2 = bytes.GetSize();
185 PyErr_SetString(PyExc_ValueError, "Expecting a buffer");
190 // For SBDebugger::DispatchInput
191 %typemap(in) (const void *data, size_t data_len) {
192 using namespace lldb_private;
193 if (PythonString::Check($input)) {
194 PythonString str(PyRefType::Borrowed, $input);
195 $1 = (void*)str.GetString().data();
198 else if(PythonByteArray::Check($input)) {
199 PythonByteArray bytearray(PyRefType::Borrowed, $input);
200 $1 = (void*)bytearray.GetBytes().data();
201 $2 = bytearray.GetSize();
203 else if (PythonBytes::Check($input)) {
204 PythonBytes bytes(PyRefType::Borrowed, $input);
205 $1 = (void*)bytes.GetBytes().data();
206 $2 = bytes.GetSize();
209 PyErr_SetString(PyExc_ValueError, "Expecting a buffer");
214 // typemap for an incoming buffer
215 // See also SBProcess::ReadMemory.
216 %typemap(in) (void *buf, size_t size) {
217 if (PyInt_Check($input)) {
218 $2 = PyInt_AsLong($input);
219 } else if (PyLong_Check($input)) {
220 $2 = PyLong_AsLong($input);
222 PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object");
226 PyErr_SetString(PyExc_ValueError, "Positive integer expected");
229 $1 = (void *) malloc($2);
232 // Return the buffer. Discarding any previous return result
233 // See also SBProcess::ReadMemory.
234 %typemap(argout) (void *buf, size_t size) {
235 Py_XDECREF($result); /* Blow away any previous result */
240 lldb_private::PythonBytes bytes(static_cast<const uint8_t*>($1), result);
241 $result = bytes.release();
246 // these typemaps allow Python users to pass list objects
247 // and have them turn into C++ arrays (this is useful, for instance
248 // when creating SBData objects from lists of numbers)
249 %typemap(in) (uint64_t* array, size_t array_len) {
250 /* Check if is a list */
251 if (PyList_Check($input)) {
252 int size = PyList_Size($input);
255 $1 = (uint64_t*) malloc(size * sizeof(uint64_t));
256 for (i = 0; i < size; i++) {
257 PyObject *o = PyList_GetItem($input,i);
258 if (PyInt_Check(o)) {
259 $1[i] = PyInt_AsLong(o);
261 else if (PyLong_Check(o)) {
262 $1[i] = PyLong_AsUnsignedLongLong(o);
265 PyErr_SetString(PyExc_TypeError,"list must contain numbers");
270 if (PyErr_Occurred()) {
275 } else if ($input == Py_None) {
279 PyErr_SetString(PyExc_TypeError,"not a list");
284 %typemap(freearg) (uint64_t* array, size_t array_len) {
288 %typemap(in) (uint32_t* array, size_t array_len) {
289 /* Check if is a list */
290 if (PyList_Check($input)) {
291 int size = PyList_Size($input);
294 $1 = (uint32_t*) malloc(size * sizeof(uint32_t));
295 for (i = 0; i < size; i++) {
296 PyObject *o = PyList_GetItem($input,i);
297 if (PyInt_Check(o)) {
298 $1[i] = PyInt_AsLong(o);
300 else if (PyLong_Check(o)) {
301 $1[i] = PyLong_AsUnsignedLong(o);
304 PyErr_SetString(PyExc_TypeError,"list must contain numbers");
309 if (PyErr_Occurred()) {
314 } else if ($input == Py_None) {
318 PyErr_SetString(PyExc_TypeError,"not a list");
323 %typemap(freearg) (uint32_t* array, size_t array_len) {
327 %typemap(in) (int64_t* array, size_t array_len) {
328 /* Check if is a list */
329 if (PyList_Check($input)) {
330 int size = PyList_Size($input);
333 $1 = (int64_t*) malloc(size * sizeof(int64_t));
334 for (i = 0; i < size; i++) {
335 PyObject *o = PyList_GetItem($input,i);
336 if (PyInt_Check(o)) {
337 $1[i] = PyInt_AsLong(o);
339 else if (PyLong_Check(o)) {
340 $1[i] = PyLong_AsLongLong(o);
343 PyErr_SetString(PyExc_TypeError,"list must contain numbers");
348 if (PyErr_Occurred()) {
353 } else if ($input == Py_None) {
357 PyErr_SetString(PyExc_TypeError,"not a list");
362 %typemap(freearg) (int64_t* array, size_t array_len) {
366 %typemap(in) (int32_t* array, size_t array_len) {
367 /* Check if is a list */
368 if (PyList_Check($input)) {
369 int size = PyList_Size($input);
372 $1 = (int32_t*) malloc(size * sizeof(int32_t));
373 for (i = 0; i < size; i++) {
374 PyObject *o = PyList_GetItem($input,i);
375 if (PyInt_Check(o)) {
376 $1[i] = PyInt_AsLong(o);
378 else if (PyLong_Check(o)) {
379 $1[i] = PyLong_AsLong(o);
382 PyErr_SetString(PyExc_TypeError,"list must contain numbers");
387 if (PyErr_Occurred()) {
392 } else if ($input == Py_None) {
396 PyErr_SetString(PyExc_TypeError,"not a list");
401 %typemap(freearg) (int32_t* array, size_t array_len) {
405 %typemap(in) (double* array, size_t array_len) {
406 /* Check if is a list */
407 if (PyList_Check($input)) {
408 int size = PyList_Size($input);
411 $1 = (double*) malloc(size * sizeof(double));
412 for (i = 0; i < size; i++) {
413 PyObject *o = PyList_GetItem($input,i);
414 if (PyFloat_Check(o)) {
415 $1[i] = PyFloat_AsDouble(o);
418 PyErr_SetString(PyExc_TypeError,"list must contain floating-point numbers");
423 } else if ($input == Py_None) {
427 PyErr_SetString(PyExc_TypeError,"not a list");
432 %typemap(freearg) (double* array, size_t array_len) {
436 // these typemaps wrap SBModule::GetVersion() from requiring a memory buffer
437 // to the more Pythonic style where a list is returned and no previous allocation
438 // is necessary - this will break if more than 50 versions are ever returned
439 %typemap(typecheck) (uint32_t *versions, uint32_t num_versions) {
440 $1 = ($input == Py_None ? 1 : 0);
443 %typemap(in, numinputs=0) (uint32_t *versions) {
444 $1 = (uint32_t*)malloc(sizeof(uint32_t) * 50);
447 %typemap(in, numinputs=0) (uint32_t num_versions) {
451 %typemap(argout) (uint32_t *versions, uint32_t num_versions) {
452 uint32_t count = result;
455 PyObject* list = PyList_New(count);
456 for (uint32_t j = 0; j < count; j++)
458 if ($1[j] < UINT32_MAX)
460 PyObject* item = PyInt_FromLong($1[j]);
461 int ok = PyList_SetItem(list,j,item);
474 %typemap(freearg) (uint32_t *versions) {
479 // For Log::LogOutputCallback
480 %typemap(in) (lldb::LogOutputCallback log_callback, void *baton) {
481 if (!($input == Py_None || PyCallable_Check(reinterpret_cast<PyObject*>($input)))) {
482 PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
486 // FIXME (filcab): We can't currently check if our callback is already
487 // LLDBSwigPythonCallPythonLogOutputCallback (to DECREF the previous
488 // baton) nor can we just remove all traces of a callback, if we want to
489 // revert to a file logging mechanism.
491 // Don't lose the callback reference
493 $1 = LLDBSwigPythonCallPythonLogOutputCallback;
497 %typemap(typecheck) (lldb::LogOutputCallback log_callback, void *baton) {
498 $1 = $input == Py_None;
499 $1 = $1 || PyCallable_Check(reinterpret_cast<PyObject*>($input));
502 %typemap(in) FILE * {
503 using namespace lldb_private;
504 if ($input == Py_None)
506 else if (!lldb_private::PythonFile::Check($input)) {
507 int fd = PyObject_AsFileDescriptor($input);
508 PythonObject py_input(PyRefType::Borrowed, $input);
509 PythonString py_mode = py_input.GetAttributeValue("mode").AsType<PythonString>();
511 if (-1 != fd && py_mode.IsValid()) {
513 if ((f = fdopen(fd, py_mode.GetString().str().c_str())))
516 PyErr_SetString(PyExc_TypeError, strerror(errno));
518 PyErr_SetString(PyExc_TypeError,"not a file-like object");
524 PythonFile py_file(PyRefType::Borrowed, $input);
526 if (!py_file.GetUnderlyingFile(file))
529 $1 = file.GetStream();
535 %typemap(out) FILE * {
541 short flags = $1->_flags;
545 else if (flags & __SWR)
547 else // if (flags & __SRW)
551 using namespace lldb_private;
552 File file($1, false);
553 PythonFile py_file(file, mode);
554 $result = py_file.release();
562 %typemap(in) (const char* string, int len) {
563 using namespace lldb_private;
564 if ($input == Py_None)
569 else if (PythonString::Check($input))
571 PythonString py_str(PyRefType::Borrowed, $input);
572 llvm::StringRef str = py_str.GetString();
573 $1 = const_cast<char*>(str.data());
575 // In Python 2, if $input is a PyUnicode object then this
576 // will trigger a Unicode -> String conversion, in which
577 // case the `PythonString` will now own the PyString. Thus
578 // if it goes out of scope, the data will be deleted. The
579 // only way to avoid this is to leak the Python object in
580 // that case. Note that if there was no conversion, then
581 // releasing the string will not leak anything, since we
582 // created this as a borrowed reference.
587 PyErr_SetString(PyExc_TypeError,"not a string-like object");