Enrico Granata | 2c3f140 | 2013-12-12 00:02:05 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | The LLVM Compiler Infrastructure |
| 3 | |
| 4 | This file is distributed under the University of Illinois Open Source |
| 5 | License. See LICENSE.TXT for details. |
| 6 | ****************************************************************************** |
| 7 | |
| 8 | * This C file vends a simple interface to set the Application Specific Info |
| 9 | * on Mac OS X through Python. To use, compile as a dylib, import crashinfo |
| 10 | * and call crashinfo.setCrashReporterDescription("hello world") |
| 11 | * The testCrashReporterDescription() API is simply there to let you test that this |
| 12 | * is doing what it is intended to do without having to actually cons up a crash |
Enrico Granata | 2c3f140 | 2013-12-12 00:02:05 +0000 | [diff] [blame] | 13 | ******************************************************************************/ |
| 14 | |
| 15 | #include <Python/Python.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <stdio.h> |
| 19 | |
Enrico Granata | 601ec50 | 2013-12-12 01:47:35 +0000 | [diff] [blame] | 20 | void *__crashreporter_info__ = NULL; |
Enrico Granata | 2c3f140 | 2013-12-12 00:02:05 +0000 | [diff] [blame] | 21 | |
| 22 | asm(".desc ___crashreporter_info__, 0x10"); |
| 23 | |
| 24 | static PyObject* setCrashReporterDescription(PyObject* self, PyObject* string) |
| 25 | { |
| 26 | if (__crashreporter_info__) |
| 27 | { |
| 28 | free(__crashreporter_info__); |
| 29 | __crashreporter_info__ = NULL; |
| 30 | } |
| 31 | |
| 32 | if (string && PyString_Check(string)) |
| 33 | { |
| 34 | Py_ssize_t size = PyString_Size(string); |
| 35 | char* data = PyString_AsString(string); |
Greg Clayton | d3b16b3 | 2013-12-13 02:02:44 +0000 | [diff] [blame] | 36 | if (size > 0 && data) |
Enrico Granata | 2c3f140 | 2013-12-12 00:02:05 +0000 | [diff] [blame] | 37 | { |
Enrico Granata | 29726a9 | 2014-01-08 03:14:18 +0000 | [diff] [blame] | 38 | ++size; // Include the NULL terminateor in allocation and memcpy() |
Enrico Granata | 2c3f140 | 2013-12-12 00:02:05 +0000 | [diff] [blame] | 39 | __crashreporter_info__ = malloc(size); |
Greg Clayton | d3b16b3 | 2013-12-13 02:02:44 +0000 | [diff] [blame] | 40 | memcpy(__crashreporter_info__, data, size); |
Enrico Granata | 2c3f140 | 2013-12-12 00:02:05 +0000 | [diff] [blame] | 41 | return Py_True; |
| 42 | } |
| 43 | } |
| 44 | return Py_False; |
| 45 | } |
| 46 | |
| 47 | static PyObject* testCrashReporterDescription(PyObject*self, PyObject* arg) |
| 48 | { |
| 49 | int* ptr = 0; |
| 50 | *ptr = 1; |
| 51 | return Py_None; |
| 52 | } |
| 53 | |
| 54 | static PyMethodDef crashinfo_methods[] = { |
| 55 | {"setCrashReporterDescription", setCrashReporterDescription, METH_O}, |
| 56 | {"testCrashReporterDescription", testCrashReporterDescription, METH_O}, |
| 57 | {NULL, NULL} |
| 58 | }; |
| 59 | |
| 60 | void initcrashinfo() |
| 61 | { |
| 62 | (void) Py_InitModule("crashinfo", crashinfo_methods); |
| 63 | } |
| 64 | |