libproc_impl.c 14.6 KB
Newer Older
N
never 已提交
1
/*
R
rbackman 已提交
2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
N
never 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */
#include "libproc_impl.h"

static const char* alt_root = NULL;
static int alt_root_len = -1;

#define SA_ALTROOT "SA_ALTROOT"

M
minqi 已提交
31 32 33 34
off_t ltell(int fd) {
  return lseek(fd, 0, SEEK_CUR);
}

N
never 已提交
35
static void init_alt_root() {
M
minqi 已提交
36 37 38 39 40 41 42 43
  if (alt_root_len == -1) {
    alt_root = getenv(SA_ALTROOT);
    if (alt_root) {
      alt_root_len = strlen(alt_root);
    } else {
      alt_root_len = 0;
    }
  }
N
never 已提交
44 45 46
}

int pathmap_open(const char* name) {
M
minqi 已提交
47 48 49 50
  int fd;
  char alt_path[PATH_MAX + 1];

  init_alt_root();
N
never 已提交
51

M
minqi 已提交
52 53 54 55 56 57
  if (alt_root_len > 0) {
    strcpy(alt_path, alt_root);
    strcat(alt_path, name);
    fd = open(alt_path, O_RDONLY);
    if (fd >= 0) {
      print_debug("path %s substituted for %s\n", alt_path, name);
N
never 已提交
58
      return fd;
M
minqi 已提交
59
    }
N
never 已提交
60

M
minqi 已提交
61
    if (strrchr(name, '/')) {
N
never 已提交
62
      strcpy(alt_path, alt_root);
M
minqi 已提交
63
      strcat(alt_path, strrchr(name, '/'));
N
never 已提交
64 65
      fd = open(alt_path, O_RDONLY);
      if (fd >= 0) {
M
minqi 已提交
66 67
        print_debug("path %s substituted for %s\n", alt_path, name);
        return fd;
N
never 已提交
68
      }
M
minqi 已提交
69 70 71 72 73 74 75 76
    }
  } else {
    fd = open(name, O_RDONLY);
    if (fd >= 0) {
      return fd;
    }
  }
  return -1;
N
never 已提交
77 78 79 80 81
}

static bool _libsaproc_debug;

void print_debug(const char* format,...) {
M
minqi 已提交
82 83
  if (_libsaproc_debug) {
    va_list alist;
N
never 已提交
84

M
minqi 已提交
85 86 87 88 89
    va_start(alist, format);
    fputs("libsaproc DEBUG: ", stderr);
    vfprintf(stderr, format, alist);
    va_end(alist);
  }
N
never 已提交
90 91
}

R
rbackman 已提交
92 93 94 95 96 97 98 99
void print_error(const char* format,...) {
  va_list alist;
  va_start(alist, format);
  fputs("ERROR: ", stderr);
  vfprintf(stderr, format, alist);
  va_end(alist);
}

N
never 已提交
100
bool is_debug() {
M
minqi 已提交
101
  return _libsaproc_debug;
N
never 已提交
102 103
}

M
minqi 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#ifdef __APPLE__
// get arch offset in file
bool get_arch_off(int fd, cpu_type_t cputype, off_t *offset) {
  struct fat_header fatheader;
  struct fat_arch fatarch;
  off_t img_start = 0;

  off_t pos = ltell(fd);
  if (read(fd, (void *)&fatheader, sizeof(struct fat_header)) != sizeof(struct fat_header)) {
    return false;
  }
  if (fatheader.magic == FAT_CIGAM) {
    int i;
    for (i = 0; i < ntohl(fatheader.nfat_arch); i++) {
      if (read(fd, (void *)&fatarch, sizeof(struct fat_arch)) != sizeof(struct fat_arch)) {
        return false;
      }
      if (ntohl(fatarch.cputype) == cputype) {
        print_debug("fat offset=%x\n", ntohl(fatarch.offset));
        img_start = ntohl(fatarch.offset);
        break;
      }
    }
    if (img_start == 0) {
      return false;
    }
  }
  lseek(fd, pos, SEEK_SET);
  *offset = img_start;
  return true;
}

