src/jdk.hotspot.agent/linux/native/libsaproc/ps_proc.c
changeset 53379 e47074d2d8cc
parent 53004 b9d34a97a4be
equal deleted inserted replaced
53378:52abdb8a48b2 53379:e47074d2d8cc
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   337        if (*--p=='\n') *p='\0';
   337        if (*--p=='\n') *p='\0';
   338    }
   338    }
   339    return rslt;
   339    return rslt;
   340 }
   340 }
   341 
   341 
   342 // callback for read_thread_info
       
   343 static bool add_new_thread(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
       
   344   return add_thread_info(ph, pthread_id, lwp_id) != NULL;
       
   345 }
       
   346 
       
   347 static bool read_lib_info(struct ps_prochandle* ph) {
   342 static bool read_lib_info(struct ps_prochandle* ph) {
   348   char fname[32];
   343   char fname[32];
   349   char buf[PATH_MAX];
   344   char buf[PATH_MAX];
   350   FILE *fp = NULL;
   345   FILE *fp = NULL;
   351 
   346 
   441   .get_lwp_regs= process_get_lwp_regs
   436   .get_lwp_regs= process_get_lwp_regs
   442 };
   437 };
   443 
   438 
   444 // attach to the process. One and only one exposed stuff
   439 // attach to the process. One and only one exposed stuff
   445 JNIEXPORT struct ps_prochandle* JNICALL
   440 JNIEXPORT struct ps_prochandle* JNICALL
   446 Pgrab(pid_t pid, char* err_buf, size_t err_buf_len, bool is_in_container) {
   441 Pgrab(pid_t pid, char* err_buf, size_t err_buf_len) {
   447   struct ps_prochandle* ph = NULL;
   442   struct ps_prochandle* ph = NULL;
   448   thread_info* thr = NULL;
   443   thread_info* thr = NULL;
   449   attach_state_t attach_status = ATTACH_SUCCESS;
   444   attach_state_t attach_status = ATTACH_SUCCESS;
   450 
   445 
   451   if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
   446   if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
   462     return NULL;
   457     return NULL;
   463   }
   458   }
   464 
   459 
   465   // initialize ps_prochandle
   460   // initialize ps_prochandle
   466   ph->pid = pid;
   461   ph->pid = pid;
       
   462   add_thread_info(ph, ph->pid);
   467 
   463 
   468   // initialize vtable
   464   // initialize vtable
   469   ph->ops = &process_ops;
   465   ph->ops = &process_ops;
   470 
   466 
   471   // read library info and symbol tables, must do this before attaching threads,
   467   // read library info and symbol tables, must do this before attaching threads,
   472   // as the symbols in the pthread library will be used to figure out
   468   // as the symbols in the pthread library will be used to figure out
   473   // the list of threads within the same process.
   469   // the list of threads within the same process.
   474   read_lib_info(ph);
   470   read_lib_info(ph);
   475 
   471 
   476   // read thread info
   472   /*
   477   if (is_in_container) {
   473    * Read thread info.
   478     /*
   474    * SA scans all tasks in /proc/<PID>/task to read all threads info.
   479      * If the process is running in the container, SA scans all tasks in
   475    */
   480      * /proc/<PID>/task to read all threads info.
   476   char taskpath[PATH_MAX];
   481      */
   477   DIR *dirp;
   482     char taskpath[PATH_MAX];
   478   struct dirent *entry;
   483     DIR *dirp;
   479 
   484     struct dirent *entry;
   480   snprintf(taskpath, PATH_MAX, "/proc/%d/task", ph->pid);
   485 
   481   dirp = opendir(taskpath);
   486     snprintf(taskpath, PATH_MAX, "/proc/%d/task", ph->pid);
   482   int lwp_id;
   487     dirp = opendir(taskpath);
   483   while ((entry = readdir(dirp)) != NULL) {
   488     int lwp_id;
   484     if (*entry->d_name == '.') {
   489     while ((entry = readdir(dirp)) != NULL) {
   485       continue;
   490       if (*entry->d_name == '.') {
   486     }
   491         continue;
   487     lwp_id = atoi(entry->d_name);
   492       }
   488     if (lwp_id == ph->pid) {
   493       lwp_id = atoi(entry->d_name);
   489       continue;
   494       if (lwp_id == ph->pid) {
   490     }
   495         continue;
   491     if (!process_doesnt_exist(lwp_id)) {
   496       }
   492       add_thread_info(ph, lwp_id);
   497       add_new_thread(ph, -1, lwp_id);
   493     }
   498     }
   494   }
   499     closedir(dirp);
   495   closedir(dirp);
   500   } else {
       
   501     read_thread_info(ph, add_new_thread);
       
   502   }
       
   503 
   496 
   504   // attach to the threads
   497   // attach to the threads
   505   thr = ph->threads;
   498   thr = ph->threads;
   506 
   499 
   507   while (thr) {
   500   while (thr) {