src/jdk.hotspot.agent/macosx/native/libsaproc/ps_core.c
author redestad
Fri, 18 Jan 2019 08:00:05 +0100
changeset 53380 c8208421a860
parent 51477 e77d7687c831
child 55069 6e0786d8451b
child 56873 af61810ccd5b
permissions -rw-r--r--
8217325: Enable inlining of java_lang_Class::oop_size_raw Reviewed-by: shade, coleenp, rehn, stefank
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     1
/*
49896
ec2dd30adbc1 8174994: SA: clhsdb printmdo throws WrongTypeException when attached to a process with CDS
jgeorge
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     4
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     8
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    13
 * accompanied this code).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    14
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    18
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    21
 * questions.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    22
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    23
 */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    24
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    25
#include <jni.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    26
#include <unistd.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    27
#include <fcntl.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    28
#include <string.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    29
#include <stdlib.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    30
#include <stddef.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    31
#include "libproc_impl.h"
51477
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
    32
#include "cds.h"
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    33
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    34
#ifdef __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    35
#include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    36
#endif
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    37
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    38
// This file has the libproc implementation to read core files.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    39
// For live processes, refer to ps_proc.c. Portions of this is adapted
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    40
// /modelled after Solaris libproc.so (in particular Pcore.c)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    41
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    42
//----------------------------------------------------------------------
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    43
// ps_prochandle cleanup helper functions
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    44
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    45
// close all file descriptors
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    46
static void close_files(struct ps_prochandle* ph) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    47
  lib_info* lib = NULL;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
    48
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    49
  // close core file descriptor
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    50
  if (ph->core->core_fd >= 0)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    51
    close(ph->core->core_fd);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    52
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    53
  // close exec file descriptor
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    54
  if (ph->core->exec_fd >= 0)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    55
    close(ph->core->exec_fd);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    56
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    57
  // close interp file descriptor
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    58
  if (ph->core->interp_fd >= 0)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    59
    close(ph->core->interp_fd);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    60
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    61
  // close class share archive file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    62
  if (ph->core->classes_jsa_fd >= 0)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    63
    close(ph->core->classes_jsa_fd);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    64
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    65
  // close all library file descriptors
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    66
  lib = ph->libs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    67
  while (lib) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    68
    int fd = lib->fd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    69
    if (fd >= 0 && fd != ph->core->exec_fd) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    70
      close(fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    71
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    72
    lib = lib->next;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    73
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    74
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    75
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    76
// clean all map_info stuff
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    77
static void destroy_map_info(struct ps_prochandle* ph) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    78
  map_info* map = ph->core->maps;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    79
  while (map) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    80
    map_info* next = map->next;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    81
    free(map);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    82
    map = next;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    83
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    84
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    85
  if (ph->core->map_array) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    86
    free(ph->core->map_array);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    87
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    88
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    89
  // Part of the class sharing workaround
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    90
  map = ph->core->class_share_maps;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    91
  while (map) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    92
    map_info* next = map->next;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    93
    free(map);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    94
    map = next;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    95
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    96
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    97
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    98
// ps_prochandle operations
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    99
static void core_release(struct ps_prochandle* ph) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   100
  if (ph->core) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   101
    close_files(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   102
    destroy_map_info(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   103
    free(ph->core);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   104
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   105
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   106
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   107
static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   108
  map_info* map;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   109
  if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   110
    print_debug("can't allocate memory for map_info\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   111
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   112
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   113
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   114
  // initialize map
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   115
  map->fd     = fd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   116
  map->offset = offset;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   117
  map->vaddr  = vaddr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   118
  map->memsz  = memsz;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   119
  return map;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   120
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   121
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   122
// add map info with given fd, offset, vaddr and memsz
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   123
static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   124
                             uintptr_t vaddr, size_t memsz) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   125
  map_info* map;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   126
  if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   127
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   128
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   129
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   130
  // add this to map list
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   131
  map->next  = ph->core->maps;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   132
  ph->core->maps   = map;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   133
  ph->core->num_maps++;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   134
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   135
  return map;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   136
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   137
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   138
// Part of the class sharing workaround
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   139
static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   140
                             uintptr_t vaddr, size_t memsz) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   141
  map_info* map;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   142
  if ((map = allocate_init_map(ph->core->classes_jsa_fd,
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   143
                               offset, vaddr, memsz)) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   144
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   145
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   146
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   147
  map->next = ph->core->class_share_maps;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   148
  ph->core->class_share_maps = map;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   149
  return map;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   150
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   151
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   152
// Return the map_info for the given virtual address.  We keep a sorted
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   153
// array of pointers in ph->map_array, so we can binary search.
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   154
static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   155
  int mid, lo = 0, hi = ph->core->num_maps - 1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   156
  map_info *mp;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   157
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   158
  while (hi - lo > 1) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   159
    mid = (lo + hi) / 2;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   160
    if (addr >= ph->core->map_array[mid]->vaddr) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   161
      lo = mid;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   162
    } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   163
      hi = mid;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   164
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   165
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   166
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   167
  if (addr < ph->core->map_array[hi]->vaddr) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   168
    mp = ph->core->map_array[lo];
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   169
  } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   170
    mp = ph->core->map_array[hi];
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   171
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   172
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   173
  if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   174
    return (mp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   175
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   176
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   177
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   178
  // Part of the class sharing workaround
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   179
  // Unfortunately, we have no way of detecting -Xshare state.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   180
  // Check out the share maps atlast, if we don't find anywhere.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   181
  // This is done this way so to avoid reading share pages
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   182
  // ahead of other normal maps. For eg. with -Xshare:off we don't
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   183
  // want to prefer class sharing data to data from core.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   184
  mp = ph->core->class_share_maps;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   185
  if (mp) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   186
    print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   187
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   188
  while (mp) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   189
    if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   190
      print_debug("located map_info at 0x%lx from class share maps\n", addr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   191
      return (mp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   192
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   193
    mp = mp->next;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   194
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   195
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   196
  print_debug("can't locate map_info at 0x%lx\n", addr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   197
  return (NULL);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   198
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   199
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   200
//---------------------------------------------------------------
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   201
// Part of the class sharing workaround:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   202
//
17310
b4118c427cc0 8005038: remove crufty '_g' support from SA
sla
parents: 16351
diff changeset
   203
// With class sharing, pages are mapped from classes.jsa file.
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   204
// The read-only class sharing pages are mapped as MAP_SHARED,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   205
// PROT_READ pages. These pages are not dumped into core dump.
17310
b4118c427cc0 8005038: remove crufty '_g' support from SA
sla
parents: 16351
diff changeset
   206
// With this workaround, these pages are read from classes.jsa.
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   207
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   208
static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   209
  jboolean i;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   210
  if (ps_pread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   211
    *pvalue = i;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   212
    return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   213
  } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   214
    return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   215
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   216
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   217
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   218
static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   219
  uintptr_t uip;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   220
  if (ps_pread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   221
    *pvalue = uip;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   222
    return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   223
  } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   224
    return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   225
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   226
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   227
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   228
// used to read strings from debuggee
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   229
static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   230
  size_t i = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   231
  char  c = ' ';
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   232
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   233
  while (c != '\0') {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   234
    if (ps_pread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   235
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   236
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   237
    if (i < size - 1) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   238
      buf[i] = c;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   239
    } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   240
      // smaller buffer
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   241
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   242
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   243
    i++; addr++;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   244
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   245
  buf[i] = '\0';
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   246
  return true;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   247
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   248
49896
ec2dd30adbc1 8174994: SA: clhsdb printmdo throws WrongTypeException when attached to a process with CDS
jgeorge
parents: 47216
diff changeset
   249
// mangled name of Arguments::SharedArchivePath
ec2dd30adbc1 8174994: SA: clhsdb printmdo throws WrongTypeException when attached to a process with CDS
jgeorge
parents: 47216
diff changeset
   250
#define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE"
ec2dd30adbc1 8174994: SA: clhsdb printmdo throws WrongTypeException when attached to a process with CDS
jgeorge
parents: 47216
diff changeset
   251
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   252
#ifdef __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   253
#define USE_SHARED_SPACES_SYM "_UseSharedSpaces"
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   254
#define LIBJVM_NAME "/libjvm.dylib"
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   255
#else
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   256
#define USE_SHARED_SPACES_SYM "UseSharedSpaces"
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   257
#define LIBJVM_NAME "/libjvm.so"
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   258
#endif // __APPLE_
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   259
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   260
static bool init_classsharing_workaround(struct ps_prochandle* ph) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   261
  int m;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   262
  size_t n;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   263
  lib_info* lib = ph->libs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   264
  while (lib != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   265
    // we are iterating over shared objects from the core dump. look for
17310
b4118c427cc0 8005038: remove crufty '_g' support from SA
sla
parents: 16351
diff changeset
   266
    // libjvm.so.
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   267
    const char *jvm_name = 0;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   268
    if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   269
      char classes_jsa[PATH_MAX];
51477
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   270
      CDSFileMapHeaderBase header;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   271
      int fd = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   272
      uintptr_t base = 0, useSharedSpacesAddr = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   273
      uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   274
      jboolean useSharedSpaces = 0;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   275
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   276
      memset(classes_jsa, 0, sizeof(classes_jsa));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   277
      jvm_name = lib->name;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   278
      useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   279
      if (useSharedSpacesAddr == 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   280
        print_debug("can't lookup 'UseSharedSpaces' flag\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   281
        return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   282
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   283
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   284
      // Hotspot vm types are not exported to build this library. So
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   285
      // using equivalent type jboolean to read the value of
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   286
      // UseSharedSpaces which is same as hotspot type "bool".
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   287
      if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   288
        print_debug("can't read the value of 'UseSharedSpaces' flag\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   289
        return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   290
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   291
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   292
      if ((int)useSharedSpaces == 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   293
        print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   294
        return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   295
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   296
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   297
      sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   298
      if (sharedArchivePathAddrAddr == 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   299
        print_debug("can't lookup shared archive path symbol\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   300
        return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   301
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   302
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   303
      if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   304
        print_debug("can't read shared archive path pointer\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   305
        return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   306
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   307
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   308
      if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   309
        print_debug("can't read shared archive path value\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   310
        return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   311
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   312
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   313
      print_debug("looking for %s\n", classes_jsa);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   314
      // open the class sharing archive file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   315
      fd = pathmap_open(classes_jsa);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   316
      if (fd < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   317
        print_debug("can't open %s!\n", classes_jsa);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   318
        ph->core->classes_jsa_fd = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   319
        return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   320
      } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   321
        print_debug("opened %s\n", classes_jsa);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   322
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   323
51477
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   324
      // read CDSFileMapHeaderBase from the file
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   325
      memset(&header, 0, sizeof(CDSFileMapHeaderBase));
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   326
      if ((n = read(fd, &header, sizeof(CDSFileMapHeaderBase)))
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   327
           != sizeof(CDSFileMapHeaderBase)) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   328
        print_debug("can't read shared archive file map header from %s\n", classes_jsa);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   329
        close(fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   330
        return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   331
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   332
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   333
      // check file magic
51477
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   334
      if (header._magic != CDS_ARCHIVE_MAGIC) {
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   335
        print_debug("%s has bad shared archive file magic number 0x%x, expecting 0x%x\n",
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   336
                    classes_jsa, header._magic, CDS_ARCHIVE_MAGIC);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   337
        close(fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   338
        return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   339
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   340
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   341
      // check version
51477
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   342
      if (header._version != CURRENT_CDS_ARCHIVE_VERSION) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   343
        print_debug("%s has wrong shared archive file version %d, expecting %d\n",
51477
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   344
                     classes_jsa, header._version, CURRENT_CDS_ARCHIVE_VERSION);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   345
        close(fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   346
        return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   347
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   348
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   349
      ph->core->classes_jsa_fd = fd;
17310
b4118c427cc0 8005038: remove crufty '_g' support from SA
sla
parents: 16351
diff changeset
   350
      // add read-only maps from classes.jsa to the list of maps
51477
e77d7687c831 8209657: Refactor filemap.hpp to simplify integration with Serviceability Agent
iklam
parents: 49896
diff changeset
   351
      for (m = 0; m < NUM_CDS_REGIONS; m++) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   352
        if (header._space[m]._read_only) {
49896
ec2dd30adbc1 8174994: SA: clhsdb printmdo throws WrongTypeException when attached to a process with CDS
jgeorge
parents: 47216
diff changeset
   353
          base = (uintptr_t) header._space[m]._addr._base;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   354
          // no need to worry about the fractional pages at-the-end.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   355
          // possible fractional pages are handled by core_read_data.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   356
          add_class_share_map_info(ph, (off_t) header._space[m]._file_offset,
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   357
                                   base, (size_t) header._space[m]._used);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   358
          print_debug("added a share archive map at 0x%lx\n", base);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   359
        }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   360
      }
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   361
      return true;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   362
   }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   363
   lib = lib->next;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   364
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   365
  return true;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   366
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   367
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   368
//---------------------------------------------------------------------------
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   369
// functions to handle map_info
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   370
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   371
// Order mappings based on virtual address.  We use this function as the
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   372
// callback for sorting the array of map_info pointers.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   373
static int core_cmp_mapping(const void *lhsp, const void *rhsp)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   374
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   375
  const map_info *lhs = *((const map_info **)lhsp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   376
  const map_info *rhs = *((const map_info **)rhsp);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   377
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   378
  if (lhs->vaddr == rhs->vaddr) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   379
    return (0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   380
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   381
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   382
  return (lhs->vaddr < rhs->vaddr ? -1 : 1);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   383
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   384
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   385
// we sort map_info by starting virtual address so that we can do
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   386
// binary search to read from an address.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   387
static bool sort_map_array(struct ps_prochandle* ph) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   388
  size_t num_maps = ph->core->num_maps;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   389
  map_info* map = ph->core->maps;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   390
  int i = 0;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   391
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   392
  // allocate map_array
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   393
  map_info** array;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   394
  if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   395
    print_debug("can't allocate memory for map array\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   396
    return false;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   397
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   398
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   399
  // add maps to array
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   400
  while (map) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   401
    array[i] = map;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   402
    i++;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   403
    map = map->next;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   404
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   405
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   406
  // sort is called twice. If this is second time, clear map array
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   407
  if (ph->core->map_array) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   408
    free(ph->core->map_array);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   409
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   410
  ph->core->map_array = array;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   411
  // sort the map_info array by base virtual address.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   412
  qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   413
        core_cmp_mapping);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   414
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   415
  // print map
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   416
  if (is_debug()) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   417
    int j = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   418
    print_debug("---- sorted virtual address map ----\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   419
    for (j = 0; j < ph->core->num_maps; j++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   420
      print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr,
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   421
                  ph->core->map_array[j]->memsz);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   422
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   423
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   424
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   425
  return true;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   426
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   427
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   428
#ifndef MIN
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   429
#define MIN(x, y) (((x) < (y))? (x): (y))
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   430
#endif
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   431
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   432
static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   433
   ssize_t resid = size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   434
   int page_size=sysconf(_SC_PAGE_SIZE);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   435
   while (resid != 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   436
      map_info *mp = core_lookup(ph, addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   437
      uintptr_t mapoff;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   438
      ssize_t len, rem;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   439
      off_t off;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   440
      int fd;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   441
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   442
      if (mp == NULL) {
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   443
         break;  /* No mapping for this address */
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   444
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   445
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   446
      fd = mp->fd;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   447
      mapoff = addr - mp->vaddr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   448
      len = MIN(resid, mp->memsz - mapoff);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   449
      off = mp->offset + mapoff;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   450
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   451
      if ((len = pread(fd, buf, len, off)) <= 0) {
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   452
         break;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   453
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   454
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   455
      resid -= len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   456
      addr += len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   457
      buf = (char *)buf + len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   458
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   459
      // mappings always start at page boundary. But, may end in fractional
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   460
      // page. fill zeros for possible fractional page at the end of a mapping.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   461
      rem = mp->memsz % page_size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   462
      if (rem > 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   463
         rem = page_size - rem;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   464
         len = MIN(resid, rem);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   465
         resid -= len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   466
         addr += len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   467
         // we are not assuming 'buf' to be zero initialized.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   468
         memset(buf, 0, len);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   469
         buf += len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   470
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   471
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   472
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   473
   if (resid) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   474
      print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n",
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   475
              size, addr, resid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   476
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   477
   } else {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   478
      return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   479
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   480
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   481
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   482
// null implementation for write
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   483
static bool core_write_data(struct ps_prochandle* ph,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   484
                             uintptr_t addr, const char *buf , size_t size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   485
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   486
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   487
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   488
static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   489
                          struct reg* regs) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   490
   // for core we have cached the lwp regs after segment parsed
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   491
   sa_thread_info* thr = ph->threads;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   492
   while (thr) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   493
     if (thr->lwp_id == lwp_id) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   494
       memcpy(regs, &thr->regs, sizeof(struct reg));
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   495
       return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   496
     }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   497
     thr = thr->next;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   498
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   499
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   500
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   501
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   502
static bool core_get_lwp_info(struct ps_prochandle *ph, lwpid_t id, void *info) {
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   503
   print_debug("core_get_lwp_info not implemented\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   504
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   505
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   506
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   507
static ps_prochandle_ops core_ops = {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   508
   .release=  core_release,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   509
   .p_pread=  core_read_data,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   510
   .p_pwrite= core_write_data,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   511
   .get_lwp_regs= core_get_lwp_regs,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   512
   .get_lwp_info= core_get_lwp_info
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   513
};
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   514
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   515
// from this point, mainly two blocks divided by def __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   516
// one for Macosx, the other for regular Bsd
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   517
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   518
#ifdef __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   519
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   520
void print_thread(sa_thread_info *threadinfo) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   521
  print_debug("thread added: %d\n", threadinfo->lwp_id);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   522
  print_debug("registers:\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   523
  print_debug("  r_r15: 0x%" PRIx64 "\n", threadinfo->regs.r_r15);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   524
  print_debug("  r_r14: 0x%" PRIx64 "\n", threadinfo->regs.r_r14);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   525
  print_debug("  r_r13: 0x%" PRIx64 "\n", threadinfo->regs.r_r13);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   526
  print_debug("  r_r12: 0x%" PRIx64 "\n", threadinfo->regs.r_r12);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   527
  print_debug("  r_r11: 0x%" PRIx64 "\n", threadinfo->regs.r_r11);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   528
  print_debug("  r_r10: 0x%" PRIx64 "\n", threadinfo->regs.r_r10);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   529
  print_debug("  r_r9:  0x%" PRIx64 "\n", threadinfo->regs.r_r9);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   530
  print_debug("  r_r8:  0x%" PRIx64 "\n", threadinfo->regs.r_r8);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   531
  print_debug("  r_rdi: 0x%" PRIx64 "\n", threadinfo->regs.r_rdi);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   532
  print_debug("  r_rsi: 0x%" PRIx64 "\n", threadinfo->regs.r_rsi);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   533
  print_debug("  r_rbp: 0x%" PRIx64 "\n", threadinfo->regs.r_rbp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   534
  print_debug("  r_rbx: 0x%" PRIx64 "\n", threadinfo->regs.r_rbx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   535
  print_debug("  r_rdx: 0x%" PRIx64 "\n", threadinfo->regs.r_rdx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   536
  print_debug("  r_rcx: 0x%" PRIx64 "\n", threadinfo->regs.r_rcx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   537
  print_debug("  r_rax: 0x%" PRIx64 "\n", threadinfo->regs.r_rax);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   538
  print_debug("  r_fs:  0x%" PRIx32 "\n", threadinfo->regs.r_fs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   539
  print_debug("  r_gs:  0x%" PRIx32 "\n", threadinfo->regs.r_gs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   540
  print_debug("  r_rip  0x%" PRIx64 "\n", threadinfo->regs.r_rip);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   541
  print_debug("  r_cs:  0x%" PRIx64 "\n", threadinfo->regs.r_cs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   542
  print_debug("  r_rsp: 0x%" PRIx64 "\n", threadinfo->regs.r_rsp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   543
  print_debug("  r_rflags: 0x%" PRIx64 "\n", threadinfo->regs.r_rflags);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   544
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   545
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   546
// read all segments64 commands from core file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   547
// read all thread commands from core file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   548
static bool read_core_segments(struct ps_prochandle* ph) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   549
  int i = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   550
  int num_threads = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   551
  int fd = ph->core->core_fd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   552
  off_t offset = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   553
  mach_header_64      fhead;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   554
  load_command        lcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   555
  segment_command_64  segcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   556
  // thread_command      thrcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   557
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   558
  lseek(fd, offset, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   559
  if(read(fd, (void *)&fhead, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   560
     goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   561
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   562
  print_debug("total commands: %d\n", fhead.ncmds);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   563
  offset += sizeof(mach_header_64);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   564
  for (i = 0; i < fhead.ncmds; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   565
    lseek(fd, offset, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   566
    if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   567
      goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   568
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   569
    offset += lcmd.cmdsize;    // next command position
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   570
    if (lcmd.cmd == LC_SEGMENT_64) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   571
      lseek(fd, -sizeof(load_command), SEEK_CUR);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   572
      if (read(fd, (void *)&segcmd, sizeof(segment_command_64)) != sizeof(segment_command_64)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   573
        print_debug("failed to read LC_SEGMENT_64 i = %d!\n", i);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   574
        goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   575
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   576
      if (add_map_info(ph, fd, segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   577
        print_debug("Failed to add map_info at i = %d\n", i);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   578
        goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   579
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   580
      print_debug("segment added: %" PRIu64 " 0x%" PRIx64 " %d\n",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   581
                   segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   582
    } else if (lcmd.cmd == LC_THREAD || lcmd.cmd == LC_UNIXTHREAD) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   583
      typedef struct thread_fc {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   584
        uint32_t  flavor;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   585
        uint32_t  count;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   586
      } thread_fc;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   587
      thread_fc fc;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   588
      uint32_t size = sizeof(load_command);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   589
      while (size < lcmd.cmdsize) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   590
        if (read(fd, (void *)&fc, sizeof(thread_fc)) != sizeof(thread_fc)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   591
          printf("Reading flavor, count failed.\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   592
          goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   593
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   594
        size += sizeof(thread_fc);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   595
        if (fc.flavor == x86_THREAD_STATE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   596
          x86_thread_state_t thrstate;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   597
          if (read(fd, (void *)&thrstate, sizeof(x86_thread_state_t)) != sizeof(x86_thread_state_t)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   598
            printf("Reading flavor, count failed.\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   599
            goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   600
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   601
          size += sizeof(x86_thread_state_t);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   602
          // create thread info list, update lwp_id later
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   603
          sa_thread_info* newthr = add_thread_info(ph, (pthread_t) -1, (lwpid_t) num_threads++);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   604
          if (newthr == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   605
            printf("create thread_info failed\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   606
            goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   607
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   608
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   609
          // note __DARWIN_UNIX03 depengs on other definitions
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   610
#if __DARWIN_UNIX03
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   611
#define get_register_v(regst, regname) \
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   612
  regst.uts.ts64.__##regname
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   613
#else
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   614
#define get_register_v(regst, regname) \
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   615
  regst.uts.ts64.##regname
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   616
#endif // __DARWIN_UNIX03
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   617
          newthr->regs.r_rax = get_register_v(thrstate, rax);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   618
          newthr->regs.r_rbx = get_register_v(thrstate, rbx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   619
          newthr->regs.r_rcx = get_register_v(thrstate, rcx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   620
          newthr->regs.r_rdx = get_register_v(thrstate, rdx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   621
          newthr->regs.r_rdi = get_register_v(thrstate, rdi);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   622
          newthr->regs.r_rsi = get_register_v(thrstate, rsi);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   623
          newthr->regs.r_rbp = get_register_v(thrstate, rbp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   624
          newthr->regs.r_rsp = get_register_v(thrstate, rsp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   625
          newthr->regs.r_r8  = get_register_v(thrstate, r8);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   626
          newthr->regs.r_r9  = get_register_v(thrstate, r9);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   627
          newthr->regs.r_r10 = get_register_v(thrstate, r10);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   628
          newthr->regs.r_r11 = get_register_v(thrstate, r11);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   629
          newthr->regs.r_r12 = get_register_v(thrstate, r12);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   630
          newthr->regs.r_r13 = get_register_v(thrstate, r13);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   631
          newthr->regs.r_r14 = get_register_v(thrstate, r14);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   632
          newthr->regs.r_r15 = get_register_v(thrstate, r15);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   633
          newthr->regs.r_rip = get_register_v(thrstate, rip);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   634
          newthr->regs.r_rflags = get_register_v(thrstate, rflags);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   635
          newthr->regs.r_cs  = get_register_v(thrstate, cs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   636
          newthr->regs.r_fs  = get_register_v(thrstate, fs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   637
          newthr->regs.r_gs  = get_register_v(thrstate, gs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   638
          print_thread(newthr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   639
        } else if (fc.flavor == x86_FLOAT_STATE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   640
          x86_float_state_t flstate;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   641
          if (read(fd, (void *)&flstate, sizeof(x86_float_state_t)) != sizeof(x86_float_state_t)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   642
            print_debug("Reading flavor, count failed.\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   643
            goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   644
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   645
          size += sizeof(x86_float_state_t);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   646
        } else if (fc.flavor == x86_EXCEPTION_STATE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   647
          x86_exception_state_t excpstate;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   648
          if (read(fd, (void *)&excpstate, sizeof(x86_exception_state_t)) != sizeof(x86_exception_state_t)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   649
            printf("Reading flavor, count failed.\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   650
            goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   651
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   652
          size += sizeof(x86_exception_state_t);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   653
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   654
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   655
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   656
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   657
  return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   658
err:
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   659
  return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   660
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   661
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   662
/**local function **/
42549
154099b110df 8169344: Potential open file descriptor in exists() of hotspot/agent/src/os/bsd/ps_core.c
dsamersoff
parents: 35217
diff changeset
   663
bool exists(const char *fname) {
154099b110df 8169344: Potential open file descriptor in exists() of hotspot/agent/src/os/bsd/ps_core.c
dsamersoff
parents: 35217
diff changeset
   664
  return access(fname, F_OK) == 0;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   665
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   666
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   667
// we check: 1. lib
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   668
//           2. lib/server
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   669
//           3. jre/lib
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   670
//           4. jre/lib/server
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   671
// from: 1. exe path
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   672
//       2. JAVA_HOME
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   673
//       3. DYLD_LIBRARY_PATH
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   674
static bool get_real_path(struct ps_prochandle* ph, char *rpath) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   675
  /** check if they exist in JAVA ***/
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   676
  char* execname = ph->core->exec_path;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   677
  char  filepath[4096];
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   678
  char* filename = strrchr(rpath, '/');               // like /libjvm.dylib
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   679
  if (filename == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   680
    return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   681
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   682
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   683
  char* posbin = strstr(execname, "/bin/java");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   684
  if (posbin != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   685
    memcpy(filepath, execname, posbin - execname);    // not include trailing '/'
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   686
    filepath[posbin - execname] = '\0';
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   687
  } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   688
    char* java_home = getenv("JAVA_HOME");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   689
    if (java_home != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   690
      strcpy(filepath, java_home);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   691
    } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   692
      char* dyldpath = getenv("DYLD_LIBRARY_PATH");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   693
      char* dypath = strtok(dyldpath, ":");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   694
      while (dypath != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   695
        strcpy(filepath, dypath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   696
        strcat(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   697
        if (exists(filepath)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   698
           strcpy(rpath, filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   699
           return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   700
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   701
        dypath = strtok(dyldpath, ":");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   702
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   703
      // not found
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   704
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   705
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   706
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   707
  // for exec and java_home, jdkpath now is filepath
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   708
  size_t filepath_base_size = strlen(filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   709
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   710
  // first try /lib/ and /lib/server
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   711
  strcat(filepath, "/lib");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   712
  strcat(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   713
  if (exists(filepath)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   714
    strcpy(rpath, filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   715
    return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   716
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   717
  char* pos = strstr(filepath, filename);    // like /libjvm.dylib
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   718
  *pos = '\0';
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   719
  strcat(filepath, "/server");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   720
  strcat(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   721
  if (exists(filepath)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   722
    strcpy(rpath, filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   723
    return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   724
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   725
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   726
  // then try /jre/lib/ and /jre/lib/server
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   727
  filepath[filepath_base_size] = '\0';
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   728
  strcat(filepath, "/jre/lib");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   729
  strcat(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   730
  if (exists(filepath)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   731
    strcpy(rpath, filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   732
    return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   733
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   734
  pos = strstr(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   735
  *pos = '\0';
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   736
  strcat(filepath, "/server");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   737
  strcat(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   738
  if (exists(filepath)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   739
    strcpy(rpath, filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   740
    return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   741
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   742
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   743
  return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   744
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   745
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   746
static bool read_shared_lib_info(struct ps_prochandle* ph) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   747
  static int pagesize = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   748
  int fd = ph->core->core_fd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   749
  int i = 0, j;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   750
  uint32_t  v;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   751
  mach_header_64 header;        // used to check if a file header in segment
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   752
  load_command lcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   753
  dylib_command dylibcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   754
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   755
  char name[BUF_SIZE];  // use to store name
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   756
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   757
  if (pagesize == 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   758
    pagesize = getpagesize();
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   759
    print_debug("page size is %d\n", pagesize);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   760
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   761
  for (j = 0; j < ph->core->num_maps; j++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   762
    map_info *iter = ph->core->map_array[j];   // head
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   763
    off_t fpos = iter->offset;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   764
    if (iter->fd != fd) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   765
      // only search core file!
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   766
      continue;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   767
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   768
    print_debug("map_info %d: vmaddr = 0x%016" PRIx64 "  fileoff = %" PRIu64 "  vmsize = %" PRIu64 "\n",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   769
                           j, iter->vaddr, iter->offset, iter->memsz);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   770
    lseek(fd, fpos, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   771
    // we assume .dylib loaded at segment address --- which is true for JVM libraries
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   772
    // multiple files may be loaded in one segment.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   773
    // if first word is not a magic word, means this segment does not contain lib file.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   774
    if (read(fd, (void *)&v, sizeof(uint32_t)) == sizeof(uint32_t)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   775
      if (v != MH_MAGIC_64) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   776
        continue;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   777
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   778
    } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   779
      // may be encountered last map, which is not readable
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   780
      continue;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   781
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   782
    while (ltell(fd) - iter->offset < iter->memsz) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   783
      lseek(fd, fpos, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   784
      if (read(fd, (void *)&v, sizeof(uint32_t)) != sizeof(uint32_t)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   785
        break;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   786
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   787
      if (v != MH_MAGIC_64) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   788
        fpos = (ltell(fd) + pagesize -1)/pagesize * pagesize;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   789
        continue;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   790
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   791
      lseek(fd, -sizeof(uint32_t), SEEK_CUR);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   792
      // this is the file begining to core file.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   793
      if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   794
        goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   795
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   796
      fpos = ltell(fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   797
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   798
      // found a mach-o file in this segment
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   799
      for (i = 0; i < header.ncmds; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   800
        // read commands in this "file"
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   801
        // LC_ID_DYLIB is the file itself for a .dylib
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   802
        lseek(fd, fpos, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   803
        if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   804
          return false;   // error
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   805
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   806
        fpos += lcmd.cmdsize;  // next command position
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   807
        // make sure still within seg size.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   808
        if (fpos  - lcmd.cmdsize - iter->offset > iter->memsz) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   809
          print_debug("Warning: out of segement limit: %ld \n", fpos  - lcmd.cmdsize - iter->offset);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   810
          break;  // no need to iterate all commands
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   811
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   812
        if (lcmd.cmd == LC_ID_DYLIB) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   813
          lseek(fd, -sizeof(load_command), SEEK_CUR);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   814
          if (read(fd, (void *)&dylibcmd, sizeof(dylib_command)) != sizeof(dylib_command)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   815
            return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   816
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   817
          /**** name stored at dylib_command.dylib.name.offset, is a C string  */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   818
          lseek(fd, dylibcmd.dylib.name.offset - sizeof(dylib_command), SEEK_CUR);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   819
          int j = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   820
          while (j < BUF_SIZE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   821
            read(fd, (void *)(name + j), sizeof(char));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   822
            if (name[j] == '\0') break;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   823
            j++;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   824
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   825
          print_debug("%s\n", name);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   826
          // changed name from @rpath/xxxx.dylib to real path
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   827
          if (strrchr(name, '@')) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   828
            get_real_path(ph, name);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   829
            print_debug("get_real_path returned: %s\n", name);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   830
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   831
          add_lib_info(ph, name, iter->vaddr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   832
          break;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   833
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   834
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   835
      // done with the file, advanced to next page to search more files
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   836
      fpos = (ltell(fd) + pagesize - 1) / pagesize * pagesize;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   837
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   838
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   839
  return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   840
err:
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   841
  return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   842
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   843
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   844
bool read_macho64_header(int fd, mach_header_64* core_header) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   845
  bool is_macho = false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   846
  if (fd < 0) return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   847
  off_t pos = ltell(fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   848
  lseek(fd, 0, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   849
  if (read(fd, (void *)core_header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   850
    is_macho = false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   851
  } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   852
    is_macho = (core_header->magic ==  MH_MAGIC_64 || core_header->magic ==  MH_CIGAM_64);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   853
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   854
  lseek(fd, pos, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   855
  return is_macho;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   856
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   857
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   858
// the one and only one exposed stuff from this file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   859
struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   860
  mach_header_64 core_header;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   861
  mach_header_64 exec_header;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   862
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   863
  struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   864
  if (ph == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   865
    print_debug("cant allocate ps_prochandle\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   866
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   867
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   868
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   869
  if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   870
    free(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   871
    print_debug("can't allocate ps_prochandle\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   872
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   873
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   874
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   875
  // initialize ph
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   876
  ph->ops = &core_ops;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   877
  ph->core->core_fd   = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   878
  ph->core->exec_fd   = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   879
  ph->core->interp_fd = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   880
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   881
  print_debug("exec: %s   core: %s", exec_file, core_file);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   882
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   883
  strncpy(ph->core->exec_path, exec_file, sizeof(ph->core->exec_path));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   884
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   885
  // open the core file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   886
  if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   887
    print_error("can't open core file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   888
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   889
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   890
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   891
  // read core file header
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   892
  if (read_macho64_header(ph->core->core_fd, &core_header) != true || core_header.filetype != MH_CORE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   893
    print_debug("core file is not a valid Mach-O file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   894
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   895
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   896
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   897
  if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   898
    print_error("can't open executable file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   899
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   900
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   901
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   902
  if (read_macho64_header(ph->core->exec_fd, &exec_header) != true ||
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   903
                          exec_header.filetype != MH_EXECUTE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   904
    print_error("executable file is not a valid Mach-O file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   905
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   906
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   907
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   908
  // process core file segments
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   909
  if (read_core_segments(ph) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   910
    print_error("failed to read core segments\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   911
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   912
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   913
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   914
  // allocate and sort maps into map_array, we need to do this
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   915
  // here because read_shared_lib_info needs to read from debuggee
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   916
  // address space
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   917
  if (sort_map_array(ph) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   918
    print_error("failed to sort segment map array\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   919
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   920
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   921
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   922
  if (read_shared_lib_info(ph) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   923
    print_error("failed to read libraries\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   924
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   925
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   926
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   927
  // sort again because we have added more mappings from shared objects
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   928
  if (sort_map_array(ph) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   929
    print_error("failed to sort segment map array\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   930
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   931
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   932
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   933
  if (init_classsharing_workaround(ph) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   934
    print_error("failed to workaround classshareing\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   935
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   936
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   937
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   938
  print_debug("Leave Pgrab_core\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   939
  return ph;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   940
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   941
err:
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   942
  Prelease(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   943
  return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   944
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   945
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   946
#else // __APPLE__ (none macosx)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   947
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   948
// read regs and create thread from core file
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   949
static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   950
   // we have to read prstatus_t from buf
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   951
   // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   952
   prstatus_t* prstat = (prstatus_t*) buf;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   953
   sa_thread_info* newthr;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   954
   print_debug("got integer regset for lwp %d\n", prstat->pr_pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   955
   // we set pthread_t to -1 for core dump
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   956
   if((newthr = add_thread_info(ph, (pthread_t) -1,  prstat->pr_pid)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   957
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   958
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   959
   // copy regs
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   960
   memcpy(&newthr->regs, &prstat->pr_reg, sizeof(struct reg));
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   961
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   962
   if (is_debug()) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   963
      print_debug("integer regset\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   964
#ifdef i386
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   965
      // print the regset
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   966
      print_debug("\teax = 0x%x\n", newthr->regs.r_eax);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   967
      print_debug("\tebx = 0x%x\n", newthr->regs.r_ebx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   968
      print_debug("\tecx = 0x%x\n", newthr->regs.r_ecx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   969
      print_debug("\tedx = 0x%x\n", newthr->regs.r_edx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   970
      print_debug("\tesp = 0x%x\n", newthr->regs.r_esp);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   971
      print_debug("\tebp = 0x%x\n", newthr->regs.r_ebp);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   972
      print_debug("\tesi = 0x%x\n", newthr->regs.r_esi);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   973
      print_debug("\tedi = 0x%x\n", newthr->regs.r_edi);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   974
      print_debug("\teip = 0x%x\n", newthr->regs.r_eip);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   975
#endif
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   976
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   977
#if defined(amd64) || defined(x86_64)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   978
      // print the regset
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   979
      print_debug("\tr15 = 0x%lx\n", newthr->regs.r_r15);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   980
      print_debug("\tr14 = 0x%lx\n", newthr->regs.r_r14);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   981
      print_debug("\tr13 = 0x%lx\n", newthr->regs.r_r13);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   982
      print_debug("\tr12 = 0x%lx\n", newthr->regs.r_r12);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   983
      print_debug("\trbp = 0x%lx\n", newthr->regs.r_rbp);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   984
      print_debug("\trbx = 0x%lx\n", newthr->regs.r_rbx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   985
      print_debug("\tr11 = 0x%lx\n", newthr->regs.r_r11);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   986
      print_debug("\tr10 = 0x%lx\n", newthr->regs.r_r10);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   987
      print_debug("\tr9 = 0x%lx\n", newthr->regs.r_r9);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   988
      print_debug("\tr8 = 0x%lx\n", newthr->regs.r_r8);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   989
      print_debug("\trax = 0x%lx\n", newthr->regs.r_rax);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   990
      print_debug("\trcx = 0x%lx\n", newthr->regs.r_rcx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   991
      print_debug("\trdx = 0x%lx\n", newthr->regs.r_rdx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   992
      print_debug("\trsi = 0x%lx\n", newthr->regs.r_rsi);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   993
      print_debug("\trdi = 0x%lx\n", newthr->regs.r_rdi);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   994
      //print_debug("\torig_rax = 0x%lx\n", newthr->regs.orig_rax);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   995
      print_debug("\trip = 0x%lx\n", newthr->regs.r_rip);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   996
      print_debug("\tcs = 0x%lx\n", newthr->regs.r_cs);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   997
      //print_debug("\teflags = 0x%lx\n", newthr->regs.eflags);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   998
      print_debug("\trsp = 0x%lx\n", newthr->regs.r_rsp);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   999
      print_debug("\tss = 0x%lx\n", newthr->regs.r_ss);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1000
      //print_debug("\tfs_base = 0x%lx\n", newthr->regs.fs_base);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1001
      //print_debug("\tgs_base = 0x%lx\n", newthr->regs.gs_base);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1002
      //print_debug("\tds = 0x%lx\n", newthr->regs.ds);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1003
      //print_debug("\tes = 0x%lx\n", newthr->regs.es);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1004
      //print_debug("\tfs = 0x%lx\n", newthr->regs.fs);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1005
      //print_debug("\tgs = 0x%lx\n", newthr->regs.gs);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1006
#endif
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1007
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1008
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1009
   return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1010
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1011
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1012
#define ROUNDUP(x, y)  ((((x)+((y)-1))/(y))*(y))
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1013
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1014
// read NT_PRSTATUS entries from core NOTE segment
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1015
static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1016
   char* buf = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1017
   char* p = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1018
   size_t size = note_phdr->p_filesz;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1019
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1020
   // we are interested in just prstatus entries. we will ignore the rest.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1021
   // Advance the seek pointer to the start of the PT_NOTE data
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1022
   if (lseek(ph->core->core_fd, note_phdr->p_offset, SEEK_SET) == (off_t)-1) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1023
      print_debug("failed to lseek to PT_NOTE data\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1024
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1025
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1026
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1027
   // Now process the PT_NOTE structures.  Each one is preceded by
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1028
   // an Elf{32/64}_Nhdr structure describing its type and size.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1029
   if ( (buf = (char*) malloc(size)) == NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1030
      print_debug("can't allocate memory for reading core notes\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1031
      goto err;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1032
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1033
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1034
   // read notes into buffer
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1035
   if (read(ph->core->core_fd, buf, size) != size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1036
      print_debug("failed to read notes, core file must have been truncated\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1037
      goto err;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1038
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1039
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1040
   p = buf;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1041
   while (p < buf + size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1042
      ELF_NHDR* notep = (ELF_NHDR*) p;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1043
      char* descdata  = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1044
      print_debug("Note header with n_type = %d and n_descsz = %u\n",
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1045
                                   notep->n_type, notep->n_descsz);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1046
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1047
      if (notep->n_type == NT_PRSTATUS) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1048
        if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1049
          return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1050
        }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1051
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1052
      p = descdata + ROUNDUP(notep->n_descsz, 4);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1053
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1054
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1055
   free(buf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1056
   return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1057
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1058
err:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1059
   if (buf) free(buf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1060
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1061
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1062
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1063
// read all segments from core file
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1064
static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1065
   int i = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1066
   ELF_PHDR* phbuf = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1067
   ELF_PHDR* core_php = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1068
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1069
   if ((phbuf =  read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1070
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1071
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1072
   /*
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1073
    * Now iterate through the program headers in the core file.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1074
    * We're interested in two types of Phdrs: PT_NOTE (which
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1075
    * contains a set of saved /proc structures), and PT_LOAD (which
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1076
    * represents a memory mapping from the process's address space).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1077
    *
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1078
    * Difference b/w Solaris PT_NOTE and Linux/BSD PT_NOTE:
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1079
    *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1080
    *     In Solaris there are two PT_NOTE segments the first PT_NOTE (if present)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1081
    *     contains /proc structs in the pre-2.6 unstructured /proc format. the last
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1082
    *     PT_NOTE has data in new /proc format.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1083
    *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1084
    *     In Solaris, there is only one pstatus (process status). pstatus contains
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1085
    *     integer register set among other stuff. For each LWP, we have one lwpstatus
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1086
    *     entry that has integer regset for that LWP.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1087
    *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1088
    *     Linux threads are actually 'clone'd processes. To support core analysis
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1089
    *     of "multithreaded" process, Linux creates more than one pstatus (called
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1090
    *     "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1091
    *     "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1092
    *     function "elf_core_dump".
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1093
    */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1094
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1095
    for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1096
      switch (core_php->p_type) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1097
         case PT_NOTE:
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1098
            if (core_handle_note(ph, core_php) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1099
              goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1100
            }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1101
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1102
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1103
         case PT_LOAD: {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1104
            if (core_php->p_filesz != 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1105
               if (add_map_info(ph, ph->core->core_fd, core_php->p_offset,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1106
                  core_php->p_vaddr, core_php->p_filesz) == NULL) goto err;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1107
            }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1108
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1109
         }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1110
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1111
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1112
      core_php++;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1113
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1114
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1115
   free(phbuf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1116
   return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1117
err:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1118
   free(phbuf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1119
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1120
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1121
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1122
// read segments of a shared object
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1123
static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1124
  int i = 0;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1125
  ELF_PHDR* phbuf;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1126
  ELF_PHDR* lib_php = NULL;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1127
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1128
  int page_size=sysconf(_SC_PAGE_SIZE);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1129
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1130
  if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1131
    return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1132
  }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1133
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1134
  // we want to process only PT_LOAD segments that are not writable.
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1135
  // i.e., text segments. The read/write/exec (data) segments would
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1136
  // have been already added from core file segments.
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1137
  for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1138
    if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1139
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1140
      uintptr_t target_vaddr = lib_php->p_vaddr + lib_base;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1141
      map_info *existing_map = core_lookup(ph, target_vaddr);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1142
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1143
      if (existing_map == NULL){
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1144
        if (add_map_info(ph, lib_fd, lib_php->p_offset,
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1145
                          target_vaddr, lib_php->p_filesz) == NULL) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1146
          goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1147
        }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1148
      } else {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1149
        if ((existing_map->memsz != page_size) &&
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1150
            (existing_map->fd != lib_fd) &&
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1151
            (existing_map->memsz != lib_php->p_filesz)){
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1152
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1153
          print_debug("address conflict @ 0x%lx (size = %ld, flags = %d\n)",
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1154
                        target_vaddr, lib_php->p_filesz, lib_php->p_flags);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1155
          goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1156
        }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1157
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1158
        /* replace PT_LOAD segment with library segment */
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1159
        print_debug("overwrote with new address mapping (memsz %ld -> %ld)\n",
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1160
                     existing_map->memsz, lib_php->p_filesz);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1161
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1162
        existing_map->fd = lib_fd;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1163
        existing_map->offset = lib_php->p_offset;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1164
        existing_map->memsz = lib_php->p_filesz;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1165
      }
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1166
    }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1167
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1168
    lib_php++;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1169
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1170
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1171
  free(phbuf);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1172
  return true;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1173
err:
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1174
  free(phbuf);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1175
  return false;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1176
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1177
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1178
// process segments from interpreter (ld.so or ld-linux.so or ld-elf.so)
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1179
static bool read_interp_segments(struct ps_prochandle* ph) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1180
   ELF_EHDR interp_ehdr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1181
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1182
   if (read_elf_header(ph->core->interp_fd, &interp_ehdr) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1183
       print_debug("interpreter is not a valid ELF file\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1184
       return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1185
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1186
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1187
   if (read_lib_segments(ph, ph->core->interp_fd, &interp_ehdr, ph->core->ld_base_addr) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1188
       print_debug("can't read segments of interpreter\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1189
       return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1190
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1191
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1192
   return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1193
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1194
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1195
// process segments of a a.out
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1196
static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1197
   int i = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1198
   ELF_PHDR* phbuf = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1199
   ELF_PHDR* exec_php = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1200
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1201
   if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1202
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1203
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1204
   for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1205
      switch (exec_php->p_type) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1206
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1207
         // add mappings for PT_LOAD segments
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1208
         case PT_LOAD: {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1209
            // add only non-writable segments of non-zero filesz
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1210
            if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1211
               if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1212
            }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1213
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1214
         }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1215
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1216
         // read the interpreter and it's segments
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1217
         case PT_INTERP: {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1218
            char interp_name[BUF_SIZE];
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1219
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1220
            pread(ph->core->exec_fd, interp_name, MIN(exec_php->p_filesz, BUF_SIZE), exec_php->p_offset);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1221
            print_debug("ELF interpreter %s\n", interp_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1222
            // read interpreter segments as well
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1223
            if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1224
               print_debug("can't open runtime loader\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1225
               goto err;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1226
            }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1227
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1228
         }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1229
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1230
         // from PT_DYNAMIC we want to read address of first link_map addr
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1231
         case PT_DYNAMIC: {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1232
            ph->core->dynamic_addr = exec_php->p_vaddr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1233
            print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1234
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1235
         }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1236
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1237
      } // switch
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1238
      exec_php++;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1239
   } // for
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1240
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1241
   free(phbuf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1242
   return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1243
err:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1244
   free(phbuf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1245
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1246
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1247
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1248
#define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug,  r_map)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1249
#define LD_BASE_OFFSET        offsetof(struct r_debug,  r_ldbase)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1250
#define LINK_MAP_ADDR_OFFSET  offsetof(struct link_map, l_addr)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1251
#define LINK_MAP_NAME_OFFSET  offsetof(struct link_map, l_name)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1252
#define LINK_MAP_NEXT_OFFSET  offsetof(struct link_map, l_next)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1253
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1254
// read shared library info from runtime linker's data structures.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1255
// This work is done by librtlb_db in Solaris
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1256
static bool read_shared_lib_info(struct ps_prochandle* ph) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1257
  uintptr_t addr = ph->core->dynamic_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1258
  uintptr_t debug_base;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1259
  uintptr_t first_link_map_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1260
  uintptr_t ld_base_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1261
  uintptr_t link_map_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1262
  uintptr_t lib_base_diff;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1263
  uintptr_t lib_base;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1264
  uintptr_t lib_name_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1265
  char lib_name[BUF_SIZE];
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1266
  ELF_DYN dyn;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1267
  ELF_EHDR elf_ehdr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1268
  int lib_fd;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1269
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1270
  // _DYNAMIC has information of the form
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1271
  //         [tag] [data] [tag] [data] .....
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1272
  // Both tag and data are pointer sized.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1273
  // We look for dynamic info with DT_DEBUG. This has shared object info.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1274
  // refer to struct r_debug in link.h
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1275
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1276
  dyn.d_tag = DT_NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1277
  while (dyn.d_tag != DT_DEBUG) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1278
    if (ps_pread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1279
      print_debug("can't read debug info from _DYNAMIC\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1280
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1281
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1282
    addr += sizeof(ELF_DYN);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1283
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1284
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1285
  // we have got Dyn entry with DT_DEBUG
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1286
  debug_base = dyn.d_un.d_ptr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1287
  // at debug_base we have struct r_debug. This has first link map in r_map field
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1288
  if (ps_pread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET,
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1289
                 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1290
    print_debug("can't read first link map address\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1291
    return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1292
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1293
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1294
  // read ld_base address from struct r_debug
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1295
#if 0  // There is no r_ldbase member on BSD
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1296
  if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr,
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1297
                  sizeof(uintptr_t)) != PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1298
    print_debug("can't read ld base address\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1299
    return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1300
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1301
  ph->core->ld_base_addr = ld_base_addr;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1302
#else
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1303
  ph->core->ld_base_addr = 0;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1304
#endif
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1305
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1306
  print_debug("interpreter base address is 0x%lx\n", ld_base_addr);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1307
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1308
  // now read segments from interp (i.e ld.so or ld-linux.so or ld-elf.so)
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1309
  if (read_interp_segments(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1310
    return false;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1311
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1312
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1313
  // after adding interpreter (ld.so) mappings sort again
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1314
  if (sort_map_array(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1315
    return false;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1316
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1317
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1318
  print_debug("first link map is at 0x%lx\n", first_link_map_addr);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1319
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1320
  link_map_addr = first_link_map_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1321
  while (link_map_addr != 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1322
    // read library base address of the .so. Note that even though <sys/link.h> calls
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1323
    // link_map->l_addr as "base address",  this is * not * really base virtual
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1324
    // address of the shared object. This is actually the difference b/w the virtual
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1325
    // address mentioned in shared object and the actual virtual base where runtime
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1326
    // linker loaded it. We use "base diff" in read_lib_segments call below.
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1327
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1328
    if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_ADDR_OFFSET,
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1329
                 &lib_base_diff, sizeof(uintptr_t)) != PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1330
      print_debug("can't read shared object base address diff\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1331
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1332
    }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1333
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1334
    // read address of the name
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1335
    if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NAME_OFFSET,
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1336
                  &lib_name_addr, sizeof(uintptr_t)) != PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1337
      print_debug("can't read address of shared object name\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1338
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1339
    }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1340
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1341
    // read name of the shared object
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1342
    if (read_string(ph, (uintptr_t) lib_name_addr, lib_name, sizeof(lib_name)) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1343
      print_debug("can't read shared object name\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1344
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1345
    }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1346
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1347
    if (lib_name[0] != '\0') {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1348
      // ignore empty lib names
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1349
      lib_fd = pathmap_open(lib_name);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1350
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1351
      if (lib_fd < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1352
        print_debug("can't open shared object %s\n", lib_name);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1353
        // continue with other libraries...
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1354
      } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1355
        if (read_elf_header(lib_fd, &elf_ehdr)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1356
          lib_base = lib_base_diff + find_base_address(lib_fd, &elf_ehdr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1357
          print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1358
                       lib_name, lib_base, lib_base_diff);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1359
          // while adding library mappings we need to use "base difference".
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1360
          if (! read_lib_segments(ph, lib_fd, &elf_ehdr, lib_base_diff)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1361
            print_debug("can't read shared object's segments\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1362
            close(lib_fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1363
            return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1364
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1365
          add_lib_info_fd(ph, lib_name, lib_fd, lib_base);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1366
          // Map info is added for the library (lib_name) so
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1367
          // we need to re-sort it before calling the p_pdread.
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1368
          if (sort_map_array(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1369
            return false;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1370
          }
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1371
        } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1372
          print_debug("can't read ELF header for shared object %s\n", lib_name);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1373
          close(lib_fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1374
          // continue with other libraries...
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1375
        }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1376
      }
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1377
    }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1378
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1379
    // read next link_map address
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1380
    if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET,
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1381
                  &link_map_addr, sizeof(uintptr_t)) != PS_OK) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1382
      print_debug("can't read next link in link_map\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1383
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1384
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1385
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1386
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1387
  return true;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1388
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1389
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1390
// the one and only one exposed stuff from this file
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1391
struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1392
  ELF_EHDR core_ehdr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1393
  ELF_EHDR exec_ehdr;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1394
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1395
  struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1396
  if (ph == NULL) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1397
    print_debug("can't allocate ps_prochandle\n");
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1398
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1399
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1400
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1401
  if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1402
    free(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1403
    print_debug("can't allocate ps_prochandle\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1404
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1405
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1406
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1407
  // initialize ph
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1408
  ph->ops = &core_ops;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1409
  ph->core->core_fd   = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1410
  ph->core->exec_fd   = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1411
  ph->core->interp_fd = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1412
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1413
  print_debug("exec: %s   core: %s", exec_file, core_file);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1414
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1415
  // open the core file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1416
  if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1417
    print_debug("can't open core file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1418
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1419
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1420
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1421
  // read core file ELF header
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1422
  if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1423
    print_debug("core file is not a valid ELF ET_CORE file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1424
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1425
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1426
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1427
  if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1428
    print_debug("can't open executable file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1429
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1430
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1431
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1432
  if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1433
    print_debug("executable file is not a valid ELF ET_EXEC file\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1434
    goto err;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1435
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1436
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1437
  // process core file segments
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1438
  if (read_core_segments(ph, &core_ehdr) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1439
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1440
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1441
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1442
  // process exec file segments
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1443
  if (read_exec_segments(ph, &exec_ehdr) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1444
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1445
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1446
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1447
  // exec file is also treated like a shared object for symbol search
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1448
  if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd,
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1449
                      (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1450
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1451
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1452
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1453
  // allocate and sort maps into map_array, we need to do this
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1454
  // here because read_shared_lib_info needs to read from debuggee
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1455
  // address space
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1456
  if (sort_map_array(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1457
    goto err;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1458
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1459
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1460
  if (read_shared_lib_info(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1461
    goto err;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1462
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1463
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1464
  // sort again because we have added more mappings from shared objects
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1465
  if (sort_map_array(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1466
    goto err;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1467
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1468
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1469
  if (init_classsharing_workaround(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1470
    goto err;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1471
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1472
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1473
  print_debug("Leave Pgrab_core\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1474
  return ph;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1475
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1476
err:
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1477
  Prelease(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1478
  return NULL;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1479
}
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1480
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1481
#endif // __APPLE__