bool is_macho_file(int fd) {
  mach_header_64 fhdr;
  off_t x86_64_off;

  if (fd < 0) {
    print_debug("Invalid file handle passed to is_macho_file\n");
    return false;
  }

  off_t pos = ltell(fd);
  // check fat header
  if (!get_arch_off(fd, CPU_TYPE_X86_64, &x86_64_off)) {
    print_debug("failed to get fat header\n");
    return false;
  }
  lseek(fd, x86_64_off, SEEK_SET);
  if (read(fd, (void *)&fhdr, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
     return false;
  }
  lseek(fd, pos, SEEK_SET);               // restore
  print_debug("fhdr.magic %x\n", fhdr.magic);
  return (fhdr.magic == MH_MAGIC_64 || fhdr.magic == MH_CIGAM_64);
}

#endif //__APPLE__

N
never 已提交
162 163 164
// initialize libproc
bool init_libproc(bool debug) {
   _libsaproc_debug = debug;
M
minqi 已提交
165
#ifndef __APPLE__
N
never 已提交
166 167 168 169 170
   // initialize the thread_db library
   if (td_init() != TD_OK) {
     print_debug("libthread_db's td_init failed\n");
     return false;
   }
M
minqi 已提交
171
#endif // __APPLE__
N
never 已提交
172 173 174
   return true;
}

M
minqi 已提交
175 176 177 178 179 180 181 182 183 184
void destroy_lib_info(struct ps_prochandle* ph) {
  lib_info* lib = ph->libs;
  while (lib) {
    lib_info* next = lib->next;
    if (lib->symtab) {
      destroy_symtab(lib->symtab);
    }
    free(lib);
    lib = next;
  }
N
never 已提交
185 186
}

M
minqi 已提交
187 188 189 190 191 192 193
void destroy_thread_info(struct ps_prochandle* ph) {
  sa_thread_info* thr = ph->threads;
  while (thr) {
    sa_thread_info* n = thr->next;
    free(thr);
    thr = n;
  }
N
never 已提交
194 195 196 197
}

// ps_prochandle cleanup
void Prelease(struct ps_prochandle* ph) {
M
minqi 已提交
198 199 200 201 202
  // do the "derived class" clean-up first
  ph->ops->release(ph);
  destroy_lib_info(ph);
  destroy_thread_info(ph);
  free(ph);
N
never 已提交
203 204 205
}

lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) {
M
minqi 已提交
206
  return add_lib_info_fd(ph, libname, -1, base);
N
never 已提交
207 208 209 210
}

lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) {
   lib_info* newlib;
M
minqi 已提交
211
  print_debug("add_lib_info_fd %s\n", libname);
N
never 已提交
212

M
minqi 已提交
213 214 215 216
  if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) {
    print_debug("can't allocate memory for lib_info\n");
    return NULL;
  }
N
never 已提交
217

M
minqi 已提交
218 219
  strncpy(newlib->name, libname, sizeof(newlib->name));
  newlib->base = base;
N
never 已提交
220

M
minqi 已提交
221 222 223
  if (fd == -1) {
    if ( (newlib->fd = pathmap_open(newlib->name)) < 0) {
      print_debug("can't open shared object %s\n", newlib->name);
N
never 已提交
224 225
      free(newlib);
      return NULL;
M
minqi 已提交
226 227 228 229
    }
  } else {
    newlib->fd = fd;
  }
N
never 已提交
230

M
minqi 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
#ifdef __APPLE__
  // check whether we have got an Macho file.
  if (is_macho_file(newlib->fd) == false) {
    close(newlib->fd);
    free(newlib);
    print_debug("not a mach-o file\n");
    return NULL;
  }
#else
  // check whether we have got an ELF file. /proc/<pid>/map
  // gives out all file mappings and not just shared objects
  if (is_elf_file(newlib->fd) == false) {
    close(newlib->fd);
    free(newlib);
    return NULL;
  }
#endif // __APPLE__
N
never 已提交
248

M
minqi 已提交
249 250 251 252 253 254
  newlib->symtab = build_symtab(newlib->fd);
  if (newlib->symtab == NULL) {
    print_debug("symbol table build failed for %s\n", newlib->name);
  } else {
    print_debug("built symbol table for %s\n", newlib->name);
  }
N
never 已提交
255

M
minqi 已提交
256 257 258 259 260 261 262 263 264 265 266
  // even if symbol table building fails, we add the lib_info.
  // This is because we may need to read from the ELF file or MachO file for core file
  // address read functionality. lookup_symbol checks for NULL symtab.
  if (ph->libs) {
    ph->lib_tail->next = newlib;
    ph->lib_tail = newlib;
  }  else {
    ph->libs = ph->lib_tail = newlib;
  }
  ph->num_libs++;
  return newlib;
