| 1 | // name-finder.cc - Convert addresses to names | 
|---|
| 2 |  | 
|---|
| 3 | /* Copyright (C) 2000, 2002  Free Software Foundation, Inc | 
|---|
| 4 |  | 
|---|
| 5 | This file is part of libgcj. | 
|---|
| 6 |  | 
|---|
| 7 | This software is copyrighted work licensed under the terms of the | 
|---|
| 8 | Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for | 
|---|
| 9 | details.  */ | 
|---|
| 10 |  | 
|---|
| 11 | /** | 
|---|
| 12 | * @author Andrew Haley <aph@cygnus.com> | 
|---|
| 13 | * @date Jan 6  2000 | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | /* _Jv_name_finder is a class wrapper around a mechanism that can | 
|---|
| 17 | convert address of methods to their names and the names of files in | 
|---|
| 18 | which they appear. | 
|---|
| 19 |  | 
|---|
| 20 | Right now, the only implementation of this involves running a copy | 
|---|
| 21 | of addr2line, but at some point it is worth building this | 
|---|
| 22 | functionality into libgcj, if only for embedded systems.  */ | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | #ifndef _GNU_SOURCE | 
|---|
| 26 | #define _GNU_SOURCE 1 | 
|---|
| 27 | #endif | 
|---|
| 28 |  | 
|---|
| 29 | #include <config.h> | 
|---|
| 30 |  | 
|---|
| 31 | #include <string.h> | 
|---|
| 32 |  | 
|---|
| 33 | #include <gcj/cni.h> | 
|---|
| 34 | #include <jvm.h> | 
|---|
| 35 | #include <java/lang/Object.h> | 
|---|
| 36 | #include <java-threads.h> | 
|---|
| 37 | #include <java/lang/Throwable.h> | 
|---|
| 38 | #include <java/io/PrintStream.h> | 
|---|
| 39 | #include <java/io/PrintWriter.h> | 
|---|
| 40 |  | 
|---|
| 41 | #include <sys/types.h> | 
|---|
| 42 |  | 
|---|
| 43 | #include <stdlib.h> | 
|---|
| 44 | #include <stdio.h> | 
|---|
| 45 |  | 
|---|
| 46 | #ifdef HAVE_UNISTD_H | 
|---|
| 47 | #include <unistd.h> | 
|---|
| 48 | #endif | 
|---|
| 49 |  | 
|---|
| 50 | #ifdef HAVE_DLFCN_H | 
|---|
| 51 | #include <dlfcn.h> | 
|---|
| 52 | #endif | 
|---|
| 53 |  | 
|---|
| 54 | #include <name-finder.h> | 
|---|
| 55 |  | 
|---|
| 56 | /* Create a new name finder which will perform address lookups on an | 
|---|
| 57 | executable. */ | 
|---|
| 58 |  | 
|---|
| 59 | _Jv_name_finder::_Jv_name_finder (char *executable) | 
|---|
| 60 | { | 
|---|
| 61 | #if defined (HAVE_PIPE) && defined (HAVE_FORK) && defined (HAVE_EXECVP) | 
|---|
| 62 | error = 0; | 
|---|
| 63 |  | 
|---|
| 64 | // Initialize file descriptors so that shutdown works properly. | 
|---|
| 65 | f_pipe[0] = -1; | 
|---|
| 66 | f_pipe[1] = -1; | 
|---|
| 67 | b_pipe[0] = -1; | 
|---|
| 68 | b_pipe[1] = -1; | 
|---|
| 69 | b_pipe_fd = NULL; | 
|---|
| 70 |  | 
|---|
| 71 | char *argv[6]; | 
|---|
| 72 | { | 
|---|
| 73 | int arg = 0; | 
|---|
| 74 | #ifdef __ia64__ | 
|---|
| 75 | argv[arg++] = "addr2name.awk"; | 
|---|
| 76 | #else | 
|---|
| 77 | argv[arg++] = "addr2line"; | 
|---|
| 78 | argv[arg++] = "-C"; | 
|---|
| 79 | argv[arg++] = "-f"; | 
|---|
| 80 | argv[arg++] = "-e"; | 
|---|
| 81 | #endif | 
|---|
| 82 | argv[arg++] = executable; | 
|---|
| 83 | argv[arg] = NULL; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | error |= pipe (f_pipe) < 0; | 
|---|
| 87 | error |= pipe (b_pipe) < 0; | 
|---|
| 88 |  | 
|---|
| 89 | if (error) | 
|---|
| 90 | return; | 
|---|
| 91 |  | 
|---|
| 92 | pid = fork (); | 
|---|
| 93 | if (pid == 0) | 
|---|
| 94 | { | 
|---|
| 95 | close (f_pipe[1]); | 
|---|
| 96 | close (b_pipe[0]); | 
|---|
| 97 | dup2 (f_pipe[0], fileno (stdin)); | 
|---|
| 98 | dup2 (b_pipe[1], fileno (stdout)); | 
|---|
| 99 | execvp (argv[0], argv); | 
|---|
| 100 | _exit (127); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | // Close child end of pipes.  Set local descriptors to -1 so we | 
|---|
| 104 | // don't try to close the fd again. | 
|---|
| 105 | close (f_pipe [0]); | 
|---|
| 106 | f_pipe[0] = -1; | 
|---|
| 107 | close (b_pipe [1]); | 
|---|
| 108 | b_pipe[1] = -1; | 
|---|
| 109 |  | 
|---|
| 110 | if (pid < 0) | 
|---|
| 111 | { | 
|---|
| 112 | error |= 1; | 
|---|
| 113 | return; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | b_pipe_fd = fdopen (b_pipe[0], "r"); | 
|---|
| 117 | error |= !b_pipe_fd; | 
|---|
| 118 |  | 
|---|
| 119 | if (! error) | 
|---|
| 120 | { | 
|---|
| 121 | // Don't try to close the fd twice. | 
|---|
| 122 | b_pipe[0] = -1; | 
|---|
| 123 | } | 
|---|
| 124 | #endif | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | /* Convert a pointer to hex. */ | 
|---|
| 128 |  | 
|---|
| 129 | void | 
|---|
| 130 | _Jv_name_finder::toHex (void *p) | 
|---|
| 131 | { | 
|---|
| 132 | typedef unsigned word_t __attribute ((mode (word))); | 
|---|
| 133 | word_t n = (word_t) p; | 
|---|
| 134 | int digits = sizeof (void *) * 2; | 
|---|
| 135 |  | 
|---|
| 136 | strcpy (hex, "0x"); | 
|---|
| 137 | for (int i = digits - 1; i >= 0; i--) | 
|---|
| 138 | { | 
|---|
| 139 | int digit = n % 16; | 
|---|
| 140 |  | 
|---|
| 141 | n /= 16; | 
|---|
| 142 | hex[i+2] = digit > 9 ? 'a' + digit - 10 : '0' + digit; | 
|---|
| 143 | } | 
|---|
| 144 | hex [digits+2] = 0; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | /* Given a pointer to a function or method, try to convert it into a | 
|---|
| 148 | name and the appropriate line and source file.  The caller passes | 
|---|
| 149 | the code pointer in p. | 
|---|
| 150 |  | 
|---|
| 151 | Returns false if the lookup fails.  Even if this happens, the field | 
|---|
| 152 | he will have been correctly filled in with the pointer.  */ | 
|---|
| 153 |  | 
|---|
| 154 | bool | 
|---|
| 155 | _Jv_name_finder::lookup (void *p) | 
|---|
| 156 | { | 
|---|
| 157 | extern char **_Jv_argv; | 
|---|
| 158 | toHex (p); | 
|---|
| 159 |  | 
|---|
| 160 | #if defined (HAVE_DLFCN_H) && defined (HAVE_DLADDR) | 
|---|
| 161 | { | 
|---|
| 162 | Dl_info dl_info; | 
|---|
| 163 |  | 
|---|
| 164 | if (dladdr (p, &dl_info)) | 
|---|
| 165 | { | 
|---|
| 166 | if (dl_info.dli_fname) | 
|---|
| 167 | strncpy (file_name, dl_info.dli_fname, sizeof file_name); | 
|---|
| 168 | if (dl_info.dli_sname) | 
|---|
| 169 | strncpy (method_name, dl_info.dli_sname, sizeof method_name); | 
|---|
| 170 |  | 
|---|
| 171 | /* Don't trust dladdr() if the address is from the main program. */ | 
|---|
| 172 | if (dl_info.dli_fname != NULL | 
|---|
| 173 | && dl_info.dli_sname != NULL | 
|---|
| 174 | && (_Jv_argv == NULL || strcmp (file_name, _Jv_argv[0]) != 0)) | 
|---|
| 175 | return true; | 
|---|
| 176 | } | 
|---|
| 177 | } | 
|---|
| 178 | #endif | 
|---|
| 179 |  | 
|---|
| 180 | #if defined (HAVE_PIPE) && defined (HAVE_FORK) && defined (HAVE_EXECVP) | 
|---|
| 181 | if (error) | 
|---|
| 182 | return false; | 
|---|
| 183 |  | 
|---|
| 184 | error |= write (f_pipe[1], hex, strlen (hex)) < 0; | 
|---|
| 185 | if (error) | 
|---|
| 186 | return false; | 
|---|
| 187 | error |= write (f_pipe[1], "\n", 1) < 0; | 
|---|
| 188 | if (error) | 
|---|
| 189 | return false; | 
|---|
| 190 |  | 
|---|
| 191 | error |= (fgets (method_name, sizeof method_name, b_pipe_fd) == NULL); | 
|---|
| 192 | if (error) | 
|---|
| 193 | return false; | 
|---|
| 194 | error |= (fgets (file_name, sizeof file_name, b_pipe_fd) == NULL); | 
|---|
| 195 | if (error) | 
|---|
| 196 | return false; | 
|---|
| 197 |  | 
|---|
| 198 | char *newline = strchr (method_name, '\n'); | 
|---|
| 199 | if (newline) | 
|---|
| 200 | *newline = 0; | 
|---|
| 201 | newline = strchr (file_name, '\n'); | 
|---|
| 202 | if (newline) | 
|---|
| 203 | *newline = 0; | 
|---|
| 204 |  | 
|---|
| 205 | return true; | 
|---|
| 206 |  | 
|---|
| 207 | #else | 
|---|
| 208 | return false; | 
|---|
| 209 | #endif /* defined (HAVE_PIPE) && defined (HAVE_FORK) && defined (HAVE_EXECVP) */ | 
|---|
| 210 | } | 
|---|