Integrate base-address statedump into lttng-ust
[lttng-ust.git] / liblttng-ust-dl / ustdl.c
1 /*
2 * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; version 2.1 of
7 * the License.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #define _GNU_SOURCE
20 #include <inttypes.h>
21 #include <dlfcn.h>
22 #include <link.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <limits.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <signal.h>
29 #include <sched.h>
30 #include <stdarg.h>
31 #include "usterr.h"
32
33 #include <lttng/ust-compiler.h>
34 #include <lttng/ust.h>
35
36 #define TRACEPOINT_DEFINE
37 #include "ust_baddr.h"
38
39 static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flag);
40 static int (*__lttng_ust_plibc_dlclose)(void *handle);
41
42 static
43 void *_lttng_ust_dl_libc_dlopen(const char *filename, int flag)
44 {
45 if (!__lttng_ust_plibc_dlopen) {
46 __lttng_ust_plibc_dlopen = dlsym(RTLD_NEXT, "dlopen");
47 if (__lttng_ust_plibc_dlopen == NULL) {
48 fprintf(stderr, "%s\n", dlerror());
49 return NULL;
50 }
51 }
52 return __lttng_ust_plibc_dlopen(filename, flag);
53 }
54
55 static
56 int _lttng_ust_dl_libc_dlclose(void *handle)
57 {
58 if (!__lttng_ust_plibc_dlclose) {
59 __lttng_ust_plibc_dlclose = dlsym(RTLD_NEXT, "dlclose");
60 if (__lttng_ust_plibc_dlclose == NULL) {
61 fprintf(stderr, "%s\n", dlerror());
62 return -1;
63 }
64 }
65 return __lttng_ust_plibc_dlclose(handle);
66 }
67
68 static
69 void lttng_ust_baddr_push(void *so_base, const char *so_name)
70 {
71 char resolved_path[PATH_MAX];
72 struct stat sostat;
73
74 if (!realpath(so_name, resolved_path)) {
75 ERR("could not resolve path '%s'", so_name);
76 return;
77 }
78
79 if (stat(resolved_path, &sostat)) {
80 ERR("could not access file status for %s", resolved_path);
81 return;
82 }
83
84 tracepoint(ust_baddr, push,
85 so_base, resolved_path, sostat.st_size, sostat.st_mtime);
86 return;
87 }
88
89 void *dlopen(const char *filename, int flag)
90 {
91 void *handle = _lttng_ust_dl_libc_dlopen(filename, flag);
92 if (handle) {
93 struct link_map *p = NULL;
94 if (dlinfo(handle, RTLD_DI_LINKMAP, &p) != -1 && p != NULL
95 && p->l_addr != 0)
96 lttng_ust_baddr_push((void *) p->l_addr, p->l_name);
97 }
98 return handle;
99 }
100
101 int dlclose(void *handle)
102 {
103 if (handle) {
104 struct link_map *p = NULL;
105 if (dlinfo(handle, RTLD_DI_LINKMAP, &p) != -1 && p != NULL
106 && p->l_addr != 0)
107 tracepoint(ust_baddr, pop, (void *) p->l_addr);
108 }
109 return _lttng_ust_dl_libc_dlclose(handle);
110 }
This page took 0.033017 seconds and 4 git commands to generate.