N
never 已提交
267 268 269 270 271
}

// lookup for a specific symbol
uintptr_t lookup_symbol(struct ps_prochandle* ph,  const char* object_name,
                       const char* sym_name) {
M
minqi 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  // ignore object_name. search in all libraries
  // FIXME: what should we do with object_name?? The library names are obtained
  // by parsing /proc/<pid>/maps, which may not be the same as object_name.
  // What we need is a utility to map object_name to real file name, something
  // dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For
  // now, we just ignore object_name and do a global search for the symbol.

  lib_info* lib = ph->libs;
  while (lib) {
    if (lib->symtab) {
      uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL);
      if (res) return res;
    }
    lib = lib->next;
  }
N
never 已提交
287

M
minqi 已提交
288
  print_debug("lookup failed for symbol '%s' in obj '%s'\n",
N
never 已提交
289
                          sym_name, object_name);
M
minqi 已提交
290
  return (uintptr_t) NULL;
N
never 已提交
291 292 293
}

const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) {
M
minqi 已提交
294 295 296 297 298 299 300 301 302 303
  const char* res = NULL;
  lib_info* lib = ph->libs;
  while (lib) {
    if (lib->symtab && addr >= lib->base) {
      res = nearest_symbol(lib->symtab, addr - lib->base, poffset);
      if (res) return res;
    }
    lib = lib->next;
  }
  return NULL;
N
never 已提交
304 305 306
}

// add a thread to ps_prochandle
M
minqi 已提交
307 308 309 310 311 312
sa_thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
  sa_thread_info* newthr;
  if ( (newthr = (sa_thread_info*) calloc(1, sizeof(sa_thread_info))) == NULL) {
    print_debug("can't allocate memory for thread_info\n");
    return NULL;
  }
N
never 已提交
313

M
minqi 已提交
314 315 316
  // initialize thread info
  newthr->pthread_id = pthread_id;
  newthr->lwp_id = lwp_id;
N
never 已提交
317

M
minqi 已提交
318 319 320 321 322
  // add new thread to the list
  newthr->next = ph->threads;
  ph->threads = newthr;
  ph->num_threads++;
  return newthr;
N
never 已提交
323 324
}

M
minqi 已提交
325
#ifndef __APPLE__
N
never 已提交
326 327
// struct used for client data from thread_db callback
struct thread_db_client_data {
M
minqi 已提交
328 329
  struct ps_prochandle* ph;
  thread_info_callback callback;
N
never 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
};

// callback function for libthread_db
static int thread_db_callback(const td_thrhandle_t *th_p, void *data) {
  struct thread_db_client_data* ptr = (struct thread_db_client_data*) data;
  td_thrinfo_t ti;
  td_err_e err;

  memset(&ti, 0, sizeof(ti));
  err = td_thr_get_info(th_p, &ti);
  if (err != TD_OK) {
    print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n");
    return err;
  }

  print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid);

  if (ptr->callback(ptr->ph, (pthread_t)ti.ti_tid, ti.ti_lid) != true)
    return TD_ERR;

  return TD_OK;
}

// read thread_info using libthread_db
bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) {
  struct thread_db_client_data mydata;
  td_thragent_t* thread_agent = NULL;
  if (td_ta_new(ph, &thread_agent) != TD_OK) {
     print_debug("can't create libthread_db agent\n");
     return false;
  }

  mydata.ph = ph;
  mydata.callback = cb;

  // we use libthread_db iterator to iterate thru list of threads.
  if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata,
                 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
                 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) {
     td_ta_delete(thread_agent);
     return false;
  }

  // delete thread agent
  td_ta_delete(thread_agent);
  return true;
}

M
minqi 已提交
378
#endif // __APPLE__
N
never 已提交
379 380 381 382 383 384 385 386

// get number of threads
int get_num_threads(struct ps_prochandle* ph) {
   return ph->num_threads;
}

// get lwp_id of n'th thread
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) {
M
minqi 已提交
387 388 389 390 391 392 393 394 395 396
  int count = 0;
  sa_thread_info* thr = ph->threads;
  while (thr) {
    if (count == index) {
      return thr->lwp_id;
    }
    count++;
    thr = thr->next;
  }
  return 0;
N
never 已提交
397 398
}

M
minqi 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
#ifdef __APPLE__
// set lwp_id of n'th thread
bool set_lwp_id(struct ps_prochandle* ph, int index, lwpid_t lwpid) {
  int count = 0;
  sa_thread_info* thr = ph->threads;
  while (thr) {
    if (count == index) {
      thr->lwp_id = lwpid;
      return true;
    }
    count++;
    thr = thr->next;
  }
  return false;
}

