OMCompiler/SimulationRuntime/c/meta/meta_modelica_segv.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | |||
| 29 | /* The stack-overflow trace as a MetaModelica list. The handling itself is in | ||
| 30 | util/omc_stackoverflow.c, shared with the simulation runtime. */ | ||
| 31 | |||
| 32 | #include "meta_modelica.h" | ||
| 33 | #include "meta_modelica_string.h" | ||
| 34 | #include "meta_modelica_segv.h" | ||
| 35 | #include "../util/omc_stackoverflow.h" | ||
| 36 | #include <assert.h> | ||
| 37 | #include <stdlib.h> | ||
| 38 | |||
| 39 | #if (defined(__linux__) && defined(__GLIBC__)) || defined(__APPLE__) || defined(__FreeBSD__) | ||
| 40 | #include <execinfo.h> | ||
| 41 | |||
| 42 | #define _OMC_LIT_OOM_data "Out of memory! Printed backtrace to stderr." | ||
| 43 | static const MMC_DEFSTRINGLIT(_OMC_LIT_STRUCT_OOM,45,_OMC_LIT_OOM_data); | ||
| 44 | static const MMC_DEFSTRUCTLIT(_OMC_LIT_STRUCT_OOM_LST,2,1) {MMC_REFSTRINGLIT(_OMC_LIT_STRUCT_OOM),MMC_REFSTRUCTLIT(mmc_nil)}}; | ||
| 45 | |||
| 46 | ✗ | void mmc_setStacktraceMessages_threadData(threadData_t *threadData, int numSkip, int numFrames) | |
| 47 | { | ||
| 48 | void **trace; | ||
| 49 | char **messages; | ||
| 50 | void *res; | ||
| 51 | int i; | ||
| 52 | int trace_size; | ||
| 53 | |||
| 54 | ✗ | assert(numFrames > 0); | |
| 55 | |||
| 56 | ✗ | trace = (void**) malloc(numFrames*sizeof(void*)); | |
| 57 | ✗ | if (trace==0) { | |
| 58 | ✗ | mmc_setStacktraceMessages(numSkip, numFrames); | |
| 59 | ✗ | printStacktraceMessages(); | |
| 60 | ✗ | threadData->localRoots[LOCAL_ROOT_STACK_OVERFLOW] = MMC_REFSTRINGLIT(_OMC_LIT_STRUCT_OOM_LST); | |
| 61 | ✗ | return; | |
| 62 | } | ||
| 63 | ✗ | trace_size = backtrace(trace, numFrames); | |
| 64 | |||
| 65 | res = mmc_mk_nil(); | ||
| 66 | ✗ | messages = backtrace_symbols(trace, trace_size); | |
| 67 | |||
| 68 | ✗ | if (trace_size==numFrames) { | |
| 69 | ✗ | res = mmc_mk_cons(mmc_mk_scon("[...]"), res); | |
| 70 | } | ||
| 71 | ✗ | for (i=trace_size-1; i>=numSkip; --i) { | |
| 72 | ✗ | res = mmc_mk_cons(mmc_mk_scon(messages[i]), res); | |
| 73 | } | ||
| 74 | ✗ | free(trace); | |
| 75 | ✗ | free(messages); | |
| 76 | ✗ | threadData->localRoots[LOCAL_ROOT_STACK_OVERFLOW] = res; | |
| 77 | } | ||
| 78 | |||
| 79 | #else | ||
| 80 | |||
| 81 | void mmc_setStacktraceMessages_threadData(threadData_t *threadData, int numSkip, int numFrames) | ||
| 82 | { | ||
| 83 | threadData->localRoots[LOCAL_ROOT_STACK_OVERFLOW] = mmc_mk_cons(mmc_mk_scon("[... unsupported platform for backtraces]"), mmc_mk_nil()); | ||
| 84 | } | ||
| 85 | |||
| 86 | #endif | ||
| 87 | |||
| 88 | ✗ | void* mmc_getStacktraceMessages_threadData(threadData_t *threadData) | |
| 89 | { | ||
| 90 | ✗ | if (!threadData->localRoots[LOCAL_ROOT_STACK_OVERFLOW]) { | |
| 91 | ✗ | MMC_THROW_INTERNAL(); | |
| 92 | } | ||
| 93 | ✗ | return threadData->localRoots[LOCAL_ROOT_STACK_OVERFLOW]; | |
| 94 | } | ||
| 95 | |||
| 96 | /* omc captures the trace; a simulation leaves the hook unset. */ | ||
| 97 | 2677 | static void __attribute__((constructor)) install_stacktrace_capture(void) | |
| 98 | { | ||
| 99 | 2677 | omc_set_stacktrace_capture(mmc_setStacktraceMessages_threadData); | |
| 100 | 2677 | } | |
| 101 |