Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Display the name of the canceled RPC
[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 #endif
31
32 void gras_init(int *argc,char **argv) {
33
34   VERB0("Initialize GRAS");
35   
36   /* First initialize the XBT */
37   xbt_init(argc,argv);
38    
39   /* module registrations: 
40    *    - declare process specific data we need (without creating them) 
41    */
42   if (gras_running_process == 0) {
43      gras_trp_register();
44      gras_msg_register();
45   }
46    
47   /*
48    * Initialize the process specific stuff
49    */
50   gras_process_init(); /* calls procdata_init, which creates process specific data for each module */
51   
52   /*
53    * Initialize the global stuff if it's not the first process created
54    */
55   if (gras_running_process++ == 0) {
56     gras_emul_init();
57     gras_msg_init();
58     gras_trp_init();
59     gras_datadesc_init();
60 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
61     signal(SIGUSR1,gras_sigusr_handler);
62 #endif     
63   }
64 }
65
66 void gras_exit(void) {
67   INFO0("Exiting GRAS");
68   if (--gras_running_process == 0) {
69     gras_msg_exit();
70     gras_trp_exit();
71     gras_datadesc_exit();
72     gras_emul_exit();
73   }
74   gras_process_exit();
75   xbt_exit();
76 }
77
78 const char *hexa_str(unsigned char *data, int size) {
79   static char*buff=NULL;
80   static int buffsize=0;
81   int i,pos=0;  
82   
83   if (buffsize<5*(size+1)) {
84     if (buff)
85       free(buff);
86     buffsize=5*(size+1);
87     buff=xbt_malloc(buffsize);
88   }
89   for (i=0;i<size;i++)  {
90     if (data[i]<32 || data[i]>126)
91       sprintf(buff+pos,".(%02x)",data[i]);
92     else
93       sprintf(buff+pos,"%c(%02x)",data[i],data[i]);
94     while (buff[++pos]);
95    }
96   buff[pos]='\0';  
97   return buff;
98 }
99 void hexa_print(const char*name, unsigned char *data, int size) {
100    printf("%s: %s\n", name,hexa_str(data,size));
101 }
102