Communication protocol: use fixed-size integer rather than enum
[lttng-ust.git] / liblttng-ust-dl / lttng-ust-dl.c
CommitLineData
b13d93c2
PW
1/*
2 * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com>
8e2aed3f 3 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
b13d93c2
PW
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 _GNU_SOURCE
1ddceb36 21#define _LGPL_SOURCE
13436238 22#include <limits.h>
8e2aed3f 23#include <stdio.h>
13436238 24#include <sys/types.h>
8e2aed3f
AB
25#include <unistd.h>
26
27#include <lttng/ust-dlfcn.h>
28#include <lttng/ust-elf.h>
171fcc6f 29#include <helper.h>
eb2b066f 30#include "usterr-signal-safe.h"
b13d93c2 31
8e2aed3f
AB
32/* Include link.h last else it conflicts with ust-dlfcn. */
33#include <link.h>
b13d93c2 34
13436238 35#define TRACEPOINT_DEFINE
6d4658aa 36#include "ust_dl.h"
13436238 37
b13d93c2
PW
38static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flag);
39static int (*__lttng_ust_plibc_dlclose)(void *handle);
b13d93c2
PW
40
41static
42void *_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");
8e2aed3f 46 if (!__lttng_ust_plibc_dlopen) {
b13d93c2
PW
47 fprintf(stderr, "%s\n", dlerror());
48 return NULL;
49 }
50 }
51 return __lttng_ust_plibc_dlopen(filename, flag);
52}
53
54static
55int _lttng_ust_dl_libc_dlclose(void *handle)
56{
57 if (!__lttng_ust_plibc_dlclose) {
58 __lttng_ust_plibc_dlclose = dlsym(RTLD_NEXT, "dlclose");
8e2aed3f 59 if (!__lttng_ust_plibc_dlclose) {
b13d93c2
PW
60 fprintf(stderr, "%s\n", dlerror());
61 return -1;
62 }
63 }
64 return __lttng_ust_plibc_dlclose(handle);
65}
66
67static
03db42de 68void lttng_ust_dl_dlopen(void *so_base, const char *so_name, void *ip)
b13d93c2 69{
13436238 70 char resolved_path[PATH_MAX];
8e2aed3f
AB
71 struct lttng_ust_elf *elf;
72 uint64_t memsz;
83215d66 73 uint8_t *build_id = NULL;
8e2aed3f 74 size_t build_id_len;
83215d66 75 char *dbg_file = NULL;
8e2aed3f
AB
76 uint32_t crc;
77 int has_build_id = 0, has_debug_link = 0;
78 int ret;
b13d93c2 79
13436238
PW
80 if (!realpath(so_name, resolved_path)) {
81 ERR("could not resolve path '%s'", so_name);
82 return;
b13d93c2 83 }
b13d93c2 84
8e2aed3f
AB
85 elf = lttng_ust_elf_create(resolved_path);
86 if (!elf) {
87 ERR("could not acces file %s", resolved_path);
13436238 88 return;
b13d93c2 89 }
13436238 90
8e2aed3f
AB
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
6d4658aa 106 tracepoint(lttng_ust_dl, dlopen,
c5c4fd82
MD
107 ip, so_base, resolved_path, memsz,
108 has_build_id, has_debug_link);
8e2aed3f
AB
109
110 if (has_build_id) {
111 tracepoint(lttng_ust_dl, build_id,
112 ip, so_base, build_id, build_id_len);
8e2aed3f
AB
113 }
114
115 if (has_debug_link) {
116 tracepoint(lttng_ust_dl, debug_link,
117 ip, so_base, dbg_file, crc);
8e2aed3f
AB
118 }
119
120end:
83215d66
MD
121 free(dbg_file);
122 free(build_id);
8e2aed3f 123 lttng_ust_elf_destroy(elf);
13436238 124 return;
b13d93c2
PW
125}
126
127void *dlopen(const char *filename, int flag)
128{
8e2aed3f
AB
129 void *handle;
130
131 handle = _lttng_ust_dl_libc_dlopen(filename, flag);
bd703713 132 if (__tracepoint_ptrs_registered && handle) {
b13d93c2 133 struct link_map *p = NULL;
8e2aed3f
AB
134 int ret;
135
136 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
137 if (ret != -1 && p != NULL && p->l_addr != 0) {
03db42de 138 lttng_ust_dl_dlopen((void *) p->l_addr, p->l_name,
171fcc6f 139 LTTNG_UST_CALLER_IP());
8e2aed3f 140 }
b13d93c2 141 }
8e2aed3f 142
b13d93c2
PW
143 return handle;
144}
145
146int dlclose(void *handle)
147{
e7953e6e 148 if (__tracepoint_ptrs_registered) {
b13d93c2 149 struct link_map *p = NULL;
8e2aed3f
AB
150 int ret;
151
152 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
153 if (ret != -1 && p != NULL && p->l_addr != 0) {
154 tracepoint(lttng_ust_dl, dlclose,
171fcc6f 155 LTTNG_UST_CALLER_IP(),
8e2aed3f
AB
156 (void *) p->l_addr);
157 }
b13d93c2 158 }
8e2aed3f 159
b13d93c2
PW
160 return _lttng_ust_dl_libc_dlclose(handle);
161}
This page took 0.031942 seconds and 4 git commands to generate.