Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
If the timeout is negative, the user wants to block
[simgrid.git] / src / gras / gras.c
index 740c7a7..7ca7b9e 100644 (file)
@@ -9,25 +9,46 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "xbt/log.h"
+#include "xbt/virtu.h" /* set the XBT virtualization to use GRAS */
 #include "xbt/module.h" /* xbt_init/exit */
+#include "xbt/xbt_os_time.h" /* xbt_os_time */
 
+#include "Virtu/virtu_interface.h" /* Module mechanism FIXME: deplace&rename */
 #include "gras_modinter.h"   /* module init/exit */
+#include "amok/amok_modinter.h"   /* module init/exit */
 #include "xbt_modinter.h"   /* module init/exit */
 
 #include "gras.h"
 #include "gras/process.h" /* FIXME: killme and put process_init in modinter */
-
-/* FIXME: move it to some random header */
-void hexa_print(const char*name, unsigned char *data, int size);
-
+  
+#include "portable.h" /* hexa_*(); signalling stuff */
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories (cf. section \ref GRAS_API)");
 static int gras_running_process = 0;
+#if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
+static void gras_sigusr_handler(int sig) {
+   INFO0("SIGUSR1 received. Display the backtrace");
+   xbt_backtrace_display_current();
+}
+
+static void gras_sigint_handler(int sig) {
+   static double lastone = 0;
+   if (lastone == 0 || xbt_os_time() - lastone > 5) {
+      xbt_backtrace_display_current();
+      fprintf(stderr,
+             "\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
+      lastone = xbt_os_time();
+   } else {
+      exit(1);
+   }
+}
+#endif
 
 void gras_init(int *argc,char **argv) {
 
   VERB0("Initialize GRAS");
-  
+
+  xbt_getpid = &gras_os_getpid;
   /* First initialize the XBT */
   xbt_init(argc,argv);
    
@@ -52,30 +73,74 @@ void gras_init(int *argc,char **argv) {
     gras_msg_init();
     gras_trp_init();
     gras_datadesc_init();
+#if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
+# ifdef SIGUSR1
+    signal(SIGUSR1,gras_sigusr_handler);
+# endif
+    signal(SIGINT,gras_sigint_handler);
+#endif     
   }
+   
+  /* and then init amok */
+  amok_init();
 }
 
 void gras_exit(void) {
   INFO0("Exiting GRAS");
+  amok_exit();
+  gras_moddata_leave();
+  gras_process_exit();
   if (--gras_running_process == 0) {
     gras_msg_exit();
     gras_trp_exit();
     gras_datadesc_exit();
     gras_emul_exit();
+    gras_moddata_exit();
   }
-  gras_process_exit();
   xbt_exit();
 }
 
-void hexa_print(const char*name, unsigned char *data, int size) {
-   int i;
-   printf("%s: ", name);
-   for (i=0;i<size;i++)  {
-      if (data[i]<32) /* || data[i]>'9') */
-       printf("'\\%d'",data[i]);
-      else
-       printf("%c",data[i]);
+const char *hexa_str(unsigned char *data, int size, int downside) {
+  static char*buff=NULL;
+  static int buffsize=0;
+  int i,pos=0;
+  int begin,increment; 
+  
+  if (buffsize<5*(size+1)) {
+    if (buff)
+      free(buff);
+    buffsize=5*(size+1);
+    buff=xbt_malloc(buffsize);
+  }
+   
+
+  if (downside) {
+     begin=size-1;
+     increment=-1;
+  } else {
+     begin=0;
+     increment=1;
+  }
+   
+  for (i=begin; 0<=i && i<size ; i+=increment)  {
+    if (data[i]<32 || data[i]>126)
+      sprintf(buff+pos,".");
+    else
+      sprintf(buff+pos,"%c",data[i]);
+    while (buff[++pos]);
+   }
+  sprintf(buff+pos,"(");
+  while (buff[++pos]);
+  for (i=begin; 0<=i && i<size ; i+=increment)  {
+    sprintf(buff+pos,"%02x",data[i]);
+    while (buff[++pos]);
    }
-   printf("\n");
+  sprintf(buff+pos,")");
+  while (buff[++pos]);
+  buff[pos]='\0';  
+  return buff;
+}
+void hexa_print(const char*name, unsigned char *data, int size) {
+   printf("%s: %s\n", name,hexa_str(data,size,0));
 }