| 1 | /* |
| 2 | This file is part of GOTCHA. For copyright information see the COPYRIGHT |
| 3 | file in the top level directory, or at |
| 4 | https://github.com/LLNL/gotcha/blob/master/COPYRIGHT |
| 5 | This program is free software; you can redistribute it and/or modify it under |
| 6 | the terms of the GNU Lesser General Public License (as published by the Free |
| 7 | Software Foundation) version 2.1 dated February 1999. This program is |
| 8 | distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 9 | without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 10 | PURPOSE. See the terms and conditions of the GNU Lesser General Public License |
| 11 | for more details. You should have received a copy of the GNU Lesser General |
| 12 | Public License along with this program; if not, write to the Free Software |
| 13 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 14 | */ |
| 15 | |
| 16 | /*! |
| 17 | ****************************************************************************** |
| 18 | * |
| 19 | * \file gotcha_utils.h |
| 20 | * |
| 21 | * \brief Header file containing the internal gotcha mechanisms |
| 22 | * for manipulating the running process to redirect calls |
| 23 | * |
| 24 | ****************************************************************************** |
| 25 | */ |
| 26 | #ifndef GOTCHA_UTILS_H |
| 27 | #define GOTCHA_UTILS_H |
| 28 | #include <sys/mman.h> |
| 29 | |
| 30 | #include "gotcha/gotcha_types.h" |
| 31 | #include "hash.h" |
| 32 | // TODO: remove these includes |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | // END TODO |
| 37 | #include <elf.h> |
| 38 | #include <link.h> |
| 39 | #include <string.h> |
| 40 | |
| 41 | #define KNOWN_UNUSED __attribute__((unused)) |
| 42 | |
| 43 | #define GOTCHA_DEBUG_ENV "GOTCHA_DEBUG" |
| 44 | extern int debug_level; |
| 45 | void gotcha_init(); |
| 46 | extern hash_table_t function_hash_table; |
| 47 | extern hash_table_t notfound_binding_table; |
| 48 | #define debug_bare_printf(lvl, format, ...) \ |
| 49 | do { \ |
| 50 | if (debug_level >= lvl) { \ |
| 51 | gotcha_dbg_printf(format, ##__VA_ARGS__); \ |
| 52 | } \ |
| 53 | } while (0); |
| 54 | |
| 55 | #define SHORT_FILE__ ((strrchr(__FILE__, '/') ?: __FILE__ - 1) + 1) |
| 56 | |
| 57 | #define debug_printf(lvl, format, ...) \ |
| 58 | do { \ |
| 59 | if (debug_level >= lvl) { \ |
| 60 | gotcha_dbg_printf("[%d/%d][%s:%u] - " format, gotcha_gettid(), \ |
| 61 | gotcha_getpid(), SHORT_FILE__, __LINE__, \ |
| 62 | ##__VA_ARGS__); \ |
| 63 | } \ |
| 64 | } while (0); |
| 65 | |
| 66 | #define error_printf(format, ...) \ |
| 67 | do { \ |
| 68 | if (debug_level) { \ |
| 69 | gotcha_dbg_printf("ERROR [%d/%d][%s:%u] - " format, gotcha_gettid(), \ |
| 70 | gotcha_getpid(), SHORT_FILE__, __LINE__, \ |
| 71 | ##__VA_ARGS__); \ |
| 72 | } \ |
| 73 | } while (0); |
| 74 | |
| 75 | #define LIB_NAME(X) \ |
| 76 | (!X->l_name ? "[NULL]" : (!*X->l_name ? "[EMPTY]" : X->l_name)) |
| 77 | |
| 78 | /*! |
| 79 | ****************************************************************************** |
| 80 | * \def R_SYM(X) |
| 81 | * \brief Returns an ELF symbol which is correct for the current architecture |
| 82 | * \param X The value you wish to cast to Symbol type |
| 83 | ****************************************************************************** |
| 84 | */ |
| 85 | #if __WORDSIZE == 64 |
| 86 | #define R_SYM(X) ELF64_R_SYM(X) |
| 87 | #else |
| 88 | #define R_SYM(X) ELF32_R_SYM(X) |
| 89 | #endif |
| 90 | |
| 91 | /*! |
| 92 | ****************************************************************************** |
| 93 | * \def BOUNDARY_BEFORE(ptr,pagesize) |
| 94 | * \brief Returns the address on page boundary before the given pointer |
| 95 | * \param ptr The address you wish to get the page boundary before |
| 96 | * \param pagesize The page size you wish to align to |
| 97 | ****************************************************************************** |
| 98 | */ |
| 99 | #define BOUNDARY_BEFORE(ptr, pagesize) \ |
| 100 | (ElfW(Addr))(((ElfW(Addr))ptr) & (-pagesize)) |
| 101 | |
| 102 | #endif |