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