src/jdk.hotspot.agent/linux/native/libsaproc/libproc_impl.h
author jgeorge
Thu, 13 Dec 2018 13:03:26 +0530
changeset 53004 b9d34a97a4be
parent 47216 71c04702a3d5
child 53379 e47074d2d8cc
permissions -rw-r--r--
8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching Summary: While doing a ptrace attach, do not attach to threads which are in the process of exiting or are zombies -- skip these threads. Reviewed-by: jcbeyler, ysuenaga
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
1
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
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
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_IMPL_H_
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#define _LIBPROC_IMPL_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 <limits.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
#include "libproc.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
#include "symtab.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// data structures in this file mimic those of Solaris 8.0 - libproc's Pcontrol.h
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
#define BUF_SIZE     (PATH_MAX + NAME_MAX + 1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// list of shared objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
typedef struct lib_info {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  char             name[BUF_SIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  uintptr_t        base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  struct symtab*   symtab;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  int              fd;        // file descriptor for lib
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  struct lib_info* next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
} lib_info;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
// list of threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
typedef struct thread_info {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
   lwpid_t                  lwp_id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
   pthread_t                pthread_id; // not used cores, always -1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
   struct user_regs_struct  regs;       // not for process, core uses for caching regset
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
   struct thread_info*      next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
} thread_info;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
// list of virtual memory maps
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
typedef struct map_info {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
   int              fd;       // file descriptor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
   off_t            offset;   // file offset of this mapping
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
   uintptr_t        vaddr;    // starting virtual address
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
   size_t           memsz;    // size of the mapping
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
   struct map_info* next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
} map_info;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
// vtable for ps_prochandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
typedef struct ps_prochandle_ops {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
   // "derived class" clean-up
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
   void (*release)(struct ps_prochandle* ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
   // read from debuggee
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
   bool (*p_pread)(struct ps_prochandle *ph,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
            uintptr_t addr, char *buf, size_t size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
   // write into debuggee
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
   bool (*p_pwrite)(struct ps_prochandle *ph,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
            uintptr_t addr, const char *buf , size_t size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
   // get integer regset of a thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
   bool (*get_lwp_regs)(struct ps_prochandle* ph, lwpid_t lwp_id, struct user_regs_struct* regs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
} ps_prochandle_ops;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
// the ps_prochandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
struct core_data {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
   int                core_fd;   // file descriptor of core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
   int                exec_fd;   // file descriptor of exec file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
   int                interp_fd; // file descriptor of interpreter (ld-linux.so.2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
   // part of the class sharing workaround
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
   int                classes_jsa_fd; // file descriptor of class share archive
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
   uintptr_t          dynamic_addr;  // address of dynamic section of a.out
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
   uintptr_t          ld_base_addr;  // base address of ld.so
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
   size_t             num_maps;  // number of maps.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
   map_info*          maps;      // maps in a linked list
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
   // part of the class sharing workaround
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
   map_info*          class_share_maps;// class share maps in a linked list
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
   map_info**         map_array; // sorted (by vaddr) array of map_info pointers
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
struct ps_prochandle {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
   ps_prochandle_ops* ops;       // vtable ptr
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
   pid_t              pid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
   int                num_libs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
   lib_info*          libs;      // head of lib list
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
   lib_info*          lib_tail;  // tail of lib list - to append at the end
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
   int                num_threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
   thread_info*       threads;   // head of thread list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
   struct core_data*  core;      // data only used for core dumps, NULL for process
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
int pathmap_open(const char* name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
void print_debug(const char* format,...);
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 5547
diff changeset
   108
void print_error(const char* format,...);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
bool is_debug();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
typedef bool (*thread_info_callback)(struct ps_prochandle* ph, pthread_t pid, lwpid_t lwpid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
// reads thread info using libthread_db and calls above callback for each thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 47216
diff changeset
   116
// deletes a thread from the thread list
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 47216
diff changeset
   117
void delete_thread_info(struct ps_prochandle* ph, thread_info* thr);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 47216
diff changeset
   118
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
// adds a new shared object to lib list, returns NULL on failure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
// adds a new shared object to lib list, supply open lib file descriptor as well
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
                          uintptr_t base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
// adds a new thread to threads list, returns NULL on failure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
// a test for ELF signature without using libelf
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
bool is_elf_file(int fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
#endif //_LIBPROC_IMPL_H_