Line data Source code
1 : #ifndef FD_LOG_STYLE
2 : #if FD_HAS_HOSTED
3 : #define FD_LOG_STYLE 0
4 : #else
5 : #error "Define FD_LOG_STYLE for this platform"
6 : #endif
7 : #endif
8 :
9 : #if FD_LOG_STYLE==0 /* POSIX style */
10 :
11 : #ifndef FD_HAS_BACKTRACE
12 : #if __has_include( <execinfo.h> )
13 : #define FD_HAS_BACKTRACE 1
14 : #else
15 : #define FD_HAS_BACKTRACE 0
16 : #endif
17 : #endif
18 :
19 : /* FIXME: SANITIZE VARIOUS USER SET STRINGS */
20 :
21 : #define _GNU_SOURCE
22 :
23 : #include "fd_log.h"
24 : #include "fd_backtrace.h"
25 :
26 : #include <stdio.h>
27 : #include <stdlib.h>
28 : #include <stdarg.h>
29 : #include <ctype.h>
30 : #include <errno.h>
31 : #include <fcntl.h>
32 : #include <unistd.h>
33 : #include <signal.h>
34 : #include <sched.h>
35 : #include <time.h>
36 : #if defined(__linux__)
37 : #include <syscall.h>
38 : #endif
39 : #include <sys/mman.h>
40 :
41 : #if FD_HAS_BACKTRACE
42 : #include <execinfo.h>
43 : #endif
44 :
45 : #if defined(__FreeBSD__)
46 : #include <sys/stat.h> /* S_IRUSR */
47 : #endif /* defined(__FreeBSD__) */
48 :
49 : #include "../tile/fd_tile_private.h"
50 :
51 : #ifdef __has_include
52 : #if __has_include("../../app/fdctl/version.h")
53 : #include "../../app/fdctl/version.h"
54 : #endif
55 : #endif
56 :
57 : #ifndef FDCTL_MAJOR_VERSION
58 : #define FDCTL_MAJOR_VERSION 0
59 : #endif
60 : #ifndef FDCTL_MINOR_VERSION
61 : #define FDCTL_MINOR_VERSION 0
62 : #endif
63 : #ifndef FDCTL_PATCH_VERSION
64 : #define FDCTL_PATCH_VERSION 0
65 : #endif
66 :
67 : #ifdef FD_BUILD_INFO
68 : FD_IMPORT_CSTR( fd_log_build_info, FD_BUILD_INFO );
69 : #else
70 : char const fd_log_build_info[1] __attribute__((aligned(1))) = { '\0' };
71 : ulong const fd_log_build_info_sz = 1UL;
72 : #endif
73 :
74 : /* TEXT_* are quick-and-dirty color terminal hacks. Probably should
75 : do something more robust longer term. */
76 :
77 81232 : #define TEXT_NORMAL "\033[0m"
78 : #define TEXT_BOLD "\033[1m"
79 : #define TEXT_UNDERLINE "\033[4m"
80 : #define TEXT_BLINK "\033[5m"
81 :
82 10154 : #define TEXT_BLUE "\033[34m"
83 10154 : #define TEXT_GREEN "\033[32m"
84 10154 : #define TEXT_YELLOW "\033[93m"
85 40616 : #define TEXT_RED "\033[31m"
86 :
87 : /* APPLICATION LOGICAL ID APIS ****************************************/
88 :
89 : /* App id */
90 :
91 : static ulong fd_log_private_app_id; /* 0 outside boot/halt, init on boot */
92 :
93 2476 : void fd_log_private_app_id_set( ulong app_id ) { fd_log_private_app_id = app_id; }
94 :
95 18005024 : ulong fd_log_app_id( void ) { return fd_log_private_app_id; }
96 :
97 : /* App */
98 :
99 : static char fd_log_private_app[ FD_LOG_NAME_MAX ]; /* "" outside boot/halt, init on boot */
100 :
101 : void
102 2476 : fd_log_private_app_set( char const * app ) {
103 2476 : if( FD_UNLIKELY( !app ) ) app = "[app]";
104 2476 : if( FD_LIKELY( app!=fd_log_private_app ) )
105 2476 : fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( fd_log_private_app ), app, FD_LOG_NAME_MAX-1UL ) );
106 2476 : }
107 :
108 2392695 : char const * fd_log_app( void ) { return fd_log_private_app; }
109 :
110 : /* Thread ID */
111 :
112 : #if FD_HAS_THREADS
113 : static ulong fd_log_private_thread_id_ctr; /* 0 outside boot/halt, init on boot */
114 :
115 : static ulong
116 2512 : fd_log_private_thread_id_next( void ) {
117 2512 : return FD_ATOMIC_FETCH_AND_ADD( &fd_log_private_thread_id_ctr, 1UL );
118 2512 : }
119 : #endif
120 :
121 : static FD_TL ulong fd_log_private_thread_id; /* 0 at thread start */
122 : static FD_TL int fd_log_private_thread_id_init; /* 0 at thread start */
123 :
124 : void
125 2512 : fd_log_private_thread_id_set( ulong thread_id ) {
126 2512 : fd_log_private_thread_id = thread_id;
127 2512 : fd_log_private_thread_id_init = 1;
128 2512 : }
129 :
130 : ulong
131 36007563 : fd_log_thread_id( void ) {
132 36007563 : # if FD_HAS_THREADS
133 36007563 : if( FD_UNLIKELY( !fd_log_private_thread_id_init ) ) fd_log_private_thread_id_set( fd_log_private_thread_id_next() );
134 : # else
135 : FD_COMPILER_MFENCE(); /* Work around FD_FN_CONST */
136 : # endif
137 36007563 : return fd_log_private_thread_id;
138 36007563 : }
139 :
140 : /* Thread */
141 :
142 : /* Initialize name to a reasonable default thread description (FIXME:
143 : WEEN THIS OFF STDLIB) */
144 :
145 : static void
146 2521 : fd_log_private_thread_default( char * name ) { /* FD_LOG_NAME_MAX bytes */
147 2521 : sprintf( name, "%lu", fd_log_thread_id() );
148 2521 : }
149 :
150 : static FD_TL char fd_log_private_thread[ FD_LOG_NAME_MAX ]; /* "" at thread start */
151 : static FD_TL int fd_log_private_thread_init; /* 0 at thread start */
152 :
153 : void
154 2521 : fd_log_thread_set( char const * thread ) {
155 2521 : if( FD_UNLIKELY( !thread ) || FD_UNLIKELY( thread[0]=='\0') ) {
156 2521 : fd_log_private_thread_default( fd_log_private_thread );
157 2521 : fd_log_private_thread_init = 1;
158 2521 : } else if( FD_LIKELY( thread!=fd_log_private_thread ) ) {
159 0 : fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( fd_log_private_thread ), thread, FD_LOG_NAME_MAX-1UL ) );
160 0 : fd_log_private_thread_init = 1;
161 0 : }
162 2521 : }
163 :
164 : char const *
165 3553926 : fd_log_thread( void ) {
166 3553926 : if( FD_UNLIKELY( !fd_log_private_thread_init ) ) fd_log_thread_set( NULL );
167 3553926 : return fd_log_private_thread;
168 3553926 : }
169 :
170 : /* APPLICATION PHYSICAL ID APIS ***************************************/
171 :
172 : /* Host ID */
173 :
174 : static ulong fd_log_private_host_id; /* 0 outside boot/halt, initialized on boot */
175 :
176 2476 : void fd_log_private_host_id_set( ulong host_id ) { fd_log_private_host_id = host_id; }
177 :
178 4988 : ulong fd_log_host_id( void ) { return fd_log_private_host_id; }
179 :
180 : /* Host */
181 :
182 : static char fd_log_private_host[ FD_LOG_NAME_MAX ]; /* "" outside boot/halt, initialized on boot */
183 :
184 2392695 : char const * fd_log_host( void ) { return fd_log_private_host; }
185 :
186 : void
187 2476 : fd_log_private_host_set( char const * host ) {
188 2476 : if( FD_UNLIKELY( !host ) || FD_UNLIKELY( host[0]=='\0') ) host = "[host]";
189 2476 : if( FD_LIKELY( host!=fd_log_private_host ) )
190 2476 : fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( fd_log_private_host ), host, FD_LOG_NAME_MAX-1UL ) );
191 2476 : }
192 :
193 : /* CPU ID */
194 :
195 : /* First CPU scheduled to run on or ULONG_MAX on failure */
196 :
197 : ulong
198 2476 : fd_log_private_cpu_id_default( void ) {
199 2476 : FD_CPUSET_DECL( cpu_set );
200 2476 : if( FD_UNLIKELY( fd_cpuset_getaffinity( (pid_t)0, cpu_set ) ) ) return ULONG_MAX;
201 2476 : ulong idx = fd_cpuset_first( cpu_set );
202 2476 : idx = fd_ulong_if( idx<FD_TILE_MAX, idx, ULONG_MAX );
203 2476 : return idx;
204 2476 : }
205 :
206 : static FD_TL ulong fd_log_private_cpu_id; /* 0 at thread start */
207 : static FD_TL int fd_log_private_cpu_id_init; /* 0 at thread start */
208 :
209 : void
210 2485 : fd_log_private_cpu_id_set( ulong cpu_id ) {
211 2485 : fd_log_private_cpu_id = cpu_id;
212 2485 : fd_log_private_cpu_id_init = 1;
213 2485 : }
214 :
215 : ulong
216 18002545 : fd_log_cpu_id( void ) {
217 18002545 : if( FD_UNLIKELY( !fd_log_private_cpu_id_init ) ) fd_log_private_cpu_id_set( fd_log_private_cpu_id_default() );
218 18002545 : return fd_log_private_cpu_id;
219 18002545 : }
220 :
221 : /* CPU */
222 :
223 : /* Initialize name to a reasonable default CPU description (FIXME: WEEN
224 : THIS OFF STDLIB) */
225 :
226 : static void
227 2521 : fd_log_private_cpu_default( char * name ) { /* FD_LOG_NAME_MAX bytes */
228 2521 : FD_CPUSET_DECL( set );
229 :
230 2521 : int err = fd_cpuset_getaffinity( (pid_t)0, set );
231 2521 : if( FD_UNLIKELY( err ) ) { sprintf( name, "e%i", err ); return; }
232 :
233 2521 : ulong cnt = fd_cpuset_cnt( set );
234 2521 : if( FD_UNLIKELY( !((0UL<cnt) & (cnt<=FD_TILE_MAX)) ) ) { sprintf( name, "ec" ); return; }
235 :
236 2521 : ulong idx = fd_cpuset_first( set );
237 2521 : sprintf( name, (cnt>1) ? "f%lu" : "%lu", idx );
238 2521 : }
239 :
240 : static FD_TL char fd_log_private_cpu[ FD_LOG_NAME_MAX ]; /* "" at thread start */
241 : static FD_TL int fd_log_private_cpu_init; /* 0 at thread start */
242 :
243 : void
244 2521 : fd_log_cpu_set( char const * cpu ) {
245 2521 : if( FD_UNLIKELY( !cpu ) || FD_UNLIKELY( cpu[0]=='\0') ) {
246 2521 : fd_log_private_cpu_default( fd_log_private_cpu );
247 2521 : fd_log_private_cpu_init = 1;
248 2521 : } else if( FD_LIKELY( cpu!=fd_log_private_cpu ) ) {
249 0 : fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( fd_log_private_cpu ), cpu, FD_LOG_NAME_MAX-1UL ) );
250 0 : fd_log_private_cpu_init = 1;
251 0 : }
252 2521 : }
253 :
254 : char const *
255 3553926 : fd_log_cpu( void ) {
256 3553926 : if( FD_UNLIKELY( !fd_log_private_cpu_init ) ) fd_log_cpu_set( NULL );
257 3553926 : return fd_log_private_cpu;
258 3553926 : }
259 :
260 : /* THREAD GROUP ID APIS ***********************************************/
261 :
262 : /* Group id */
263 :
264 : static ulong fd_log_private_group_id; /* 0 outside boot/halt, init on boot */
265 :
266 2476 : void fd_log_private_group_id_set( ulong group_id ) { fd_log_private_group_id = group_id; }
267 :
268 313704568 : ulong fd_log_group_id( void ) { return fd_log_private_group_id; }
269 :
270 : /* Group */
271 :
272 : static char fd_log_private_group[ FD_LOG_NAME_MAX ]; /* "" outside boot/halt, init on boot */
273 :
274 2392694 : char const * fd_log_group( void ) { return fd_log_private_group; }
275 :
276 : void
277 2476 : fd_log_private_group_set( char const * group ) {
278 2476 : if( FD_UNLIKELY( !group ) || FD_UNLIKELY( group[0]=='\0') ) group = "[group]";
279 2476 : if( FD_LIKELY( group!=fd_log_private_group ) )
280 2476 : fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( fd_log_private_group ), group, FD_LOG_NAME_MAX-1UL ) );
281 2476 : }
282 :
283 : /* System TID or ULONG_MAX on failure */
284 :
285 : ulong
286 2512 : fd_log_private_tid_default( void ) {
287 2512 : # if defined(__linux__)
288 2512 : long tid = syscall( SYS_gettid );
289 : # else
290 : long tid = getpid();
291 : # endif
292 2512 : return fd_ulong_if( tid>0L, (ulong)tid, ULONG_MAX );
293 2512 : }
294 :
295 : static FD_TL ulong fd_log_private_tid; /* 0 at thread start */
296 : static FD_TL int fd_log_private_tid_init; /* 0 at thread start */
297 :
298 : void
299 2512 : fd_log_private_tid_set( ulong tid ) {
300 2512 : fd_log_private_tid = tid;
301 2512 : fd_log_private_tid_init = 1;
302 2512 : }
303 :
304 : ulong
305 3556365 : fd_log_tid( void ) {
306 3556365 : if( FD_UNLIKELY( !fd_log_private_tid_init ) ) fd_log_private_tid_set( fd_log_private_tid_default() );
307 3556365 : return fd_log_private_tid;
308 3556365 : }
309 :
310 : /* User id */
311 :
312 : static ulong
313 2476 : fd_log_private_user_id_default( void ) {
314 2476 : return (ulong)getuid(); /* POSIX spec seems ambiguous as to whether or not this is a signed type */
315 2476 : }
316 :
317 : static ulong fd_log_private_user_id; /* 0 outside boot/halt, init on boot */
318 : static int fd_log_private_user_id_init;
319 :
320 : void
321 2476 : fd_log_private_user_id_set( ulong user_id ) {
322 2476 : fd_log_private_user_id = user_id;
323 2476 : fd_log_private_user_id_init = 1;
324 2476 : }
325 :
326 : ulong
327 2512 : fd_log_user_id( void ) {
328 2512 : if( FD_UNLIKELY( !fd_log_private_user_id_init ) ) {
329 0 : fd_log_private_user_id = fd_log_private_user_id_default();
330 0 : fd_log_private_user_id_init = 1;
331 0 : }
332 2512 : return fd_log_private_user_id;
333 2512 : }
334 :
335 : /* User */
336 :
337 : static char fd_log_private_user[ FD_LOG_NAME_MAX ]; /* "" outside boot/halt, init on boot */
338 :
339 2392695 : char const * fd_log_user( void ) { return fd_log_private_user; }
340 :
341 : void
342 2476 : fd_log_private_user_set( char const * user ) {
343 2476 : if( FD_UNLIKELY( !user ) || FD_UNLIKELY( user[0]=='\0') ) user = "[user]";
344 2476 : if( FD_LIKELY( user!=fd_log_private_user ) )
345 2476 : fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( fd_log_private_user ), user, FD_LOG_NAME_MAX-1UL ) );
346 2476 : }
347 :
348 : int
349 0 : fd_log_group_id_query( ulong group_id ) {
350 0 : if( group_id==fd_log_group_id() ) return FD_LOG_GROUP_ID_QUERY_LIVE; /* Avoid O/S call for self queries */
351 0 : pid_t pid = (pid_t)group_id;
352 0 : if( FD_UNLIKELY( ((group_id!=(ulong)pid) | (pid<=(pid_t)0)) ) ) return FD_LOG_GROUP_ID_QUERY_INVAL;
353 0 : if( !kill( (pid_t)group_id, 0 ) ) return FD_LOG_GROUP_ID_QUERY_LIVE;
354 0 : if( FD_LIKELY( errno==ESRCH ) ) return FD_LOG_GROUP_ID_QUERY_DEAD;
355 0 : if( FD_LIKELY( errno==EPERM ) ) return FD_LOG_GROUP_ID_QUERY_PERM;
356 0 : return FD_LOG_GROUP_ID_QUERY_FAIL;
357 0 : }
358 :
359 : /* WALLCLOCK APIS *****************************************************/
360 :
361 : long
362 117779734 : fd_log_wallclock_host( void const * _ ) {
363 117779734 : (void)_;
364 117779734 : struct timespec ts[1];
365 117779734 : clock_gettime( CLOCK_REALTIME, ts );
366 117779734 : return ((long)1e9)*((long)ts->tv_sec) + (long)ts->tv_nsec;
367 117779734 : }
368 :
369 : static fd_clock_func_t fd_log_private_clock_func = fd_log_wallclock_host;
370 : static void const * fd_log_private_clock_args = NULL;
371 :
372 : long
373 116558771 : fd_log_wallclock( void ) {
374 116558771 : return fd_log_private_clock_func( fd_log_private_clock_args );
375 116558771 : }
376 :
377 : void
378 : fd_log_wallclock_set( fd_clock_func_t clock,
379 0 : void const * args ) {
380 0 : fd_log_private_clock_func = clock;
381 0 : fd_log_private_clock_args = args;
382 0 : }
383 :
384 : char *
385 : fd_log_wallclock_cstr( long now,
386 2397274 : char * buf ) {
387 2397274 : uint YYYY;
388 2397274 : uint MM;
389 2397274 : uint DD;
390 2397274 : uint hh;
391 2397274 : uint mm;
392 2397274 : ulong ns;
393 2397274 : int tz;
394 :
395 2397274 : static long const ns_per_m = 60000000000L;
396 2397274 : static long const ns_per_s = 1000000000L;
397 :
398 2397274 : static FD_TL long now_ref = 1262325600000000000L; /* 2010-01-01 00:00:00.000000000 GMT-06 */
399 2397274 : static FD_TL uint YYYY_ref = 2010U; /* Initialized to what now0 corresponds to */
400 2397274 : static FD_TL uint MM_ref = 1U; /* " */
401 2397274 : static FD_TL uint DD_ref = 1U; /* " */
402 2397274 : static FD_TL uint hh_ref = 0U; /* " */
403 2397274 : static FD_TL uint mm_ref = 0U; /* " */
404 2397274 : static FD_TL int tz_ref = -6; /* " */
405 :
406 2397274 : if( FD_LIKELY( (now_ref<=now) & (now<(now_ref+ns_per_m)) ) ) {
407 :
408 : /* now is near the reference timestamp so we reuse the reference
409 : calculation timestamp. */
410 :
411 2394710 : YYYY = YYYY_ref;
412 2394710 : MM = MM_ref;
413 2394710 : DD = DD_ref;
414 2394710 : hh = hh_ref;
415 2394710 : mm = mm_ref;
416 2394710 : ns = (ulong)(now - now_ref);
417 2394710 : tz = tz_ref;
418 :
419 2394710 : } else {
420 :
421 2564 : long _t = now / ns_per_s;
422 2564 : long _ns = now - ns_per_s*_t;
423 2564 : if( _ns<0L ) _ns += ns_per_s, _t--;
424 2564 : time_t t = (time_t)_t;
425 :
426 2564 : struct tm tm[1];
427 2564 : static FD_TL int localtime_broken = 0;
428 2564 : if( FD_UNLIKELY( !localtime_broken && !localtime_r( &t, tm ) ) ) localtime_broken = 1;
429 2564 : if( FD_UNLIKELY( localtime_broken ) ) { /* If localtime_r doesn't work, pretty print as a raw UNIX time */
430 : /* Note: These can all run in parallel */
431 0 : fd_cstr_append_fxp10_as_text( buf, ' ', fd_char_if( now<0L, '-', '\0' ), 9UL, fd_long_abs( now ), 29UL );
432 0 : fd_cstr_append_text ( buf+29, " s UNIX", 7UL );
433 0 : fd_cstr_append_char ( buf+36, '\0' );
434 0 : return buf;
435 0 : }
436 :
437 2564 : YYYY = (uint)(1900+tm->tm_year);
438 2564 : MM = (uint)( 1+tm->tm_mon );
439 2564 : DD = (uint)tm->tm_mday;
440 2564 : hh = (uint)tm->tm_hour;
441 2564 : mm = (uint)tm->tm_min;
442 2564 : ns = ((ulong)((uint)tm->tm_sec))*((ulong)ns_per_s) + ((ulong)_ns);
443 2564 : # if defined(__linux__)
444 2564 : tz = (int)(-timezone/3600L+(long)tm->tm_isdst);
445 : # else
446 : tz = 0;
447 : # endif
448 :
449 2564 : now_ref = now - (long)ns;
450 2564 : YYYY_ref = YYYY;
451 2564 : MM_ref = MM;
452 2564 : DD_ref = DD;
453 2564 : hh_ref = hh;
454 2564 : mm_ref = mm;
455 2564 : tz_ref = tz;
456 :
457 2564 : }
458 :
459 : /* Note: These can all run in parallel! */
460 2397274 : fd_cstr_append_uint_as_text ( buf, '0', '\0', YYYY, 4UL );
461 2397274 : fd_cstr_append_char ( buf+ 4, '-' );
462 2397274 : fd_cstr_append_uint_as_text ( buf+ 5, '0', '\0', MM, 2UL );
463 2397274 : fd_cstr_append_char ( buf+ 7, '-' );
464 2397274 : fd_cstr_append_uint_as_text ( buf+ 8, '0', '\0', DD, 2UL );
465 2397274 : fd_cstr_append_char ( buf+10, ' ' );
466 2397274 : fd_cstr_append_uint_as_text ( buf+11, '0', '\0', hh, 2UL );
467 2397274 : fd_cstr_append_char ( buf+13, ':' );
468 2397274 : fd_cstr_append_uint_as_text ( buf+14, '0', '\0', mm, 2UL );
469 2397274 : fd_cstr_append_char ( buf+16, ':' );
470 2397274 : fd_cstr_append_fxp10_as_text( buf+17, '0', '\0', 9UL, ns, 12UL );
471 2397274 : fd_cstr_append_text ( buf+29, " GMT", 4UL );
472 2397274 : fd_cstr_append_char ( buf+33, fd_char_if( tz<0, '-', '+' ) );
473 2397274 : fd_cstr_append_uint_as_text ( buf+34, '0', '\0', fd_int_abs( tz ), 2UL );
474 2397274 : fd_cstr_append_char ( buf+36, '\0' );
475 2397274 : return buf;
476 2397274 : }
477 :
478 : long
479 600 : fd_log_sleep( long dt ) {
480 600 : if( FD_UNLIKELY( dt < 1L ) ) {
481 0 : sched_yield();
482 0 : return 0L;
483 0 : }
484 :
485 : /* dt is in [1,LONG_MAX] at this point */
486 600 : long ns_dt = fd_long_min( dt, (((long)1e9)<<31)-1L ); /* in [1,2^31*1e9) and <= dt at this point */
487 600 : dt -= ns_dt;
488 :
489 600 : struct timespec req[1];
490 600 : struct timespec rem[1];
491 600 : req->tv_sec = (time_t)( ((ulong)ns_dt) / ((ulong)1e9) ); /* in [0,2^31-1] */
492 600 : req->tv_nsec = (long) ( ((ulong)ns_dt) % ((ulong)1e9) ); /* in [0,1e9) */
493 600 : int sleep_res;
494 600 : #if defined(__linux__)
495 : /* Always use clock_nanosleep on Linux to be predictable */
496 600 : sleep_res = clock_nanosleep( CLOCK_REALTIME, 0, req, rem );
497 : #else
498 : sleep_res = nanosleep( req, rem );
499 : #endif
500 600 : if( FD_UNLIKELY( sleep_res ) && FD_LIKELY( errno==EINTR ) ) dt += ((long)1e9)*((long)rem->tv_sec) + rem->tv_nsec;
501 600 : return dt;
502 600 : }
503 :
504 : long
505 3 : fd_log_wait_until( long then ) {
506 3 : long now;
507 2586044 : for(;;) {
508 2586044 : now = fd_log_wallclock();
509 2586044 : long rem = then - now;
510 2586044 : if( FD_LIKELY( rem<=0L ) ) break; /* we've waited long enough */
511 2586041 : if( FD_UNLIKELY( rem>(long)1e9 ) ) { /* long wait (over ~1 s) ... sleep until medium long */
512 0 : fd_log_sleep( rem-(long)0.1e9 );
513 0 : continue;
514 0 : }
515 2586041 : if( FD_UNLIKELY( rem>(long)0.1e9 ) ) { /* medium long wait (over ~0.1 s) ... yield */
516 0 : FD_YIELD();
517 0 : continue;
518 0 : }
519 2586041 : if( FD_UNLIKELY( rem>(long)1e3 ) ) { /* medium short wait (over ~1 us) ... hyperthreading friendly spin */
520 2585923 : FD_SPIN_PAUSE();
521 2585923 : continue;
522 2585923 : }
523 : /* short wait ... spin on fd_log_wallclock */
524 2586041 : }
525 3 : return now;
526 3 : }
527 :
528 : /* LOG APIS ***********************************************************/
529 :
530 : char fd_log_private_path[ 1024 ]; /* "" outside boot/halt, init at boot */
531 : static int fd_log_private_fileno = -1; /* -1 outside boot/halt, init at boot */
532 : static int fd_log_private_dedup; /* 0 outside boot/halt, init at boot */
533 :
534 : void
535 4524 : fd_log_flush( void ) {
536 4524 : int log_fileno = FD_VOLATILE_CONST( fd_log_private_fileno );
537 4524 : if( FD_LIKELY( log_fileno!=-1 ) ) fsync( log_fileno );
538 4524 : }
539 :
540 : static int fd_log_private_colorize; /* 0 outside boot/halt, init at boot */
541 : static int fd_log_private_level_logfile; /* 0 outside boot/halt, init at boot */
542 : static int fd_log_private_level_stderr; /* 0 outside boot/halt, init at boot */
543 : static int fd_log_private_level_flush; /* 0 outside boot/halt, init at boot */
544 : static int fd_log_private_level_core; /* 0 outside boot/halt, init at boot */
545 : static int fd_log_private_unclean_exit;
546 : static int fd_log_private_signal_handler;
547 :
548 2476 : int fd_log_colorize ( void ) { return FD_VOLATILE_CONST( fd_log_private_colorize ); }
549 3688208 : int fd_log_level_logfile( void ) { return FD_VOLATILE_CONST( fd_log_private_level_logfile ); }
550 3553855 : int fd_log_level_stderr ( void ) { return FD_VOLATILE_CONST( fd_log_private_level_stderr ); }
551 2399265 : int fd_log_level_flush ( void ) { return FD_VOLATILE_CONST( fd_log_private_level_flush ); }
552 3601 : int fd_log_level_core ( void ) { return FD_VOLATILE_CONST( fd_log_private_level_core ); }
553 :
554 2476 : void fd_log_colorize_set ( int mode ) { FD_VOLATILE( fd_log_private_colorize ) = mode; }
555 2485 : void fd_log_level_logfile_set( int level ) { FD_VOLATILE( fd_log_private_level_logfile ) = level; }
556 2488 : void fd_log_level_stderr_set ( int level ) { FD_VOLATILE( fd_log_private_level_stderr ) = level; }
557 2476 : void fd_log_level_flush_set ( int level ) { FD_VOLATILE( fd_log_private_level_flush ) = level; }
558 2488 : void fd_log_level_core_set ( int level ) { FD_VOLATILE( fd_log_private_level_core ) = level; }
559 :
560 12 : int fd_log_private_logfile_fd( void ) { return FD_VOLATILE_CONST( fd_log_private_fileno ); }
561 :
562 0 : void fd_log_enable_signal_handler( void ) { fd_log_private_signal_handler = 1; }
563 0 : void fd_log_enable_unclean_exit( void ) { fd_log_private_unclean_exit = 1; }
564 :
565 : /* Buffer size used for vsnprintf calls (this is also one more than the
566 : maximum size that this can passed to fd_io_write) */
567 :
568 12169850 : #define FD_LOG_BUF_SZ (32UL*4096UL)
569 :
570 : /* Lock to used by fd_log_private_fprintf_0 to sequence calls writes
571 : between different _processes_ that share the same fd. */
572 :
573 : static int fd_log_private_shared_lock_local[1] __attribute__((aligned(128))); /* location of lock if boot mmap fails */
574 : int * fd_log_private_shared_lock = fd_log_private_shared_lock_local; /* Local lock outside boot/halt, init at boot */
575 :
576 : void
577 : fd_log_private_fprintf_0( int fd,
578 2405504 : char const * fmt, ... ) {
579 :
580 : /* Note: while this function superficially looks vdprintf-ish, we don't
581 : use that as it can do all sorts of unpleasantness under the hood
582 : (fflush, mutex / futex on fd, non-AS-safe buffering, ...) that this
583 : function deliberately avoids. Also, the function uses the shared
584 : lock to help keep messages generated from processes that share the
585 : same log fd sane. */
586 :
587 : /* TODO:
588 : - Consider moving to util/io as fd_io_printf or renaming to
589 : fd_log_printf?
590 : - Is msg better to have on stack or in thread local storage?
591 : - Is msg even necessary given shared lock? (probably still useful to
592 : keep the message write to be a single-system-call best effort)
593 : - Allow partial write to fd_io_write? (e.g. src_min=0 such that
594 : the fd_io_write below is guaranteed to be a single system call) */
595 :
596 2405504 : char msg[ FD_LOG_BUF_SZ ];
597 :
598 2405504 : va_list ap;
599 2405504 : va_start( ap, fmt );
600 2405504 : int len = vsnprintf( msg, FD_LOG_BUF_SZ, fmt, ap );
601 2405504 : if( len<0 ) len = 0; /* cmov */
602 2405504 : if( len>(int)(FD_LOG_BUF_SZ-1UL) ) len = (int)(FD_LOG_BUF_SZ-1UL); /* cmov */
603 2405504 : msg[ len ] = '\0';
604 2405504 : va_end( ap );
605 :
606 2405504 : # if FD_HAS_ATOMIC
607 2405504 : FD_COMPILER_MFENCE();
608 2411601 : while(( FD_LIKELY( FD_ATOMIC_CAS( fd_log_private_shared_lock, 0, 1 ) ) )) ;
609 2405504 : FD_COMPILER_MFENCE();
610 2405504 : # endif
611 :
612 2405504 : ulong wsz;
613 2405504 : fd_io_write( fd, msg, (ulong)len, (ulong)len, &wsz ); /* Note: we ignore errors because what are we doing to do? log them? */
614 :
615 2405504 : # if FD_HAS_ATOMIC
616 2405504 : FD_COMPILER_MFENCE();
617 2405504 : FD_VOLATILE( *fd_log_private_shared_lock ) = 0;
618 2405504 : FD_COMPILER_MFENCE();
619 2405504 : # endif
620 :
621 2405504 : }
622 :
623 : /* This is the same as fd_log_private_fprintf_0 except that it does not try to
624 : take a lock when writing to the log file. This should almost never be used
625 : except in exceptional cases when logging while the process is shutting down.
626 :
627 : It exists because if a child process dies while holding the lock, we may
628 : want to log some diagnostic messages when tearing down the process tree. */
629 : void
630 : fd_log_private_fprintf_nolock_0( int fd,
631 0 : char const * fmt, ... ) {
632 :
633 : /* Note: while this function superficially looks vdprintf-ish, we don't
634 : use that as it can do all sorts of unpleasantness under the hood
635 : (fflush, mutex / futex on fd, non-AS-safe buffering, ...) that this
636 : function deliberately avoids. Also, the function uses the shared
637 : lock to help keep messages generated from processes that share the
638 : same log fd sane. */
639 :
640 : /* TODO:
641 : - Consider moving to util/io as fd_io_printf or renaming to
642 : fd_log_printf?
643 : - Is msg better to have on stack or in thread local storage?
644 : - Is msg even necessary given shared lock? (probably still useful to
645 : keep the message write to be a single-system-call best effort)
646 : - Allow partial write to fd_io_write? (e.g. src_min=0 such that
647 : the fd_io_write below is guaranteed to be a single system call) */
648 :
649 0 : char msg[ FD_LOG_BUF_SZ ];
650 :
651 0 : va_list ap;
652 0 : va_start( ap, fmt );
653 0 : int len = vsnprintf( msg, FD_LOG_BUF_SZ, fmt, ap );
654 0 : if( len<0 ) len = 0; /* cmov */
655 0 : if( len>(int)(FD_LOG_BUF_SZ-1UL) ) len = (int)(FD_LOG_BUF_SZ-1UL); /* cmov */
656 0 : msg[ len ] = '\0';
657 0 : va_end( ap );
658 :
659 0 : ulong wsz;
660 0 : fd_io_write( fd, msg, (ulong)len, (ulong)len, &wsz ); /* Note: we ignore errors because what are we doing to do? log them? */
661 0 : }
662 :
663 : /* Log buffer used by fd_log_private_0 and fd_log_private_hexdump_msg */
664 :
665 : static FD_TL char fd_log_private_log_msg[ FD_LOG_BUF_SZ ];
666 :
667 : char const *
668 3679421 : fd_log_private_0( char const * fmt, ... ) {
669 3679421 : va_list ap;
670 3679421 : va_start( ap, fmt );
671 3679421 : int len = vsnprintf( fd_log_private_log_msg, FD_LOG_BUF_SZ, fmt, ap );
672 3679421 : if( len<0 ) len = 0; /* cmov */
673 3679421 : if( len>(int)(FD_LOG_BUF_SZ-1UL) ) len = (int)(FD_LOG_BUF_SZ-1UL); /* cmov */
674 3679421 : fd_log_private_log_msg[ len ] = '\0';
675 3679421 : va_end( ap );
676 3679421 : return fd_log_private_log_msg;
677 3679421 : }
678 :
679 : char const *
680 : fd_log_private_hexdump_msg( char const * descr,
681 : void const * mem,
682 6300 : ulong sz ) {
683 :
684 3159756 : # define FD_LOG_HEXDUMP_BYTES_PER_LINE (16UL)
685 6300 : # define FD_LOG_HEXDUMP_BLOB_DESCRIPTION_MAX_LEN (32UL)
686 6300 : # define FD_LOG_HEXDUMP_MAX_INPUT_BLOB_SZ (1664UL) /* multiple of 128 >= 1542 */
687 :
688 3560238 : # define FD_LOG_HEXDUMP_ADD_TO_LOG_BUF(...) do { log_buf_ptr += fd_int_max( sprintf( log_buf_ptr, __VA_ARGS__ ), 0 ); } while(0)
689 6300 : char * log_buf_ptr = fd_log_private_log_msg; /* used by FD_LOG_HEXDUMP_ADD_TO_LOG_BUF macro */
690 :
691 : /* Print the hexdump header */
692 : /* FIXME: consider additional sanitization of descr or using compiler
693 : tricks to prevent user from passing a non-const-char string (i.e.
694 : data they got from somewhere else that might not be sanitized). */
695 :
696 6300 : if( FD_UNLIKELY( !descr ) ) {
697 :
698 0 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "HEXDUMP - (%lu bytes at 0x%lx)", sz, (ulong)mem );
699 :
700 6300 : } else if( FD_UNLIKELY( strlen( descr )>FD_LOG_HEXDUMP_BLOB_DESCRIPTION_MAX_LEN ) ) {
701 :
702 3 : char tmp[ FD_LOG_HEXDUMP_BLOB_DESCRIPTION_MAX_LEN + 1UL ];
703 3 : fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( tmp ), descr, FD_LOG_HEXDUMP_BLOB_DESCRIPTION_MAX_LEN ) );
704 3 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "HEXDUMP \"%s\"... (%lu bytes at 0x%lx)", tmp, sz, (ulong)mem );
705 :
706 6297 : } else {
707 :
708 6297 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "HEXDUMP \"%s\" (%lu bytes at 0x%lx)", descr, sz, (ulong)mem );
709 :
710 6297 : }
711 :
712 6300 : if( FD_UNLIKELY( !sz ) ) return fd_log_private_log_msg;
713 :
714 6300 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "\n" );
715 :
716 6300 : if( FD_UNLIKELY( !mem ) ) {
717 0 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "\t... snip (unreadable memory) ..." );
718 0 : return fd_log_private_log_msg;
719 0 : }
720 :
721 6300 : char line_buf[ FD_LOG_HEXDUMP_BYTES_PER_LINE+1 ];
722 6300 : char const * blob = (char const *)mem;
723 6300 : ulong blob_off = 0UL;
724 6300 : ulong blob_sz = fd_ulong_min( sz, FD_LOG_HEXDUMP_MAX_INPUT_BLOB_SZ );
725 :
726 3158370 : for( ; blob_off<blob_sz; blob_off++ ) {
727 3152070 : ulong col_idx = blob_off % FD_LOG_HEXDUMP_BYTES_PER_LINE;
728 :
729 : /* New line. Print previous line's ASCII representation and then print the offset. */
730 3152070 : if( FD_UNLIKELY( !col_idx ) ) {
731 197091 : if( FD_LIKELY( blob_off ) ) FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( " %s\n", line_buf );
732 197091 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "\t%04lx: ", blob_off );
733 197091 : }
734 : /* FIXME: consider extra space between col 7 and 8 to make easier
735 : for visual inspection */
736 :
737 3152070 : char c = blob[blob_off];
738 3152070 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( " %02x", (uint)(uchar)c );
739 :
740 : /* If not a printable ASCII character, output a dot. */
741 3152070 : line_buf[ col_idx ] = fd_char_if( fd_isalnum( (int)c ) | fd_ispunct( (int)c ) | (c==' '), c, '.' );
742 3152070 : line_buf[ col_idx+1UL ] = '\0';
743 3152070 : }
744 :
745 : /* Print the 2nd column of last blob line */
746 7686 : while( blob_off % FD_LOG_HEXDUMP_BYTES_PER_LINE ) {
747 1386 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( " " );
748 1386 : blob_off++;
749 1386 : }
750 6300 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( " %s", line_buf );
751 :
752 6300 : if( FD_UNLIKELY( blob_sz < sz ) )
753 0 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "\n\t... snip (printed %lu bytes, omitted %lu bytes) ...", blob_sz, sz-blob_sz );
754 :
755 6300 : return fd_log_private_log_msg;
756 :
757 6300 : # undef FD_LOG_HEXDUMP_BYTES_PER_LINE
758 6300 : # undef FD_LOG_HEXDUMP_BLOB_DESCRIPTION_MAX_LEN
759 6300 : # undef FD_LOG_HEXDUMP_MAX_INPUT_BLOB_SZ
760 6300 : # undef FD_LOG_HEXDUMP_ADD_TO_LOG_BUF
761 6300 : }
762 :
763 : void
764 : fd_log_private_1( int level,
765 : long now,
766 : char const * file,
767 : int line,
768 : char const * func,
769 3685730 : char const * msg ) {
770 :
771 3685730 : if( level<fd_log_level_logfile() ) return;
772 :
773 : /* These are thread init so we call them regardless of permanent log
774 : enabled to their initialization time is guaranteed independent of
775 : whether the permanent log is enabled. */
776 :
777 3551378 : char const * thread = fd_log_thread();
778 3551378 : char const * cpu = fd_log_cpu();
779 3551378 : ulong tid = fd_log_tid();
780 :
781 3551378 : int log_fileno = FD_VOLATILE_CONST( fd_log_private_fileno );
782 3551378 : int to_logfile = (log_fileno!=-1);
783 3551378 : int to_stderr = (level>=fd_log_level_stderr());
784 3551378 : if( !(to_logfile | to_stderr) ) return;
785 :
786 : /* Deduplicate the log if requested */
787 :
788 3486125 : if( fd_log_private_dedup ) {
789 :
790 : /* Compute if this message appears to be a recent duplicate of
791 : a previous log message */
792 :
793 3486124 : ulong hash = fd_cstr_hash_append( fd_cstr_hash_append( fd_cstr_hash_append( fd_ulong_hash(
794 3486124 : (ulong)(8L*(long)line+(long)level) ), file ), func ), msg );
795 :
796 3486124 : static long const dedup_interval = 20000000L; /* 1/50 s */
797 :
798 3486124 : static FD_TL int init; /* 0 on thread start */
799 3486124 : static FD_TL ulong last_hash; /* 0UL on thread start */
800 3486124 : static FD_TL long then; /* 0L on thread start */
801 :
802 3486124 : int is_dup = init & (hash==last_hash) & ((now-then)<dedup_interval);
803 3486124 : init = 1;
804 :
805 : /* Update how many messages from this thread in row have been
806 : duplicates */
807 :
808 3486124 : static FD_TL ulong dedup_cnt; /* 0UL on thread start */
809 3486124 : static FD_TL int in_dedup; /* 0 on thread start */
810 :
811 3486124 : if( is_dup ) dedup_cnt++;
812 2396057 : else {
813 2396057 : if( in_dedup ) {
814 :
815 : /* This message appears to end a long string of duplicates.
816 : Log the end of the deduplication. */
817 :
818 284 : char then_cstr[ FD_LOG_WALLCLOCK_CSTR_BUF_SZ ];
819 284 : fd_log_wallclock_cstr( then, then_cstr );
820 :
821 284 : if( to_logfile )
822 284 : fd_log_private_fprintf_0( log_fileno, "SNIP %s %6lu:%-6lu %s:%s:%-4s %s:%s:%-4s "
823 284 : "stopped repeating (%lu identical messages)\n",
824 284 : then_cstr, fd_log_group_id(),tid, fd_log_user(),fd_log_host(),cpu,
825 284 : fd_log_app(),fd_log_group(),thread, dedup_cnt+1UL );
826 :
827 284 : if( to_stderr ) {
828 216 : char * then_short_cstr = then_cstr+5; then_short_cstr[21] = '\0'; /* Lop off the year, ns resolution and timezone */
829 216 : fd_log_private_fprintf_0( STDERR_FILENO, "SNIP %s %-6lu %-4s %-4s stopped repeating (%lu identical messages)\n",
830 216 : then_short_cstr, tid,cpu,thread, dedup_cnt+1UL );
831 216 : }
832 :
833 284 : in_dedup = 0;
834 284 : }
835 :
836 2396057 : dedup_cnt = 0UL;
837 2396057 : }
838 :
839 : /* dedup_cnt previous messages from this thread appear to be
840 : duplicates. Decide whether to let the raw message print or
841 : deduplicate to the log. FIXME: CONSIDER RANDOMIZING THE
842 : THROTTLE. */
843 :
844 3486124 : static ulong const dedup_thresh = 3UL; /* let initial dedup_thresh duplicates go out the door */
845 3486124 : static long const dedup_throttle = 1000000000L; /* ~1s, how often to update status on current duplication */
846 :
847 3486124 : static FD_TL long dedup_last; /* 0L on thread start */
848 :
849 3486124 : if( dedup_cnt < dedup_thresh ) dedup_last = now;
850 1089338 : else {
851 1089338 : if( (now-dedup_last) >= dedup_throttle ) {
852 36 : char now_cstr[ FD_LOG_WALLCLOCK_CSTR_BUF_SZ ];
853 36 : fd_log_wallclock_cstr( now, now_cstr );
854 36 : if( to_logfile )
855 36 : fd_log_private_fprintf_0( log_fileno, "SNIP %s %6lu:%-6lu %s:%s:%-4s %s:%s:%-4s repeating (%lu identical messages)\n",
856 36 : now_cstr, fd_log_group_id(),tid, fd_log_user(),fd_log_host(),cpu,
857 36 : fd_log_app(),fd_log_group(),thread, dedup_cnt+1UL );
858 36 : if( to_stderr ) {
859 36 : char * now_short_cstr = now_cstr+5; now_short_cstr[21] = '\0'; /* Lop off the year, ns resolution and timezone */
860 36 : fd_log_private_fprintf_0( STDERR_FILENO, "SNIP %s %-6lu %-4s %-4s repeating (%lu identical messages)\n",
861 36 : now_short_cstr, tid,cpu,thread, dedup_cnt+1UL );
862 36 : }
863 36 : dedup_last = now;
864 36 : }
865 1089338 : in_dedup = 1;
866 1089338 : }
867 :
868 3486124 : last_hash = hash;
869 3486124 : then = now;
870 :
871 3486124 : if( in_dedup ) return;
872 3486124 : }
873 :
874 2396789 : char now_cstr[ FD_LOG_WALLCLOCK_CSTR_BUF_SZ ];
875 2396789 : fd_log_wallclock_cstr( now, now_cstr );
876 :
877 2396789 : static char const * level_cstr[] = {
878 2396789 : /* 0 */ "DEBUG ",
879 2396789 : /* 1 */ "INFO ",
880 2396789 : /* 2 */ "NOTICE ",
881 2396789 : /* 3 */ "WARNING",
882 2396789 : /* 4 */ "ERR ",
883 2396789 : /* 5 */ "CRIT ",
884 2396789 : /* 6 */ "ALERT ",
885 2396789 : /* 7 */ "EMERG "
886 2396789 : };
887 :
888 2396789 : if( to_logfile )
889 2389826 : fd_log_private_fprintf_0( log_fileno, "%s %s %6lu:%-6lu %s:%s:%-4s %s:%s:%-4s %s(%i)[%s]: %s\n",
890 2389826 : level_cstr[level], now_cstr, fd_log_group_id(),tid, fd_log_user(),fd_log_host(),cpu,
891 2389826 : fd_log_app(),fd_log_group(),thread, file,line,func, msg );
892 :
893 2396789 : if( to_stderr ) {
894 10154 : static char const * color_level_cstr[] = {
895 10154 : /* 0 */ TEXT_NORMAL "DEBUG ",
896 10154 : /* 1 */ TEXT_BLUE "INFO " TEXT_NORMAL,
897 10154 : /* 2 */ TEXT_GREEN "NOTICE " TEXT_NORMAL,
898 10154 : /* 3 */ TEXT_YELLOW "WARNING" TEXT_NORMAL,
899 10154 : /* 4 */ TEXT_RED "ERR " TEXT_NORMAL,
900 10154 : /* 5 */ TEXT_RED TEXT_BOLD "CRIT " TEXT_NORMAL,
901 10154 : /* 6 */ TEXT_RED TEXT_BOLD TEXT_UNDERLINE "ALERT " TEXT_NORMAL,
902 10154 : /* 7 */ TEXT_RED TEXT_BOLD TEXT_UNDERLINE TEXT_BLINK "EMERG " TEXT_NORMAL
903 10154 : };
904 10154 : char * now_short_cstr = now_cstr+5; now_short_cstr[21] = '\0'; /* Lop off the year, ns resolution and timezone */
905 10154 : fd_log_private_fprintf_0( STDERR_FILENO, "%s %s %-6lu %-4s %-4s %s(%i): %s\n",
906 10154 : fd_log_private_colorize ? color_level_cstr[level] : level_cstr[level],
907 10154 : now_short_cstr, tid,cpu,thread, file, line, msg );
908 10154 : }
909 :
910 2396789 : if( level<fd_log_level_flush() ) return;
911 :
912 4491 : fd_log_flush();
913 4491 : }
914 :
915 : void
916 : fd_log_private_2( int level,
917 : long now,
918 : char const * file,
919 : int line,
920 : char const * func,
921 1125 : char const * msg ) {
922 1125 : fd_log_private_1( level, now, file, line, func, msg );
923 :
924 1125 : if( level<fd_log_level_core() ) {
925 1125 : # if defined(__linux__)
926 1125 : if( fd_log_private_unclean_exit ) {
927 0 : syscall( SYS_exit_group, 1 );
928 0 : }
929 1125 : # endif /* defined(__linux__) */
930 1125 : exit(1); /* atexit will call fd_log_private_cleanup implicitly */
931 1125 : }
932 :
933 0 : abort();
934 1125 : }
935 :
936 : void
937 : fd_log_private_raw_2( char const * file,
938 : int line,
939 : char const * func,
940 0 : char const * msg ) {
941 0 : fd_log_private_fprintf_nolock_0( STDERR_FILENO, "%s(%i)[%s]: %s\n", file, line, func, msg );
942 0 : # if defined(__linux__)
943 0 : syscall( SYS_exit_group, 1 );
944 : # else
945 : exit(1);
946 : # endif
947 0 : abort();
948 0 : }
949 :
950 : /* BOOT/HALT APIS *****************************************************/
951 :
952 : static void
953 3800 : fd_log_private_cleanup( void ) {
954 :
955 : /* The atexit below means that all calls to "exit();" implicitly
956 : become "fd_log_private_cleanup(); exit();". It also implies that
957 : programs that terminate via a top level return from main implicitly
958 : call fd_log_private_cleanup().
959 :
960 : As such it is possible that a thread other than the booter will
961 : trigger cleanup either by triggering this directly (e.g. calling
962 : exit) or indirectly (e.g. by logging a message with an exit
963 : triggering priority) and that the booter itself might call this
964 : more than once sequentially (e.g. fd_halt() calling cleanup
965 : explicitly followed by return from main triggering it again.
966 :
967 : Accordingly we protect this with a ONCE block so it only will
968 : execute once per program. Further, if cleanup gets triggered by
969 : multiple threads concurrently, the ONCE block will prevent them
970 : from progressing until the first thread that hits the once block
971 : has completed cleanup. */
972 :
973 12552 : FD_ONCE_BEGIN {
974 2476 : int log_fileno = FD_VOLATILE_CONST( fd_log_private_fileno );
975 2476 : if( log_fileno==-1 ) fd_log_private_fprintf_0( STDERR_FILENO, "No log\n" );
976 652 : else if( !strcmp( fd_log_private_path, "-" ) ) fd_log_private_fprintf_0( STDERR_FILENO, "Log to stdout\n" );
977 640 : else {
978 640 : # if FD_HAS_THREADS
979 640 : if( fd_log_private_thread_id_ctr>1UL ) { /* There are potentially other log users running */
980 : /* Just closing the permanent log file is not multithreading
981 : safe in the case where other threads are still running
982 : normally and thus potentially logging to the permanent log.
983 : Such should not happen in a correctly written and functioning
984 : application but logging exists in large part to help
985 : understand when applications misbehave. So we try to be as
986 : robust and informative as we can here. FIXME: THE SECOND
987 : USLEEP IS AN UGLY HACK TO REDUCE (BUT NOT FULLY ELIMINATE)
988 : THE RISK OF USE AFTER CLOSE BY THOSE OTHER THREADS. IT IS
989 : POSSIBLE WITH A MORE INVASIVE CHANGES TO FULLY ELIMINATE THIS
990 : RISK. */
991 0 : usleep( (useconds_t)40000 ); /* Give potentially concurrent users a chance to get their dying messages out */
992 0 : FD_COMPILER_MFENCE();
993 0 : FD_VOLATILE( fd_log_private_fileno ) = -1; /* Turn off the permanent log for concurrent users */
994 0 : FD_COMPILER_MFENCE();
995 0 : usleep( (useconds_t)40000 ); /* Give any concurrent log operations progress at turn off a chance to wrap */
996 0 : }
997 : # else
998 : FD_VOLATILE( fd_log_private_fileno ) = -1;
999 : # endif
1000 :
1001 640 : fsync( log_fileno );
1002 640 : sync();
1003 640 : fd_log_private_fprintf_0( STDERR_FILENO, "Log at \"%s\"\n", fd_log_private_path );
1004 640 : }
1005 2476 : } FD_ONCE_END;
1006 3800 : }
1007 :
1008 : static void
1009 : fd_log_private_sig_abort( int sig,
1010 : siginfo_t * info,
1011 0 : void * context ) {
1012 0 : (void)sig; (void)info; (void)context;
1013 :
1014 : /* Thread could have caught signal while holding a lock.
1015 : Hack around this re-entrancy problem by pointing the log lock to
1016 : a dummy buffer. */
1017 0 : int lock = 0;
1018 0 : fd_log_private_shared_lock = &lock;
1019 :
1020 0 : #define FD_LOG_ERR_NOEXIT(a) do { long _fd_log_msg_now = fd_log_wallclock(); fd_log_private_1( 4, _fd_log_msg_now, __FILE__, __LINE__, __func__, fd_log_private_0 a ); } while(0)
1021 0 : FD_LOG_ERR_NOEXIT(( "Received signal %s", fd_io_strsignal( sig ) ));
1022 0 : #undef FD_LOG_ERR_NOEXIT
1023 :
1024 0 : # if FD_HAS_BACKTRACE
1025 :
1026 0 : void * btrace[ 128UL ];
1027 0 : int btrace_cnt = backtrace( btrace, 128 );
1028 :
1029 0 : fd_backtrace_log( btrace, (ulong)btrace_cnt );
1030 :
1031 0 : # endif
1032 :
1033 : /* Returning is going to cause SIGSYS since it's probably not allowed
1034 : in the sandbox, which is OK. The parent process will terminate
1035 : everything anyway. If we allow rt_sigreturn to be called in the
1036 : sandbox, then we will get a correct signal. */
1037 0 : return;
1038 0 : }
1039 :
1040 : static void
1041 44298 : fd_log_private_sig_trap( int sig ) {
1042 44298 : struct sigaction act[1];
1043 : /* FIXME: CONSIDER NOT OVERRIDING IF THE SIGNAL HANDLER HAS ALREADY
1044 : BEEN SET BY THE USER. */
1045 44298 : act->sa_sigaction = fd_log_private_sig_abort;
1046 44298 : if( sigemptyset( &act->sa_mask ) ) FD_LOG_ERR(( "sigempty set failed" ));
1047 44298 : act->sa_flags = (int)(SA_SIGINFO | SA_RESETHAND);
1048 44298 : if( sigaction( sig, act, NULL ) ) FD_LOG_ERR(( "unable to override signal %i", sig ));
1049 44298 : }
1050 :
1051 : static int
1052 : fd_log_private_open_path( int cmdline,
1053 2476 : char const * log_path ) {
1054 2476 : ulong log_path_sz = log_path ? (strlen( log_path )+1UL) : 0UL;
1055 :
1056 2476 : if( !log_path_sz ) { /* Use default log path */
1057 0 : char tag[ FD_LOG_WALLCLOCK_CSTR_BUF_SZ ];
1058 0 : fd_log_wallclock_cstr( fd_log_wallclock(), tag );
1059 0 : for( ulong b=0UL; tag[b]; b++ ) if( tag[b]==' ' || tag[b]=='-' || tag[b]=='.' || tag[b]==':' ) tag[b] = '_';
1060 0 : ulong len; fd_cstr_printf( fd_log_private_path, 1024UL, &len, "/tmp/fd-%i.%i.%i_%lu_%s_%s_%s",
1061 0 : FDCTL_MAJOR_VERSION, FDCTL_MINOR_VERSION, FDCTL_PATCH_VERSION,
1062 0 : fd_log_group_id(), fd_log_user(), fd_log_host(), tag );
1063 0 : if( len==1023UL ) { fd_log_private_fprintf_0( STDERR_FILENO, "default log path too long; unable to boot\n" ); exit(1); }
1064 0 : }
1065 2476 : else if( log_path_sz==1UL ) fd_log_private_path[0] = '\0'; /* User disabled */
1066 652 : else if( log_path_sz<=1024UL ) fd_memcpy( fd_log_private_path, log_path, log_path_sz ); /* User specified */
1067 0 : else { fd_log_private_fprintf_0( STDERR_FILENO, "log path too long; unable to boot\n" ); exit(1); } /* Invalid */
1068 :
1069 2476 : int log_fileno;
1070 2476 : if( fd_log_private_path[0]=='\0' ) {
1071 1824 : if( cmdline ) fd_log_private_fprintf_0( STDERR_FILENO, "--log-path \"\"\nNo log\n" );
1072 0 : else fd_log_private_fprintf_0( STDERR_FILENO, "No log\n" );
1073 1824 : log_fileno = -1;
1074 1824 : } else if( !strcmp( fd_log_private_path, "-" ) ) {
1075 12 : if( cmdline ) fd_log_private_fprintf_0( STDERR_FILENO, "--log-path \"-\"\nLog to stdout\n" );
1076 0 : else fd_log_private_fprintf_0( STDERR_FILENO, "Log to stdout\n" );
1077 12 : log_fileno = STDOUT_FILENO;
1078 640 : } else {
1079 640 : if( cmdline && !log_path_sz ) fd_log_private_fprintf_0( STDERR_FILENO, "--log-path not specified; using autogenerated path\n" );
1080 640 : log_fileno = open( fd_log_private_path, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH );
1081 640 : if( log_fileno==-1 ) {
1082 0 : fd_log_private_fprintf_0( STDERR_FILENO, "open failed (--log-path \"%s\"); unable to boot (%d-%s)\n", fd_log_private_path, errno, fd_io_strerror( errno ) );
1083 0 : exit(1);
1084 0 : }
1085 640 : fd_log_private_fprintf_0( STDERR_FILENO, "Log at \"%s\"\n", fd_log_private_path );
1086 640 : }
1087 2476 : return log_fileno;
1088 2476 : }
1089 :
1090 : void
1091 : fd_log_private_boot( int * pargc,
1092 2476 : char *** pargv ) {
1093 : //FD_LOG_INFO(( "fd_log: booting" )); /* Log not online yet */
1094 :
1095 2476 : char buf[ FD_LOG_NAME_MAX ];
1096 :
1097 2476 : fd_log_private_shared_lock = fd_log_private_shared_lock_local;
1098 :
1099 : /* Init our our application logical ids */
1100 : /* FIXME: CONSIDER EXPLICIT SPECIFICATION OF RANGE OF THREADS
1101 : INSTEAD OF ATOMIC COUNTER FROM BASE */
1102 :
1103 2476 : fd_log_private_app_id_set( fd_env_strip_cmdline_ulong( pargc, pargv, "--log-app-id", "FD_LOG_APP_ID", 0UL ) );
1104 :
1105 2476 : fd_log_private_app_set( fd_env_strip_cmdline_cstr( pargc, pargv, "--log-app", "FD_LOG_APP", NULL ) );
1106 :
1107 2476 : # if FD_HAS_THREADS
1108 2476 : fd_log_private_thread_id_ctr = fd_env_strip_cmdline_ulong( pargc, pargv, "--log-thread-id", "FD_LOG_THREAD_ID", 0UL );
1109 2476 : ulong thread_id = fd_log_private_thread_id_next();
1110 : # else
1111 : ulong thread_id = fd_env_strip_cmdline_ulong( pargc, pargv, "--log-thread-id", "FD_LOG_THREAD_ID", 0UL );
1112 : # endif
1113 2476 : fd_log_private_thread_id_set( thread_id );
1114 :
1115 2476 : fd_log_thread_set( fd_env_strip_cmdline_cstr( pargc, pargv, "--log-thread", "FD_LOG_THREAD", NULL ) );
1116 :
1117 : /* Init our application physical ids */
1118 : /* We ignore any user specified cpu-id in favor of the actual core
1119 : assigned by the host OS. We strip it from the command line so
1120 : downstream command line handling is identical from user's point of
1121 : view. */
1122 :
1123 2476 : fd_log_private_host_id_set( fd_env_strip_cmdline_ulong( pargc, pargv, "--log-host-id", "FD_LOG_HOST_ID", 0UL ) );
1124 :
1125 2476 : char const * host = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-host", "FD_LOG_HOST", NULL );
1126 2476 : if( !host ) { if( !gethostname( buf, FD_LOG_NAME_MAX ) ) buf[ FD_LOG_NAME_MAX-1UL ] = '\0', host = buf; }
1127 2476 : fd_msan_unpoison( (void *)host, FD_LOG_NAME_MAX );
1128 2476 : fd_log_private_host_set( host );
1129 :
1130 2476 : fd_env_strip_cmdline_ulong( pargc, pargv, "--log-cpu-id", "FD_LOG_CPU_ID", 0UL ); /* FIXME: LOG IGNORING? */
1131 2476 : fd_log_private_cpu_id_set( fd_log_private_cpu_id_default() );
1132 :
1133 2476 : fd_log_cpu_set( fd_env_strip_cmdline_cstr( pargc, pargv, "--log-cpu", "FD_LOG_CPU", NULL ) );
1134 :
1135 : /* Init our thread group ids */
1136 : /* We ignore any user specified group id and tid in favor of the actual
1137 : group id and tid assigned by the host OS. We strip it from the
1138 : command line so downstream command line handling is identical from
1139 : user's point of view. */
1140 :
1141 2476 : fd_env_strip_cmdline_ulong( pargc, pargv, "--log-group-id", "FD_LOG_GROUP_ID", 0UL ); /* FIXME: LOG IGNORING? */
1142 2476 : pid_t pid = getpid();
1143 2476 : fd_log_private_group_id_set( fd_ulong_if( pid>(pid_t)0, (ulong)pid, ULONG_MAX ) );
1144 :
1145 2476 : char const * group = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-group", "FD_LOG_GROUP", NULL );
1146 2476 : # if defined(__linux__)
1147 2476 : if( !group ) group = program_invocation_short_name;
1148 : # elif defined(__FreeBSD__)
1149 : if( !group ) group = getprogname();
1150 : # endif
1151 2476 : if( !group ) group = (pargc && pargv && (*pargc)>0) ? (*pargv)[0] : NULL;
1152 2476 : fd_log_private_group_set( group );
1153 :
1154 2476 : fd_env_strip_cmdline_ulong( pargc, pargv, "--log-tid", "FD_LOG_TID", 0UL ); /* FIXME: LOG IGNORING? */
1155 2476 : fd_log_private_tid_set( fd_log_private_tid_default() );
1156 :
1157 2476 : fd_env_strip_cmdline_ulong( pargc, pargv, "--log-user-id", "FD_LOG_USER_ID", 0UL ); /* FIXME: LOG IGNORING? */
1158 2476 : fd_log_private_user_id_set( fd_log_private_user_id_default() );
1159 :
1160 2476 : char const * user = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-user", "FD_LOG_USER", NULL );
1161 2476 : if( !user ) user = getenv( "LOGNAME" );
1162 2476 : if( !user ) user = getlogin();
1163 2476 : fd_log_private_user_set( user );
1164 :
1165 : /* Configure the log */
1166 :
1167 2476 : fd_log_private_dedup = fd_env_strip_cmdline_int( pargc, pargv, "--log-dedup", "FD_LOG_DEDUP", 1 );
1168 :
1169 2476 : int colorize = 0;
1170 2476 : do {
1171 2476 : char const * cstr = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-colorize", "FD_LOG_COLORIZE", NULL );
1172 2476 : if( cstr ) { colorize = fd_cstr_to_int( cstr ); break; }
1173 :
1174 2476 : cstr = fd_env_strip_cmdline_cstr( NULL, NULL, NULL, "COLORTERM", NULL );
1175 2476 : if( cstr && !strcmp( cstr, "truecolor" ) ) { colorize = 1; break; }
1176 :
1177 2476 : cstr = fd_env_strip_cmdline_cstr( NULL, NULL, NULL, "TERM", NULL );
1178 2476 : if( cstr && strstr( cstr, "256color" ) ) { colorize = 1; break; }
1179 :
1180 2476 : } while(0);
1181 0 : fd_log_colorize_set( colorize );
1182 :
1183 2476 : fd_log_level_logfile_set( fd_env_strip_cmdline_int( pargc, pargv, "--log-level-logfile", "FD_LOG_LEVEL_LOGFILE", 1 ) );
1184 2476 : fd_log_level_stderr_set ( fd_env_strip_cmdline_int( pargc, pargv, "--log-level-stderr", "FD_LOG_LEVEL_STDERR", 2 ) );
1185 2476 : fd_log_level_flush_set ( fd_env_strip_cmdline_int( pargc, pargv, "--log-level-flush", "FD_LOG_LEVEL_FLUSH", 3 ) );
1186 2476 : fd_log_level_core_set ( fd_env_strip_cmdline_int( pargc, pargv, "--log-level-core", "FD_LOG_LEVEL_CORE", 5 ) );
1187 :
1188 : /* Hook up signal handlers */
1189 :
1190 2476 : int log_backtrace = fd_env_strip_cmdline_int( pargc, pargv, "--log-backtrace", "FD_LOG_BACKTRACE", 1 );
1191 2476 : if( log_backtrace || fd_log_private_signal_handler ) {
1192 :
1193 2461 : # if FD_HAS_BACKTRACE
1194 : /* If libgcc isn't already linked into the program when a trapped
1195 : signal is received by an application, calls to backtrace and
1196 : backtrace_symbols_fd within the signal handler can silently
1197 : invoke the dynamic linker, which in turn can do silent async
1198 : signal unsafe behavior behind our back. We do dummy calls to
1199 : backtrace and backtrace_symbols_fd here to avoid dynamic linking
1200 : surprises in the signal handler. (Hat tip to runtimeverification
1201 : for finding this.) */
1202 :
1203 2461 : void * btrace[128];
1204 2461 : (void)backtrace( btrace, 128 );
1205 2461 : # endif /* FD_HAS_BACKTRACE */
1206 :
1207 : /* This is all overridable POSIX sigs whose default behavior is to
1208 : abort the program. It will backtrace and then fallback to the
1209 : default behavior. */
1210 2461 : fd_log_private_sig_trap( SIGABRT );
1211 2461 : fd_log_private_sig_trap( SIGALRM );
1212 2461 : fd_log_private_sig_trap( SIGFPE );
1213 2461 : fd_log_private_sig_trap( SIGHUP );
1214 2461 : fd_log_private_sig_trap( SIGILL );
1215 2461 : fd_log_private_sig_trap( SIGQUIT );
1216 2461 : fd_log_private_sig_trap( SIGPIPE );
1217 2461 : fd_log_private_sig_trap( SIGSEGV );
1218 2461 : fd_log_private_sig_trap( SIGUSR1 );
1219 2461 : fd_log_private_sig_trap( SIGUSR2 );
1220 2461 : fd_log_private_sig_trap( SIGBUS );
1221 2461 : fd_log_private_sig_trap( SIGPOLL );
1222 2461 : fd_log_private_sig_trap( SIGPROF );
1223 2461 : fd_log_private_sig_trap( SIGSYS );
1224 2461 : fd_log_private_sig_trap( SIGTRAP );
1225 2461 : fd_log_private_sig_trap( SIGVTALRM );
1226 2461 : fd_log_private_sig_trap( SIGXCPU );
1227 2461 : fd_log_private_sig_trap( SIGXFSZ );
1228 2461 : }
1229 :
1230 : /* Hook up the permanent log */
1231 2476 : char const * log_path = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-path", "FD_LOG_PATH", NULL );
1232 2476 : FD_VOLATILE( fd_log_private_fileno ) = fd_log_private_open_path( 1, log_path );
1233 :
1234 2476 : if( !fd_log_private_unclean_exit ) {
1235 2476 : if( atexit( fd_log_private_cleanup ) ) { fd_log_private_fprintf_0( STDERR_FILENO, "atexit failed; unable to boot\n" ); exit(1); }
1236 2476 : }
1237 :
1238 : /* At this point, logging online */
1239 2476 : if( fd_log_build_info_sz>1UL ) FD_LOG_INFO(( "fd_log: build info:\n%s", fd_log_build_info ));
1240 0 : else FD_LOG_INFO(( "fd_log: build info not available" ));
1241 2476 : FD_LOG_INFO(( "fd_log: --log-path %s", fd_log_private_path ));
1242 2476 : FD_LOG_INFO(( "fd_log: --log-dedup %i", fd_log_private_dedup ));
1243 2476 : FD_LOG_INFO(( "fd_log: --log-colorize %i", fd_log_colorize() ));
1244 2476 : FD_LOG_INFO(( "fd_log: --log-level-logfile %i", fd_log_level_logfile() ));
1245 2476 : FD_LOG_INFO(( "fd_log: --log-level-stderr %i", fd_log_level_stderr() ));
1246 2476 : FD_LOG_INFO(( "fd_log: --log-level-flush %i", fd_log_level_flush() ));
1247 2476 : FD_LOG_INFO(( "fd_log: --log-level-core %i", fd_log_level_core() ));
1248 2476 : FD_LOG_INFO(( "fd_log: --log-app-id %lu", fd_log_app_id() ));
1249 2476 : FD_LOG_INFO(( "fd_log: --log-app %s", fd_log_app() ));
1250 2476 : FD_LOG_INFO(( "fd_log: --log-thread-id %lu", fd_log_thread_id() ));
1251 2476 : FD_LOG_INFO(( "fd_log: --log-thread %s", fd_log_thread() ));
1252 2476 : FD_LOG_INFO(( "fd_log: --log-host-id %lu", fd_log_host_id() ));
1253 2476 : FD_LOG_INFO(( "fd_log: --log-host %s", fd_log_host() ));
1254 2476 : FD_LOG_INFO(( "fd_log: --log-cpu-id %lu", fd_log_cpu_id() ));
1255 2476 : FD_LOG_INFO(( "fd_log: --log-cpu %s", fd_log_cpu() ));
1256 2476 : FD_LOG_INFO(( "fd_log: --log-group-id %lu", fd_log_group_id() ));
1257 2476 : FD_LOG_INFO(( "fd_log: --log-group %s", fd_log_group() ));
1258 2476 : FD_LOG_INFO(( "fd_log: --log-tid %lu", fd_log_tid() ));
1259 2476 : FD_LOG_INFO(( "fd_log: --log-user-id %lu", fd_log_user_id() ));
1260 2476 : FD_LOG_INFO(( "fd_log: --log-user %s", fd_log_user() ));
1261 :
1262 2476 : FD_LOG_INFO(( "fd_log: boot success" ));
1263 2476 : }
1264 :
1265 : void
1266 : fd_log_private_boot_custom( int * lock,
1267 : ulong app_id,
1268 : char const * app,
1269 : ulong thread_id,
1270 : char const * thread,
1271 : ulong host_id,
1272 : char const * host,
1273 : ulong cpu_id,
1274 : char const * cpu,
1275 : ulong group_id,
1276 : char const * group,
1277 : ulong tid,
1278 : ulong user_id,
1279 : char const * user,
1280 : int dedup,
1281 : int colorize,
1282 : int level_logfile,
1283 : int level_stderr,
1284 : int level_flush,
1285 : int level_core,
1286 : int log_fd,
1287 0 : char const * log_path ) {
1288 0 : fd_log_private_shared_lock = lock;
1289 :
1290 0 : fd_log_private_app_id_set( app_id );
1291 0 : fd_log_private_app_set( app );
1292 0 : fd_log_private_thread_id_set( thread_id );
1293 0 : fd_log_thread_set( thread );
1294 0 : fd_log_private_host_id_set( host_id );
1295 0 : fd_log_private_host_set( host );
1296 0 : fd_log_private_cpu_id_set( cpu_id );
1297 0 : fd_log_cpu_set( cpu );
1298 0 : fd_log_private_group_id_set( group_id );
1299 0 : fd_log_private_group_set( group );
1300 0 : fd_log_private_tid_set( tid );
1301 0 : fd_log_private_user_id_set( user_id );
1302 0 : fd_log_private_user_set( user );
1303 :
1304 0 : fd_log_private_dedup = dedup;
1305 0 : fd_log_colorize_set( colorize );
1306 :
1307 0 : fd_log_level_logfile_set( level_logfile );
1308 0 : fd_log_level_stderr_set ( level_stderr );
1309 0 : fd_log_level_flush_set ( level_flush );
1310 0 : fd_log_level_core_set ( level_core );
1311 :
1312 : /* Hook up the permanent log */
1313 0 : if( -1!=log_fd ) {
1314 : /* Log file descriptor was set up before boot, user wishes to log there directly. */
1315 0 : fd_log_private_path[0] = '\0';
1316 0 : FD_VOLATILE( fd_log_private_fileno ) = log_fd;
1317 0 : } else {
1318 0 : FD_VOLATILE( fd_log_private_fileno ) = fd_log_private_open_path( 0, log_path );
1319 0 : }
1320 :
1321 0 : if( FD_UNLIKELY( fd_log_private_signal_handler ) ) {
1322 : /* See note above about needing to prime backtrace */
1323 0 : void * btrace[128];
1324 0 : (void)backtrace( btrace, 128 );
1325 :
1326 : /* This is all overridable POSIX sigs whose default behavior is to
1327 : abort the program. It will backtrace and then fallback to the
1328 : default behavior. */
1329 0 : fd_log_private_sig_trap( SIGABRT );
1330 0 : fd_log_private_sig_trap( SIGALRM );
1331 0 : fd_log_private_sig_trap( SIGFPE );
1332 0 : fd_log_private_sig_trap( SIGHUP );
1333 0 : fd_log_private_sig_trap( SIGILL );
1334 0 : fd_log_private_sig_trap( SIGQUIT );
1335 0 : fd_log_private_sig_trap( SIGPIPE );
1336 0 : fd_log_private_sig_trap( SIGSEGV );
1337 0 : fd_log_private_sig_trap( SIGUSR1 );
1338 0 : fd_log_private_sig_trap( SIGUSR2 );
1339 0 : fd_log_private_sig_trap( SIGBUS );
1340 0 : fd_log_private_sig_trap( SIGPOLL );
1341 0 : fd_log_private_sig_trap( SIGPROF );
1342 0 : fd_log_private_sig_trap( SIGSYS );
1343 0 : fd_log_private_sig_trap( SIGTRAP );
1344 0 : fd_log_private_sig_trap( SIGVTALRM );
1345 0 : fd_log_private_sig_trap( SIGXCPU );
1346 0 : fd_log_private_sig_trap( SIGXFSZ );
1347 0 : }
1348 :
1349 : /* At this point, logging online */
1350 0 : if( fd_log_build_info_sz>1UL ) FD_LOG_INFO(( "fd_log: build info:\n%s", fd_log_build_info ));
1351 0 : else FD_LOG_INFO(( "fd_log: build info not available" ));
1352 0 : FD_LOG_INFO(( "fd_log: --log-path %s", fd_log_private_path ));
1353 0 : FD_LOG_INFO(( "fd_log: --log-dedup %i", fd_log_private_dedup ));
1354 0 : FD_LOG_INFO(( "fd_log: --log-colorize %i", fd_log_colorize() ));
1355 0 : FD_LOG_INFO(( "fd_log: --log-level-logfile %i", fd_log_level_logfile() ));
1356 0 : FD_LOG_INFO(( "fd_log: --log-level-logfile %i", fd_log_level_logfile() ));
1357 0 : FD_LOG_INFO(( "fd_log: --log-level-stderr %i", fd_log_level_stderr() ));
1358 0 : FD_LOG_INFO(( "fd_log: --log-level-flush %i", fd_log_level_flush() ));
1359 0 : FD_LOG_INFO(( "fd_log: --log-level-core %i", fd_log_level_core() ));
1360 0 : FD_LOG_INFO(( "fd_log: --log-app-id %lu", fd_log_app_id() ));
1361 0 : FD_LOG_INFO(( "fd_log: --log-app %s", fd_log_app() ));
1362 0 : FD_LOG_INFO(( "fd_log: --log-thread-id %lu", fd_log_thread_id() ));
1363 0 : FD_LOG_INFO(( "fd_log: --log-thread %s", fd_log_thread() ));
1364 0 : FD_LOG_INFO(( "fd_log: --log-host-id %lu", fd_log_host_id() ));
1365 0 : FD_LOG_INFO(( "fd_log: --log-host %s", fd_log_host() ));
1366 0 : FD_LOG_INFO(( "fd_log: --log-cpu-id %lu", fd_log_cpu_id() ));
1367 0 : FD_LOG_INFO(( "fd_log: --log-cpu %s", fd_log_cpu() ));
1368 0 : FD_LOG_INFO(( "fd_log: --log-group-id %lu", fd_log_group_id() ));
1369 0 : FD_LOG_INFO(( "fd_log: --log-group %s", fd_log_group() ));
1370 0 : FD_LOG_INFO(( "fd_log: --log-tid %lu", fd_log_tid() ));
1371 0 : FD_LOG_INFO(( "fd_log: --log-user-id %lu", fd_log_user_id() ));
1372 0 : FD_LOG_INFO(( "fd_log: --log-user %s", fd_log_user() ));
1373 :
1374 0 : FD_LOG_INFO(( "fd_log: boot success" ));
1375 0 : }
1376 :
1377 : void
1378 1324 : fd_log_private_halt( void ) {
1379 1324 : FD_LOG_INFO(( "fd_log: halting" ));
1380 :
1381 1324 : fd_log_private_cleanup();
1382 :
1383 : /* At this point, log is offline */
1384 :
1385 1324 : fd_log_private_path[0] = '\0';
1386 : //fd_log_private_fileno = -1; /* Already handled by cleanup */
1387 1324 : fd_log_private_dedup = 0;
1388 :
1389 1324 : fd_log_private_level_core = 0;
1390 1324 : fd_log_private_level_flush = 0;
1391 1324 : fd_log_private_level_stderr = 0;
1392 1324 : fd_log_private_level_logfile = 0;
1393 1324 : fd_log_private_colorize = 0;
1394 :
1395 1324 : fd_log_private_clock_func = fd_log_wallclock_host;
1396 1324 : fd_log_private_clock_args = NULL;
1397 :
1398 1324 : fd_log_private_user[0] = '\0';
1399 1324 : fd_log_private_user_id_init = 0;
1400 1324 : fd_log_private_user_id = 0UL;
1401 1324 : fd_log_private_tid_init = 0;
1402 1324 : fd_log_private_tid = 0UL;
1403 1324 : fd_log_private_group[0] = '\0';
1404 1324 : fd_log_private_group_id = 0UL;
1405 :
1406 1324 : fd_log_private_cpu_init = 0;
1407 1324 : fd_log_private_cpu[0] = '\0';
1408 1324 : fd_log_private_cpu_id_init = 0;
1409 1324 : fd_log_private_cpu_id = 0UL;
1410 1324 : fd_log_private_host[0] = '\0';
1411 1324 : fd_log_private_host_id = 0UL;
1412 :
1413 1324 : fd_log_private_thread_init = 0;
1414 1324 : fd_log_private_thread[0] = '\0';
1415 1324 : fd_log_private_thread_id_init = 0;
1416 1324 : fd_log_private_thread_id = 0UL;
1417 1324 : # if FD_HAS_THREADS
1418 1324 : fd_log_private_thread_id_ctr = 0UL;
1419 1324 : # endif
1420 1324 : fd_log_private_app[0] = '\0';
1421 1324 : fd_log_private_app_id = 0UL;
1422 :
1423 1324 : if( FD_LIKELY( fd_log_private_shared_lock!=fd_log_private_shared_lock_local ) ) {
1424 : /* Note: the below will not unmap this in any clones that also
1425 : inherited this mapping unless they were cloned with CLONE_VM. In
1426 : cases like this, the caller is expected to handle the cleanup
1427 : semantics sanely (e.g. only have the parent do boot/halt and then
1428 : children only use log while parent has log booted). */
1429 0 : if( FD_UNLIKELY( munmap( fd_log_private_shared_lock, sizeof(int) ) ) )
1430 0 : fd_log_private_fprintf_0( STDERR_FILENO,
1431 0 : "munmap( fd_log_private_shared_lock, sizeof(int) ) failed (%i-%s); attempting to continue",
1432 0 : errno, fd_io_strerror( errno ) );
1433 0 : }
1434 1324 : fd_log_private_shared_lock = NULL;
1435 :
1436 : //FD_LOG_INFO(( "fd_log: halt success" )); /* Log not online anymore */
1437 1324 : }
1438 :
1439 : #include <sys/resource.h>
1440 :
1441 : ulong
1442 2476 : fd_log_private_main_stack_sz( void ) {
1443 :
1444 : /* We are extra paranoid about what rlimit returns and we don't trust
1445 : environments that claim an unlimited stack size (because it just
1446 : isn't unlimited ... even if rlimit says otherwise ... which it will
1447 : if a user tries to be clever with a "ulimit -s unlimited" ... e.g.
1448 : tile0's stack highest address is at 128 TiB-4KiB typically on
1449 : modern Linux and grows down while the text / data / heap grow up
1450 : from 0B ... so stack size is practically always going to be << 128
1451 : TiB irrespective of any getrlimit claim). TODO: It looks like
1452 : pthead_attr_getstack might be getrlimit based under the hood, so
1453 : maybe just use pthread_attr_getstack here too? */
1454 :
1455 2476 : struct rlimit rlim[1];
1456 2476 : int err = getrlimit( RLIMIT_STACK, rlim );
1457 2476 : if( FD_UNLIKELY( err ) ) {
1458 0 : FD_LOG_WARNING(( "fd_log: getrlimit failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1459 0 : return 0UL;
1460 0 : }
1461 :
1462 2476 : ulong stack_sz = (ulong)rlim->rlim_cur;
1463 2476 : if( FD_UNLIKELY( (rlim->rlim_cur>rlim->rlim_max) | (rlim->rlim_max>RLIM_INFINITY ) |
1464 2476 : (rlim->rlim_cur==RLIM_INFINITY) | (rlim->rlim_cur!=(rlim_t)stack_sz) ) ) {
1465 0 : FD_LOG_WARNING(( "fd_log: unexpected stack limits (rlim_cur %lu, rlim_max %lu)",
1466 0 : (ulong)rlim->rlim_cur, (ulong)rlim->rlim_max ));
1467 0 : return 0UL;
1468 0 : }
1469 :
1470 2476 : return stack_sz;
1471 2476 : }
1472 :
1473 : /* When pthread_setstack is not used to explicitly set the memory region
1474 : for a new thread's stack, pthread_create will create a memory region
1475 : (using either the requested size or a default size). And, while
1476 : pthread allows us to set and get the size of the stack region it
1477 : creates and we can get a pointer into a thread's stack by just
1478 : declaring a stack variable in that thread and we obviously know where
1479 : a thread's stack is when we explicitly specify it to pthread create,
1480 : pthreads does not seem to provide a simple way to get the extents of
1481 : the stacks it creates.
1482 :
1483 : But the relationship between a pointer in the stack and the stack
1484 : extents is non-trival because pthreads will use some of the stack for
1485 : things like thread local storage (and it will not tell us how much
1486 : stack was used by that and this is practically only known after
1487 : linking is complete and then it is not simply exposed to the
1488 : application).
1489 :
1490 : Similar uncertainty applies to the first thread's stack. We can
1491 : learn how large the stack is and get a pointer into the stack via a
1492 : stack variable but we have no simple way to get the extents. And, in
1493 : this case on recent Linux, things like command line strings and
1494 : environment strings are typically allowed to consume up to 1/4 of
1495 : main's thread stack ... these are only known at application load
1496 : time. (There is the additional caveat that the main stack might be
1497 : dynamically allocated such that the address space reserved for it
1498 : might not be backed by memory yet.)
1499 :
1500 : But, if we want to do useful run-time stack diagnostics (e.g. alloca
1501 : bounds checking / stack overflow prevention / etc), having explicit
1502 : knowledge of a thread's stack extents is very useful. Hence the
1503 : below. It would be nice if there was portable and non-horrific way
1504 : to do this (an even more horrific way is trigger seg faults by
1505 : scanning for the guard pages and then recover from the seg fault via
1506 : a longjmp ... shivers). */
1507 :
1508 : void
1509 : fd_log_private_stack_discover( ulong stack_sz,
1510 : ulong * _stack0,
1511 2476 : ulong * _stack1 ) {
1512 :
1513 2476 : if( FD_UNLIKELY( !stack_sz ) ) {
1514 0 : *_stack0 = 0UL;
1515 0 : *_stack1 = 0UL;
1516 0 : return;
1517 0 : }
1518 :
1519 2476 : ulong stack0 = 0UL;
1520 2476 : ulong stack1 = 0UL;
1521 :
1522 : /* Create a variable on the caller's stack and scan the thread group's
1523 : memory map for the memory region holding the variable. That should
1524 : be the caller's stack. */
1525 :
1526 2476 : uchar stack_mem[1];
1527 2476 : FD_VOLATILE( stack_mem[0] ) = (uchar)1; /* Paranoia to make sure compiler puts this in stack */
1528 2476 : ulong stack_addr = (ulong)stack_mem;
1529 :
1530 2476 : int filefd;
1531 2476 : if( FD_UNLIKELY( ( filefd = open( "/proc/self/maps", O_RDONLY ) ) < 0 ) ) {
1532 0 : FD_LOG_WARNING(( "open( \"/proc/self/maps\" ) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1533 0 : *_stack0 = 0UL;
1534 0 : *_stack1 = 0UL;
1535 0 : return;
1536 0 : }
1537 :
1538 2476 : char filebuf[1<<12];
1539 2476 : ulong filelen = 0;
1540 2476 : char * p = filebuf;
1541 2476 : int found = 0;
1542 94505 : while( !found ) {
1543 :
1544 : /* Scan a line */
1545 :
1546 94505 : int full_line = 0;
1547 94505 : char * nextp;
1548 9725193 : for( nextp = p; nextp < filebuf + filelen; ) {
1549 9722708 : if( *(nextp++) == '\n' ) {
1550 92020 : full_line = 1;
1551 92020 : break;
1552 92020 : }
1553 9722708 : }
1554 94505 : if( !full_line ) {
1555 : /* We need to read more data. First shift the old data down. */
1556 2485 : filelen = (ulong)((filebuf + filelen) - p);
1557 2485 : if( filelen )
1558 0 : memmove( filebuf, p, filelen );
1559 2485 : p = filebuf;
1560 : /* Now read data */
1561 2485 : ssize_t tlen;
1562 2485 : if( FD_UNLIKELY( ( tlen = read( filefd, filebuf + filelen, sizeof(filebuf)-1U-filelen ) ) < 0 ) ) {
1563 0 : FD_LOG_WARNING(( "read( \"/proc/self/maps\" ) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1564 0 : break;
1565 0 : }
1566 2485 : if( tlen == 0 ) break; /* End of file */
1567 2485 : filelen += (ulong)tlen;
1568 2485 : filebuf[filelen] = '\0'; /* For sscanf */
1569 2485 : continue;
1570 2485 : }
1571 :
1572 92020 : ulong m0;
1573 92020 : ulong m1;
1574 92020 : int r = sscanf( p, "%lx-%lx", &m0, &m1 );
1575 92020 : p = nextp;
1576 92020 : if( FD_UNLIKELY( r !=2 ) ) continue;
1577 :
1578 : /* Test if the stack allocation is in the discovered region */
1579 :
1580 92020 : if( FD_UNLIKELY( (m0<=stack_addr) & (stack_addr<m1) ) ) {
1581 2476 : found = 1;
1582 2476 : ulong msz = m1 - m0;
1583 2476 : if( msz==stack_sz ) { /* Memory region matches expectations */
1584 0 : stack0 = m0;
1585 0 : stack1 = m1;
1586 2476 : } else if( ((fd_log_group_id()==fd_log_tid()) & (msz<stack_sz)) ) {
1587 : /* This is the main thread, which, on recent Linux, seems to
1588 : just reserve address space for main's stack at program
1589 : start up to the application stack size limits then uses
1590 : page faults to dynamically back the stack with DRAM as the
1591 : stack grows (which is awful for performance, jitter and
1592 : reliability ... sigh). This assumes stack grows down such
1593 : that m1 is the fixed value in this process. */
1594 2476 : stack0 = m1 - stack_sz;
1595 2476 : stack1 = m1;
1596 2476 : } else {
1597 0 : FD_LOG_WARNING(( "unexpected caller stack memory region size (got %lu bytes, expected %lu bytes)", msz, stack_sz ));
1598 : /* don't trust the discovered region */
1599 0 : }
1600 2476 : break;
1601 2476 : }
1602 :
1603 92020 : }
1604 2476 : if( !found )
1605 0 : FD_LOG_WARNING(( "unable to find stack size around address 0x%lx", stack_addr ));
1606 :
1607 2476 : close(filefd);
1608 :
1609 2476 : *_stack0 = stack0;
1610 2476 : *_stack1 = stack1;
1611 2476 : }
1612 :
1613 : #else
1614 : #error "Unknown FD_LOG_STYLE"
1615 : #endif
|