| 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; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 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 <dlfcn.h> |
| 21 | #include <link.h> |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
| 27 | #include <limits.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | #include <stdint.h> |
| 31 | #include <stddef.h> |
| 32 | #include <stdio.h> |
| 33 | #include "usterr.h" |
| 34 | |
| 35 | #define TRACEPOINT_DEFINE |
| 36 | #include "ust_baddr.h" |
| 37 | #include "ust_baddr_statedump.h" |
| 38 | |
| 39 | int |
| 40 | lttng_ust_push_baddr(void *so_base, const char *so_name) |
| 41 | { |
| 42 | char resolved_path[PATH_MAX]; |
| 43 | struct stat sostat; |
| 44 | |
| 45 | if (!realpath(so_name, resolved_path)) { |
| 46 | ERR("could not resolve path '%s'", so_name); |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | if (stat(resolved_path, &sostat)) { |
| 51 | ERR("could not access file status for %s", resolved_path); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | tracepoint(ust_baddr, push, |
| 56 | so_base, resolved_path, sostat.st_size, sostat.st_mtime); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int |
| 61 | lttng_ust_pop_baddr(void *so_base) |
| 62 | { |
| 63 | tracepoint(ust_baddr, pop, so_base); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int |
| 68 | extract_soinfo_events(struct dl_phdr_info *info, size_t size, void *data) |
| 69 | { |
| 70 | int j; |
| 71 | int num_loadable_segment = 0; |
| 72 | |
| 73 | for (j = 0; j < info->dlpi_phnum; j++) { |
| 74 | char resolved_path[PATH_MAX]; |
| 75 | struct stat sostat; |
| 76 | void *base_addr_ptr; |
| 77 | |
| 78 | if (info->dlpi_phdr[j].p_type != PT_LOAD) |
| 79 | continue; |
| 80 | |
| 81 | /* Calculate virtual memory address of the loadable segment */ |
| 82 | base_addr_ptr = (void *) info->dlpi_addr |
| 83 | + info->dlpi_phdr[j].p_vaddr; |
| 84 | |
| 85 | num_loadable_segment += 1; |
| 86 | if ((info->dlpi_name == NULL || info->dlpi_name[0] == 0) |
| 87 | && num_loadable_segment == 1) { |
| 88 | /* |
| 89 | * If the iterated element is the executable itself we |
| 90 | * have to use Dl_info to determine its full path |
| 91 | */ |
| 92 | Dl_info dl_info = { 0 }; |
| 93 | if (!dladdr(base_addr_ptr, &dl_info)) |
| 94 | return 0; |
| 95 | if (!realpath(dl_info.dli_fname, resolved_path)) |
| 96 | return 0; |
| 97 | } else { |
| 98 | /* |
| 99 | * For regular dl_phdr_info entries we have to check if |
| 100 | * the path to the shared object really exists |
| 101 | */ |
| 102 | if (!realpath(info->dlpi_name, resolved_path)) { |
| 103 | /* Found vDSO, put the 'path' into brackets */ |
| 104 | snprintf(resolved_path, PATH_MAX - 1, "[%s]", |
| 105 | info->dlpi_name); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (stat(resolved_path, &sostat)) { |
| 110 | sostat.st_size = 0; |
| 111 | sostat.st_mtime = -1; |
| 112 | } |
| 113 | |
| 114 | tracepoint(ust_baddr_statedump, soinfo, |
| 115 | (struct lttng_session *) data, base_addr_ptr, |
| 116 | resolved_path, sostat.st_size, sostat.st_mtime); |
| 117 | |
| 118 | /* |
| 119 | * We are only interested in the base address (lowest virtual |
| 120 | * address associated with the memory image), skip the rest |
| 121 | */ |
| 122 | break; |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int |
| 128 | lttng_ust_baddr_statedump(struct lttng_session *session) |
| 129 | { |
| 130 | /* |
| 131 | * Iterate through the list of currently loaded shared objects and |
| 132 | * generate events for loadable segments using extract_soinfo_events |
| 133 | */ |
| 134 | dl_iterate_phdr(extract_soinfo_events, session); |
| 135 | return 0; |
| 136 | } |