hotspot/src/jdk.hotspot.agent/macosx/native/libsaproc/libproc.h
author sspitsyn
Wed, 03 May 2017 02:32:02 +0000
changeset 46426 02a1fc064144
parent 35217 ce4b5303a813
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     1
/*
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
     2
 * Copyright (c) 2003, 2013, 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
#ifndef _LIBPROC_H_
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    26
#define _LIBPROC_H_
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    27
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    28
#include <unistd.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    29
#include <stdint.h>
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    30
#include <stdarg.h>
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    31
#include <stdio.h>
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    32
#include <stdlib.h>
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    33
#include <string.h>
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    34
#include <fcntl.h>
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    35
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    36
#ifdef __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    37
typedef enum ps_err_e {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    38
  PS_OK, PS_ERR, PS_BADPID, PS_BADLID,
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    39
  PS_BADADDR, PS_NOSYM, PS_NOFREGS
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    40
} ps_err_e;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    41
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    42
#ifndef psaddr_t
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    43
#define psaddr_t uintptr_t
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    44
#endif
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    45
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    46
#ifndef bool
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    47
typedef int bool;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    48
#define true  1
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    49
#define false 0
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    50
#endif  // bool
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    51
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    52
#ifndef lwpid_t
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    53
#define lwpid_t uintptr_t
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    54
#endif
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    55
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    56
#include <mach/thread_status.h>
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    57
#else   // __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    58
#include <elf.h>
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    59
#include <link.h>
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    60
#include <machine/reg.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    61
#include <proc_service.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    62
#if defined(sparc) || defined(sparcv9)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    63
/*
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    64
  If _LP64 is defined ptrace.h should be taken from /usr/include/asm-sparc64
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    65
  otherwise it should be from /usr/include/asm-sparc
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    66
  These two files define pt_regs structure differently
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    67
*/
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    68
#ifdef _LP64
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    69
#include "asm-sparc64/ptrace.h"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    70
#else
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    71
#include "asm-sparc/ptrace.h"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    72
#endif
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    73
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    74
#endif //sparc or sparcv9
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    75
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    76
// This C bool type must be int for compatibility with BSD calls and
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    77
// it would be a mistake to equivalence it to C++ bool on many platforms
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    78
typedef int bool;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    79
#define true  1
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    80
#define false 0
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    81
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    82
#endif // __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
    83
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    84
/************************************************************************************
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    85
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    86
0. This is very minimal subset of Solaris libproc just enough for current application.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    87
Please note that the bulk of the functionality is from proc_service interface. This
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    88
adds Pgrab__ and some missing stuff. We hide the difference b/w live process and core
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    89
file by this interface.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    90
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    91
1. pthread_id is unique. We store this in OSThread::_pthread_id in JVM code.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    92
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    93
2. All threads see the same pid when they call getpid().
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    94
We used to save the result of ::getpid() call in OSThread::_thread_id.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    95
Because gettid returns actual pid of thread (lwp id), this is
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    96
unique again. We therefore use OSThread::_thread_id as unique identifier.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    97
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    98
3. There is a unique LWP id under both thread libraries. libthread_db  maps pthread_id
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    99
to its underlying lwp_id under both the thread libraries. thread_info.lwp_id stores
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   100
lwp_id of the thread. The lwp id is nothing but the actual pid of clone'd processes. But
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   101
unfortunately libthread_db does not work very well for core dumps. So, we get pthread_id
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   102
only for processes. For core dumps, we don't use libthread_db at all (like gdb).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   103
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   104
4. ptrace operates on this LWP id under both the thread libraries. When we say 'pid' for
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   105
ptrace call, we refer to lwp_id of the thread.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   106
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   107
5. for core file, we parse ELF files and read data from them. For processes we  use
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   108
combination of ptrace and /proc calls.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   109
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   110
*************************************************************************************/
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   111
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 10565
diff changeset
   112
struct reg;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   113
struct ps_prochandle;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   114
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   115
// attach to a process
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   116
struct ps_prochandle* Pgrab(pid_t pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   117
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   118
// attach to a core dump
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   119
struct ps_prochandle* Pgrab_core(const char* execfile, const char* corefile);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   120
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   121
// release a process or core
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   122
void Prelease(struct ps_prochandle* ph);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   123
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   124
// functions not directly available in Solaris libproc
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   125
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   126
// initialize libproc (call this only once per app)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   127
// pass true to make library verbose
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   128
bool init_libproc(bool verbose);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   129
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   130
// get number of threads
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   131
int get_num_threads(struct ps_prochandle* ph);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   132
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   133
// get lwp_id of n'th thread
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   134
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   135
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   136
// get regs for a given lwp
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   137
bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lid, struct reg* regs);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   138
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   139
// get number of shared objects
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   140
int get_num_libs(struct ps_prochandle* ph);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   141
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   142
// get name of n'th lib
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   143
const char* get_lib_name(struct ps_prochandle* ph, int index);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   144
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   145
// get base of lib
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   146
uintptr_t get_lib_base(struct ps_prochandle* ph, int index);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   147
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   148
// returns true if given library is found in lib list
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   149
bool find_lib(struct ps_prochandle* ph, const char *lib_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   150
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   151
// symbol lookup
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   152
uintptr_t lookup_symbol(struct ps_prochandle* ph,  const char* object_name,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   153
                       const char* sym_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   154
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   155
// address->nearest symbol lookup. return NULL for no symbol
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   156
const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   157
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   158
#endif //__LIBPROC_H_