Fix: remove NULL check of nonnull parameter in dlclose
[lttng-ust.git] / liblttng-ust-dl / lttng-ust-dl.c
1 /*
2 * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com>
3 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; version 2.1 of
8 * the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #define _LGPL_SOURCE
21 #define _GNU_SOURCE
22
23 #include <limits.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include <lttng/ust-dlfcn.h>
29 #include <lttng/ust-elf.h>
30 #include "usterr-signal-safe.h"
31
32 /* Include link.h last else it conflicts with ust-dlfcn. */
33 #include <link.h>
34
35 #define TRACEPOINT_DEFINE
36 #include "ust_dl.h"
37
38 static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flag);
39 static int (*__lttng_ust_plibc_dlclose)(void *handle);
40
41 static
42 void *_lttng_ust_dl_libc_dlopen(const char *filename, int flag)
43 {
44 if (!__lttng_ust_plibc_dlopen) {
45 __lttng_ust_plibc_dlopen = dlsym(RTLD_NEXT, "dlopen");
46 if (!__lttng_ust_plibc_dlopen) {
47 fprintf(stderr, "%s\n", dlerror());
48 return NULL;
49 }
50 }
51 return __lttng_ust_plibc_dlopen(filename, flag);
52 }
53
54 static
55 int _lttng_ust_dl_libc_dlclose(void *handle)
56 {
57 if (!__lttng_ust_plibc_dlclose) {
58 __lttng_ust_plibc_dlclose = dlsym(RTLD_NEXT, "dlclose");
59 if (!__lttng_ust_plibc_dlclose) {
60 fprintf(stderr, "%s\n", dlerror());
61 return -1;
62 }
63 }
64 return __lttng_ust_plibc_dlclose(handle);
65 }
66
67 static
68 void lttng_ust_dl_dlopen(void *so_base, const char *so_name, void *ip)
69 {
70 char resolved_path[PATH_MAX];
71 struct lttng_ust_elf *elf;
72 uint64_t memsz;
73 uint8_t *build_id = NULL;
74 size_t build_id_len;
75 char *dbg_file = NULL;
76 uint32_t crc;
77 int has_build_id = 0, has_debug_link = 0;
78 int ret;
79
80 if (!realpath(so_name, resolved_path)) {
81 ERR("could not resolve path '%s'", so_name);
82 return;
83 }
84
85 elf = lttng_ust_elf_create(resolved_path);
86 if (!elf) {
87 ERR("could not acces file %s", resolved_path);
88 return;
89 }
90
91 ret = lttng_ust_elf_get_memsz(elf, &memsz);
92 if (ret) {
93 goto end;
94 }
95 ret = lttng_ust_elf_get_build_id(
96 elf, &build_id, &build_id_len, &has_build_id);
97 if (ret) {
98 goto end;
99 }
100 ret = lttng_ust_elf_get_debug_link(
101 elf, &dbg_file, &crc, &has_debug_link);
102 if (ret) {
103 goto end;
104 }
105
106 tracepoint(lttng_ust_dl, dlopen,
107 ip, so_base, resolved_path, memsz);
108
109 if (has_build_id) {
110 tracepoint(lttng_ust_dl, build_id,
111 ip, so_base, build_id, build_id_len);
112 }
113
114 if (has_debug_link) {
115 tracepoint(lttng_ust_dl, debug_link,
116 ip, so_base, dbg_file, crc);
117 }
118
119 end:
120 free(dbg_file);
121 free(build_id);
122 lttng_ust_elf_destroy(elf);
123 return;
124 }
125
126 void *dlopen(const char *filename, int flag)
127 {
128 void *handle;
129
130 handle = _lttng_ust_dl_libc_dlopen(filename, flag);
131 if (__tracepoint_ptrs_registered && handle) {
132 struct link_map *p = NULL;
133 int ret;
134
135 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
136 if (ret != -1 && p != NULL && p->l_addr != 0) {
137 lttng_ust_dl_dlopen((void *) p->l_addr, p->l_name,
138 __builtin_return_address(0));
139 }
140 }
141
142 return handle;
143 }
144
145 int dlclose(void *handle)
146 {
147 if (__tracepoint_ptrs_registered) {
148 struct link_map *p = NULL;
149 int ret;
150
151 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
152 if (ret != -1 && p != NULL && p->l_addr != 0) {
153 tracepoint(lttng_ust_dl, dlclose,
154 __builtin_return_address(0),
155 (void *) p->l_addr);
156 }
157 }
158
159 return _lttng_ust_dl_libc_dlclose(handle);
160 }
This page took 0.039127 seconds and 5 git commands to generate.