PIPS
errors.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "boolean.h"
#include "arithmetique.h"
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include "linear_assert.h"
+ Include dependency graph for errors.c:

Go to the source code of this file.

Data Structures

struct  linear_exception_holder
 A structure for the exception stack. More...
 
struct  timeout_t
 hold a timeout prescription More...
 

Macros

#define MAX_STACKED_CONTEXTS   64
 exception stack. More...
 
#define exception_debug_message(type)
 
#define exception_debug_trace(type)
 
#define same_string_p(s1, s2)   (strcmp((s1),(s2))==0)
 
#define throw_exception   linear_throw_exception
 
#define TIMEOUT_MAX_STACK_SIZE   10
 

Functions

char const * __attribute__ ((weak))
 This can be overrided in the PolyLib. More...
 
void linear_reset_error_counters (void)
 reset linear counters More...
 
unsigned int linear_error_count (void)
 return number of linear errors may be used as a test after a reset to know whether new errors occured. More...
 
void linear_get_error_counts (unsigned int *poe, unsigned int *pse, unsigned int *pme)
 return various errors counts through unsigned int pointer overflow, simplex & misc (aka others) NULL pointers are ignored More...
 
static void linear_record_exception (const linear_exception_t exception)
 
void __attribute__ ((weak, noreturn))
 throws an exception of a given type by searching for the specified 'what' in the current exception stack. More...
 
bool linear_with_gmp (void)
 whether linear can use gmp (i.e. More...
 
bool linear_require_gmp (void)
 whether linear is asked to use gmp if possible (env variable) More...
 
bool linear_use_gmp (void)
 whether linear is to use gmp More...
 
void set_timeout_callback (timeout_callback_f callback)
 
static void __attribute__ ((noreturn))
 
void push_timeout (const unsigned int delay, const char *funcname, const char *filename, const int lineno)
 
static int env2int (const char *env)
 
bool push_timeout_env (const char *env, const char *funcname, const char *filename, const int lineno)
 
void pop_timeout (__attribute__((unused)) const char *funcname, __attribute__((unused)) const char *filename, __attribute__((unused)) const int lineno)
 
void pop_timeout_env (const char *env, const char *funcname, const char *filename, const int lineno)
 

Variables

static int timeout_index = 0
 
int linear_assertion_result = -1
 errors.c More...
 
static unsigned int overflow_error_count = 0
 
static unsigned int simplex_error_count = 0
 
static unsigned int misc_error_count = 0
 
static bool linear_exception_debug_mode = false
 whether to run in debug mode (that is to trace catch/uncatch/throw) More...
 
static unsigned int linear_exception_verbose = 1 | 2 | 16
 
static linear_exception_holder exception_stack [MAX_STACKED_CONTEXTS]
 
static int exception_index = 0
 
static exception_callback_t push_callback = NULL
 callbacks... More...
 
static exception_callback_t pop_callback = NULL
 
static bool warned_if_inconsistent_gmp = false
 
static timeout_callback_f timeout_callback = NULL
 a stack of timeouts is kept. More...
 
static timeout_t timeouts [TIMEOUT_MAX_STACK_SIZE]
 

Macro Definition Documentation

◆ exception_debug_message

#define exception_debug_message (   type)
Value:
fprintf(stderr, "%s[%s:%d %s (%d)/%d]\n", \
type, file, line, function, what, exception_index)
static int exception_index
Definition: errors.c:150
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
static int line
FLEX_SCANNER.
Definition: scanner.c:852

Definition at line 207 of file errors.c.

◆ exception_debug_trace

#define exception_debug_trace (   type)
Value:
exception_debug_message(type);
static bool linear_exception_debug_mode
whether to run in debug mode (that is to trace catch/uncatch/throw)
Definition: errors.c:121

Definition at line 211 of file errors.c.

◆ MAX_STACKED_CONTEXTS

#define MAX_STACKED_CONTEXTS   64

exception stack.

maximum extension.

  • current index (next available bucket)

Definition at line 148 of file errors.c.

◆ same_string_p

#define same_string_p (   s1,
  s2 
)    (strcmp((s1),(s2))==0)

Definition at line 265 of file errors.c.

◆ throw_exception

