Print thread IDs nicely.
[openssl.git] / test / testutil / init.c
1 /*
2  * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11 #include <openssl/opensslconf.h>
12 #include <openssl/trace.h>
13 #include "apps.h"
14 #include "../testutil.h"
15
16 #ifndef OPENSSL_NO_TRACE
17 typedef struct tracedata_st {
18     BIO *bio;
19     unsigned int ingroup:1;
20 } tracedata;
21
22 static size_t internal_trace_cb(const char *buf, size_t cnt,
23                                 int category, int cmd, void *vdata)
24 {
25     int ret = 0;
26     tracedata *trace_data = vdata;
27     char buffer[256], *hex;
28     CRYPTO_THREAD_ID tid;
29
30     switch (cmd) {
31     case OSSL_TRACE_CTRL_BEGIN:
32         trace_data->ingroup = 1;
33
34         tid = CRYPTO_THREAD_get_current_id();
35         hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
36         BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ",
37                      hex, OSSL_trace_get_category_name(category));
38         OPENSSL_free(hex);
39         BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX,
40                  strlen(buffer), buffer);
41         break;
42     case OSSL_TRACE_CTRL_WRITE:
43         ret = BIO_write(trace_data->bio, buf, cnt);
44         break;
45     case OSSL_TRACE_CTRL_END:
46         trace_data->ingroup = 0;
47
48         BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX, 0, NULL);
49         break;
50     }
51
52     return ret < 0 ? 0 : ret;
53 }
54
55 DEFINE_STACK_OF(tracedata)
56 static STACK_OF(tracedata) *trace_data_stack;
57
58 static void tracedata_free(tracedata *data)
59 {
60     BIO_free_all(data->bio);
61     OPENSSL_free(data);
62 }
63
64 static STACK_OF(tracedata) *trace_data_stack;
65
66 static void cleanup_trace(void)
67 {
68     sk_tracedata_pop_free(trace_data_stack, tracedata_free);
69 }
70
71 static void setup_trace_category(int category)
72 {
73     BIO *channel;
74     tracedata *trace_data;
75
76     if (OSSL_trace_enabled(category))
77         return;
78
79     channel = BIO_push(BIO_new(apps_bf_prefix()),
80                        BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT));
81     trace_data = OPENSSL_zalloc(sizeof(*trace_data));
82
83     if (trace_data == NULL
84         || (trace_data->bio = channel) == NULL
85         || OSSL_trace_set_callback(category, internal_trace_cb,
86                                    trace_data) == 0
87         || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
88
89         fprintf(stderr,
90                 "warning: unable to setup trace callback for category '%s'.\n",
91                 OSSL_trace_get_category_name(category));
92
93         OSSL_trace_set_callback(category, NULL, NULL);
94         BIO_free_all(channel);
95     }
96 }
97
98 static void setup_trace(const char *str)
99 {
100     char *val;
101
102     /*
103      * We add this handler as early as possible to ensure it's executed
104      * as late as possible, i.e. after the TRACE code has done its cleanup
105      * (which happens last in OPENSSL_cleanup).
106      */
107     atexit(cleanup_trace);
108
109     trace_data_stack = sk_tracedata_new_null();
110     val = OPENSSL_strdup(str);
111
112     if (val != NULL) {
113         char *valp = val;
114         char *item;
115
116         for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
117             int category = OSSL_trace_get_category_num(item);
118
119             if (category == OSSL_TRACE_CATEGORY_ALL) {
120                 while (++category < OSSL_TRACE_CATEGORY_NUM)
121                     setup_trace_category(category);
122                 break;
123             } else if (category > 0) {
124                 setup_trace_category(category);
125             } else {
126                 fprintf(stderr,
127                         "warning: unknown trace category: '%s'.\n", item);
128             }
129         }
130     }
131
132     OPENSSL_free(val);
133 }
134 #endif /* OPENSSL_NO_TRACE */
135
136 int global_init(void)
137 {
138 #ifndef OPENSSL_NO_TRACE
139     setup_trace(getenv("OPENSSL_TRACE"));
140 #endif
141
142     return 1;
143 }