Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Also display the backtrace on Ctrl-C (use Ctrl-C Ctrl-C to quit)
[simgrid.git] / src / gras / gras.c
1 /* $Id$ */
2
3 /* gras.c -- generic functions not fitting anywhere else                    */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson.                                 */
6 /* All rights reserved.                                                     */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "xbt/log.h"
12 #include "xbt/module.h" /* xbt_init/exit */
13
14 #include "gras_modinter.h"   /* module init/exit */
15 #include "xbt_modinter.h"   /* module init/exit */
16
17 #include "gras.h"
18 #include "gras/process.h" /* FIXME: killme and put process_init in modinter */
19
20 #include "portable.h" /* hexa_*(); signalling stuff */
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories (cf. section \ref GRAS_API)");
23 static int gras_running_process = 0;
24
25 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
26 static void gras_sigusr_handler(int sig) {
27    INFO0("SIGUSR1 received. Display the backtrace");
28    xbt_backtrace_display();
29 }
30
31 static void gras_sigint_handler(int sig) {
32    static double lastone = 0;
33    if (lastone == 0 || gras_os_time() - lastone > 5) {
34       lastone = gras_os_time();
35       xbt_backtrace_display();
36       fprintf(stderr,"\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
37    } else {
38       exit(1);
39    }
40 }
41 #endif
42
43 void gras_init(int *argc,char **argv) {
44
45   VERB0("Initialize GRAS");
46   
47   /* First initialize the XBT */
48   xbt_init(argc,argv);
49    
50   /* module registrations: 
51    *    - declare process specific data we need (without creating them) 
52    */
53   if (gras_running_process == 0) {
54      gras_trp_register();
55      gras_msg_register();
56   }
57    
58   /*
59    * Initialize the process specific stuff
60    */
61   gras_process_init(); /* calls procdata_init, which creates process specific data for each module */
62   
63   /*
64    * Initialize the global stuff if it's not the first process created
65    */
66   if (gras_running_process++ == 0) {
67     gras_emul_init();
68     gras_msg_init();
69     gras_trp_init();
70     gras_datadesc_init();
71 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
72     signal(SIGUSR1,gras_sigusr_handler);
73     signal(SIGINT,gras_sigint_handler);
74 #endif     
75   }
76 }
77
78 void gras_exit(void) {
79   INFO0("Exiting GRAS");
80   if (--gras_running_process == 0) {
81     gras_msg_exit();
82     gras_trp_exit();
83     gras_datadesc_exit();
84     gras_emul_exit();
85   }
86   gras_process_exit();
87   xbt_exit();
88 }
89
90 const char *hexa_str(unsigned char *data, int size) {
91   static char*buff=NULL;
92   static int buffsize=0;
93   int i,pos=0;  
94   
95   if (buffsize<5*(size+1)) {
96     if (buff)
97       free(buff);
98     buffsize=5*(size+1);
99     buff=xbt_malloc(buffsize);
100   }
101   for (i=0;i<size;i++)  {
102     if (data[i]<32 || data[i]>126)
103       sprintf(buff+pos,".(%02x)",data[i]);
104     else
105       sprintf(buff+pos,"%c(%02x)",data[i],data[i]);
106     while (buff[++pos]);
107    }
108   buff[pos]='\0';  
109   return buff;
110 }
111 void hexa_print(const char*name, unsigned char *data, int size) {
112    printf("%s: %s\n", name,hexa_str(data,size));
113 }
114