@@ -2089,40 +2089,42 @@ void os::print_dll_info(outputStream *st) {
2089
2089
}
2090
2090
}
2091
2091
2092
- int os::get_loaded_modules_info (os::LoadedModulesCallbackFunc callback, void *param) {
2093
- FILE *procmapsFile = NULL ;
2094
-
2095
- // Open the procfs maps file for the current process
2096
- if ((procmapsFile = fopen (" /proc/self/maps" , " r" )) != NULL ) {
2097
- // Allocate PATH_MAX for file name plus a reasonable size for other fields.
2098
- char line[PATH_MAX + 100 ];
2099
-
2100
- // Read line by line from 'file'
2101
- while (fgets (line, sizeof (line), procmapsFile) != NULL ) {
2102
- u8 base, top, inode;
2103
- char name[sizeof (line)];
2104
-
2105
- // Parse fields from line, discard perms, offset and device
2106
- int matches = sscanf (line, UINT64_FORMAT_X " -" UINT64_FORMAT_X " %*s %*s %*s " INT64_FORMAT " %s" ,
2107
- &base, &top, &inode, name);
2108
- // the last entry 'name' is empty for some entries, so we might have 3 matches instead of 4 for some lines
2109
- if (matches < 3 ) continue ;
2110
- if (matches == 3 ) name[0 ] = ' \0 ' ;
2111
-
2112
- // Filter by inode 0 so that we only get file system mapped files.
2113
- if (inode != 0 ) {
2114
-
2115
- // Call callback with the fields of interest
2116
- if (callback (name, (address)base, (address)top, param)) {
2117
- // Oops abort, callback aborted
2118
- fclose (procmapsFile);
2119
- return 1 ;
2120
- }
2092
+ struct loaded_modules_info_param {
2093
+ os::LoadedModulesCallbackFunc callback;
2094
+ void *param;
2095
+ };
2096
+
2097
+ static int dl_iterate_callback (struct dl_phdr_info *info, size_t size, void *data) {
2098
+ if ((info->dlpi_name == NULL ) || (*info->dlpi_name == ' \0 ' )) {
2099
+ return 0 ;
2100
+ }
2101
+
2102
+ struct loaded_modules_info_param *callback_param = reinterpret_cast <struct loaded_modules_info_param *>(data);
2103
+ address base = NULL ;
2104
+ address top = NULL ;
2105
+ for (int idx = 0 ; idx < info->dlpi_phnum ; idx++) {
2106
+ const ElfW (Phdr) *phdr = info->dlpi_phdr + idx;
2107
+ if (phdr->p_type == PT_LOAD) {
2108
+ address raw_phdr_base = reinterpret_cast <address>(info->dlpi_addr + phdr->p_vaddr );
2109
+
2110
+ address phdr_base = align_down (raw_phdr_base, phdr->p_align );
2111
+ if ((base == NULL ) || (base > phdr_base)) {
2112
+ base = phdr_base;
2113
+ }
2114
+
2115
+ address phdr_top = align_up (raw_phdr_base + phdr->p_memsz , phdr->p_align );
2116
+ if ((top == NULL ) || (top < phdr_top)) {
2117
+ top = phdr_top;
2121
2118
}
2122
2119
}
2123
- fclose (procmapsFile);
2124
2120
}
2125
- return 0 ;
2121
+
2122
+ return callback_param->callback (info->dlpi_name , base, top, callback_param->param );
2123
+ }
2124
+
2125
+ int os::get_loaded_modules_info (os::LoadedModulesCallbackFunc callback, void *param) {
2126
+ struct loaded_modules_info_param callback_param = {callback, param};
2127
+ return dl_iterate_phdr (&dl_iterate_callback, &callback_param);
2126
2128
}
2127
2129
2128
2130
void os::print_os_info_brief (outputStream* st) {
0 commit comments