spiv: Fix image list counter
[gfxprim.git] / libs / core / GP_Common.c
blobd854cec0d431aac0bb48765b717f00a719a7c4fe
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2011-2012 Tomas Gavenciak <gavento@ucw.cz> *
20 * Copyright (C) 2012 Cyril Hrubis <metan@ucw.cz> *
21 * *
22 *****************************************************************************/
24 #define _GNU_SOURCE
26 #include "../config.h"
28 #include <stdio.h>
29 #include <stdarg.h>
31 #ifdef HAVE_BACKTRACE
32 #include <execinfo.h>
33 #endif /* HAVE_BACKTRACE */
35 #ifdef HAVE_DL
36 #include <dlfcn.h>
37 #endif /* HAVE_DL */
39 #include "core/GP_Common.h"
41 #define GP_ABORT_INFO_TRACE_LEVELS 20
44 void GP_DebugPrintCStack(void)
46 #ifdef HAVE_BACKTRACE
47 #if GP_ABORT_INFO_TRACE_LEVELS > 0
48 void * buffer[GP_ABORT_INFO_TRACE_LEVELS + 1];
49 int size = backtrace(buffer, GP_ABORT_INFO_TRACE_LEVELS);
50 fprintf(stderr, "\nC stack trace (most recent call first):\n");
51 fflush(stderr);
52 backtrace_symbols_fd(buffer, size, fileno(stderr));
53 #endif /* GP_ABORT_INFO_TRACE_LEVELS > 0 */
54 #endif /* HAVE_BACKTRACE */
57 static void print_python_stack(void)
59 #ifdef HAVE_DL
60 /* Print python stack trace in case python lib is loaded and
61 * a python interpreter is initialized */
62 int (*dl_Py_IsInitialized)();
63 int (*dl_PyRun_SimpleString)(const char *);
64 dl_Py_IsInitialized = dlsym(RTLD_DEFAULT, "Py_IsInitialized");
65 dl_PyRun_SimpleString = dlsym(RTLD_DEFAULT, "PyRun_SimpleString");
66 if (dl_Py_IsInitialized && dl_PyRun_SimpleString && dl_Py_IsInitialized()) {
67 fprintf(stderr, "\nPython stack trace (most recent call last; ignore last line):\n");
68 fflush(stderr);
69 dl_PyRun_SimpleString("import traceback; traceback.print_stack();");
71 #endif /* HAVE_DL */
74 void GP_PrintAbortInfo(const char *file, const char *func, unsigned int line,
75 const char *msg, const char *fmt, ...)
77 va_list va;
79 fprintf(stderr, "*** gfxprim: %s:%d: in %s: %s\n",
80 file, line, func, msg);
82 va_start(va, fmt);
83 vfprintf(stderr, fmt, va);
84 va_end(va);
86 fprintf(stderr, "\n");
88 print_python_stack();
89 GP_DebugPrintCStack();