#define throw_exception   linear_throw_exception

Definition at line 415 of file errors.c.

◆ TIMEOUT_MAX_STACK_SIZE

#define TIMEOUT_MAX_STACK_SIZE   10

Definition at line 507 of file errors.c.

Function Documentation

◆ __attribute__() [1/3]

static void __attribute__ ( (noreturn)  )
static

Definition at line 512 of file errors.c.

513 {
514  int toidx = timeout_index - 1;
515 
516  assert(sig == SIGALRM);
517  assert(toidx >= 0);
518 
519  // always say something... even if redundant with callback
520  fprintf(stderr, "timeout[%d] after %ds from %s (\"%s\":%d)\n", toidx,
521  timeouts[toidx].delay,
522  timeouts[toidx].funcname,
523  timeouts[toidx].filename,
524  timeouts[toidx].lineno);
525  fflush(stderr);
526 
527  // note: the timeout may not return
528  if (timeout_callback != NULL)
529  (*timeout_callback)(timeouts[toidx].delay,
530  timeouts[toidx].funcname,
531  timeouts[toidx].filename,
532  timeouts[toidx].lineno);
533 
534  // may try to go on, although it is probably not a good idea
535  POP_TIMEOUT();
537 }
@ timeout_error
#define THROW(what)
#define POP_TIMEOUT()
static timeout_t timeouts[TIMEOUT_MAX_STACK_SIZE]
Definition: errors.c:509
static int timeout_index
Definition: errors.c:40
static timeout_callback_f timeout_callback
a stack of timeouts is kept.
Definition: errors.c:483
#define assert(ex)
Definition: newgen_assert.h:41
unsigned int delay
Definition: errors.c:497
const char * funcname
Definition: errors.c:501
const char * filename
Definition: errors.c:502
int lineno
Definition: errors.c:503

References assert, timeout_t::delay, timeout_t::filename, fprintf(), timeout_t::funcname, timeout_t::lineno, POP_TIMEOUT, THROW, timeout_callback, timeout_error, timeout_index, and timeouts.

+ Here is the call graph for this function:

◆ __attribute__() [2/3]

char const* __attribute__ ( (weak)  )

This can be overrided in the PolyLib.

This can be overridden in the PolyLib.

pop a what exception.

push a what exception on stack.

dump stack

total number of exceptions thrown, for statistics.

keep track of last thrown exception for RETHROW()

And seems to be according to gdb who cannot find the corresponding source code.

This can be overrided in the PolyLib

check for any mismatch! This can be overrided in the PolyLib

Definition at line 47 of file errors.c.

49 {
50  if (exception==overflow_error)
51  return "overflow_error exception";
52  if (exception==simplex_arithmetic_error)
53  return "simplex_arithmetic_error exception";
54  if (exception==user_exception_error)
55  return "user_exception_error exception";
56  if (exception==parser_exception_error)
57  return "parser_exception_error exception";
58  if (exception==timeout_error)
59  return "timeout_error exception";
60  if (exception==any_exception_error)
61  return "all exceptions mask";
62 
63  return "unknown or mixed exception";
64 }
@ overflow_error
@ simplex_arithmetic_error
@ user_exception_error
@ any_exception_error
catch all
@ parser_exception_error

References any_exception_error, overflow_error, parser_exception_error, simplex_arithmetic_error, timeout_error, and user_exception_error.

◆ __attribute__() [3/3]

void __attribute__ ( (weak, noreturn)  )

throws an exception of a given type by searching for the specified 'what' in the current exception stack.

This can be overridden in the PolyLib

Definition at line 337 of file errors.c.

