Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / nonnacl_util / linux / get_plugin_dirname.cc
blobb89a64fdd9dbe870c4537212f74161db49b980a1
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /* TODO: add comment why this is needed */
34 #ifndef _GNU_SOURCE
35 #define _GNU_SOURCE
36 #endif
37 #include <dlfcn.h>
38 #include <libgen.h>
39 #include <linux/limits.h>
40 #include <signal.h>
41 #include <stdio.h>
42 #include <stdint.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
47 #include <unistd.h>
49 #include "native_client/nonnacl_util/sel_ldr_launcher.h"
51 namespace nacl {
54 const char* SelLdrLauncher::GetPluginDirname() {
55 Dl_info info;
56 char* pathname = NULL;
57 static char bin_dir[2 * PATH_MAX + 1];
58 // C++ really does not like to convert function pointer to regular pointers.
59 // This is apparently the only way to do it without compiler warnings
60 void* sym_addr = reinterpret_cast<void*>(
61 reinterpret_cast<uintptr_t>(GetPluginDirname));
63 // First, try looking a symbol up in the dynamic loader's records.
64 if (0 == dladdr(sym_addr, &info)) {
65 printf("dladdr failed\n");
66 } else {
67 strncpy(bin_dir, info.dli_fname, sizeof(bin_dir));
68 pathname = bin_dir;
70 // What follows is probably major overkill if the dladdr scheme works.
71 if (NULL == pathname) {
72 // Identify the path the plugin was loaded from. For Linux we look through
73 // the address ranges in /proc/self/maps for the address of a static
74 // variable. If that fails, look in /proc/self/exe.
75 FILE* fp;
77 // Open the file, returning NULL for failure.
78 if (NULL == (fp = fopen("/proc/self/maps", "r"))) {
79 return NULL;
81 // Read line by line.
82 while (!feof(fp)) {
83 static int dummy_for_address;
84 const unsigned kAddressInSo = (unsigned) &dummy_for_address;
85 uintptr_t low_addr;
86 uintptr_t high_addr;
87 char perm[5];
88 uintptr_t offset;
89 unsigned dev_maj;
90 unsigned dev_min;
91 unsigned inode;
93 if (7 != fscanf(fp, "%x-%x %s %x %x:%x %x",
94 &low_addr, &high_addr, perm, &offset, &dev_maj,
95 &dev_min, &inode)) {
96 return NULL;
98 if (NULL == fgets(bin_dir, sizeof(bin_dir), fp)) {
99 return NULL;
101 // If the address is in the range, break out of the loop.
102 if (kAddressInSo >= low_addr && kAddressInSo < high_addr) {
103 pathname = strchr(bin_dir, '/');
104 break;
107 // Close /proc/self/maps.
108 fclose(fp);
110 // If we didn't find a matching pathname, try the path of the current
111 // executable.
112 if (NULL == pathname) {
113 int bin_dir_size = readlink("/proc/self/exe", bin_dir, PATH_MAX);
114 if (bin_dir_size < 0 || bin_dir_size > PATH_MAX) {
115 return NULL;
117 bin_dir[bin_dir_size] = '\0';
118 pathname = bin_dir;
120 // Return just the directory path.
121 return dirname(pathname);
125 } // namespace nacl