hotspot/agent/src/os/linux/libproc.h
author never
Mon, 04 May 2009 22:06:47 -0700
changeset 2744 57f0579fbe09
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
6837224: libsaproc.so on linux needs version of 6799141 Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#ifndef _LIBPROC_H_
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#define _LIBPROC_H_
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#include <unistd.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
#include <stdint.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
#include "proc_service.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
#if defined(sparc) || defined(sparcv9)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  If _LP64 is defined ptrace.h should be taken from /usr/include/asm-sparc64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  otherwise it should be from /usr/include/asm-sparc
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  These two files define pt_regs structure differently
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
*/
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
#ifdef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
#include "asm-sparc64/ptrace.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
#include "asm-sparc/ptrace.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
#endif //sparc or sparcv9
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
/************************************************************************************
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
0. This is very minimal subset of Solaris libproc just enough for current application.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
Please note that the bulk of the functionality is from proc_service interface. This
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
adds Pgrab__ and some missing stuff. We hide the difference b/w live process and core
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
file by this interface.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
1. pthread_id unique in both NPTL & LinuxThreads. We store this in
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
OSThread::_pthread_id in JVM code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
2. All threads see the same pid when they call getpid() under NPTL.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
Threads receive different pid under LinuxThreads. We used to save the result of
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
::getpid() call in OSThread::_thread_id. This way uniqueness of OSThread::_thread_id
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
was lost under NPTL. Now, we store the result of ::gettid() call in
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
OSThread::_thread_id. Because gettid returns actual pid of thread (lwp id), this is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
unique again. We therefore use OSThread::_thread_id as unique identifier.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
3. There is a unique LWP id under both thread libraries. libthread_db  maps pthread_id
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
to its underlying lwp_id under both the thread libraries. thread_info.lwp_id stores
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
lwp_id of the thread. The lwp id is nothing but the actual pid of clone'd processes. But
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
unfortunately libthread_db does not work very well for core dumps. So, we get pthread_id
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
only for processes. For core dumps, we don't use libthread_db at all (like gdb).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
4. ptrace operates on this LWP id under both the thread libraries. When we say 'pid' for
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
ptrace call, we refer to lwp_id of the thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
5. for core file, we parse ELF files and read data from them. For processes we  use
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
combination of ptrace and /proc calls.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
*************************************************************************************/
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
#ifdef ia64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
struct user_regs_struct {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
/* copied from user.h which doesn't define this in a struct */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
#define IA64_REG_COUNT (EF_SIZE/8+32)   /* integer and fp regs */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
unsigned long   regs[IA64_REG_COUNT];     /* integer and fp regs */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
#if defined(sparc)  || defined(sparcv9)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
#define user_regs_struct  pt_regs
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
// This C bool type must be int for compatibility with Linux calls and
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
// it would be a mistake to equivalence it to C++ bool on many platforms
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
typedef int bool;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
#define true  1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
#define false 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
struct ps_prochandle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
// attach to a process
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
struct ps_prochandle* Pgrab(pid_t pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
// attach to a core dump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
struct ps_prochandle* Pgrab_core(const char* execfile, const char* corefile);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
// release a process or core
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
void Prelease(struct ps_prochandle* ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
// functions not directly available in Solaris libproc
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
// initialize libproc (call this only once per app)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
// pass true to make library verbose
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
bool init_libproc(bool verbose);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
// get number of threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
int get_num_threads(struct ps_prochandle* ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
// get lwp_id of n'th thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
// get regs for a given lwp
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lid, struct user_regs_struct* regs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
// get number of shared objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
int get_num_libs(struct ps_prochandle* ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
// get name of n'th lib
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
const char* get_lib_name(struct ps_prochandle* ph, int index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
// get base of lib
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
uintptr_t get_lib_base(struct ps_prochandle* ph, int index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
// returns true if given library is found in lib list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
bool find_lib(struct ps_prochandle* ph, const char *lib_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
// symbol lookup
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
uintptr_t lookup_symbol(struct ps_prochandle* ph,  const char* object_name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
                       const char* sym_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
// address->nearest symbol lookup. return NULL for no symbol
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
#endif //__LIBPROC_H_