344 {
345  int i;
346 
347  exception_debug_trace("THROW");
348 
349  the_last_just_thrown_exception = what; // for rethrow
351 
352  for (i=exception_index-1; i>=0; i--)
353  {
354  if (pop_callback)
355  // call pop callback with push parameters
357  exception_stack[i].function,
358  exception_stack[i].line);
359 
360  if (exception_stack[i].what & what)
361  {
362  exception_index = i;
364 
366  fprintf(stderr, "---->[%s:%d %s (%d)/%d]\n",
367  exception_stack[i].file,
369  exception_stack[i].function,
370  exception_stack[i].what,
371  i);
372 
373  // trace some exceptions...
374  if (linear_exception_verbose & what)
375  fprintf(stderr, "exception %d/%d: %s(%s:%d) -> %s(%s:%d)\n",
376  what, exception_stack[i].what,
377  function, file, line,
378  exception_stack[i].function,
379  exception_stack[i].file,
380  exception_stack[i].line);
381 
382  // pop timeouts
383  while (timeout_index > exception_stack[i].timeout)
384  POP_TIMEOUT();
385 
386  longjmp(exception_stack[i].where, 0);
387  }
388  }
389 
390  // internal error, THROW without matching CATCH
391  exception_debug_message("throw");
392  fprintf(stderr,
393  "exception not found in stack:\n"
394  "an exception was THROWN without a matching CATCH\n");
396 
397  // timeout triggers an exception which may be intentionnaly not caught
398  if (what == timeout_error)
399  exit(203);
400  else
401  abort();
402 }
linear_exception_t the_last_just_thrown_exception
int linear_number_of_exception_thrown
void dump_exception_stack(void)
static unsigned int linear_exception_verbose
Definition: errors.c:122
static exception_callback_t pop_callback
Definition: errors.c:155
static void linear_record_exception(const linear_exception_t exception)
Definition: errors.c:102
#define exception_debug_trace(type)
Definition: errors.c:211
#define exception_debug_message(type)
Definition: errors.c:207
static linear_exception_holder exception_stack[MAX_STACKED_CONTEXTS]
Definition: errors.c:149
#define exit(code)
Definition: misc-local.h:54
#define abort()
Definition: misc-local.h:53

References abort, dump_exception_stack(), exception_debug_message, exception_debug_trace, exception_index, exception_stack, exit, fprintf(), line, linear_exception_debug_mode, linear_exception_verbose, linear_number_of_exception_thrown, linear_record_exception(), pop_callback, POP_TIMEOUT, the_last_just_thrown_exception, timeout_error, and timeout_index.

+ Here is the call graph for this function:

◆ env2int()

static int env2int ( const char *  env)
static

Definition at line 563 of file errors.c.

564 {
565  char * val = getenv(env);
566  if (val != NULL)
567  {
568  int i = atoi(val);
569  if (i >= 0)
570  return i;
571  }
572  return -1;
573 }
static jmp_buf env
Definition: genClib.c:2473

References env.

Referenced by pop_timeout_env(), and push_timeout_env().

+ Here is the caller graph for this function:

◆ linear_error_count()

unsigned int linear_error_count ( void  )

return number of linear errors may be used as a test after a reset to know whether new errors occured.

Definition at line 85 of file errors.c.

86 {
88 }
static unsigned int misc_error_count
Definition: errors.c:71
static unsigned int simplex_error_count
Definition: errors.c:70
static unsigned int overflow_error_count
Definition: errors.c:69

References misc_error_count, overflow_error_count, and simplex_error_count.

Referenced by catch_user_error().

+ Here is the caller graph for this function:

◆ linear_get_error_counts()

void linear_get_error_counts ( unsigned int poe,
unsigned int pse,
unsigned int pme 
)

return various errors counts through unsigned int pointer overflow, simplex & misc (aka others) NULL pointers are ignored

Parameters
poeoe
psese
pmeme

Definition at line 94 of file errors.c.

96 {
97  if (poe) *poe = overflow_error_count;
98  if (pse) *pse = simplex_error_count;
99  if (pme) *pme = misc_error_count;
100 }

References misc_error_count, overflow_error_count, and simplex_error_count.

Referenced by catch_user_error().

+ Here is the caller graph for this function:

◆ linear_record_exception()

static void linear_record_exception ( const linear_exception_t  exception)
static

Definition at line 102 of file errors.c.

103 {
104  if (exception==overflow_error)
106  else if (exception==simplex_arithmetic_error)
108  else if (exception==user_exception_error ||
109  exception==parser_exception_error ||
110  exception==timeout_error)
111  misc_error_count ++;
112 }

References misc_error_count, overflow_error, overflow_error_count, parser_exception_error, simplex_arithmetic_error, simplex_error_count, timeout_error, and user_exception_error.

