PIPS
debug.c
Go to the documentation of this file.
1 /*
2 
3  $Id: debug.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 /* Debugging functions
28  *
29  * Modifications:
30  * - set_debug_level function has been changed.
31  * Mainly the condition test, from (l >= 0 || l <= 9) to (l >= 0 && l <= 9)
32  * and else clause has been added, that is pips_error function.
33  * (Mar. 21,91 L.Zhou )
34  * - debug_off(): idls-- was replaced by (--idls)-1
35  * (April 7, 1991, Francois Irigoin)
36  */
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <string.h>
42 
43 #include "genC.h"
44 #include "misc.h"
45 
46 /* I switched this variable from static to non-static in order to
47  * avoid a function call in the pips_debug macro. This does not mean
48  * that this variable is intended to be touch by anyone directly.
49  * the interface remains the set/get functions. Just to reduce the
50  * debuging impact on the performance (from low to very low:-)
51  * FC, May 95.
52  */
54 
55 /*
56 SET_DEBUG_LEVEL is a function that sets the current debuging level to
57 the value passed as argument. Valid debuging values are from 0 to 9.
58 0 means no debug, 9 means extensive debug. Other values are illegal.
59 */
60 void set_debug_level(const int l)
61 {
62  message_assert("debug level not in 0-9", l>=0 && l<=9);
64 }
65 
66 /* GET_DEBUG_LEVEL returns the current debugging level. */
67 int get_debug_level(void)
68 {
70 }
71 
72 #define STACK_LENGTH 50
73 
74 typedef struct
75 {
76  char * name;
77  char * function;
78  char * file;
79  int line;
80  int level;
81 } debug_level ;
82 
83 /* idls points to the first free bucket in debug_stack
84  */
86 static _int idls = 0;
87 
88 /* The pair get_ and set_debug_stack_pointer() should never be used
89  except to clean up the stack after a long jump
90 */
91 
93 {
94  return idls;
95 }
96 
97 void set_debug_stack_pointer(const int i)
98 {
99  if(i >= 0 && i <= idls) {
100  if (i!=idls) {
101  pips_user_warning("debug level stack is set to %d\n", i);
102  idls = i;
104  }
105  }
106  else
107  pips_internal_error("value %d out of stack range [0..%d]. "
108  "Too many calls to debug_off()\n", i, idls);
109 }
110 
111 void
113  const char * env,
114  const char * function,
115  const char * file,
116  const int line)
117 {
118  pips_assert("stack not full", idls < STACK_LENGTH-1);
119 
120  char * level_env;
121  int dl = ((level_env = getenv(env)) != NULL) ? atoi(level_env) : 0;
122 
123  debug_stack[idls].name = (char*) env;
124  debug_stack[idls].function = (char*) function;
125  debug_stack[idls].file = (char*) file;
127  debug_stack[idls].level = dl;
128 
129  idls++;
130 
131  set_debug_level(dl);
132 }
133 
134 void
136  const char * function,
137  const char * file,
138  const int line)
139 {
141 
142  pips_assert("debug stack not empty", idls > 0);
143 
144  idls--;
146 
147  if (!same_string_p(current->file, file) ||
148  !same_string_p(current->function, function))
149  {
150  pips_internal_error("\ndebug %s (level is %d)"
151  "[%s] (%s:%d) debug on and\n"
152  "[%s] (%s:%d) debug off don't match\n",
153  current->name, current->level,
154  current->function, current->file, current->line,
155  function, file, line);
156  }
157 
159 }
160 
161 /* function used to debug (can be called from dbx)
162  * BB 6.12.91
163  */
165 {
166  int i;
167 
168  (void) fprintf(stderr, "Debug stack (last debug_on first): ");
169 
170  for(i=idls-1;i>=0;i--)
171  (void) fprintf(stderr, "%s=%d [%s] (%s:%d)\n",
172  debug_stack[i].name,
173  debug_stack[i].level,
174  debug_stack[i].function,
175  debug_stack[i].file,
176  debug_stack[i].line);
177 }
178 
179 /*
180 DEBUG prints debuging messages. DEBUG should be called as:
181  debug(debug_level, function_name, format, expression_list)
182 DEBUG prints a message if the value of DEBUG_LEVEL is greater or equal
183 to the current debuging level (see SET_DEBUG_LEVEL). function_name is a
184 string containing the name of the function calling DEBUG, and format and
185 expression_list are passed as arguments to vprintf.
186 */
187 /*VARARGS0*/
188 void
189 debug(const int the_expected_debug_level,
190  const char * calling_function_name,
191  const char * a_message_format,
192  ...)
193 {
194  va_list some_arguments;
195 #define MAX_MARGIN (8)
196  static char * margin = " ";
197  /* margin_length is the length of the part of the margin that is not used */
198  int margin_length;
199 
200  /* If the current debug level is not high enough, do nothing: */
201  if (the_expected_debug_level > the_current_debug_level)
202  return;
203 
204  /* print name of function printing debug message */
205  margin_length = MAX_MARGIN+1-the_expected_debug_level;
206  (void) fprintf(stderr, "%s[%s] ",
207  margin + (margin_length>0 ? margin_length : 0),
208  calling_function_name);
209 
210  va_start(some_arguments, a_message_format);
211 
212  /* print out remainder of message */
213  (void) vfprintf(stderr, a_message_format, some_arguments);
214 
215  va_end(some_arguments);
216 }
217 
218 /* pips_debug is a nice macro that depends on gcc to generate the
219  * function name and to handle a variable number of arguments.
220  * if these feature are not available, it will be this function.
221  * the function name won't be available. FC May 95.
222  */
223 void
224 pips_debug_function(const int the_expected_debug_level,
225  const char * a_message_format,
226  ...)
227 {
228  va_list some_arguments;
229 
230  /* If the current debug level is not high enough, do nothing: */
231  if (the_expected_debug_level > the_current_debug_level)
232  return;
233 
234  (void) fprintf(stderr, "[unknown] ");
235 
236  va_start(some_arguments, a_message_format);
237 
238  /* print out remainder of message */
239  (void) vfprintf(stderr, a_message_format, some_arguments);
240 
241  va_end(some_arguments);
242 }
243 
245 {
246  /* This is about the always increasing swap space */
247  /* etext is not portable. it is not even documented on SUN:-) */
248  /* extern etext;
249  double memory_size = (sbrk(0) - etext)/(double)(1 << 20);
250  */
251  return 0.0;
252 }
253 
255 {
256  /* mallinfo is not portable */
257  /* This is *used* part of the heap, but it may be bigger */
258  /* struct mallinfo heap_info = mallinfo(); */
259  /* double memory_size = (heap_info.uordbytes)/(double)(1 << 20); */
260  double memory_size = -1.0 /*(sbrk(0))/(double)(1 << 20)*/;
261  return memory_size;
262 }
263 
264 /* is that all? :-)
265  */
static jmp_buf env
Definition: genClib.c:2473
#define pips_user_warning
Definition: misc-local.h:146
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define pips_internal_error
Definition: misc-local.h:149
double get_process_memory_size(void)
Definition: debug.c:244
void print_debug_stack(void)
function used to debug (can be called from dbx) BB 6.12.91
Definition: debug.c:164
void debug_off_function(const char *function, const char *file, const int line)
Definition: debug.c:135
#define STACK_LENGTH
Definition: debug.c:72
int get_debug_level(void)
GET_DEBUG_LEVEL returns the current debugging level.
Definition: debug.c:67
void pips_debug_function(const int the_expected_debug_level, const char *a_message_format,...)
pips_debug is a nice macro that depends on gcc to generate the function name and to handle a variable...
Definition: debug.c:224
void set_debug_stack_pointer(const int i)
Definition: debug.c:97
static _int idls
Definition: debug.c:86
void debug(const int the_expected_debug_level, const char *calling_function_name, const char *a_message_format,...)
ARARGS0.
Definition: debug.c:189
int the_current_debug_level
Debugging functions.
Definition: debug.c:53
void set_debug_level(const int l)
SET_DEBUG_LEVEL is a function that sets the current debuging level to the value passed as argument.
Definition: debug.c:60
void debug_on_function(const char *env, const char *function, const char *file, const int line)
Definition: debug.c:112
static debug_level debug_stack[STACK_LENGTH]
idls points to the first free bucket in debug_stack
Definition: debug.c:85
double get_process_gross_heap_size(void)
Definition: debug.c:254
_int get_debug_stack_pointer(void)
The pair get_ and set_debug_stack_pointer() should never be used except to clean up the stack after a...
Definition: debug.c:92
#define MAX_MARGIN
#define message_assert(msg, ex)
Definition: newgen_assert.h:47
#define same_string_p(s1, s2)
intptr_t _int
_INT
Definition: newgen_types.h:53
#define level
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
static int line
FLEX_SCANNER.
Definition: scanner.c:852
static size_t current
Definition: string.c:115
int level
Definition: debug.c:80
char * name
Definition: debug.c:76
char * function
Definition: debug.c:77
char * file
Definition: debug.c:78
int line
Definition: debug.c:79