Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a leak
[simgrid.git] / src / xbt / ex.c
1 /*
2 **  OSSP ex - Exception Handling (modified to fit into SimGrid)
3 **  Copyright (c) 2005 Martin Quinson
4 **  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>
5 **  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>
6 **  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>
7 **
8 **  This file is part of OSSP ex, an exception handling library
9 **  which can be found at http://www.ossp.org/pkg/lib/ex/.
10 **
11 **  Permission to use, copy, modify, and distribute this software for
12 **  any purpose with or without fee is hereby granted, provided that
13 **  the above copyright notice and this permission notice appear in all
14 **  copies.
15 **
16 **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
17 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
20 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 **  SUCH DAMAGE.
28 **
29 **  ex.c: exception handling (compiler part)
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <execinfo.h>
35
36 #include "xbt/ex.h"
37
38 /* default __ex_ctx callback function */
39 ex_ctx_t *__xbt_ex_ctx_default(void) {
40     static ex_ctx_t ctx = XBT_CTX_INITIALIZER;
41
42     return &ctx;
43 }
44
45 /* default __ex_terminate callback function */
46 void __xbt_ex_terminate_default(xbt_ex_t *e)  {
47   char **strings;
48   size_t i;
49
50   fprintf(stderr,
51           "** SimGrid: UNCAUGHT EXCEPTION: category: %s; value: %d\n"
52           "** %s\n"
53           "** Thrown by %s%s%s at %s:%d:%s\n",
54           xbt_ex_catname(e->category), e->value, e->msg,
55           e->procname, (e->host?"@":""),(e->host?e->host:"localhost"),
56           e->file,e->line,e->func);
57
58   fprintf(stderr,"** Backtrace:\n");
59   strings = backtrace_symbols (e->bt, e->used);
60
61   for (i = 0; i < e->used; i++)
62      printf ("   %s\n", strings[i]);
63
64   free (strings);
65   abort();
66 }
67
68 /* the externally visible API */
69 ex_ctx_cb_t  __xbt_ex_ctx       = &__xbt_ex_ctx_default;
70 ex_term_cb_t __xbt_ex_terminate = &__xbt_ex_terminate_default;
71
72 void xbt_ex_free(xbt_ex_t e) {
73   free(e.msg);
74   free(e.procname);
75 }
76
77 /** \brief returns a short name for the given exception category */
78 const char * xbt_ex_catname(xbt_errcat_t cat) {
79   switch (cat) {
80   case unknown_error:   return  "unknown_err";
81   case arg_error:       return "invalid_arg";
82   case mismatch_error:  return "mismatch";
83   case not_found_error: return "not found";
84   case system_error:    return "system_err";
85   case network_error:   return "network_err";
86   case timeout_error:   return "timeout";
87   case thread_error:    return "thread_err";
88   default:              return "INVALID_ERR";
89   }
90 }