hotspot/agent/src/os/linux/libproc_impl.c
author never
Tue, 09 Mar 2010 11:02:39 -0800
changeset 5045 59dd2a304f1d
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
6932270: Allow Java's ELF symtab reader to use separate debuginfo files Reviewed-by: never Contributed-by: Andrew Haley <aph@redhat.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
#include <stdarg.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#include <stdio.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#include <fcntl.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
#include <thread_db.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
#include "libproc_impl.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
static const char* alt_root = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
static int alt_root_len = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
#define SA_ALTROOT "SA_ALTROOT"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
static void init_alt_root() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
   if (alt_root_len == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
      alt_root = getenv(SA_ALTROOT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
      if (alt_root) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
         alt_root_len = strlen(alt_root);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
         alt_root_len = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
int pathmap_open(const char* name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
   int fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
   char alt_path[PATH_MAX + 1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
   init_alt_root();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
   fd = open(name, O_RDONLY);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
   if (fd >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
      return fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
   if (alt_root_len > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
      strcpy(alt_path, alt_root);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
      strcat(alt_path, name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
      fd = open(alt_path, O_RDONLY);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
      if (fd >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
         print_debug("path %s substituted for %s\n", alt_path, name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
         return fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
      if (strrchr(name, '/')) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
         strcpy(alt_path, alt_root);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
         strcat(alt_path, strrchr(name, '/'));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
         fd = open(alt_path, O_RDONLY);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
         if (fd >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
            print_debug("path %s substituted for %s\n", alt_path, name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
            return fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
   return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
static bool _libsaproc_debug;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
void print_debug(const char* format,...) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
   if (_libsaproc_debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
     va_list alist;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
     va_start(alist, format);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
     fputs("libsaproc DEBUG: ", stderr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
     vfprintf(stderr, format, alist);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
     va_end(alist);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
bool is_debug() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
   return _libsaproc_debug;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
// initialize libproc
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
bool init_libproc(bool debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
   // init debug mode
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
   _libsaproc_debug = debug;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
   // initialize the thread_db library
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
   if (td_init() != TD_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
     print_debug("libthread_db's td_init failed\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
     return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
static void destroy_lib_info(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
   lib_info* lib = ph->libs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
   while (lib) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
     lib_info *next = lib->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
     if (lib->symtab) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
        destroy_symtab(lib->symtab);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
     free(lib);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
     lib = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
static void destroy_thread_info(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
   thread_info* thr = ph->threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
   while (thr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
     thread_info *next = thr->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
     free(thr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
     thr = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
// ps_prochandle cleanup
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
// ps_prochandle cleanup
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
void Prelease(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
   // do the "derived class" clean-up first
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
   ph->ops->release(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
   destroy_lib_info(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
   destroy_thread_info(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
   free(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
   return add_lib_info_fd(ph, libname, -1, base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
   lib_info* newlib;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
   if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
      print_debug("can't allocate memory for lib_info\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
   strncpy(newlib->name, libname, sizeof(newlib->name));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
   newlib->base = base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
   if (fd == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
      if ( (newlib->fd = pathmap_open(newlib->name)) < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
         print_debug("can't open shared object %s\n", newlib->name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
         free(newlib);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
         return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
   } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
      newlib->fd = fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
   // check whether we have got an ELF file. /proc/<pid>/map
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
   // gives out all file mappings and not just shared objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
   if (is_elf_file(newlib->fd) == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
      close(newlib->fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      free(newlib);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
5045
59dd2a304f1d 6932270: Allow Java's ELF symtab reader to use separate debuginfo files
never
parents: 1
diff changeset
   177
   newlib->symtab = build_symtab(newlib->fd, libname);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
   if (newlib->symtab == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
      print_debug("symbol table build failed for %s\n", newlib->name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
   // even if symbol table building fails, we add the lib_info.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
   // This is because we may need to read from the ELF file for core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
   // address read functionality. lookup_symbol checks for NULL symtab.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
   if (ph->libs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
      ph->lib_tail->next = newlib;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
      ph->lib_tail = newlib;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
   }  else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
      ph->libs = ph->lib_tail = newlib;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
   ph->num_libs++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
   return newlib;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
// lookup for a specific symbol
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
uintptr_t lookup_symbol(struct ps_prochandle* ph,  const char* object_name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
                       const char* sym_name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
   // ignore object_name. search in all libraries
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
   // FIXME: what should we do with object_name?? The library names are obtained
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
   // by parsing /proc/<pid>/maps, which may not be the same as object_name.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
   // What we need is a utility to map object_name to real file name, something
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
   // dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
   // now, we just ignore object_name and do a global search for the symbol.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
   lib_info* lib = ph->libs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
   while (lib) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
      if (lib->symtab) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
         uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
         if (res) return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
      lib = lib->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
   print_debug("lookup failed for symbol '%s' in obj '%s'\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
                          sym_name, object_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
   return (uintptr_t) NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
   const char* res = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
   lib_info* lib = ph->libs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
   while (lib) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
      if (lib->symtab && addr >= lib->base) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
         res = nearest_symbol(lib->symtab, addr - lib->base, poffset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
         if (res) return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
      lib = lib->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
   return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
// add a thread to ps_prochandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
   thread_info* newthr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
   if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      print_debug("can't allocate memory for thread_info\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
   // initialize thread info
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
   newthr->pthread_id = pthread_id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
   newthr->lwp_id = lwp_id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
   // add new thread to the list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
   newthr->next = ph->threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
   ph->threads = newthr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
   ph->num_threads++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
   return newthr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
// struct used for client data from thread_db callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
struct thread_db_client_data {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
   struct ps_prochandle* ph;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
   thread_info_callback callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
// callback function for libthread_db
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
static int thread_db_callback(const td_thrhandle_t *th_p, void *data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  struct thread_db_client_data* ptr = (struct thread_db_client_data*) data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  td_thrinfo_t ti;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  td_err_e err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  memset(&ti, 0, sizeof(ti));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  err = td_thr_get_info(th_p, &ti);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  if (err != TD_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  if (ptr->callback(ptr->ph, ti.ti_tid, ti.ti_lid) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    return TD_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  return TD_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
// read thread_info using libthread_db
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  struct thread_db_client_data mydata;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  td_thragent_t* thread_agent = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  if (td_ta_new(ph, &thread_agent) != TD_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
     print_debug("can't create libthread_db agent\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
     return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  mydata.ph = ph;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  mydata.callback = cb;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  // we use libthread_db iterator to iterate thru list of threads.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
                 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
                 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
     td_ta_delete(thread_agent);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
     return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  // delete thread agent
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  td_ta_delete(thread_agent);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
// get number of threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
int get_num_threads(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
   return ph->num_threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
// get lwp_id of n'th thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
   int count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
   thread_info* thr = ph->threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
   while (thr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
      if (count == index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
         return thr->lwp_id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
      count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
      thr = thr->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
   return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
// get regs for a given lwp
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct user_regs_struct* regs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  return ph->ops->get_lwp_regs(ph, lwp_id, regs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
// get number of shared objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
int get_num_libs(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
   return ph->num_libs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
// get name of n'th solib
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
const char* get_lib_name(struct ps_prochandle* ph, int index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
   int count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
   lib_info* lib = ph->libs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
   while (lib) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
      if (count == index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
         return lib->name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
      count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
      lib = lib->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
   return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
// get base address of a lib
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
uintptr_t get_lib_base(struct ps_prochandle* ph, int index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
   int count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
   lib_info* lib = ph->libs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
   while (lib) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
      if (count == index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
         return lib->base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
      count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
      lib = lib->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
   return (uintptr_t)NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
bool find_lib(struct ps_prochandle* ph, const char *lib_name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  lib_info *p = ph->libs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  while (p) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    if (strcmp(p->name, lib_name) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
    p = p->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
//--------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
// proc service functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
// get process id
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
pid_t ps_getpid(struct ps_prochandle *ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
   return ph->pid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
// of the load object object_name in the target process identified by ph.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
// It returns the symbol's value as an address in the target process in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
// *sym_addr.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
                    const char *sym_name, psaddr_t *sym_addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  *sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  return (*sym_addr ? PS_OK : PS_NOSYM);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
// read "size" bytes info "buf" from address "addr"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
ps_err_e ps_pdread(struct ps_prochandle *ph, psaddr_t  addr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
                   void *buf, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
// write "size" bytes of data to debuggee at address "addr"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
ps_err_e ps_pdwrite(struct ps_prochandle *ph, psaddr_t addr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
                    const void *buf, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
// ------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
// Functions below this point are not yet implemented. They are here only
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
// to make the linker happy.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  print_debug("ps_lsetfpregs not implemented\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
ps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  print_debug("ps_lsetregs not implemented\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
ps_err_e  ps_lgetfpregs(struct  ps_prochandle  *ph,  lwpid_t lid, prfpregset_t *fpregs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  print_debug("ps_lgetfpregs not implemented\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
ps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  print_debug("ps_lgetfpregs not implemented\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
// new libthread_db of NPTL seem to require this symbol
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
ps_err_e ps_get_thread_area() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  print_debug("ps_get_thread_area not implemented\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
}