3 /* gras.c -- generic functions not fitting anywhere else */
5 /* Copyright (c) 2003, 2004 Martin Quinson. */
6 /* All rights reserved. */
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. */
12 #include "xbt/module.h" /* xbt_init/exit */
14 #include "gras_modinter.h" /* module init/exit */
15 #include "amok/amok_modinter.h" /* module init/exit */
16 #include "xbt_modinter.h" /* module init/exit */
19 #include "gras/process.h" /* FIXME: killme and put process_init in modinter */
21 #include "portable.h" /* hexa_*(); signalling stuff */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories (cf. section \ref GRAS_API)");
24 static int gras_running_process = 0;
26 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
27 static void gras_sigusr_handler(int sig) {
28 INFO0("SIGUSR1 received. Display the backtrace");
29 xbt_backtrace_display();
32 static void gras_sigint_handler(int sig) {
33 static double lastone = 0;
34 if (lastone == 0 || gras_os_time() - lastone > 5) {
35 lastone = gras_os_time();
36 xbt_backtrace_display();
37 fprintf(stderr,"\nBacktrace displayed because Ctrl-C was pressed. Press again (within 5 sec) to abort the process.\n");
44 void gras_init(int *argc,char **argv) {
46 VERB0("Initialize GRAS");
48 /* First initialize the XBT */
51 /* module registrations:
52 * - declare process specific data we need (without creating them)
54 if (gras_running_process == 0) {
60 * Initialize the process specific stuff
62 gras_process_init(); /* calls procdata_init, which creates process specific data for each module */
65 * Initialize the global stuff if it's not the first process created
67 if (gras_running_process++ == 0) {
72 #if defined(HAVE_SIGNAL) && defined(HAVE_SIGNAL_H)
74 signal(SIGUSR1,gras_sigusr_handler);
76 signal(SIGINT,gras_sigint_handler);
80 /* and then init amok */
84 void gras_exit(void) {
85 INFO0("Exiting GRAS");
87 if (--gras_running_process == 0) {
97 const char *hexa_str(unsigned char *data, int size, int downside) {
98 static char*buff=NULL;
99 static int buffsize=0;
102 if (buffsize<5*(size+1)) {
106 buff=xbt_malloc(buffsize);
118 for (i=begin; 0<=i && i<size ; i+=increment) {
119 if (data[i]<32 || data[i]>126)
120 sprintf(buff+pos,".");
122 sprintf(buff+pos,"%c",data[i]);
125 sprintf(buff+pos,"(");
127 for (i=begin; 0<=i && i<size ; i+=increment) {
128 sprintf(buff+pos,"%02x",data[i]);
131 sprintf(buff+pos,")");
136 void hexa_print(const char*name, unsigned char *data, int size) {
137 printf("%s: %s\n", name,hexa_str(data,size,0));