Debian Codegraph @

ext/GOTCHA/src/gotcha_utils.c

raw symbols SCIP
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 #include "gotcha_utils.h"
17
18 #include <stdlib.h>
19
20 #include "elf_ops.h"
21 #include "gotcha/gotcha.h"
22 #include "gotcha_dl.h"
23 #include "hash.h"
24 #include "libc_wrappers.h"
25 #include "tool.h"
26
27 int debug_level;
28 static void debug_init() {
29 static int debug_initialized = 0;
30
31 char *debug_str;
32 if (debug_initialized) {
33 return; // GCOVR_EXCL_LINE
34 }
35 debug_initialized = 1;
36
37 debug_str = gotcha_getenv(GOTCHA_DEBUG_ENV);
38 if (!debug_str) {
39 return;
40 }
41
42 debug_level = gotcha_atoi(debug_str);
43 if (debug_level <= 0) debug_level = 1; // GCOVR_EXCL_LINE
44
45 debug_printf(0, "Gotcha debug initialized at level %d\n", debug_level);
46 }
47
48 hash_table_t function_hash_table;
49 hash_table_t notfound_binding_table;
50
51 static hash_table_t library_table;
52 static library_t *library_list = NULL;
53 unsigned int current_generation;
54
55 static hash_hashvalue_t link_map_hash(struct link_map *map) {
56 hash_hashvalue_t hashval = (hash_hashvalue_t)((unsigned long)map);
57 hashval ^= strhash(LIB_NAME(map));
58 return hashval;
59 }
60
61 static int link_map_cmp(struct link_map *a, struct link_map *b) {
62 return ((unsigned long)a) < ((unsigned long)b);
63 }
64
65 static void setup_hash_tables() {
66 create_hashtable(&library_table, 128, (hash_func_t)link_map_hash,
67 (hash_cmp_t)link_map_cmp);
68 create_hashtable(&function_hash_table, 4096, (hash_func_t)strhash,
69 (hash_cmp_t)gotcha_strcmp);
70 create_hashtable(&notfound_binding_table, 128, (hash_func_t)strhash,
71 (hash_cmp_t)gotcha_strcmp);
72 }
73
74 struct library_t *get_library(struct link_map *map) {
75 library_t *lib;
76 int result;
77 result =
78 lookup_hashtable(&library_table, (hash_key_t)map, (hash_data_t *)&lib);
79 if (result == -1) return NULL;
80 return lib;
81 }
82
83 struct library_t *add_library(struct link_map *map) {
84 library_t *newlib = gotcha_malloc(sizeof(library_t));
85 newlib->map = map;
86 newlib->flags = 0;
87 newlib->generation = 0;
88 newlib->next = library_list;
89 newlib->prev = NULL;
90 if (library_list) library_list->prev = newlib;
91 library_list = newlib;
92 addto_hashtable(&library_table, (hash_key_t)map, (hash_data_t)newlib);
93 return newlib;
94 }
95
96 void remove_library(struct link_map *map) {
97 library_t *lib = get_library(map);
98 if (!lib) return;
99 if (lib->prev) lib->prev->next = lib->next;
100 if (lib->next) lib->next->prev = lib->prev;
101 if (lib == library_list) library_list = library_list->next;
102 removefrom_hashtable(&library_table, (hash_key_t)map);
103 memset(lib, 0, sizeof(library_t));
104 gotcha_free(lib);
105 }
106
107 void gotcha_init() {
108 static int gotcha_initialized = 0;
109 if (gotcha_initialized) {
110 return;
111 }
112 gotcha_initialized = 1;
113 debug_init();
114 setup_hash_tables();
115 handle_libdl();
116 }