hotspot/agent/src/os/bsd/libproc.h
author minqi
Mon, 24 Sep 2012 12:44:00 -0700
changeset 13873 7b72e3873785
parent 10565 dc90c239f4ec
child 16351 032b310a3e2f
permissions -rw-r--r--
6879063: SA should use hsdis for disassembly Summary: We should in SA to use hsdis for it like the JVM does to replace the current java based disassembler. Reviewed-by: twisti, jrose, sla Contributed-by: yumin.qi@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     1
/*
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     2
 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
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>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    30
#include <machine/reg.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    31
#include <proc_service.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    32
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    33
#if defined(sparc) || defined(sparcv9)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    34
/*
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    35
  If _LP64 is defined ptrace.h should be taken from /usr/include/asm-sparc64
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    36
  otherwise it should be from /usr/include/asm-sparc
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    37
  These two files define pt_regs structure differently
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    38
*/
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    39
#ifdef _LP64
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    40
#include "asm-sparc64/ptrace.h"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    41
#else
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    42
#include "asm-sparc/ptrace.h"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    43
#endif
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    44
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    45
#endif //sparc or sparcv9
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    46
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    47
/************************************************************************************
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    48
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    49
0. This is very minimal subset of Solaris libproc just enough for current application.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    50
Please note that the bulk of the functionality is from proc_service interface. This
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    51
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
    52
file by this interface.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    53
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    54
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
    55
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    56
2. All threads see the same pid when they call getpid().
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    57
We used to save the result of ::getpid() call in OSThread::_thread_id.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    58
Because gettid returns actual pid of thread (lwp id), this is
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    59
unique again. We therefore use OSThread::_thread_id as unique identifier.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    60
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    61
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
    62
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
    63
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
    64
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
    65
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
    66
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    67
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
    68
ptrace call, we refer to lwp_id of the thread.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    69
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    70
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
    71
combination of ptrace and /proc calls.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    72
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    73
*************************************************************************************/
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    74
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    75
// This C bool type must be int for compatibility with BSD calls and
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    76
// it would be a mistake to equivalence it to C++ bool on many platforms
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    77
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    78
typedef int bool;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    79
#define true  1
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    80
#define false 0
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    81
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    82
struct ps_prochandle;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    83
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    84
// attach to a process
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    85
struct ps_prochandle* Pgrab(pid_t pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    86
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    87
// attach to a core dump
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    88
struct ps_prochandle* Pgrab_core(const char* execfile, const char* corefile);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    89
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    90
// release a process or core
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    91
void Prelease(struct ps_prochandle* ph);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    92
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    93
// functions not directly available in Solaris libproc
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    94
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    95
// initialize libproc (call this only once per app)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    96
// pass true to make library verbose
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    97
bool init_libproc(bool verbose);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    98
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    99
// get number of threads
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   100
int get_num_threads(struct ps_prochandle* ph);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   101
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   102
// get lwp_id of n'th thread
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   103
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   104
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   105
// get regs for a given lwp
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   106
bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lid, struct reg* regs);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   107
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   108
// get number of shared objects
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   109
int get_num_libs(struct ps_prochandle* ph);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   110
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   111
// get name of n'th lib
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   112
const char* get_lib_name(struct ps_prochandle* ph, int index);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   113
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   114
// get base of lib
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   115
uintptr_t get_lib_base(struct ps_prochandle* ph, int index);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   116
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   117
// returns true if given library is found in lib list
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   118
bool find_lib(struct ps_prochandle* ph, const char *lib_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   119
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   120
// symbol lookup
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   121
uintptr_t lookup_symbol(struct ps_prochandle* ph,  const char* object_name,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   122
                       const char* sym_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   123
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   124
// address->nearest symbol lookup. return NULL for no symbol
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   125
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
   126
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   127
#endif //__LIBPROC_H_