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 : #if !defined(FD_HAS_BACKTRACE)
12 : #if __has_include( <execinfo.h> ) && !FD_HAS_ASAN && !FD_HAS_MSAN && FD_HAS_HOSTED
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 78016 : #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 9752 : #define TEXT_BLUE "\033[34m"
83 9752 : #define TEXT_GREEN "\033[32m"
84 9752 : #define TEXT_YELLOW "\033[93m"
85 39008 : #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 2555 : void fd_log_private_app_id_set( ulong app_id ) { fd_log_private_app_id = app_id; }
94 :
95 18005182 : 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 2555 : fd_log_private_app_set( char const * app ) {
103 2555 : if( FD_UNLIKELY( !app ) ) app = "[app]";
104 2555 : if( FD_LIKELY( app!=fd_log_private_app ) )
105 2555 : fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( fd_log_private_app ), app, FD_LOG_NAME_MAX-1UL ) );
106 2555 : }
107 :
108 1825665 : 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 2594 : fd_log_private_thread_id_next( void ) {
117 2594 : return FD_ATOMIC_FETCH_AND_ADD( &fd_log_private_thread_id_ctr, 1UL );
118 2594 : }
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 2594 : fd_log_private_thread_id_set( ulong thread_id ) {
126 2594 : fd_log_private_thread_id = thread_id;
127 2594 : fd_log_private_thread_id_init = 1;
128 2594 : }
129 :
130 : ulong
131 36007803 : fd_log_thread_id( void ) {
132 36007803 : # if FD_HAS_THREADS
133 36007803 : 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 36007803 : return fd_log_private_thread_id;
138 36007803 : }
139 :
140 : /* Thread */
141 :
142 : /* Initialize name to a reasonable default thread description (FIXME:
143 : WEEN THIS OFF STDLIB) */
144 :
145 : static void
146 2603 : fd_log_private_thread_default( char * name ) { /* FD_LOG_NAME_MAX bytes */
147 2603 : sprintf( name, "%lu", fd_log_thread_id() );
148 2603 : }
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 2603 : fd_log_thread_set( char const * thread ) {
155 2603 : if( FD_UNLIKELY( !thread ) || FD_UNLIKELY( thread[0]=='\0') ) {
156 2603 : fd_log_private_thread_default( fd_log_private_thread );
157 2603 : fd_log_private_thread_init = 1;
158 2603 : } 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 2603 : }
163 :
164 : char const *
165 2893052 : fd_log_thread( void ) {
166 2893052 : if( FD_UNLIKELY( !fd_log_private_thread_init ) ) fd_log_thread_set( NULL );
167 2893052 : return fd_log_private_thread;
168 2893052 : }
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 2555 : void fd_log_private_host_id_set( ulong host_id ) { fd_log_private_host_id = host_id; }
177 :
178 5146 : 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 1825665 : char const * fd_log_host( void ) { return fd_log_private_host; }
185 :
186 : void
187 2555 : fd_log_private_host_set( char const * host ) {
188 2555 : if( FD_UNLIKELY( !host ) || FD_UNLIKELY( host[0]=='\0') ) host = "[host]";
189 2555 : if( FD_LIKELY( host!=fd_log_private_host ) )
190 2555 : fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( fd_log_private_host ), host, FD_LOG_NAME_MAX-1UL ) );
191 2555 : }
192 :
193 : /* CPU ID */
194 :
195 : /* First CPU scheduled to run on or ULONG_MAX on failure */
196 :
197 : ulong
198 2555 : fd_log_private_cpu_id_default( void ) {
199 2555 : FD_CPUSET_DECL( cpu_set );
200 2555 : if( FD_UNLIKELY( fd_cpuset_getaffinity( (pid_t)0, cpu_set ) ) ) return ULONG_MAX;
201 2555 : ulong idx = fd_cpuset_first( cpu_set );
202 2555 : idx = fd_ulong_if( idx<FD_TILE_MAX, idx, ULONG_MAX );
203 2555 : return idx;
204 2555 : }
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 2564 : fd_log_private_cpu_id_set( ulong cpu_id ) {
211 2564 : fd_log_private_cpu_id = cpu_id;
212 2564 : fd_log_private_cpu_id_init = 1;
213 2564 : }
214 :
215 : ulong
216 18002657 : fd_log_cpu_id( void ) {
217 18002657 : if( FD_UNLIKELY( !fd_log_private_cpu_id_init ) ) fd_log_private_cpu_id_set( fd_log_private_cpu_id_default() );
218 18002657 : return fd_log_private_cpu_id;
219 18002657 : }
220 :
221 : /* CPU */
222 :
223 : /* Initialize name to a reasonable default CPU description (FIXME: WEEN
224 : THIS OFF STDLIB) */
225 :
226 : static void
227 2603 : fd_log_private_cpu_default( char * name ) { /* FD_LOG_NAME_MAX bytes */
228 2603 : FD_CPUSET_DECL( set );
229 :
230 2603 : int err = fd_cpuset_getaffinity( (pid_t)0, set );
231 2603 : if( FD_UNLIKELY( err ) ) { sprintf( name, "e%i", err ); return; }
232 :
233 2603 : ulong cnt = fd_cpuset_cnt( set );
234 2603 : if( FD_UNLIKELY( !((0UL<cnt) & (cnt<=FD_TILE_MAX)) ) ) { sprintf( name, "ec" ); return; }
235 :
236 2603 : ulong idx = fd_cpuset_first( set );
237 2603 : sprintf( name, (cnt>1) ? "f%lu" : "%lu", idx );
238 2603 : }
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 2603 : fd_log_cpu_set( char const * cpu ) {
245 2603 : if( FD_UNLIKELY( !cpu ) || FD_UNLIKELY( cpu[0]=='\0') ) {
246 2603 : fd_log_private_cpu_default( fd_log_private_cpu );
247 2603 : fd_log_private_cpu_init = 1;
248 2603 : } 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 2603 : }
253 :
254 : char const *
255 2893048 : fd_log_cpu( void ) {
256 2893048 : if( FD_UNLIKELY( !fd_log_private_cpu_init ) ) fd_log_cpu_set( NULL );
257 2893048 : return fd_log_private_cpu;
258 2893048 : }
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 2555 : void fd_log_private_group_id_set( ulong group_id ) { fd_log_private_group_id = group_id; }
267 :
268 313142132 : 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 1825664 : char const * fd_log_group( void ) { return fd_log_private_group; }
275 :
276 : void
277 2555 : fd_log_private_group_set( char const * group ) {
278 2555 : if( FD_UNLIKELY( !group ) || FD_UNLIKELY( group[0]=='\0') ) group = "[group]";
279 2555 : if( FD_LIKELY( group!=fd_log_private_group ) )
280 2555 : fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( fd_log_private_group ), group, FD_LOG_NAME_MAX-1UL ) );
281 2555 : }
282 :
283 : /* System TID or ULONG_MAX on failure */
284 :
285 : ulong
286 2594 : fd_log_private_tid_default( void ) {
287 2594 : # if defined(__linux__)
288 2594 : long tid = syscall( SYS_gettid );
289 : # else
290 : long tid = getpid();
291 : # endif
292 2594 : return fd_ulong_if( tid>0L, (ulong)tid, ULONG_MAX );
293 2594 : }
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 2594 : fd_log_private_tid_set( ulong tid ) {
300 2594 : fd_log_private_tid = tid;
301 2594 : fd_log_private_tid_init = 1;
302 2594 : }
303 :
304 : ulong
305 2895565 : fd_log_tid( void ) {
306 2895565 : if( FD_UNLIKELY( !fd_log_private_tid_init ) ) fd_log_private_tid_set( fd_log_private_tid_default() );
307 2895565 : return fd_log_private_tid;
308 2895565 : }
309 :
310 : /* User id */
311 :
312 : static ulong
313 2555 : fd_log_private_user_id_default( void ) {
314 2555 : return (ulong)getuid(); /* POSIX spec seems ambiguous as to whether or not this is a signed type */
315 2555 : }
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 2555 : fd_log_private_user_id_set( ulong user_id ) {
322 2555 : fd_log_private_user_id = user_id;
323 2555 : fd_log_private_user_id_init = 1;
324 2555 : }
325 :
326 : ulong
327 2591 : fd_log_user_id( void ) {
328 2591 : 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 2591 : return fd_log_private_user_id;
333 2591 : }
334 :
335 : /* User */
336 :
337 : static char fd_log_private_user[ FD_LOG_NAME_MAX ]; /* "" outside boot/halt, init on boot */
338 :
339 1825666 : char const * fd_log_user( void ) { return fd_log_private_user; }
340 :
341 : void
342 2555 : fd_log_private_user_set( char const * user ) {
343 2555 : if( FD_UNLIKELY( !user ) || FD_UNLIKELY( user[0]=='\0') ) user = "[user]";
344 2555 : if( FD_LIKELY( user!=fd_log_private_user ) )
345 2555 : fd_cstr_fini( fd_cstr_append_cstr_safe( fd_cstr_init( fd_log_private_user ), user, FD_LOG_NAME_MAX-1UL ) );
346 2555 : }
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 298203421 : fd_log_wallclock_host( void const * _ ) {
363 298203421 : (void)_;
364 298203421 : struct timespec ts[1];
365 298203421 : clock_gettime( CLOCK_REALTIME, ts );
366 298203421 : return ((long)1e9)*((long)ts->tv_sec) + (long)ts->tv_nsec;
367 298203421 : }
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 296982905 : fd_log_wallclock( void ) {
374 296982905 : return fd_log_private_clock_func( fd_log_private_clock_args );
375 296982905 : }
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 1830022 : char * buf ) {
387 1830022 : uint YYYY;
388 1830022 : uint MM;
389 1830022 : uint DD;
390 1830022 : uint hh;
391 1830022 : uint mm;
392 1830022 : ulong ns;
393 1830022 : int tz;
394 :
395 1830022 : static long const ns_per_m = 60000000000L;
396 1830022 : static long const ns_per_s = 1000000000L;
397 :
398 1830022 : static FD_TL long now_ref = 1262325600000000000L; /* 2010-01-01 00:00:00.000000000 GMT-06 */
399 1830022 : static FD_TL uint YYYY_ref = 2010U; /* Initialized to what now0 corresponds to */
400 1830022 : static FD_TL uint MM_ref = 1U; /* " */
401 1830022 : static FD_TL uint DD_ref = 1U; /* " */
402 1830022 : static FD_TL uint hh_ref = 0U; /* " */
403 1830022 : static FD_TL uint mm_ref = 0U; /* " */
404 1830022 : static FD_TL int tz_ref = -6; /* " */
405 :
406 1830022 : 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 1827380 : YYYY = YYYY_ref;
412 1827380 : MM = MM_ref;
413 1827380 : DD = DD_ref;
414 1827380 : hh = hh_ref;
415 1827380 : mm = mm_ref;
416 1827380 : ns = (ulong)(now - now_ref);
417 1827380 : tz = tz_ref;
418 :
419 1827380 : } else {
420 :
421 2642 : long _t = now / ns_per_s;
422 2642 : long _ns = now - ns_per_s*_t;
423 2642 : if( _ns<0L ) _ns += ns_per_s, _t--;
424 2642 : time_t t = (time_t)_t;
425 :
426 2642 : struct tm tm[1];
427 2642 : static FD_TL int localtime_broken = 0;
428 2642 : if( FD_UNLIKELY( !localtime_broken && !localtime_r( &t, tm ) ) ) localtime_broken = 1;
429 2642 : 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 2642 : YYYY = (uint)(1900+tm->tm_year);
438 2642 : MM = (uint)( 1+tm->tm_mon );
439 2642 : DD = (uint)tm->tm_mday;
440 2642 : hh = (uint)tm->tm_hour;
441 2642 : mm = (uint)tm->tm_min;
442 2642 : ns = ((ulong)((uint)tm->tm_sec))*((ulong)ns_per_s) + ((ulong)_ns);
443 2642 : # if defined(__linux__)
444 2642 : tz = (int)(-timezone/3600L+(long)tm->tm_isdst);
445 : # else
446 : tz = 0;
447 : # endif
448 :
449 2642 : now_ref = now - (long)ns;
450 2642 : YYYY_ref = YYYY;
451 2642 : MM_ref = MM;
452 2642 : DD_ref = DD;
453 2642 : hh_ref = hh;
454 2642 : mm_ref = mm;
455 2642 : tz_ref = tz;
456 :
457 2642 : }
458 :
459 : /* Note: These can all run in parallel! */
460 1830022 : fd_cstr_append_uint_as_text ( buf, '0', '\0', YYYY, 4UL );
461 1830022 : fd_cstr_append_char ( buf+ 4, '-' );
462 1830022 : fd_cstr_append_uint_as_text ( buf+ 5, '0', '\0', MM, 2UL );
463 1830022 : fd_cstr_append_char ( buf+ 7, '-' );
464 1830022 : fd_cstr_append_uint_as_text ( buf+ 8, '0', '\0', DD, 2UL );
465 1830022 : fd_cstr_append_char ( buf+10, ' ' );
466 1830022 : fd_cstr_append_uint_as_text ( buf+11, '0', '\0', hh, 2UL );
467 1830022 : fd_cstr_append_char ( buf+13, ':' );
468 1830022 : fd_cstr_append_uint_as_text ( buf+14, '0', '\0', mm, 2UL );
469 1830022 : fd_cstr_append_char ( buf+16, ':' );
470 1830022 : fd_cstr_append_fxp10_as_text( buf+17, '0', '\0', 9UL, ns, 12UL );
471 1830022 : fd_cstr_append_text ( buf+29, " GMT", 4UL );
472 1830022 : fd_cstr_append_char ( buf+33, fd_char_if( tz<0, '-', '+' ) );
473 1830022 : fd_cstr_append_uint_as_text ( buf+34, '0', '\0', fd_int_abs( tz ), 2UL );
474 1830022 : fd_cstr_append_char ( buf+36, '\0' );
475 1830022 : return buf;
476 1830022 : }
477 :
478 : long
479 446 : fd_log_sleep( long dt ) {
480 446 : 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 446 : long ns_dt = fd_long_min( dt, (((long)1e9)<<31)-1L ); /* in [1,2^31*1e9) and <= dt at this point */
487 446 : dt -= ns_dt;
488 :
489 446 : struct timespec req[1];
490 446 : struct timespec rem[1];
491 446 : req->tv_sec = (time_t)( ((ulong)ns_dt) / ((ulong)1e9) ); /* in [0,2^31-1] */
492 446 : req->tv_nsec = (long) ( ((ulong)ns_dt) % ((ulong)1e9) ); /* in [0,1e9) */
493 446 : int sleep_res;
494 446 : #if defined(__linux__)
495 : /* Always use clock_nanosleep on Linux to be predictable */
496 446 : sleep_res = clock_nanosleep( CLOCK_REALTIME, 0, req, rem );
497 : #else
498 : sleep_res = nanosleep( req, rem );
499 : #endif
500 446 : if( FD_UNLIKELY( sleep_res ) && FD_LIKELY( errno==EINTR ) ) dt += ((long)1e9)*((long)rem->tv_sec) + rem->tv_nsec;
501 446 : return dt;
502 446 : }
503 :
504 : long
505 3 : fd_log_wait_until( long then ) {
506 3 : long now;
507 99082 : for(;;) {
508 99082 : now = fd_log_wallclock();
509 99082 : long rem = then - now;
510 99082 : if( FD_LIKELY( rem<=0L ) ) break; /* we've waited long enough */
511 99079 : 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 99079 : 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 99079 : if( FD_UNLIKELY( rem>(long)1e3 ) ) { /* medium short wait (over ~1 us) ... hyperthreading friendly spin */
520 99043 : FD_SPIN_PAUSE();
521 99043 : continue;
522 99043 : }
523 : /* short wait ... spin on fd_log_wallclock */
524 99079 : }
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 4170 : fd_log_flush( void ) {
536 4170 : int log_fileno = FD_VOLATILE_CONST( fd_log_private_fileno );
537 4170 : if( FD_LIKELY( log_fileno!=-1 ) ) fsync( log_fileno );
538 4170 : }
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 2555 : int fd_log_colorize ( void ) { return FD_VOLATILE_CONST( fd_log_private_colorize ); }
549 3027025 : int fd_log_level_logfile( void ) { return FD_VOLATILE_CONST( fd_log_private_level_logfile ); }
550 2894090 : int fd_log_level_stderr ( void ) { return FD_VOLATILE_CONST( fd_log_private_level_stderr ); }
551 1832271 : int fd_log_level_flush ( void ) { return FD_VOLATILE_CONST( fd_log_private_level_flush ); }
552 4805 : int fd_log_level_core ( void ) { return FD_VOLATILE_CONST( fd_log_private_level_core ); }
553 :
554 2555 : void fd_log_colorize_set ( int mode ) { FD_VOLATILE( fd_log_private_colorize ) = mode; }
555 2561 : void fd_log_level_logfile_set( int level ) { FD_VOLATILE( fd_log_private_level_logfile ) = level; }
556 2564 : void fd_log_level_stderr_set ( int level ) { FD_VOLATILE( fd_log_private_level_stderr ) = level; }
557 2555 : void fd_log_level_flush_set ( int level ) { FD_VOLATILE( fd_log_private_level_flush ) = level; }
558 2567 : 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 9709762 : #define FD_LOG_BUF_SZ (32UL*4096UL)
569 :
570 : /* File descriptor to restore when logging a message that will terminate
571 : the application, incase it was redirected to some other consumer in
572 : the process which will not be able to process it in time */
573 : static int fd_log_private_stderr_fileno = STDERR_FILENO;
574 : int fd_log_private_restore_stderr = STDERR_FILENO;
575 :
576 : __attribute__((no_sanitize_address)) void
577 : fd_log_private_fprintf_0( int fd,
578 1837978 : 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 : - Allow partial write to fd_io_write? (e.g. src_min=0 such that
592 : the fd_io_write below is guaranteed to be a single system call) */
593 :
594 1837978 : char msg[ FD_LOG_BUF_SZ ];
595 :
596 1837978 : va_list ap;
597 1837978 : va_start( ap, fmt );
598 1837978 : int len = vsnprintf( msg, FD_LOG_BUF_SZ, fmt, ap );
599 1837978 : if( len<0 ) len = 0; /* cmov */
600 1837978 : if( len>(int)(FD_LOG_BUF_SZ-1UL) ) len = (int)(FD_LOG_BUF_SZ-1UL); /* cmov */
601 1837978 : msg[ len ] = '\0';
602 1837978 : va_end( ap );
603 :
604 1837978 : ulong wsz;
605 1837978 : fd_io_write( fd, msg, (ulong)len, (ulong)len, &wsz ); /* Note: we ignore errors because what are we doing to do? log them? */
606 :
607 1837978 : }
608 :
609 : /* Log buffer used by fd_log_private_0 and fd_log_private_hexdump_msg */
610 :
611 : static FD_TL char fd_log_private_log_msg[ FD_LOG_BUF_SZ ];
612 :
613 : char const *
614 3016903 : fd_log_private_0( char const * fmt, ... ) {
615 3016903 : va_list ap;
616 3016903 : va_start( ap, fmt );
617 3016903 : int len = vsnprintf( fd_log_private_log_msg, FD_LOG_BUF_SZ, fmt, ap );
618 3016903 : if( len<0 ) len = 0; /* cmov */
619 3016903 : if( len>(int)(FD_LOG_BUF_SZ-1UL) ) len = (int)(FD_LOG_BUF_SZ-1UL); /* cmov */
620 3016903 : fd_log_private_log_msg[ len ] = '\0';
621 3016903 : va_end( ap );
622 3016903 : return fd_log_private_log_msg;
623 3016903 : }
624 :
625 : char const *
626 : fd_log_private_hexdump_msg( char const * descr,
627 : void const * mem,
628 6453 : ulong sz ) {
629 :
630 3162357 : # define FD_LOG_HEXDUMP_BYTES_PER_LINE (16UL)
631 6453 : # define FD_LOG_HEXDUMP_BLOB_DESCRIPTION_MAX_LEN (32UL)
632 6453 : # define FD_LOG_HEXDUMP_MAX_INPUT_BLOB_SZ (1664UL) /* multiple of 128 >= 1542 */
633 :
634 3563298 : # define FD_LOG_HEXDUMP_ADD_TO_LOG_BUF(...) do { log_buf_ptr += fd_int_max( sprintf( log_buf_ptr, __VA_ARGS__ ), 0 ); } while(0)
635 6453 : char * log_buf_ptr = fd_log_private_log_msg; /* used by FD_LOG_HEXDUMP_ADD_TO_LOG_BUF macro */
636 :
637 : /* Print the hexdump header */
638 : /* FIXME: consider additional sanitization of descr or using compiler
639 : tricks to prevent user from passing a non-const-char string (i.e.
640 : data they got from somewhere else that might not be sanitized). */
641 :
642 6453 : if( FD_UNLIKELY( !descr ) ) {
643 :
644 0 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "HEXDUMP - (%lu bytes at 0x%lx)", sz, (ulong)mem );
645 :
646 6453 : } else if( FD_UNLIKELY( strlen( descr )>FD_LOG_HEXDUMP_BLOB_DESCRIPTION_MAX_LEN ) ) {
647 :
648 3 : char tmp[ FD_LOG_HEXDUMP_BLOB_DESCRIPTION_MAX_LEN + 1UL ];
649 3 : fd_cstr_fini( fd_cstr_append_text( fd_cstr_init( tmp ), descr, FD_LOG_HEXDUMP_BLOB_DESCRIPTION_MAX_LEN ) );
650 3 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "HEXDUMP \"%s\"... (%lu bytes at 0x%lx)", tmp, sz, (ulong)mem );
651 :
652 6450 : } else {
653 :
654 6450 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "HEXDUMP \"%s\" (%lu bytes at 0x%lx)", descr, sz, (ulong)mem );
655 :
656 6450 : }
657 :
658 6453 : if( FD_UNLIKELY( !sz ) ) return fd_log_private_log_msg;
659 :
660 6453 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "\n" );
661 :
662 6453 : if( FD_UNLIKELY( !mem ) ) {
663 0 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "\t... snip (unreadable memory) ..." );
664 0 : return fd_log_private_log_msg;
665 0 : }
666 :
667 6453 : char line_buf[ FD_LOG_HEXDUMP_BYTES_PER_LINE+1 ];
668 6453 : char const * blob = (char const *)mem;
669 6453 : ulong blob_off = 0UL;
670 6453 : ulong blob_sz = fd_ulong_min( sz, FD_LOG_HEXDUMP_MAX_INPUT_BLOB_SZ );
671 :
672 3159738 : for( ; blob_off<blob_sz; blob_off++ ) {
673 3153285 : ulong col_idx = blob_off % FD_LOG_HEXDUMP_BYTES_PER_LINE;
674 :
675 : /* New line. Print previous line's ASCII representation and then print the offset. */
676 3153285 : if( FD_UNLIKELY( !col_idx ) ) {
677 197244 : if( FD_LIKELY( blob_off ) ) FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( " %s\n", line_buf );
678 197244 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "\t%04lx: ", blob_off );
679 197244 : }
680 : /* FIXME: consider extra space between col 7 and 8 to make easier
681 : for visual inspection */
682 :
683 3153285 : char c = blob[blob_off];
684 3153285 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( " %02x", (uint)(uchar)c );
685 :
686 : /* If not a printable ASCII character, output a dot. */
687 3153285 : line_buf[ col_idx ] = fd_char_if( fd_isalnum( (int)c ) | fd_ispunct( (int)c ) | (c==' '), c, '.' );
688 3153285 : line_buf[ col_idx+1UL ] = '\0';
689 3153285 : }
690 :
691 : /* Print the 2nd column of last blob line */
692 9072 : while( blob_off % FD_LOG_HEXDUMP_BYTES_PER_LINE ) {
693 2619 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( " " );
694 2619 : blob_off++;
695 2619 : }
696 6453 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( " %s", line_buf );
697 :
698 6453 : if( FD_UNLIKELY( blob_sz < sz ) )
699 0 : FD_LOG_HEXDUMP_ADD_TO_LOG_BUF( "\n\t... snip (printed %lu bytes, omitted %lu bytes) ...", blob_sz, sz-blob_sz );
700 :
701 6453 : return fd_log_private_log_msg;
702 :
703 6453 : # undef FD_LOG_HEXDUMP_BYTES_PER_LINE
704 6453 : # undef FD_LOG_HEXDUMP_BLOB_DESCRIPTION_MAX_LEN
705 6453 : # undef FD_LOG_HEXDUMP_MAX_INPUT_BLOB_SZ
706 6453 : # undef FD_LOG_HEXDUMP_ADD_TO_LOG_BUF
707 6453 : }
708 :
709 : void
710 : fd_log_private_1( int level,
711 : long now,
712 : char const * file,
713 : int line,
714 : char const * func,
715 3023358 : char const * msg ) {
716 :
717 3023358 : if( level<fd_log_level_logfile() ) return;
718 :
719 : /* These are thread init so we call them regardless of permanent log
720 : enabled to their initialization time is guaranteed independent of
721 : whether the permanent log is enabled. */
722 :
723 2890431 : char const * thread = fd_log_thread();
724 2890431 : char const * cpu = fd_log_cpu();
725 2890431 : ulong tid = fd_log_tid();
726 :
727 2890431 : int log_fileno = FD_VOLATILE_CONST( fd_log_private_fileno );
728 2890431 : int to_logfile = (log_fileno!=-1);
729 2890431 : int to_stderr = (level>=fd_log_level_stderr());
730 2890431 : if( !(to_logfile | to_stderr) ) return;
731 :
732 : /* Deduplicate the log if requested */
733 :
734 2825145 : if( fd_log_private_dedup ) {
735 :
736 : /* Compute if this message appears to be a recent duplicate of
737 : a previous log message */
738 :
739 2825129 : ulong hash = fd_cstr_hash_append( fd_cstr_hash_append( fd_cstr_hash_append( fd_ulong_hash(
740 2825129 : (ulong)(8L*(long)line+(long)level) ), file ), func ), msg );
741 :
742 2825129 : static long const dedup_interval = 20000000L; /* 1/50 s */
743 :
744 2825129 : static FD_TL int init; /* 0 on thread start */
745 2825129 : static FD_TL ulong last_hash; /* 0UL on thread start */
746 2825129 : static FD_TL long then; /* 0L on thread start */
747 :
748 2825129 : int is_dup = init & (hash==last_hash) & ((now-then)<dedup_interval);
749 2825129 : init = 1;
750 :
751 : /* Update how many messages from this thread in row have been
752 : duplicates */
753 :
754 2825129 : static FD_TL ulong dedup_cnt; /* 0UL on thread start */
755 2825129 : static FD_TL int in_dedup; /* 0 on thread start */
756 :
757 2825129 : if( is_dup ) dedup_cnt++;
758 1829274 : else {
759 1829274 : if( in_dedup ) {
760 :
761 : /* This message appears to end a long string of duplicates.
762 : Log the end of the deduplication. */
763 :
764 104 : char then_cstr[ FD_LOG_WALLCLOCK_CSTR_BUF_SZ ];
765 104 : fd_log_wallclock_cstr( then, then_cstr );
766 :
767 104 : if( to_logfile )
768 104 : fd_log_private_fprintf_0( log_fileno, "SNIP %s %6lu:%-6lu %s:%s:%-4s %s:%s:%-4s "
769 104 : "stopped repeating (%lu identical messages)\n",
770 104 : then_cstr, fd_log_group_id(),tid, fd_log_user(),fd_log_host(),cpu,
771 104 : fd_log_app(),fd_log_group(),thread, dedup_cnt+1UL );
772 :
773 104 : if( to_stderr ) {
774 39 : char * then_short_cstr = then_cstr+5; then_short_cstr[21] = '\0'; /* Lop off the year, ns resolution and timezone */
775 39 : fd_log_private_fprintf_0( fd_log_private_stderr_fileno, "SNIP %s %-6lu %-4s %-4s stopped repeating (%lu identical messages)\n",
776 39 : then_short_cstr, tid,cpu,thread, dedup_cnt+1UL );
777 39 : }
778 :
779 104 : in_dedup = 0;
780 104 : }
781 :
782 1829274 : dedup_cnt = 0UL;
783 1829274 : }
784 :
785 : /* dedup_cnt previous messages from this thread appear to be
786 : duplicates. Decide whether to let the raw message print or
787 : deduplicate to the log. FIXME: CONSIDER RANDOMIZING THE
788 : THROTTLE. */
789 :
790 2825129 : static ulong const dedup_thresh = 3UL; /* let initial dedup_thresh duplicates go out the door */
791 2825129 : static long const dedup_throttle = 1000000000L; /* ~1s, how often to update status on current duplication */
792 :
793 2825129 : static FD_TL long dedup_last; /* 0L on thread start */
794 :
795 2825129 : if( dedup_cnt < dedup_thresh ) dedup_last = now;
796 995421 : else {
797 995421 : if( (now-dedup_last) >= dedup_throttle ) {
798 40 : char now_cstr[ FD_LOG_WALLCLOCK_CSTR_BUF_SZ ];
799 40 : fd_log_wallclock_cstr( now, now_cstr );
800 40 : if( to_logfile )
801 40 : fd_log_private_fprintf_0( log_fileno, "SNIP %s %6lu:%-6lu %s:%s:%-4s %s:%s:%-4s repeating (%lu identical messages)\n",
802 40 : now_cstr, fd_log_group_id(),tid, fd_log_user(),fd_log_host(),cpu,
803 40 : fd_log_app(),fd_log_group(),thread, dedup_cnt+1UL );
804 40 : if( to_stderr ) {
805 40 : char * now_short_cstr = now_cstr+5; now_short_cstr[21] = '\0'; /* Lop off the year, ns resolution and timezone */
806 40 : fd_log_private_fprintf_0( fd_log_private_stderr_fileno, "SNIP %s %-6lu %-4s %-4s repeating (%lu identical messages)\n",
807 40 : now_short_cstr, tid,cpu,thread, dedup_cnt+1UL );
808 40 : }
809 40 : dedup_last = now;
810 40 : }
811 995421 : in_dedup = 1;
812 995421 : }
813 :
814 2825129 : last_hash = hash;
815 2825129 : then = now;
816 :
817 2825129 : if( in_dedup ) return;
818 2825129 : }
819 :
820 1829712 : char now_cstr[ FD_LOG_WALLCLOCK_CSTR_BUF_SZ ];
821 1829712 : fd_log_wallclock_cstr( now, now_cstr );
822 :
823 1829712 : static char const * level_cstr[] = {
824 1829712 : /* 0 */ "DEBUG ",
825 1829712 : /* 1 */ "INFO ",
826 1829712 : /* 2 */ "NOTICE ",
827 1829712 : /* 3 */ "WARNING",
828 1829712 : /* 4 */ "ERR ",
829 1829712 : /* 5 */ "CRIT ",
830 1829712 : /* 6 */ "ALERT ",
831 1829712 : /* 7 */ "EMERG "
832 1829712 : };
833 :
834 1829712 : if( to_logfile )
835 1822896 : fd_log_private_fprintf_0( log_fileno, "%s %s %6lu:%-6lu %s:%s:%-4s %s:%s:%-4s %s(%i)[%s]: %s\n",
836 1822896 : level_cstr[level], now_cstr, fd_log_group_id(),tid, fd_log_user(),fd_log_host(),cpu,
837 1822896 : fd_log_app(),fd_log_group(),thread, file,line,func, msg );
838 :
839 1829712 : if( to_stderr ) {
840 9752 : static char const * color_level_cstr[] = {
841 9752 : /* 0 */ TEXT_NORMAL "DEBUG ",
842 9752 : /* 1 */ TEXT_BLUE "INFO " TEXT_NORMAL,
843 9752 : /* 2 */ TEXT_GREEN "NOTICE " TEXT_NORMAL,
844 9752 : /* 3 */ TEXT_YELLOW "WARNING" TEXT_NORMAL,
845 9752 : /* 4 */ TEXT_RED "ERR " TEXT_NORMAL,
846 9752 : /* 5 */ TEXT_RED TEXT_BOLD "CRIT " TEXT_NORMAL,
847 9752 : /* 6 */ TEXT_RED TEXT_BOLD TEXT_UNDERLINE "ALERT " TEXT_NORMAL,
848 9752 : /* 7 */ TEXT_RED TEXT_BOLD TEXT_UNDERLINE TEXT_BLINK "EMERG " TEXT_NORMAL
849 9752 : };
850 9752 : char * now_short_cstr = now_cstr+5; now_short_cstr[21] = '\0'; /* Lop off the year, ns resolution and timezone */
851 9752 : fd_log_private_fprintf_0( fd_log_private_stderr_fileno, "%s %s %-6lu %-4s %-4s %s(%i): %s\n",
852 9752 : fd_log_private_colorize ? color_level_cstr[level] : level_cstr[level],
853 9752 : now_short_cstr, tid,cpu,thread, file, line, msg );
854 9752 : }
855 :
856 1829712 : if( level<fd_log_level_flush() ) return;
857 :
858 4133 : fd_log_flush();
859 4133 : }
860 :
861 : void
862 : fd_log_private_2( int level,
863 : long now,
864 : char const * file,
865 : int line,
866 : char const * func,
867 1125 : char const * msg ) {
868 1125 : if( level<fd_log_level_core() && fd_log_private_restore_stderr!=-1 ) {
869 : /* Restore stderr to original fd in case it was redirected to
870 : something that won't be able to process the fatal message */
871 :
872 1125 : fd_log_private_stderr_fileno = fd_log_private_restore_stderr;
873 1125 : fd_log_private_restore_stderr = -1;
874 :
875 1125 : }
876 :
877 1125 : fd_log_private_1( level, now, file, line, func, msg );
878 :
879 1125 : if( level<fd_log_level_core() ) {
880 1125 : # if defined(__linux__)
881 1125 : if( fd_log_private_unclean_exit ) {
882 0 : syscall( SYS_exit_group, 1 );
883 0 : }
884 1125 : # endif /* defined(__linux__) */
885 1125 : exit(1); /* atexit will call fd_log_private_cleanup implicitly */
886 1125 : }
887 :
888 0 : abort();
889 1125 : }
890 :
891 : void
892 : fd_log_private_raw_2( char const * file,
893 : int line,
894 : char const * func,
895 0 : char const * msg ) {
896 0 : fd_log_private_fprintf_0( fd_log_private_stderr_fileno, "%s(%i)[%s]: %s\n", file, line, func, msg );
897 0 : # if defined(__linux__)
898 0 : syscall( SYS_exit_group, 1 );
899 : # else
900 : exit(1);
901 : # endif
902 0 : abort();
903 0 : }
904 :
905 : /* BOOT/HALT APIS *****************************************************/
906 :
907 : static void
908 3967 : fd_log_private_cleanup( void ) {
909 :
910 : /* The atexit below means that all calls to "exit();" implicitly
911 : become "fd_log_private_cleanup(); exit();". It also implies that
912 : programs that terminate via a top level return from main implicitly
913 : call fd_log_private_cleanup().
914 :
915 : As such it is possible that a thread other than the booter will
916 : trigger cleanup either by triggering this directly (e.g. calling
917 : exit) or indirectly (e.g. by logging a message with an exit
918 : triggering priority) and that the booter itself might call this
919 : more than once sequentially (e.g. fd_halt() calling cleanup
920 : explicitly followed by return from main triggering it again.
921 :
922 : Accordingly we protect this with a ONCE block so it only will
923 : execute once per program. Further, if cleanup gets triggered by
924 : multiple threads concurrently, the ONCE block will prevent them
925 : from progressing until the first thread that hits the once block
926 : has completed cleanup. */
927 :
928 13044 : FD_ONCE_BEGIN {
929 2555 : int log_fileno = FD_VOLATILE_CONST( fd_log_private_fileno );
930 2555 : if( log_fileno==-1 ) fd_log_private_fprintf_0( STDERR_FILENO, "No log\n" );
931 731 : else if( !strcmp( fd_log_private_path, "-" ) ) fd_log_private_fprintf_0( STDERR_FILENO, "Log to stdout\n" );
932 719 : else {
933 719 : # if FD_HAS_THREADS
934 719 : if( fd_log_private_thread_id_ctr>1UL ) { /* There are potentially other log users running */
935 : /* Just closing the permanent log file is not multithreading
936 : safe in the case where other threads are still running
937 : normally and thus potentially logging to the permanent log.
938 : Such should not happen in a correctly written and functioning
939 : application but logging exists in large part to help
940 : understand when applications misbehave. So we try to be as
941 : robust and informative as we can here. FIXME: THE SECOND
942 : USLEEP IS AN UGLY HACK TO REDUCE (BUT NOT FULLY ELIMINATE)
943 : THE RISK OF USE AFTER CLOSE BY THOSE OTHER THREADS. IT IS
944 : POSSIBLE WITH A MORE INVASIVE CHANGES TO FULLY ELIMINATE THIS
945 : RISK. */
946 0 : usleep( (useconds_t)40000 ); /* Give potentially concurrent users a chance to get their dying messages out */
947 0 : FD_COMPILER_MFENCE();
948 0 : FD_VOLATILE( fd_log_private_fileno ) = -1; /* Turn off the permanent log for concurrent users */
949 0 : FD_COMPILER_MFENCE();
950 0 : usleep( (useconds_t)40000 ); /* Give any concurrent log operations progress at turn off a chance to wrap */
951 0 : }
952 : # else
953 : FD_VOLATILE( fd_log_private_fileno ) = -1;
954 : # endif
955 :
956 719 : fsync( log_fileno );
957 719 : sync();
958 719 : fd_log_private_fprintf_0( STDERR_FILENO, "Log at \"%s\"\n", fd_log_private_path );
959 719 : }
960 2555 : } FD_ONCE_END;
961 3967 : }
962 :
963 : static void
964 : fd_log_private_sig_abort( int sig,
965 : siginfo_t * info,
966 0 : void * context ) {
967 0 : (void)sig; (void)info; (void)context;
968 :
969 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)
970 0 : FD_LOG_ERR_NOEXIT(( "Received signal %s", fd_io_strsignal( sig ) ));
971 0 : #undef FD_LOG_ERR_NOEXIT
972 :
973 0 : # if FD_HAS_BACKTRACE
974 :
975 0 : void * btrace[ 128UL ];
976 0 : int btrace_cnt = backtrace( btrace, 128 );
977 :
978 0 : fd_backtrace_log( btrace, (ulong)btrace_cnt );
979 :
980 0 : # endif
981 :
982 : /* Returning is going to cause SIGSYS since it's probably not allowed
983 : in the sandbox, which is OK. The parent process will terminate
984 : everything anyway. If we allow rt_sigreturn to be called in the
985 : sandbox, then we will get a correct signal. */
986 0 : return;
987 0 : }
988 :
989 : static void
990 45720 : fd_log_private_sig_trap( int sig ) {
991 45720 : struct sigaction act[1];
992 : /* FIXME: CONSIDER NOT OVERRIDING IF THE SIGNAL HANDLER HAS ALREADY
993 : BEEN SET BY THE USER. */
994 45720 : act->sa_sigaction = fd_log_private_sig_abort;
995 45720 : if( sigemptyset( &act->sa_mask ) ) FD_LOG_ERR(( "sigempty set failed" ));
996 45720 : act->sa_flags = (int)(SA_SIGINFO | SA_RESETHAND);
997 45720 : if( sigaction( sig, act, NULL ) ) FD_LOG_ERR(( "unable to override signal %i", sig ));
998 45720 : }
999 :
1000 : static int
1001 : fd_log_private_open_path( int cmdline,
1002 2555 : char const * log_path ) {
1003 2555 : ulong log_path_sz = log_path ? (strlen( log_path )+1UL) : 0UL;
1004 :
1005 2555 : if( !log_path_sz ) { /* Use default log path */
1006 0 : char tag[ FD_LOG_WALLCLOCK_CSTR_BUF_SZ ];
1007 0 : fd_log_wallclock_cstr( fd_log_wallclock(), tag );
1008 0 : for( ulong b=0UL; tag[b]; b++ ) if( tag[b]==' ' || tag[b]=='-' || tag[b]=='.' || tag[b]==':' ) tag[b] = '_';
1009 0 : ulong len; fd_cstr_printf( fd_log_private_path, 1024UL, &len, "/tmp/fd-%i.%i.%i_%lu_%s_%s_%s",
1010 0 : FDCTL_MAJOR_VERSION, FDCTL_MINOR_VERSION, FDCTL_PATCH_VERSION,
1011 0 : fd_log_group_id(), fd_log_user(), fd_log_host(), tag );
1012 0 : if( len==1023UL ) { fd_log_private_fprintf_0( STDERR_FILENO, "default log path too long; unable to boot\n" ); exit(1); }
1013 0 : }
1014 2555 : else if( log_path_sz==1UL ) fd_log_private_path[0] = '\0'; /* User disabled */
1015 731 : else if( log_path_sz<=1024UL ) fd_memcpy( fd_log_private_path, log_path, log_path_sz ); /* User specified */
1016 0 : else { fd_log_private_fprintf_0( STDERR_FILENO, "log path too long; unable to boot\n" ); exit(1); } /* Invalid */
1017 :
1018 2555 : int log_fileno;
1019 2555 : if( fd_log_private_path[0]=='\0' ) {
1020 1824 : if( cmdline ) fd_log_private_fprintf_0( STDERR_FILENO, "--log-path \"\"\nNo log\n" );
1021 0 : else fd_log_private_fprintf_0( STDERR_FILENO, "No log\n" );
1022 1824 : log_fileno = -1;
1023 1824 : } else if( !strcmp( fd_log_private_path, "-" ) ) {
1024 12 : if( cmdline ) fd_log_private_fprintf_0( STDERR_FILENO, "--log-path \"-\"\nLog to stdout\n" );
1025 0 : else fd_log_private_fprintf_0( STDERR_FILENO, "Log to stdout\n" );
1026 12 : log_fileno = STDOUT_FILENO;
1027 719 : } else {
1028 719 : if( cmdline && !log_path_sz ) fd_log_private_fprintf_0( STDERR_FILENO, "--log-path not specified; using autogenerated path\n" );
1029 719 : 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 );
1030 719 : if( log_fileno==-1 ) {
1031 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 ) );
1032 0 : exit(1);
1033 0 : }
1034 719 : fd_log_private_fprintf_0( STDERR_FILENO, "Log at \"%s\"\n", fd_log_private_path );
1035 719 : }
1036 2555 : return log_fileno;
1037 2555 : }
1038 :
1039 : void
1040 : fd_log_private_boot( int * pargc,
1041 2555 : char *** pargv ) {
1042 : //FD_LOG_INFO(( "fd_log: booting" )); /* Log not online yet */
1043 :
1044 2555 : char buf[ FD_LOG_NAME_MAX ];
1045 :
1046 : /* Init our our application logical ids */
1047 : /* FIXME: CONSIDER EXPLICIT SPECIFICATION OF RANGE OF THREADS
1048 : INSTEAD OF ATOMIC COUNTER FROM BASE */
1049 :
1050 2555 : fd_log_private_app_id_set( fd_env_strip_cmdline_ulong( pargc, pargv, "--log-app-id", "FD_LOG_APP_ID", 0UL ) );
1051 :
1052 2555 : fd_log_private_app_set( fd_env_strip_cmdline_cstr( pargc, pargv, "--log-app", "FD_LOG_APP", NULL ) );
1053 :
1054 2555 : # if FD_HAS_THREADS
1055 2555 : fd_log_private_thread_id_ctr = fd_env_strip_cmdline_ulong( pargc, pargv, "--log-thread-id", "FD_LOG_THREAD_ID", 0UL );
1056 2555 : ulong thread_id = fd_log_private_thread_id_next();
1057 : # else
1058 : ulong thread_id = fd_env_strip_cmdline_ulong( pargc, pargv, "--log-thread-id", "FD_LOG_THREAD_ID", 0UL );
1059 : # endif
1060 2555 : fd_log_private_thread_id_set( thread_id );
1061 :
1062 2555 : fd_log_thread_set( fd_env_strip_cmdline_cstr( pargc, pargv, "--log-thread", "FD_LOG_THREAD", NULL ) );
1063 :
1064 : /* Init our application physical ids */
1065 : /* We ignore any user specified cpu-id in favor of the actual core
1066 : assigned by the host OS. We strip it from the command line so
1067 : downstream command line handling is identical from user's point of
1068 : view. */
1069 :
1070 2555 : fd_log_private_host_id_set( fd_env_strip_cmdline_ulong( pargc, pargv, "--log-host-id", "FD_LOG_HOST_ID", 0UL ) );
1071 :
1072 2555 : char const * host = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-host", "FD_LOG_HOST", NULL );
1073 2555 : if( !host ) { if( !gethostname( buf, FD_LOG_NAME_MAX ) ) buf[ FD_LOG_NAME_MAX-1UL ] = '\0', host = buf; }
1074 2555 : fd_msan_unpoison( (void *)host, FD_LOG_NAME_MAX );
1075 2555 : fd_log_private_host_set( host );
1076 :
1077 2555 : fd_env_strip_cmdline_ulong( pargc, pargv, "--log-cpu-id", "FD_LOG_CPU_ID", 0UL ); /* FIXME: LOG IGNORING? */
1078 2555 : fd_log_private_cpu_id_set( fd_log_private_cpu_id_default() );
1079 :
1080 2555 : fd_log_cpu_set( fd_env_strip_cmdline_cstr( pargc, pargv, "--log-cpu", "FD_LOG_CPU", NULL ) );
1081 :
1082 : /* Init our thread group ids */
1083 : /* We ignore any user specified group id and tid in favor of the actual
1084 : group id and tid assigned by the host OS. We strip it from the
1085 : command line so downstream command line handling is identical from
1086 : user's point of view. */
1087 :
1088 2555 : fd_env_strip_cmdline_ulong( pargc, pargv, "--log-group-id", "FD_LOG_GROUP_ID", 0UL ); /* FIXME: LOG IGNORING? */
1089 2555 : pid_t pid = getpid();
1090 2555 : fd_log_private_group_id_set( fd_ulong_if( pid>(pid_t)0, (ulong)pid, ULONG_MAX ) );
1091 :
1092 2555 : char const * group = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-group", "FD_LOG_GROUP", NULL );
1093 2555 : # if defined(__linux__)
1094 2555 : if( !group ) group = program_invocation_short_name;
1095 : # elif defined(__FreeBSD__)
1096 : if( !group ) group = getprogname();
1097 : # endif
1098 2555 : if( !group ) group = (pargc && pargv && (*pargc)>0) ? (*pargv)[0] : NULL;
1099 2555 : fd_log_private_group_set( group );
1100 :
1101 2555 : fd_env_strip_cmdline_ulong( pargc, pargv, "--log-tid", "FD_LOG_TID", 0UL ); /* FIXME: LOG IGNORING? */
1102 2555 : fd_log_private_tid_set( fd_log_private_tid_default() );
1103 :
1104 2555 : fd_env_strip_cmdline_ulong( pargc, pargv, "--log-user-id", "FD_LOG_USER_ID", 0UL ); /* FIXME: LOG IGNORING? */
1105 2555 : fd_log_private_user_id_set( fd_log_private_user_id_default() );
1106 :
1107 2555 : char const * user = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-user", "FD_LOG_USER", NULL );
1108 2555 : if( !user ) user = getenv( "LOGNAME" );
1109 2555 : if( !user ) user = getlogin();
1110 2555 : fd_log_private_user_set( user );
1111 :
1112 : /* Configure the log */
1113 :
1114 2555 : fd_log_private_dedup = fd_env_strip_cmdline_int( pargc, pargv, "--log-dedup", "FD_LOG_DEDUP", 1 );
1115 :
1116 2555 : int colorize = 0;
1117 2555 : do {
1118 2555 : char const * cstr = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-colorize", "FD_LOG_COLORIZE", NULL );
1119 2555 : if( cstr ) { colorize = fd_cstr_to_int( cstr ); break; }
1120 :
1121 2555 : colorize = fd_log_should_colorize();
1122 2555 : } while(0);
1123 0 : fd_log_colorize_set( colorize );
1124 :
1125 2555 : fd_log_level_logfile_set( fd_env_strip_cmdline_int( pargc, pargv, "--log-level-logfile", "FD_LOG_LEVEL_LOGFILE", 1 ) );
1126 2555 : fd_log_level_stderr_set ( fd_env_strip_cmdline_int( pargc, pargv, "--log-level-stderr", "FD_LOG_LEVEL_STDERR", 2 ) );
1127 2555 : fd_log_level_flush_set ( fd_env_strip_cmdline_int( pargc, pargv, "--log-level-flush", "FD_LOG_LEVEL_FLUSH", 3 ) );
1128 2555 : fd_log_level_core_set ( fd_env_strip_cmdline_int( pargc, pargv, "--log-level-core", "FD_LOG_LEVEL_CORE", 5 ) );
1129 :
1130 : /* Hook up signal handlers */
1131 :
1132 2555 : int log_backtrace = fd_env_strip_cmdline_int( pargc, pargv, "--log-backtrace", "FD_LOG_BACKTRACE", 1 );
1133 2555 : if( log_backtrace || fd_log_private_signal_handler ) {
1134 :
1135 2540 : # if FD_HAS_BACKTRACE
1136 : /* If libgcc isn't already linked into the program when a trapped
1137 : signal is received by an application, calls to backtrace and
1138 : backtrace_symbols_fd within the signal handler can silently
1139 : invoke the dynamic linker, which in turn can do silent async
1140 : signal unsafe behavior behind our back. We do dummy calls to
1141 : backtrace and backtrace_symbols_fd here to avoid dynamic linking
1142 : surprises in the signal handler. (Hat tip to runtimeverification
1143 : for finding this.) */
1144 :
1145 2540 : void * btrace[128];
1146 2540 : (void)backtrace( btrace, 128 );
1147 2540 : # endif /* FD_HAS_BACKTRACE */
1148 :
1149 : /* This is all overridable POSIX sigs whose default behavior is to
1150 : abort the program. It will backtrace and then fallback to the
1151 : default behavior. */
1152 2540 : fd_log_private_sig_trap( SIGABRT );
1153 2540 : fd_log_private_sig_trap( SIGALRM );
1154 2540 : fd_log_private_sig_trap( SIGFPE );
1155 2540 : fd_log_private_sig_trap( SIGHUP );
1156 2540 : fd_log_private_sig_trap( SIGILL );
1157 2540 : fd_log_private_sig_trap( SIGQUIT );
1158 2540 : fd_log_private_sig_trap( SIGPIPE );
1159 2540 : fd_log_private_sig_trap( SIGSEGV );
1160 2540 : fd_log_private_sig_trap( SIGUSR1 );
1161 2540 : fd_log_private_sig_trap( SIGUSR2 );
1162 2540 : fd_log_private_sig_trap( SIGBUS );
1163 2540 : fd_log_private_sig_trap( SIGPOLL );
1164 2540 : fd_log_private_sig_trap( SIGPROF );
1165 2540 : fd_log_private_sig_trap( SIGSYS );
1166 2540 : fd_log_private_sig_trap( SIGTRAP );
1167 2540 : fd_log_private_sig_trap( SIGVTALRM );
1168 2540 : fd_log_private_sig_trap( SIGXCPU );
1169 2540 : fd_log_private_sig_trap( SIGXFSZ );
1170 2540 : }
1171 :
1172 : /* Hook up the permanent log */
1173 2555 : char const * log_path = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-path", "FD_LOG_PATH", NULL );
1174 2555 : FD_VOLATILE( fd_log_private_fileno ) = fd_log_private_open_path( 1, log_path );
1175 :
1176 2555 : if( !fd_log_private_unclean_exit ) {
1177 2555 : if( atexit( fd_log_private_cleanup ) ) { fd_log_private_fprintf_0( STDERR_FILENO, "atexit failed; unable to boot\n" ); exit(1); }
1178 2555 : }
1179 :
1180 : /* At this point, logging online */
1181 2555 : if( fd_log_build_info_sz>1UL ) FD_LOG_INFO(( "fd_log: build info:\n%s", fd_log_build_info ));
1182 0 : else FD_LOG_INFO(( "fd_log: build info not available" ));
1183 2555 : FD_LOG_INFO(( "fd_log: --log-path %s", fd_log_private_path ));
1184 2555 : FD_LOG_INFO(( "fd_log: --log-dedup %i", fd_log_private_dedup ));
1185 2555 : FD_LOG_INFO(( "fd_log: --log-colorize %i", fd_log_colorize() ));
1186 2555 : FD_LOG_INFO(( "fd_log: --log-level-logfile %i", fd_log_level_logfile() ));
1187 2555 : FD_LOG_INFO(( "fd_log: --log-level-stderr %i", fd_log_level_stderr() ));
1188 2555 : FD_LOG_INFO(( "fd_log: --log-level-flush %i", fd_log_level_flush() ));
1189 2555 : FD_LOG_INFO(( "fd_log: --log-level-core %i", fd_log_level_core() ));
1190 2555 : FD_LOG_INFO(( "fd_log: --log-app-id %lu", fd_log_app_id() ));
1191 2555 : FD_LOG_INFO(( "fd_log: --log-app %s", fd_log_app() ));
1192 2555 : FD_LOG_INFO(( "fd_log: --log-thread-id %lu", fd_log_thread_id() ));
1193 2555 : FD_LOG_INFO(( "fd_log: --log-thread %s", fd_log_thread() ));
1194 2555 : FD_LOG_INFO(( "fd_log: --log-host-id %lu", fd_log_host_id() ));
1195 2555 : FD_LOG_INFO(( "fd_log: --log-host %s", fd_log_host() ));
1196 2555 : FD_LOG_INFO(( "fd_log: --log-cpu-id %lu", fd_log_cpu_id() ));
1197 2555 : FD_LOG_INFO(( "fd_log: --log-cpu %s", fd_log_cpu() ));
1198 2555 : FD_LOG_INFO(( "fd_log: --log-group-id %lu", fd_log_group_id() ));
1199 2555 : FD_LOG_INFO(( "fd_log: --log-group %s", fd_log_group() ));
1200 2555 : FD_LOG_INFO(( "fd_log: --log-tid %lu", fd_log_tid() ));
1201 2555 : FD_LOG_INFO(( "fd_log: --log-user-id %lu", fd_log_user_id() ));
1202 2555 : FD_LOG_INFO(( "fd_log: --log-user %s", fd_log_user() ));
1203 :
1204 2555 : FD_LOG_INFO(( "fd_log: boot success" ));
1205 2555 : }
1206 :
1207 : void
1208 : fd_log_private_boot_custom( ulong app_id,
1209 : char const * app,
1210 : ulong thread_id,
1211 : char const * thread,
1212 : ulong host_id,
1213 : char const * host,
1214 : ulong cpu_id,
1215 : char const * cpu,
1216 : ulong group_id,
1217 : char const * group,
1218 : ulong tid,
1219 : ulong user_id,
1220 : char const * user,
1221 : int dedup,
1222 : int colorize,
1223 : int level_logfile,
1224 : int level_stderr,
1225 : int level_flush,
1226 : int level_core,
1227 : int log_fd,
1228 0 : char const * log_path ) {
1229 0 : fd_log_private_app_id_set( app_id );
1230 0 : fd_log_private_app_set( app );
1231 0 : fd_log_private_thread_id_set( thread_id );
1232 0 : fd_log_thread_set( thread );
1233 0 : fd_log_private_host_id_set( host_id );
1234 0 : fd_log_private_host_set( host );
1235 0 : fd_log_private_cpu_id_set( cpu_id );
1236 0 : fd_log_cpu_set( cpu );
1237 0 : fd_log_private_group_id_set( group_id );
1238 0 : fd_log_private_group_set( group );
1239 0 : fd_log_private_tid_set( tid );
1240 0 : fd_log_private_user_id_set( user_id );
1241 0 : fd_log_private_user_set( user );
1242 :
1243 0 : fd_log_private_dedup = dedup;
1244 0 : fd_log_colorize_set( colorize );
1245 :
1246 0 : fd_log_level_logfile_set( level_logfile );
1247 0 : fd_log_level_stderr_set ( level_stderr );
1248 0 : fd_log_level_flush_set ( level_flush );
1249 0 : fd_log_level_core_set ( level_core );
1250 :
1251 : /* Hook up the permanent log */
1252 0 : if( -1!=log_fd ) {
1253 : /* Log file descriptor was set up before boot, user wishes to log there directly. */
1254 0 : fd_log_private_path[0] = '\0';
1255 0 : FD_VOLATILE( fd_log_private_fileno ) = log_fd;
1256 0 : } else {
1257 0 : FD_VOLATILE( fd_log_private_fileno ) = fd_log_private_open_path( 0, log_path );
1258 0 : }
1259 :
1260 0 : if( FD_UNLIKELY( fd_log_private_signal_handler ) ) {
1261 0 : # if FD_HAS_BACKTRACE
1262 : /* See note above about needing to prime backtrace */
1263 0 : void * btrace[128];
1264 0 : (void)backtrace( btrace, 128 );
1265 0 : # endif
1266 :
1267 : /* This is all overridable POSIX sigs whose default behavior is to
1268 : abort the program. It will backtrace and then fallback to the
1269 : default behavior. */
1270 0 : fd_log_private_sig_trap( SIGABRT );
1271 0 : fd_log_private_sig_trap( SIGALRM );
1272 0 : fd_log_private_sig_trap( SIGFPE );
1273 0 : fd_log_private_sig_trap( SIGHUP );
1274 0 : fd_log_private_sig_trap( SIGILL );
1275 0 : fd_log_private_sig_trap( SIGQUIT );
1276 0 : fd_log_private_sig_trap( SIGPIPE );
1277 0 : fd_log_private_sig_trap( SIGSEGV );
1278 0 : fd_log_private_sig_trap( SIGUSR1 );
1279 0 : fd_log_private_sig_trap( SIGUSR2 );
1280 0 : fd_log_private_sig_trap( SIGBUS );
1281 0 : fd_log_private_sig_trap( SIGPOLL );
1282 0 : fd_log_private_sig_trap( SIGPROF );
1283 0 : fd_log_private_sig_trap( SIGSYS );
1284 0 : fd_log_private_sig_trap( SIGTRAP );
1285 0 : fd_log_private_sig_trap( SIGVTALRM );
1286 0 : fd_log_private_sig_trap( SIGXCPU );
1287 0 : fd_log_private_sig_trap( SIGXFSZ );
1288 0 : }
1289 :
1290 : /* At this point, logging online */
1291 0 : if( fd_log_build_info_sz>1UL ) FD_LOG_INFO(( "fd_log: build info:\n%s", fd_log_build_info ));
1292 0 : else FD_LOG_INFO(( "fd_log: build info not available" ));
1293 0 : FD_LOG_INFO(( "fd_log: --log-path %s", fd_log_private_path ));
1294 0 : FD_LOG_INFO(( "fd_log: --log-dedup %i", fd_log_private_dedup ));
1295 0 : FD_LOG_INFO(( "fd_log: --log-colorize %i", fd_log_colorize() ));
1296 0 : FD_LOG_INFO(( "fd_log: --log-level-logfile %i", fd_log_level_logfile() ));
1297 0 : FD_LOG_INFO(( "fd_log: --log-level-logfile %i", fd_log_level_logfile() ));
1298 0 : FD_LOG_INFO(( "fd_log: --log-level-stderr %i", fd_log_level_stderr() ));
1299 0 : FD_LOG_INFO(( "fd_log: --log-level-flush %i", fd_log_level_flush() ));
1300 0 : FD_LOG_INFO(( "fd_log: --log-level-core %i", fd_log_level_core() ));
1301 0 : FD_LOG_INFO(( "fd_log: --log-app-id %lu", fd_log_app_id() ));
1302 0 : FD_LOG_INFO(( "fd_log: --log-app %s", fd_log_app() ));
1303 0 : FD_LOG_INFO(( "fd_log: --log-thread-id %lu", fd_log_thread_id() ));
1304 0 : FD_LOG_INFO(( "fd_log: --log-thread %s", fd_log_thread() ));
1305 0 : FD_LOG_INFO(( "fd_log: --log-host-id %lu", fd_log_host_id() ));
1306 0 : FD_LOG_INFO(( "fd_log: --log-host %s", fd_log_host() ));
1307 0 : FD_LOG_INFO(( "fd_log: --log-cpu-id %lu", fd_log_cpu_id() ));
1308 0 : FD_LOG_INFO(( "fd_log: --log-cpu %s", fd_log_cpu() ));
1309 0 : FD_LOG_INFO(( "fd_log: --log-group-id %lu", fd_log_group_id() ));
1310 0 : FD_LOG_INFO(( "fd_log: --log-group %s", fd_log_group() ));
1311 0 : FD_LOG_INFO(( "fd_log: --log-tid %lu", fd_log_tid() ));
1312 0 : FD_LOG_INFO(( "fd_log: --log-user-id %lu", fd_log_user_id() ));
1313 0 : FD_LOG_INFO(( "fd_log: --log-user %s", fd_log_user() ));
1314 :
1315 0 : FD_LOG_INFO(( "fd_log: boot success" ));
1316 0 : }
1317 :
1318 : void
1319 1412 : fd_log_private_halt( void ) {
1320 1412 : FD_LOG_INFO(( "fd_log: halting" ));
1321 :
1322 1412 : fd_log_private_cleanup();
1323 :
1324 : /* At this point, log is offline */
1325 :
1326 1412 : fd_log_private_path[0] = '\0';
1327 : //fd_log_private_fileno = -1; /* Already handled by cleanup */
1328 1412 : fd_log_private_dedup = 0;
1329 :
1330 1412 : fd_log_private_level_core = 0;
1331 1412 : fd_log_private_level_flush = 0;
1332 1412 : fd_log_private_level_stderr = 0;
1333 1412 : fd_log_private_level_logfile = 0;
1334 1412 : fd_log_private_colorize = 0;
1335 :
1336 1412 : fd_log_private_clock_func = fd_log_wallclock_host;
1337 1412 : fd_log_private_clock_args = NULL;
1338 :
1339 1412 : fd_log_private_user[0] = '\0';
1340 1412 : fd_log_private_user_id_init = 0;
1341 1412 : fd_log_private_user_id = 0UL;
1342 1412 : fd_log_private_tid_init = 0;
1343 1412 : fd_log_private_tid = 0UL;
1344 1412 : fd_log_private_group[0] = '\0';
1345 1412 : fd_log_private_group_id = 0UL;
1346 :
1347 1412 : fd_log_private_cpu_init = 0;
1348 1412 : fd_log_private_cpu[0] = '\0';
1349 1412 : fd_log_private_cpu_id_init = 0;
1350 1412 : fd_log_private_cpu_id = 0UL;
1351 1412 : fd_log_private_host[0] = '\0';
1352 1412 : fd_log_private_host_id = 0UL;
1353 :
1354 1412 : fd_log_private_thread_init = 0;
1355 1412 : fd_log_private_thread[0] = '\0';
1356 1412 : fd_log_private_thread_id_init = 0;
1357 1412 : fd_log_private_thread_id = 0UL;
1358 1412 : # if FD_HAS_THREADS
1359 1412 : fd_log_private_thread_id_ctr = 0UL;
1360 1412 : # endif
1361 1412 : fd_log_private_app[0] = '\0';
1362 1412 : fd_log_private_app_id = 0UL;
1363 :
1364 : //FD_LOG_INFO(( "fd_log: halt success" )); /* Log not online anymore */
1365 1412 : }
1366 :
1367 : #include <sys/resource.h>
1368 :
1369 : ulong
1370 2555 : fd_log_private_main_stack_sz( void ) {
1371 :
1372 : /* We are extra paranoid about what rlimit returns and we don't trust
1373 : environments that claim an unlimited stack size (because it just
1374 : isn't unlimited ... even if rlimit says otherwise ... which it will
1375 : if a user tries to be clever with a "ulimit -s unlimited" ... e.g.
1376 : tile0's stack highest address is at 128 TiB-4KiB typically on
1377 : modern Linux and grows down while the text / data / heap grow up
1378 : from 0B ... so stack size is practically always going to be << 128
1379 : TiB irrespective of any getrlimit claim). TODO: It looks like
1380 : pthead_attr_getstack might be getrlimit based under the hood, so
1381 : maybe just use pthread_attr_getstack here too? */
1382 :
1383 2555 : struct rlimit rlim[1];
1384 2555 : int err = getrlimit( RLIMIT_STACK, rlim );
1385 2555 : if( FD_UNLIKELY( err ) ) {
1386 0 : FD_LOG_WARNING(( "fd_log: getrlimit failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1387 0 : return 0UL;
1388 0 : }
1389 :
1390 2555 : ulong stack_sz = (ulong)rlim->rlim_cur;
1391 2555 : if( FD_UNLIKELY( (rlim->rlim_cur>rlim->rlim_max) | (rlim->rlim_max>RLIM_INFINITY ) |
1392 2555 : (rlim->rlim_cur==RLIM_INFINITY) | (rlim->rlim_cur!=(rlim_t)stack_sz) ) ) {
1393 0 : FD_LOG_WARNING(( "fd_log: unexpected stack limits (rlim_cur %lu, rlim_max %lu)",
1394 0 : (ulong)rlim->rlim_cur, (ulong)rlim->rlim_max ));
1395 0 : return 0UL;
1396 0 : }
1397 :
1398 2555 : return stack_sz;
1399 2555 : }
1400 :
1401 : /* When pthread_setstack is not used to explicitly set the memory region
1402 : for a new thread's stack, pthread_create will create a memory region
1403 : (using either the requested size or a default size). And, while
1404 : pthread allows us to set and get the size of the stack region it
1405 : creates and we can get a pointer into a thread's stack by just
1406 : declaring a stack variable in that thread and we obviously know where
1407 : a thread's stack is when we explicitly specify it to pthread create,
1408 : pthreads does not seem to provide a simple way to get the extents of
1409 : the stacks it creates.
1410 :
1411 : But the relationship between a pointer in the stack and the stack
1412 : extents is non-trival because pthreads will use some of the stack for
1413 : things like thread local storage (and it will not tell us how much
1414 : stack was used by that and this is practically only known after
1415 : linking is complete and then it is not simply exposed to the
1416 : application).
1417 :
1418 : Similar uncertainty applies to the first thread's stack. We can
1419 : learn how large the stack is and get a pointer into the stack via a
1420 : stack variable but we have no simple way to get the extents. And, in
1421 : this case on recent Linux, things like command line strings and
1422 : environment strings are typically allowed to consume up to 1/4 of
1423 : main's thread stack ... these are only known at application load
1424 : time. (There is the additional caveat that the main stack might be
1425 : dynamically allocated such that the address space reserved for it
1426 : might not be backed by memory yet.)
1427 :
1428 : But, if we want to do useful run-time stack diagnostics (e.g. alloca
1429 : bounds checking / stack overflow prevention / etc), having explicit
1430 : knowledge of a thread's stack extents is very useful. Hence the
1431 : below. It would be nice if there was portable and non-horrific way
1432 : to do this (an even more horrific way is trigger seg faults by
1433 : scanning for the guard pages and then recover from the seg fault via
1434 : a longjmp ... shivers). */
1435 :
1436 : void
1437 : fd_log_private_stack_discover( ulong stack_sz,
1438 : ulong * _stack0,
1439 2555 : ulong * _stack1 ) {
1440 :
1441 2555 : if( FD_UNLIKELY( !stack_sz ) ) {
1442 0 : *_stack0 = 0UL;
1443 0 : *_stack1 = 0UL;
1444 0 : return;
1445 0 : }
1446 :
1447 2555 : ulong stack0 = 0UL;
1448 2555 : ulong stack1 = 0UL;
1449 :
1450 : /* Create a variable on the caller's stack and scan the thread group's
1451 : memory map for the memory region holding the variable. That should
1452 : be the caller's stack. */
1453 :
1454 2555 : uchar stack_mem[1];
1455 2555 : FD_VOLATILE( stack_mem[0] ) = (uchar)1; /* Paranoia to make sure compiler puts this in stack */
1456 2555 : ulong stack_addr = (ulong)stack_mem;
1457 :
1458 2555 : int filefd;
1459 2555 : if( FD_UNLIKELY( ( filefd = open( "/proc/self/maps", O_RDONLY ) ) < 0 ) ) {
1460 0 : FD_LOG_WARNING(( "open( \"/proc/self/maps\" ) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1461 0 : *_stack0 = 0UL;
1462 0 : *_stack1 = 0UL;
1463 0 : return;
1464 0 : }
1465 :
1466 2555 : char filebuf[1<<12];
1467 2555 : ulong filelen = 0;
1468 2555 : char * p = filebuf;
1469 2555 : int found = 0;
1470 97561 : while( !found ) {
1471 :
1472 : /* Scan a line */
1473 :
1474 97561 : int full_line = 0;
1475 97561 : char * nextp;
1476 10043076 : for( nextp = p; nextp < filebuf + filelen; ) {
1477 10040505 : if( *(nextp++) == '\n' ) {
1478 94990 : full_line = 1;
1479 94990 : break;
1480 94990 : }
1481 10040505 : }
1482 97561 : if( !full_line ) {
1483 : /* We need to read more data. First shift the old data down. */
1484 2571 : filelen = (ulong)((filebuf + filelen) - p);
1485 2571 : if( filelen )
1486 0 : memmove( filebuf, p, filelen );
1487 2571 : p = filebuf;
1488 : /* Now read data */
1489 2571 : ssize_t tlen;
1490 2571 : if( FD_UNLIKELY( ( tlen = read( filefd, filebuf + filelen, sizeof(filebuf)-1U-filelen ) ) < 0 ) ) {
1491 0 : FD_LOG_WARNING(( "read( \"/proc/self/maps\" ) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
1492 0 : break;
1493 0 : }
1494 2571 : if( tlen == 0 ) break; /* End of file */
1495 2571 : filelen += (ulong)tlen;
1496 2571 : filebuf[filelen] = '\0'; /* For sscanf */
1497 2571 : continue;
1498 2571 : }
1499 :
1500 94990 : ulong m0;
1501 94990 : ulong m1;
1502 94990 : int r = sscanf( p, "%lx-%lx", &m0, &m1 );
1503 94990 : p = nextp;
1504 94990 : if( FD_UNLIKELY( r !=2 ) ) continue;
1505 :
1506 : /* Test if the stack allocation is in the discovered region */
1507 :
1508 94990 : if( FD_UNLIKELY( (m0<=stack_addr) & (stack_addr<m1) ) ) {
1509 2555 : found = 1;
1510 2555 : ulong msz = m1 - m0;
1511 2555 : if( msz==stack_sz ) { /* Memory region matches expectations */
1512 0 : stack0 = m0;
1513 0 : stack1 = m1;
1514 2555 : } else if( ((fd_log_group_id()==fd_log_tid()) & (msz<stack_sz)) ) {
1515 : /* This is the main thread, which, on recent Linux, seems to
1516 : just reserve address space for main's stack at program
1517 : start up to the application stack size limits then uses
1518 : page faults to dynamically back the stack with DRAM as the
1519 : stack grows (which is awful for performance, jitter and
1520 : reliability ... sigh). This assumes stack grows down such
1521 : that m1 is the fixed value in this process. */
1522 2555 : stack0 = m1 - stack_sz;
1523 2555 : stack1 = m1;
1524 2555 : } else {
1525 0 : FD_LOG_WARNING(( "unexpected caller stack memory region size (got %lu bytes, expected %lu bytes)", msz, stack_sz ));
1526 : /* don't trust the discovered region */
1527 0 : }
1528 2555 : break;
1529 2555 : }
1530 :
1531 94990 : }
1532 2555 : if( !found )
1533 0 : FD_LOG_WARNING(( "unable to find stack size around address 0x%lx", stack_addr ));
1534 :
1535 2555 : close(filefd);
1536 :
1537 2555 : *_stack0 = stack0;
1538 2555 : *_stack1 = stack1;
1539 2555 : }
1540 :
1541 :
1542 : int
1543 2555 : fd_log_should_colorize( void ) {
1544 2555 : char const * no_color = fd_env_strip_cmdline_cstr( NULL, NULL, NULL, "NO_COLOR", NULL );
1545 2555 : if( FD_UNLIKELY( no_color && no_color[0]!='\0' ) ) return 0;
1546 :
1547 2555 : if( FD_UNLIKELY( !isatty( STDERR_FILENO ) ) ) return 0;
1548 :
1549 0 : char const * term = fd_env_strip_cmdline_cstr( NULL, NULL, NULL, "TERM", NULL );
1550 0 : if( FD_UNLIKELY( !term || !strcmp( term, "dumb" ) ) ) return 0;
1551 :
1552 0 : char const * dirs[] = {
1553 0 : fd_env_strip_cmdline_cstr( NULL, NULL, NULL, "TERMINFO", NULL ),
1554 0 : NULL,
1555 0 : "/usr/share/terminfo",
1556 0 : "/usr/lib/terminfo",
1557 0 : "/etc/terminfo",
1558 0 : };
1559 0 : char homebuf[ PATH_MAX ];
1560 0 : char const * home = fd_env_strip_cmdline_cstr( NULL, NULL, NULL, "HOME", NULL );
1561 0 : if( FD_LIKELY( home ) ) {
1562 0 : snprintf( homebuf, sizeof(homebuf), "%s/.terminfo", home );
1563 0 : dirs[1] = homebuf;
1564 0 : }
1565 :
1566 0 : FILE * f = NULL;
1567 0 : char path[ PATH_MAX ];
1568 0 : for( ulong i=0UL; i<(sizeof(dirs)/sizeof(dirs[0])); i++ ) {
1569 0 : if( FD_UNLIKELY( !dirs[i] ) ) continue;
1570 0 : snprintf( path, sizeof(path), "%s/%c/%s", dirs[i], term[0], term );
1571 0 : f = fopen( path, "rb" );
1572 0 : if( f ) break;
1573 0 : snprintf( path, sizeof(path), "%s/%02x/%s", dirs[i], (uchar)term[0], term );
1574 0 : f = fopen( path, "rb" );
1575 0 : if( f ) break;
1576 0 : }
1577 0 : if( FD_UNLIKELY( !f ) ) return 0;
1578 :
1579 0 : ushort hdr[6];
1580 0 : if( FD_UNLIKELY( fread( hdr, 2, 6, f )!=6 ) ) goto fail;
1581 :
1582 0 : ushort magic = hdr[0];
1583 0 : ushort name_sz = hdr[1];
1584 0 : ushort bool_cnt = hdr[2];
1585 0 : ushort num_cnt = hdr[3];
1586 :
1587 0 : uint num_width;
1588 0 : if( magic==0x011A ) num_width = 2;
1589 0 : else if( magic==0x021E ) num_width = 4;
1590 0 : else goto fail;
1591 :
1592 0 : if( FD_UNLIKELY( 13>=num_cnt ) ) goto fail;
1593 :
1594 0 : long skip = (long)name_sz + (long)bool_cnt;
1595 0 : if( skip%2 ) skip++;
1596 0 : skip += 13L * (long)num_width;
1597 :
1598 0 : if( FD_UNLIKELY( fseek( f, skip, SEEK_CUR ) ) ) goto fail;
1599 :
1600 0 : int colors;
1601 0 : if( num_width==2 ) {
1602 0 : short v;
1603 0 : if( FD_UNLIKELY( fread( &v, 2, 1, f )!=1 ) ) goto fail;
1604 0 : colors = v;
1605 0 : } else {
1606 0 : if( FD_UNLIKELY( fread( &colors, 4, 1, f )!=1 ) ) goto fail;
1607 0 : }
1608 :
1609 0 : fclose( f );
1610 0 : return colors>0;
1611 :
1612 0 : fail:
1613 0 : fclose( f );
1614 0 : return 0;
1615 0 : }
1616 :
1617 : #elif FD_LOG_STYLE==1 /* generic embedded target */
1618 :
1619 : #include "fd_log.h"
1620 :
1621 : FD_FN_UNUSED static inline void _unused( void ) {} /* required due to -Wpedantic */
1622 :
1623 : #else
1624 : #error "Unknown FD_LOG_STYLE"
1625 : #endif
|