src/jdk.hotspot.agent/linux/native/libsaproc/ps_proc.c
branchihse-remove-mapfiles-branch
changeset 56873 af61810ccd5b
parent 56721 01b558efd286
parent 51203 220c9188db4f
equal deleted inserted replaced
56765:d02da43d8b98 56873:af61810ccd5b
    26 #include <stdlib.h>
    26 #include <stdlib.h>
    27 #include <string.h>
    27 #include <string.h>
    28 #include <signal.h>
    28 #include <signal.h>
    29 #include <errno.h>
    29 #include <errno.h>
    30 #include <elf.h>
    30 #include <elf.h>
       
    31 #include <dirent.h>
    31 #include <sys/types.h>
    32 #include <sys/types.h>
    32 #include <sys/wait.h>
    33 #include <sys/wait.h>
    33 #include <sys/ptrace.h>
    34 #include <sys/ptrace.h>
    34 #include <sys/uio.h>
    35 #include <sys/uio.h>
    35 #include "jni.h"
    36 #include "jni.h"
   373   .get_lwp_regs= process_get_lwp_regs
   374   .get_lwp_regs= process_get_lwp_regs
   374 };
   375 };
   375 
   376 
   376 // attach to the process. One and only one exposed stuff
   377 // attach to the process. One and only one exposed stuff
   377 JNIEXPORT struct ps_prochandle* JNICALL
   378 JNIEXPORT struct ps_prochandle* JNICALL
   378 Pgrab(pid_t pid, char* err_buf, size_t err_buf_len) {
   379 Pgrab(pid_t pid, char* err_buf, size_t err_buf_len, bool is_in_container) {
   379   struct ps_prochandle* ph = NULL;
   380   struct ps_prochandle* ph = NULL;
   380   thread_info* thr = NULL;
   381   thread_info* thr = NULL;
   381 
   382 
   382   if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
   383   if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
   383      snprintf(err_buf, err_buf_len, "can't allocate memory for ps_prochandle");
   384      snprintf(err_buf, err_buf_len, "can't allocate memory for ps_prochandle");
   400   // as the symbols in the pthread library will be used to figure out
   401   // as the symbols in the pthread library will be used to figure out
   401   // the list of threads within the same process.
   402   // the list of threads within the same process.
   402   read_lib_info(ph);
   403   read_lib_info(ph);
   403 
   404 
   404   // read thread info
   405   // read thread info
   405   read_thread_info(ph, add_new_thread);
   406   if (is_in_container) {
       
   407     /*
       
   408      * If the process is running in the container, SA scans all tasks in
       
   409      * /proc/<PID>/task to read all threads info.
       
   410      */
       
   411     char taskpath[PATH_MAX];
       
   412     DIR *dirp;
       
   413     struct dirent *entry;
       
   414 
       
   415     snprintf(taskpath, PATH_MAX, "/proc/%d/task", ph->pid);
       
   416     dirp = opendir(taskpath);
       
   417     int lwp_id;
       
   418     while ((entry = readdir(dirp)) != NULL) {
       
   419       if (*entry->d_name == '.') {
       
   420         continue;
       
   421       }
       
   422       lwp_id = atoi(entry->d_name);
       
   423       if (lwp_id == ph->pid) {
       
   424         continue;
       
   425       }
       
   426       add_new_thread(ph, -1, lwp_id);
       
   427     }
       
   428     closedir(dirp);
       
   429   } else {
       
   430     read_thread_info(ph, add_new_thread);
       
   431   }
   406 
   432 
   407   // attach to the threads
   433   // attach to the threads
   408   thr = ph->threads;
   434   thr = ph->threads;
   409   while (thr) {
   435   while (thr) {
   410      // don't attach to the main thread again
   436      // don't attach to the main thread again