/
base_trace.c
483 lines (356 loc) · 11.1 KB
/
base_trace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// ============================================================================
//
// = LIBRARY
// ulibase - c library
//
// = FILENAME
// base_trace.c
//
// = AUTHOR
// Stefano Casazza
//
// ============================================================================
#include <ulib/base/trace.h>
#include <ulib/base/utility.h>
#ifndef __MINGW32__
# include <sys/uio.h>
# include <sys/mman.h>
# ifdef HAVE_PTHREAD_H
# include <pthread.h>
# include <sys/syscall.h>
# endif
#endif
#include <stdlib.h>
#include <stddef.h>
void* u_plock;
int u_trace_fd;
char u_trace_tab[256]; /* 256 max indent */
uint32_t u_trace_num_tab;
static int level_active;
static uint32_t file_size;
static void printInfo(void)
{
U_INTERNAL_TRACE("printInfo()")
/* segnala caratteristiche esecuzione modalita' trace */
if (u_trace_fd > 0)
{
U_MESSAGE("TRACE<%Won%W>: Level<%W%d%W> MaxSize<%W%d%W>%W", GREEN, YELLOW, CYAN, level_active,
YELLOW, CYAN, file_size, YELLOW, RESET);
}
else
{
U_MESSAGE("TRACE<%Woff%W>%W", RED, YELLOW, RESET);
}
}
static char* restrict file_mem;
static char* restrict file_ptr;
static char* restrict file_limit;
void u_trace_check_if_interrupt(void) /* check for context manage signal event - interrupt */
{
U_INTERNAL_TRACE("u_trace_check_if_interrupt()")
if (file_size &&
file_ptr != file_mem &&
*(file_ptr-1) != '\n')
{
*file_ptr++ = '\n';
}
}
void u_trace_writev(const struct iovec* restrict iov, int n)
{
U_INTERNAL_TRACE("u_trace_writev(%p,%d)", iov, n)
U_INTERNAL_ASSERT_MINOR(u_trace_num_tab,sizeof(u_trace_tab))
#ifdef HAVE_PTHREAD_H
if (u_plock)
{
char tid_buffer[32];
static pid_t old_tid;
pid_t tid = syscall(SYS_gettid);
(void) pthread_mutex_lock((pthread_mutex_t*)u_plock);
if (old_tid != tid)
{
int sz = snprintf(tid_buffer, sizeof(tid_buffer), "[tid %d]<--\n[tid %d]-->\n", old_tid, tid);
old_tid = tid;
if (file_size == 0) (void) write(u_trace_fd, tid_buffer, sz);
else
{
if ((file_ptr + sz) > file_limit) file_ptr = file_mem;
(void) u_memcpy(file_ptr, tid_buffer, sz);
file_ptr += sz;
}
}
}
#endif
if (file_size == 0) (void) writev(u_trace_fd, iov, n);
else
{
int i = 0;
for (; i < n; ++i)
{
/* U_INTERNAL_PRINT("iov[%d].iov_len=%d ov[%d].iov_base=%p", i, iov[i].iov_len, i, iov[i].iov_base) */
U_INTERNAL_ASSERT_MAJOR(iov[i].iov_len,0)
U_INTERNAL_ASSERT_POINTER(iov[i].iov_base)
if ((file_ptr + iov[i].iov_len) > file_limit) file_ptr = file_mem;
(void) u_memcpy(file_ptr, iov[i].iov_base, iov[i].iov_len);
file_ptr += iov[i].iov_len;
}
}
#ifdef HAVE_PTHREAD_H
if (u_plock) (void) pthread_mutex_unlock((pthread_mutex_t*)u_plock);
#endif
}
void u_trace_write(const char* restrict t, uint32_t tlen)
{
struct iovec iov[3] = { { (caddr_t)u_trace_tab, u_trace_num_tab },
{ (caddr_t)t, tlen },
{ (caddr_t)"\n", 1 } };
U_INTERNAL_TRACE("u_trace_write(%s,%u)", t, tlen)
u_trace_writev(iov, 3);
}
void u_trace_close(void)
{
/* NB: disattivazione immediata trace nel caso di ricezione signal nelle call system chiamate in questo metodo... */
int lfd = u_trace_fd;
u_trace_fd = 0;
U_INTERNAL_TRACE("u_trace_close()")
if (lfd > STDERR_FILENO)
{
if (file_size)
{
ptrdiff_t write_size = file_ptr - file_mem;
U_INTERNAL_ASSERT_MINOR(write_size,(ptrdiff_t)file_size)
(void) msync(file_mem, write_size, MS_SYNC);
(void) munmap(file_mem, file_size);
(void) ftruncate(lfd, write_size);
(void) fsync(lfd);
file_size = 0;
}
(void) close(lfd);
}
}
/* E' possibile mascheratura del trace per metodi eccessivamente complessi
(Es: ricorsivi) tramite il byte alto del parametro 'level' */
static void* restrict flag_mask_level;
/* attivazione-disattivazione trace */
static bool flag_signal; /* tramite signal SIGUSR2 */
static struct sigaction act;
static RETSIGTYPE handlerSIGUSR2(int signo)
{
U_INTERNAL_TRACE("handlerSIGUSR2(%d)", signo)
flag_signal = true;
}
static void setHandlerSIGUSR2(void)
{
U_INTERNAL_TRACE("setHandlerSIGUSR2()")
act.sa_handler = handlerSIGUSR2;
(void) sigaction(SIGUSR2, &act, 0);
}
static bool flag_init;
void u_trace_init(bool force, bool info, bool offset)
{
char* env = getenv("UTRACE");
U_INTERNAL_TRACE("u_trace_init(%d,%d,%d)", force, info, offset)
flag_init = true;
if ( env &&
*env)
{
char suffix;
if (*env == '"' || *env == '\'') ++env; // normalizzazione...
if (*env == '-')
{
++env;
u_trace_fd = STDERR_FILENO;
}
/* format: <level> <max_size_log> <u_flag_test>
-1 500k 0
*/
(void) sscanf(env, "%d%d%c%d", &level_active, &file_size, &suffix, &u_flag_test);
if (file_size) U_NUMBER_SUFFIX(file_size, suffix);
}
else
{
level_active = (force ? 0 : -1);
}
(void) memset(u_trace_tab, '\t', sizeof(u_trace_tab));
if (level_active >= 0)
{
flag_mask_level = 0; /* NB: necessary check for incoerent state... */
if (u_trace_fd == STDERR_FILENO) file_size = 0;
else
{
char name[128];
(void) u_sn_printf(name, 128, "trace.%N.%P", 0);
/* NB: O_RDWR e' necessario per mmap(MAP_SHARED)... */
u_trace_fd = open(name, O_CREAT | O_RDWR | O_BINARY | (offset ? O_APPEND : 0), 0666);
if (u_trace_fd == -1) U_WARNING("error on create file %S", name);
else
{
/* gestione dimensione massima... */
if (file_size)
{
off_t start = (offset ? lseek(u_trace_fd, 0, SEEK_END) : 0);
if (ftruncate(u_trace_fd, file_size))
{
U_WARNING("out of space on file system, (required %u bytes)", file_size);
file_size = 0;
}
else
{
/* NB: PROT_READ evita strani SIGSEGV... */
file_mem = (char* restrict) mmap(0, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, u_trace_fd, 0);
if (file_mem == MAP_FAILED)
{
file_mem = 0;
file_size = 0;
(void) ftruncate(u_trace_fd, 0);
}
file_ptr = file_mem + start;
file_limit = file_mem + file_size;
}
}
}
}
if (u_trace_fd > STDERR_FILENO)
{
/* register function of close trace at exit... */
u_atexit(&u_trace_close);
}
}
if (info) printInfo();
/* si abilita attivazione-disattivazione tramite signal SIGUSR2 */
setHandlerSIGUSR2();
}
static void handlerSignal(void)
{
U_INTERNAL_TRACE("handlerSignal()")
if (u_trace_fd <= 0 ||
level_active < 0)
{
(void) putenv((char*)"UTRACE=0");
u_trace_init(true, true, false);
}
else
{
(void) putenv((char*)"UTRACE");
u_trace_close();
level_active = -1;
printInfo();
}
flag_signal = false;
#ifdef __MINGW32__
setHandlerSIGUSR2();
#endif
}
bool __pure u_trace_isActive(int level)
{
bool result = (u_trace_fd > 0 &&
flag_mask_level == 0 &&
(level & 0x000000ff) >= level_active);
U_INTERNAL_TRACE("u_trace_isActive(%d)", level)
U_INTERNAL_PRINT("fd=%d level_active=%d flag_mask_level=%p result=%d", u_trace_fd, level_active, flag_mask_level, result)
return result;
}
bool u_trace_check_if_active(int level, void* restrict hook)
{
bool active;
U_INTERNAL_TRACE("u_trace_check_if_active(%d,%p)", level, hook)
if (flag_init)
{
if (flag_signal) handlerSignal();
}
else
{
u_trace_init(false, false, false);
}
active = (level == -1 ? (u_flag_test > 0 && level_active == 0) : u_trace_isActive(level));
if (active)
{
if (u_flag_test == 0 &&
(level & 0x0000ff00))
{
flag_mask_level = hook;
}
return true;
}
return false;
}
void u_trace_check_init(void)
{
U_INTERNAL_TRACE("u_trace_check_init()")
// controllo se sono avvenute precedenti creazioni di oggetti globali
// che possono avere forzato l'inizializzazione del file di trace...
if (flag_init &&
level_active >= 0 &&
u_trace_fd != STDERR_FILENO)
{
char name[128];
(void) u_sn_printf(name, 128, "trace.%N.%P", 0);
(void) rename("trace..", name);
}
else
{
u_trace_init(false, false, false);
}
printInfo();
}
void u_trace_dtor(int active, void* restrict hook)
{
U_INTERNAL_TRACE("u_trace_dtor(%d,%p)", active, hook)
U_INTERNAL_ASSERT_RANGE(0,active,1)
if (active && flag_mask_level == hook) flag_mask_level = 0;
if (flag_signal) handlerSignal();
}
void u_trace_suspend(int resume)
{
U_INTERNAL_TRACE("u_trace_suspend(%d)", resume)
U_INTERNAL_ASSERT_RANGE(0,resume,1)
if (u_flag_exit == 0)
{
static int cnt_suspend; /* disabilita eventuale ricorsione... */
static void* restrict flag_mask_level_save;
U_INTERNAL_PRINT("cnt_suspend=%d flag_mask_level=%p flag_mask_level_save=%p", cnt_suspend, flag_mask_level, flag_mask_level_save)
int cnt = cnt_suspend + (resume ? -1 : 1);
if (cnt == 0)
{
flag_mask_level = (flag_mask_level_save != (void*)0x0001 ? flag_mask_level_save : 0);
flag_mask_level_save = 0;
}
else if (cnt == 1)
{
flag_mask_level_save = (flag_mask_level != (void*)0x0001 ? flag_mask_level : 0);
flag_mask_level = (void*)0x0001;
}
cnt_suspend = cnt;
U_INTERNAL_PRINT("cnt_suspend=%d flag_mask_level=%p flag_mask_level_save=%p", cnt_suspend, flag_mask_level, flag_mask_level_save)
}
}
void u_trace_dump(const char* restrict format, ...)
{
char buffer[4096];
uint32_t buffer_len;
// U_INTERNAL_TRACE("u_trace_dump(%s)", format)
va_list argp;
va_start(argp, format);
buffer_len = u_vsnprintf(buffer, sizeof(buffer), format, argp);
va_end(argp);
{
struct iovec iov[3] = { { (caddr_t)u_trace_tab, u_trace_num_tab },
{ (caddr_t)buffer, buffer_len },
{ (caddr_t)"\n", 1 } };
u_trace_writev(iov, 3);
}
}
void u_trace_initFork(void)
{
U_INTERNAL_TRACE("u_trace_initFork()")
if (u_trace_fd > STDERR_FILENO)
{
if (file_size)
{
(void) munmap(file_mem, file_size);
file_size = 0;
}
(void) close(u_trace_fd);
u_trace_fd = 0;
}
u_trace_init(false, true, false);
}