Referenced by __attribute__().

+ Here is the caller graph for this function:

◆ linear_require_gmp()

bool linear_require_gmp ( void  )

whether linear is asked to use gmp if possible (env variable)

Definition at line 446 of file errors.c.

447 {
448  char* env = getenv("LINEAR_USE_GMP");
449  return env && atoi(env) != 0;
450 }

References env.

Referenced by catch_user_error(), and linear_use_gmp().

+ Here is the caller graph for this function:

◆ linear_reset_error_counters()

void linear_reset_error_counters ( void  )

reset linear counters

Definition at line 75 of file errors.c.

76 {
79  misc_error_count = 0;
80 }

References misc_error_count, overflow_error_count, and simplex_error_count.

Referenced by catch_user_error().

+ Here is the caller graph for this function:

◆ linear_use_gmp()

bool linear_use_gmp ( void  )

whether linear is to use gmp

Definition at line 454 of file errors.c.

455 {
456  bool
457  with_gmp = linear_with_gmp(),
458  req_gmp = linear_require_gmp();
459 
460  if (req_gmp && !with_gmp && !warned_if_inconsistent_gmp)
461  {
462  fprintf(stderr, "linear was compiled without GMP support\n");
464  }
465 
466  return with_gmp && req_gmp;
467 }
bool linear_require_gmp(void)
whether linear is asked to use gmp if possible (env variable)
Definition: errors.c:446
static bool warned_if_inconsistent_gmp
Definition: errors.c:431
bool linear_with_gmp(void)
whether linear can use gmp (i.e.
Definition: errors.c:435

References fprintf(), linear_require_gmp(), linear_with_gmp(), and warned_if_inconsistent_gmp.

Referenced by internal_sc_feasibility(), sc_convex_hull(), sc_to_sg_chernikova(), and sg_to_sc_chernikova().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linear_with_gmp()

bool linear_with_gmp ( void  )

whether linear can use gmp (i.e.

compiled in)

Definition at line 435 of file errors.c.

436 {
437 #ifdef HAVE_GMP_H
438  return true;
439 #else
440  return false;
441 #endif // HAVE_GMP_H
442 }

Referenced by catch_user_error(), and linear_use_gmp().

+ Here is the caller graph for this function:

◆ pop_timeout()

void pop_timeout ( __attribute__((unused)) const char *  funcname,
__attribute__((unused)) const char *  filename,
__attribute__((unused)) const int  lineno 
)

Definition at line 591 of file errors.c.

595 {
596  time_t now = time(NULL);
597 
598  assert(timeout_index > 0);
599 
600  // cleanup
601  alarm(0);
602  signal(SIGALRM, SIG_DFL);
603 
604  timeout_index--;
605 
606  // reset to previous delay
607  if (timeout_index > 0)
608  {
609  int delay = timeouts[timeout_index - 1].end - now;
610 
611  if (delay <= 0)
612  timeout_handler(SIGALRM);
613 
614  signal(SIGALRM, timeout_handler);
615  alarm(delay);
616  }
617 }
time_t end
Definition: errors.c:496

References assert, timeout_t::end, timeout_index, and timeouts.

Referenced by pop_timeout_env().

+ Here is the caller graph for this function:

◆ pop_timeout_env()

void pop_timeout_env ( const char *  env,
const char *  funcname,
const char *  filename,
const int  lineno 
)
Parameters
envnv
funcnameuncname
filenameilename
linenoineno

Definition at line 619 of file errors.c.

624 {
625  int delay = env2int(env);
626  if (delay > 0)
627  pop_timeout(funcname, filename, lineno);
628 }
void pop_timeout(__attribute__((unused)) const char *funcname, __attribute__((unused)) const char *filename, __attribute__((unused)) const int lineno)
Definition: errors.c:591
static int env2int(const char *env)
Definition: errors.c:563

References env, env2int(), and pop_timeout().

+ Here is the call graph for this function:

◆ push_timeout()

void push_timeout ( const unsigned int  delay,
const char *  funcname,
const char *  filename,
const int  lineno 
)
Parameters
delayelay
funcnameuncname
filenameilename
linenoineno

Definition at line 539 of file errors.c.

544 {
545  time_t now = time(NULL);
546 
548 
550  timeouts[timeout_index].delay = delay;
551  timeouts[timeout_index].env = NULL;
553  timeouts[timeout_index].funcname = funcname;
554  timeouts[timeout_index].filename = filename;
555  timeouts[timeout_index].lineno = lineno;
556 
557  timeout_index++;
558 
559  signal(SIGALRM, timeout_handler);
560  alarm(delay);
561 }
#define TIMEOUT_MAX_STACK_SIZE
Definition: errors.c:507
const char * env
Definition: errors.c:498
time_t start
Definition: errors.c:495

References assert, timeout_t::delay, timeout_t::end, timeout_t::env, timeout_t::filename, timeout_t::funcname, timeout_t::lineno, timeout_t::start, timeout_index, TIMEOUT_MAX_STACK_SIZE, and timeouts.

Referenced by push_timeout_env().

+ Here is the caller graph for this function:

◆ push_timeout_env()

bool push_timeout_env ( const char *  env,
const char *  funcname,
const char *  filename,
const int  lineno 
)
Parameters
envnv
funcnameuncname
filenameilename
linenoineno

Definition at line 575 of file errors.c.

580 {
581  int delay = env2int(env);
582  if (delay > 0)
583  {
584  push_timeout(delay, funcname, filename, lineno);
586  return true;
587  }
588  return false;
589 }
void push_timeout(const unsigned int delay, const char *funcname, const char *filename, const int lineno)
Definition: errors.c:539

References timeout_t::env, env, env2int(), push_timeout(), timeout_index, and timeouts.

+ Here is the call graph for this function:

◆ set_timeout_callback()

void set_timeout_callback ( timeout_callback_f  callback)
Parameters
callbackallback

Definition at line 485 of file errors.c.

486 {
488 }
static pipsmake_callback_handler_type callback
Definition: callback.c:39

References callback, and timeout_callback.

Referenced by set_pips_timeout(), and set_pips_timeout_from_env().

+ Here is the caller graph for this function:

Variable Documentation

◆ exception_index

int exception_index = 0
static

Definition at line 150 of file errors.c.

Referenced by __attribute__().

◆ exception_stack

Definition at line 149 of file errors.c.

Referenced by __attribute__().

◆ linear_assertion_result

int linear_assertion_result = -1

errors.c

Definition at line 43 of file errors.c.

◆ linear_exception_debug_mode

bool linear_exception_debug_mode = false
static

whether to run in debug mode (that is to trace catch/uncatch/throw)

Definition at line 121 of file errors.c.

Referenced by __attribute__().

◆ linear_exception_verbose

unsigned int linear_exception_verbose = 1 | 2 | 16
static

Definition at line 122 of file errors.c.

Referenced by __attribute__().

◆ misc_error_count

unsigned int misc_error_count = 0
static

◆ overflow_error_count

unsigned int overflow_error_count = 0
static

◆ pop_callback

exception_callback_t pop_callback = NULL
static

Definition at line 155 of file errors.c.

Referenced by __attribute__().

◆ push_callback

exception_callback_t push_callback = NULL
static

callbacks...

Definition at line 154 of file errors.c.

◆ simplex_error_count

unsigned int simplex_error_count = 0
static

◆ timeout_callback

timeout_callback_f timeout_callback = NULL
static

a stack of timeouts is kept.

this may make sense to deal with local timeouts within linear but still keeping a global outside timeout for pips. on timeouts, the corresponding exception is raised... what to do about it is unclear... probably the idea is to stop more or less cleanly.

Definition at line 483 of file errors.c.

Referenced by __attribute__(), and set_timeout_callback().

◆ timeout_index

static int timeout_index = 0
static

Definition at line 40 of file errors.c.

Referenced by __attribute__(), pop_timeout(), push_timeout(), and push_timeout_env().

◆ timeouts

Definition at line 509 of file errors.c.

Referenced by __attribute__(), pop_timeout(), push_timeout(), and push_timeout_env().

◆ warned_if_inconsistent_gmp

bool warned_if_inconsistent_gmp = false
static

Definition at line 431 of file errors.c.

Referenced by linear_use_gmp().