OMCompiler/SimulationRuntime/c/simulation/simulation_omc_assert.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 | #include <stdarg.h> | ||
| 29 | #include <stddef.h> | ||
| 30 | #include "../util/utility.h" | ||
| 31 | #include "simulation_omc_assert.h" | ||
| 32 | #include "../util/omc_init.h" | ||
| 33 | #include "simulation_runtime.h" | ||
| 34 | |||
| 35 | |||
| 36 | void (*omc_assert_withEquationIndexes)(threadData_t*, FILE_INFO info, const int *indexes, const char *msg, ...) = omc_assert_simulation_withEquationIndexes; | ||
| 37 | |||
| 38 | void (*omc_assert_warning_withEquationIndexes)(FILE_INFO info, const int *indexes, const char *msg, ...) = omc_assert_warning_simulation_withEquationIndexes; | ||
| 39 | |||
| 40 | |||
| 41 | int terminationTerminate = 0; /* Becomes non-zero when user terminates simulation. */ | ||
| 42 | FILE_INFO TermInfo; /* message for termination. */ | ||
| 43 | char* TermMsg; /* message for termination. */ | ||
| 44 | |||
| 45 | /*! \fn void setTermMsg(const char* msg) | ||
| 46 | * | ||
| 47 | * prints all values as arguments it need data | ||
| 48 | * and which part of the ring should printed. | ||
| 49 | */ | ||
| 50 | ✗ | static void setTermMsg(const char *msg, va_list ap) | |
| 51 | { | ||
| 52 | size_t i; | ||
| 53 | static size_t termMsgSize = 0; | ||
| 54 | ✗ | if(NULL == TermMsg) | |
| 55 | { | ||
| 56 | ✗ | termMsgSize = modelica_integer_max(strlen(msg)*2+1,(size_t)2048); | |
| 57 | ✗ | TermMsg = (char*) malloc(termMsgSize); | |
| 58 | } | ||
| 59 | ✗ | i = vsnprintf(TermMsg,termMsgSize,msg,ap); | |
| 60 | ✗ | if(i >= termMsgSize) | |
| 61 | { | ||
| 62 | ✗ | free(TermMsg); | |
| 63 | ✗ | termMsgSize = 2*i+1; | |
| 64 | ✗ | TermMsg = (char*)malloc(termMsgSize); | |
| 65 | vsnprintf(TermMsg,termMsgSize,msg,ap); | ||
| 66 | } | ||
| 67 | ✗ | } | |
| 68 | |||
| 69 | ✗ | static void va_omc_assert_simulation_withEquationIndexes(threadData_t *threadData, FILE_INFO info, const int *indexes, const char *msg, va_list args) | |
| 70 | { | ||
| 71 | ✗ | threadData = omc_thread_data_va(threadData, msg, args); | |
| 72 | ✗ | switch (threadData->currentErrorStage) | |
| 73 | { | ||
| 74 | ✗ | case ERROR_EVENTSEARCH: | |
| 75 | case ERROR_SIMULATION: | ||
| 76 | ✗ | va_errorStreamPrintWithEquationIndexes(OMC_LOG_ASSERT, info, 0, indexes, msg, args); | |
| 77 | ✗ | OMC_ERROR_RAISE(); | |
| 78 | ✗ | return; | |
| 79 | ✗ | case ERROR_NONLINEARSOLVER: | |
| 80 | ✗ | if(OMC_ACTIVE_STREAM(OMC_LOG_NLS)) | |
| 81 | { | ||
| 82 | ✗ | va_errorStreamPrintWithEquationIndexes(OMC_LOG_ASSERT, info, 0, indexes, msg, args); | |
| 83 | } | ||
| 84 | else | ||
| 85 | { | ||
| 86 | ✗ | omc_note_suppressed_error(threadData, msg, args); | |
| 87 | } | ||
| 88 | ✗ | OMC_ERROR_RAISE(); | |
| 89 | ✗ | return; | |
| 90 | ✗ | case ERROR_INTEGRATOR: | |
| 91 | ✗ | if(OMC_ACTIVE_STREAM(OMC_LOG_SOLVER)) | |
| 92 | { | ||
| 93 | ✗ | va_errorStreamPrintWithEquationIndexes(OMC_LOG_ASSERT, info, 0, indexes, msg, args); | |
| 94 | } | ||
| 95 | ✗ | OMC_ERROR_RAISE(); | |
| 96 | ✗ | return; | |
| 97 | ✗ | case ERROR_EVENTHANDLING: | |
| 98 | ✗ | va_errorStreamPrintWithEquationIndexes(OMC_LOG_ASSERT, info, 0, indexes, msg, args); | |
| 99 | ✗ | OMC_ERROR_RAISE(); | |
| 100 | ✗ | return; | |
| 101 | ✗ | case ERROR_OPTIMIZE: | |
| 102 | default: | ||
| 103 | ✗ | va_errorStreamPrintWithEquationIndexes(OMC_LOG_ASSERT, info, 0, indexes, msg, args); | |
| 104 | ✗ | throwStreamPrint(threadData, "Untreated assertion has been detected."); | |
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | ✗ | void omc_assert_simulation(threadData_t *threadData, FILE_INFO info, const char *msg, ...) | |
| 109 | { | ||
| 110 | va_list args; | ||
| 111 | ✗ | va_start(args, msg); | |
| 112 | ✗ | va_omc_assert_simulation_withEquationIndexes(threadData, info, NULL, msg, args); | |
| 113 | ✗ | va_end(args); | |
| 114 | ✗ | } | |
| 115 | |||
| 116 | ✗ | void omc_assert_simulation_withEquationIndexes(threadData_t *threadData, FILE_INFO info, const int *indexes, const char *msg, ...) | |
| 117 | { | ||
| 118 | va_list args; | ||
| 119 | ✗ | va_start(args, msg); | |
| 120 | ✗ | va_omc_assert_simulation_withEquationIndexes(threadData, info, indexes, msg, args); | |
| 121 | ✗ | va_end(args); | |
| 122 | ✗ | } | |
| 123 | |||
| 124 | |||
| 125 | static void va_omc_assert_warning_simulation(FILE_INFO info, const int *indexes, const char *msg, va_list args) | ||
| 126 | { | ||
| 127 | ✗ | va_warningStreamPrintWithEquationIndexes(OMC_LOG_ASSERT, info, 0, indexes, msg, args); | |
| 128 | } | ||
| 129 | |||
| 130 | ✗ | void omc_assert_warning_simulation(FILE_INFO info, const char *msg, ...) | |
| 131 | { | ||
| 132 | va_list args; | ||
| 133 | ✗ | va_start(args, msg); | |
| 134 | ✗ | va_omc_assert_warning_simulation(info, NULL, msg, args); | |
| 135 | ✗ | va_end(args); | |
| 136 | ✗ | } | |
| 137 | |||
| 138 | ✗ | void omc_assert_warning_simulation_withEquationIndexes(FILE_INFO info, const int *indexes, const char *msg, ...) | |
| 139 | { | ||
| 140 | va_list args; | ||
| 141 | ✗ | va_start(args, msg); | |
| 142 | ✗ | va_omc_assert_warning_simulation(info, indexes, msg, args); | |
| 143 | ✗ | va_end(args); | |
| 144 | ✗ | } | |
| 145 | |||
| 146 | ✗ | void omc_terminate_simulation(FILE_INFO info, const char *msg, ...) | |
| 147 | { | ||
| 148 | va_list ap; | ||
| 149 | ✗ | va_start(ap,msg); | |
| 150 | ✗ | terminationTerminate = 1; | |
| 151 | ✗ | setTermMsg(msg,ap); | |
| 152 | ✗ | va_end(ap); | |
| 153 | ✗ | TermInfo = info; | |
| 154 | ✗ | } | |
| 155 | |||
| 156 | /* | ||
| 157 | * adrpo: workaround function to call setTermMsg with empty va_list! | ||
| 158 | * removes the uninitialized warning for va_list variable. | ||
| 159 | */ | ||
| 160 | ✗ | void setTermMsg_empty_va_list(const char *msg, ...) { | |
| 161 | va_list dummy; | ||
| 162 | ✗ | va_start(dummy, msg); | |
| 163 | ✗ | setTermMsg(msg, dummy); | |
| 164 | ✗ | va_end(dummy); | |
| 165 | ✗ | } | |
| 166 | |||
| 167 | ✗ | void omc_throw_simulation(threadData_t* threadData) | |
| 168 | { | ||
| 169 | /* Ends the simulation: unlike a model's assert, it lands at the run's own | ||
| 170 | catch, not at a step that would retry it. */ | ||
| 171 | ✗ | setTermMsg_empty_va_list("Assertion triggered by external C function"); | |
| 172 | ✗ | set_struct(FILE_INFO, TermInfo, omc_dummyFileInfo); | |
| 173 | ✗ | threadData = omc_thread_data(threadData, "Assertion triggered by external C function"); | |
| 174 | ✗ | longjmp(*threadData->globalJumpBuffer, 1); | |
| 175 | } | ||
| 176 |