Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
trace.h311 linesDownload Raw Back to openssl
1/*2 * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.3 *4 * Licensed under the Apache License 2.0 (the "License").  You may not use5 * this file except in compliance with the License.  You can obtain a copy6 * in the file LICENSE in the source distribution or at7 * https://www.openssl.org/source/license.html8 */9 10#ifndef OPENSSL_TRACE_H11# define OPENSSL_TRACE_H12# pragma once13 14# include <stdarg.h>15 16# include <openssl/bio.h>17 18# ifdef  __cplusplus19extern "C" {20# endif21 22/*23 * TRACE CATEGORIES24 */25 26/*27 * The trace messages of the OpenSSL libraries are organized into different28 * categories. For every trace category, the application can register a separate29 * tracer callback. When a callback is registered, a so called trace channel is30 * created for this category. This channel consists essentially of an internal31 * BIO which sends all trace output it receives to the registered application32 * callback.33 *34 * The ALL category can be used as a fallback category to register a single35 * channel which receives the output from all categories. However, if the36 * application intends to print the trace channel name in the line prefix,37 * it is better to register channels for all categories separately.38 * (This is how the openssl application does it.)39 */40# define OSSL_TRACE_CATEGORY_ALL                 0 /* The fallback */41# define OSSL_TRACE_CATEGORY_TRACE               142# define OSSL_TRACE_CATEGORY_INIT                243# define OSSL_TRACE_CATEGORY_TLS                 344# define OSSL_TRACE_CATEGORY_TLS_CIPHER          445# define OSSL_TRACE_CATEGORY_CONF                546# define OSSL_TRACE_CATEGORY_ENGINE_TABLE        647# define OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT    748# define OSSL_TRACE_CATEGORY_PKCS5V2             849# define OSSL_TRACE_CATEGORY_PKCS12_KEYGEN       950# define OSSL_TRACE_CATEGORY_PKCS12_DECRYPT     1051# define OSSL_TRACE_CATEGORY_X509V3_POLICY      1152# define OSSL_TRACE_CATEGORY_BN_CTX             1253# define OSSL_TRACE_CATEGORY_CMP                1354# define OSSL_TRACE_CATEGORY_STORE              1455# define OSSL_TRACE_CATEGORY_DECODER            1556# define OSSL_TRACE_CATEGORY_ENCODER            1657# define OSSL_TRACE_CATEGORY_REF_COUNT          1758/* Count of available categories. */59# define OSSL_TRACE_CATEGORY_NUM                1860 61/* Returns the trace category number for the given |name| */62int OSSL_trace_get_category_num(const char *name);63 64/* Returns the trace category name for the given |num| */65const char *OSSL_trace_get_category_name(int num);66 67/*68 * TRACE CONSUMERS69 */70 71/*72 * Enables tracing for the given |category| by providing a BIO sink73 * as |channel|. If a null pointer is passed as |channel|, an existing74 * trace channel is removed and tracing for the category is disabled.75 *76 * Returns 1 on success and 0 on failure77 */78int OSSL_trace_set_channel(int category, BIO* channel);79 80/*81 * Attach a prefix and a suffix to the given |category|, to be printed at the82 * beginning and at the end of each trace output group, i.e. when83 * OSSL_trace_begin() and OSSL_trace_end() are called.84 * If a null pointer is passed as argument, the existing prefix or suffix is85 * removed.86 *87 * They return 1 on success and 0 on failure88 */89int OSSL_trace_set_prefix(int category, const char *prefix);90int OSSL_trace_set_suffix(int category, const char *suffix);91 92/*93 * OSSL_trace_cb is the type tracing callback provided by the application.94 * It MUST return the number of bytes written, or 0 on error (in other words,95 * it can never write zero bytes).96 *97 * The |buffer| will always contain text, which may consist of several lines.98 * The |data| argument points to whatever data was provided by the application99 * when registering the tracer function.100 *101 * The |category| number is given, as well as a |cmd| number, described below.102 */103typedef size_t (*OSSL_trace_cb)(const char *buffer, size_t count,104                                int category, int cmd, void *data);105/*106 * Possible |cmd| numbers.107 */108# define OSSL_TRACE_CTRL_BEGIN  0109# define OSSL_TRACE_CTRL_WRITE  1110# define OSSL_TRACE_CTRL_END    2111 112/*113 * Enables tracing for the given |category| by creating an internal114 * trace channel which sends the output to the given |callback|.115 * If a null pointer is passed as callback, an existing trace channel116 * is removed and tracing for the category is disabled.117 *118 * NOTE: OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually119 *       exclusive.120 *121 * Returns 1 on success and 0 on failure122 */123int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data);124 125/*126 * TRACE PRODUCERS127 */128 129/*130 * Returns 1 if tracing for the specified category is enabled, otherwise 0131 */132int OSSL_trace_enabled(int category);133 134/*135 * Wrap a group of tracing output calls.  OSSL_trace_begin() locks tracing and136 * returns the trace channel associated with the given category, or NULL if no137 * channel is associated with the category.  OSSL_trace_end() unlocks tracing.138 *139 * Usage:140 *141 *    BIO *out;142 *    if ((out = OSSL_trace_begin(category)) != NULL) {143 *        ...144 *        BIO_fprintf(out, ...);145 *        ...146 *        OSSL_trace_end(category, out);147 *    }148 *149 * See also the convenience macros OSSL_TRACE_BEGIN and OSSL_TRACE_END below.150 */151BIO *OSSL_trace_begin(int category);152void OSSL_trace_end(int category, BIO *channel);153 154/*155 * OSSL_TRACE* Convenience Macros156 */157 158/*159 * When the tracing feature is disabled, these macros are defined to160 * produce dead code, which a good compiler should eliminate.161 */162 163/*164 * OSSL_TRACE_BEGIN, OSSL_TRACE_END - Define a Trace Group165 *166 * These two macros can be used to create a block which is executed only167 * if the corresponding trace category is enabled. Inside this block, a168 * local variable named |trc_out| is defined, which points to the channel169 * associated with the given trace category.170 *171 * Usage: (using 'TLS' as an example category)172 *173 *     OSSL_TRACE_BEGIN(TLS) {174 *175 *         BIO_fprintf(trc_out, ... );176 *177 *     } OSSL_TRACE_END(TLS);178 *179 *180 * This expands to the following code181 *182 *     do {183 *         BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);184 *         if (trc_out != NULL) {185 *             ...186 *             BIO_fprintf(trc_out, ...);187 *         }188 *         OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);189 *     } while (0);190 *191 * The use of the inner '{...}' group and the trailing ';' is enforced192 * by the definition of the macros in order to make the code look as much193 * like C code as possible.194 *195 * Before returning from inside the trace block, it is necessary to196 * call OSSL_TRACE_CANCEL(category).197 */198 199# if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE200 201#  define OSSL_TRACE_BEGIN(category) \202    do { \203        BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_##category); \204 \205        if (trc_out != NULL)206 207#  define OSSL_TRACE_END(category) \208        OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out); \209    } while (0)210 211#  define OSSL_TRACE_CANCEL(category) \212        OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out) \213 214# else215 216#  define OSSL_TRACE_BEGIN(category)           \217    do {                                        \218        BIO *trc_out = NULL;                    \219        if (0)220 221#  define OSSL_TRACE_END(category)             \222    } while(0)223 224#  define OSSL_TRACE_CANCEL(category)          \225    ((void)0)226 227# endif228 229/*230 * OSSL_TRACE_ENABLED() - Check whether tracing is enabled for |category|231 *232 * Usage:233 *234 *     if (OSSL_TRACE_ENABLED(TLS)) {235 *         ...236 *     }237 */238# if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE239 240#  define OSSL_TRACE_ENABLED(category) \241    OSSL_trace_enabled(OSSL_TRACE_CATEGORY_##category)242 243# else244 245#  define OSSL_TRACE_ENABLED(category) (0)246 247# endif248 249/*250 * OSSL_TRACE*() - OneShot Trace Macros251 *252 * These macros are intended to produce a simple printf-style trace output.253 * Unfortunately, C90 macros don't support variable arguments, so the254 * "vararg" OSSL_TRACEV() macro has a rather weird usage pattern:255 *256 *    OSSL_TRACEV(category, (trc_out, "format string", ...args...));257 *258 * Where 'channel' is the literal symbol of this name, not a variable.259 * For that reason, it is currently not intended to be used directly,260 * but only as helper macro for the other oneshot trace macros261 * OSSL_TRACE(), OSSL_TRACE1(), OSSL_TRACE2(), ...262 *263 * Usage:264 *265 *    OSSL_TRACE(INIT, "Hello world!\n");266 *    OSSL_TRACE1(TLS, "The answer is %d\n", 42);267 *    OSSL_TRACE2(TLS, "The ultimate question to answer %d is '%s'\n",268 *                42, "What do you get when you multiply six by nine?");269 */270 271# if !defined OPENSSL_NO_TRACE && !defined FIPS_MODULE272 273#  define OSSL_TRACEV(category, args) \274    OSSL_TRACE_BEGIN(category) \275        BIO_printf args; \276    OSSL_TRACE_END(category)277 278# else279 280#  define OSSL_TRACEV(category, args) ((void)0)281 282# endif283 284# define OSSL_TRACE(category, text) \285    OSSL_TRACEV(category, (trc_out, "%s", text))286 287# define OSSL_TRACE1(category, format, arg1) \288    OSSL_TRACEV(category, (trc_out, format, arg1))289# define OSSL_TRACE2(category, format, arg1, arg2) \290    OSSL_TRACEV(category, (trc_out, format, arg1, arg2))291# define OSSL_TRACE3(category, format, arg1, arg2, arg3) \292    OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3))293# define OSSL_TRACE4(category, format, arg1, arg2, arg3, arg4) \294    OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4))295# define OSSL_TRACE5(category, format, arg1, arg2, arg3, arg4, arg5) \296    OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5))297# define OSSL_TRACE6(category, format, arg1, arg2, arg3, arg4, arg5, arg6) \298    OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6))299# define OSSL_TRACE7(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \300    OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7))301# define OSSL_TRACE8(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \302    OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))303# define OSSL_TRACE9(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \304    OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))305 306# ifdef  __cplusplus307}308# endif309 310#endif311 
codekingpro/portable-devtools · Team Ai