// get regs of n-th thread, only used in fillThreads the first time called
bool get_nth_lwp_regs(struct ps_prochandle* ph, int index, struct reg* regs) {
  int count = 0;
  sa_thread_info* thr = ph->threads;
  while (thr) {
    if (count == index) {
      break;
    }
    count++;
    thr = thr->next;
  }
  if (thr != NULL) {
    memcpy(regs, &thr->regs, sizeof(struct reg));
    return true;
  }
  return false;
}

#endif // __APPLE__

N
never 已提交
435 436 437 438 439 440 441
// get regs for a given lwp
bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct reg* regs) {
  return ph->ops->get_lwp_regs(ph, lwp_id, regs);
}

// get number of shared objects
int get_num_libs(struct ps_prochandle* ph) {
M
minqi 已提交
442
  return ph->num_libs;
N
never 已提交
443 444 445 446
}

// get name of n'th solib
const char* get_lib_name(struct ps_prochandle* ph, int index) {
M
minqi 已提交
447 448 449 450 451 452 453 454 455 456
  int count = 0;
  lib_info* lib = ph->libs;
  while (lib) {
    if (count == index) {
      return lib->name;
    }
    count++;
    lib = lib->next;
  }
  return NULL;
N
never 已提交
457 458 459 460
}

// get base address of a lib
uintptr_t get_lib_base(struct ps_prochandle* ph, int index) {
M
minqi 已提交
461 462 463 464 465 466 467 468 469 470
  int count = 0;
  lib_info* lib = ph->libs;
  while (lib) {
    if (count == index) {
      return lib->base;
    }
    count++;
    lib = lib->next;
  }
  return (uintptr_t)NULL;
N
never 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
}

bool find_lib(struct ps_prochandle* ph, const char *lib_name) {
  lib_info *p = ph->libs;
  while (p) {
    if (strcmp(p->name, lib_name) == 0) {
      return true;
    }
    p = p->next;
  }
  return false;
}

//--------------------------------------------------------------------------
// proc service functions

// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table
// of the load object object_name in the target process identified by ph.
// It returns the symbol's value as an address in the target process in
// *sym_addr.

ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name,
                    const char *sym_name, psaddr_t *sym_addr) {
  *sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name);
  return (*sym_addr ? PS_OK : PS_NOSYM);
}

// read "size" bytes info "buf" from address "addr"
ps_err_e ps_pread(struct ps_prochandle *ph, psaddr_t  addr,
                  void *buf, size_t size) {
  return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR;
}

// write "size" bytes of data to debuggee at address "addr"
ps_err_e ps_pwrite(struct ps_prochandle *ph, psaddr_t addr,
                   const void *buf, size_t size) {
  return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR;
}

// fill in ptrace_lwpinfo for lid
ps_err_e ps_linfo(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo) {
  return ph->ops->get_lwp_info(ph, lwp_id, linfo)? PS_OK: PS_ERR;
}

// needed for when libthread_db is compiled with TD_DEBUG defined
void
ps_plog (const char *format, ...)
{
  va_list alist;

  va_start(alist, format);
  vfprintf(stderr, format, alist);
  va_end(alist);
}

M
minqi 已提交
526
#ifndef __APPLE__
N
never 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
// ------------------------------------------------------------------------
// Functions below this point are not yet implemented. They are here only
// to make the linker happy.

ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) {
  print_debug("ps_lsetfpregs not implemented\n");
  return PS_OK;
}

ps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) {
  print_debug("ps_lsetregs not implemented\n");
  return PS_OK;
}

ps_err_e  ps_lgetfpregs(struct  ps_prochandle  *ph,  lwpid_t lid, prfpregset_t *fpregs) {
  print_debug("ps_lgetfpregs not implemented\n");
  return PS_OK;
}

ps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) {
  print_debug("ps_lgetfpregs not implemented\n");
  return PS_OK;
}

ps_err_e ps_lstop(struct ps_prochandle *ph, lwpid_t lid) {
  print_debug("ps_lstop not implemented\n");
  return PS_OK;
}

ps_err_e ps_pcontinue(struct ps_prochandle *ph) {
  print_debug("ps_pcontinue not implemented\n");
  return PS_OK;
}
M
minqi 已提交
560
#endif // __APPLE__