| 1 | /* |
| 2 | * Copyright (C) 2012 Christian Babeux <christian.babeux@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef NTESTPOINT |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include "testpoint.hpp" |
| 12 | |
| 13 | #include <dlfcn.h> /* for dlsym */ |
| 14 | #include <stdlib.h> /* for getenv */ |
| 15 | #include <string.h> /* for strncmp */ |
| 16 | |
| 17 | /* Environment variable used to enable the testpoints facilities. */ |
| 18 | static const char *lttng_testpoint_env_var = "LTTNG_TESTPOINT_ENABLE"; |
| 19 | |
| 20 | /* Testpoint toggle flag */ |
| 21 | int lttng_testpoint_activated; |
| 22 | |
| 23 | /* |
| 24 | * Toggle the support for testpoints on the application startup. |
| 25 | */ |
| 26 | static void __attribute__((constructor)) lttng_testpoint_check() |
| 27 | { |
| 28 | char *testpoint_env_val = nullptr; |
| 29 | |
| 30 | testpoint_env_val = getenv(lttng_testpoint_env_var); |
| 31 | if (testpoint_env_val != nullptr && (strncmp(testpoint_env_val, "1", 1) == 0)) { |
| 32 | lttng_testpoint_activated = 1; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * Lookup a symbol by name. |
| 38 | * |
| 39 | * Return the address where the symbol is loaded or NULL if the symbol was not |
| 40 | * found. |
| 41 | */ |
| 42 | void *lttng_testpoint_lookup(const char *name) |
| 43 | { |
| 44 | if (!name) { |
| 45 | return nullptr; |
| 46 | } |
| 47 | |
| 48 | return dlsym(RTLD_DEFAULT, name); |
| 49 | } |
| 50 | |
| 51 | #endif /* NTESTPOINT */ |