Bytealyzer Diego Devesa commited on
Commit
ccee17d
·
1 Parent(s): ec4b1b3

ggml : Callback before abort (llama/14481)

Browse files

* Add a callback that will be called just before abort. This allows apps without a console to display a message to the user and save data if needed.

* Return previous callback to allow callback chaining

* style fixes

---------

Co-authored-by: Diego Devesa <slarengh@gmail.com>

Files changed (2) hide show
  1. ggml/include/ggml.h +7 -0
  2. ggml/src/ggml.c +19 -4
ggml/include/ggml.h CHANGED
@@ -314,6 +314,13 @@
314
  extern "C" {
315
  #endif
316
 
 
 
 
 
 
 
 
317
  GGML_NORETURN GGML_ATTRIBUTE_FORMAT(3, 4)
318
  GGML_API void ggml_abort(const char * file, int line, const char * fmt, ...);
319
 
 
314
  extern "C" {
315
  #endif
316
 
317
+ // Function type used in fatal error callbacks
318
+ typedef void (*ggml_abort_callback_t)(const char * error_message);
319
+
320
+ // Set the abort callback (passing null will restore original abort functionality: printing a message to stdout)
321
+ // Returns the old callback for chaining
322
+ GGML_API ggml_abort_callback_t ggml_set_abort_callback(ggml_abort_callback_t callback);
323
+
324
  GGML_NORETURN GGML_ATTRIBUTE_FORMAT(3, 4)
325
  GGML_API void ggml_abort(const char * file, int line, const char * fmt, ...);
326
 
ggml/src/ggml.c CHANGED
@@ -202,19 +202,34 @@ void ggml_print_backtrace(void) {
202
  }
203
  #endif
204
 
 
 
 
 
 
 
 
 
 
205
  void ggml_abort(const char * file, int line, const char * fmt, ...) {
206
  fflush(stdout);
207
 
208
- fprintf(stderr, "%s:%d: ", file, line);
 
209
 
210
  va_list args;
211
  va_start(args, fmt);
212
- vfprintf(stderr, fmt, args);
213
  va_end(args);
214
 
215
- fprintf(stderr, "\n");
 
 
 
 
 
 
216
 
217
- ggml_print_backtrace();
218
  abort();
219
  }
220
 
 
202
  }
203
  #endif
204
 
205
+ static ggml_abort_callback_t g_abort_callback = NULL;
206
+
207
+ // Set the abort callback (passing null will restore original abort functionality: printing a message to stdout)
208
+ GGML_API ggml_abort_callback_t ggml_set_abort_callback(ggml_abort_callback_t callback) {
209
+ ggml_abort_callback_t ret_val = g_abort_callback;
210
+ g_abort_callback = callback;
211
+ return ret_val;
212
+ }
213
+
214
  void ggml_abort(const char * file, int line, const char * fmt, ...) {
215
  fflush(stdout);
216
 
217
+ char message[2048];
218
+ int offset = snprintf(message, sizeof(message), "%s:%d: ", file, line);
219
 
220
  va_list args;
221
  va_start(args, fmt);
222
+ vsnprintf(message + offset, sizeof(message) - offset, fmt, args);
223
  va_end(args);
224
 
225
+ if (g_abort_callback) {
226
+ g_abort_callback(message);
227
+ } else {
228
+ // default: print error and backtrace to stderr
229
+ fprintf(stderr, "%s\n", message);
230
+ ggml_print_backtrace();
231
+ }
232
 
 
233
  abort();
234
  }
235