@@ -878,7 +878,9 @@ bool os::address_is_in_vm(address addr) {
878878}
879879
880880
881+ #if defined(__APPLE__)
881882#define MACH_MAXSYMLEN 256
883+ #endif
882884
883885bool os::dll_address_to_function_name (address addr, char *buf,
884886 int buflen, int *offset,
@@ -887,9 +889,18 @@ bool os::dll_address_to_function_name(address addr, char *buf,
887889 assert (buf != NULL , " sanity check" );
888890
889891 Dl_info dlinfo;
890- char localbuf[MACH_MAXSYMLEN];
891892
892893 if (dladdr ((void *)addr, &dlinfo) != 0 ) {
894+ #if defined(__APPLE__)
895+ if (addr == (address)(intptr_t )-1 ) {
896+ // dladdr() in macOS12/Monterey returns success for -1, but that addr
897+ // value should not be allowed to work to avoid confusion.
898+ buf[0 ] = ' \0 ' ;
899+ if (offset) *offset = -1 ;
900+ return false ;
901+ }
902+ #endif
903+
893904 // see if we have a matching symbol
894905 if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL ) {
895906 if (!(demangle && Decoder::demangle (dlinfo.dli_sname , buf, buflen))) {
@@ -898,14 +909,25 @@ bool os::dll_address_to_function_name(address addr, char *buf,
898909 if (offset != NULL ) *offset = addr - (address)dlinfo.dli_saddr ;
899910 return true ;
900911 }
912+
913+ #if !defined(__APPLE__)
914+ // The 6-parameter Decoder::decode() function is not implemented on macOS.
915+ // The Mach-O binary format does not contain a "list of files" with address
916+ // ranges like ELF. That makes sense since Mach-O can contain binaries for
917+ // than one instruction set so there can be more than one address range for
918+ // each "file".
919+
901920 // no matching symbol so try for just file info
902921 if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL ) {
903922 if (Decoder::decode ((address)(addr - (address)dlinfo.dli_fbase ),
904923 buf, buflen, offset, dlinfo.dli_fname , demangle)) {
905924 return true ;
906925 }
907926 }
927+ #endif
908928
929+ #if defined(__APPLE__)
930+ char localbuf[MACH_MAXSYMLEN];
909931 // Handle non-dynamic manually:
910932 if (dlinfo.dli_fbase != NULL &&
911933 Decoder::decode (addr, localbuf, MACH_MAXSYMLEN, offset,
@@ -916,6 +938,8 @@ bool os::dll_address_to_function_name(address addr, char *buf,
916938 return true ;
917939 }
918940 }
941+ #endif
942+
919943 buf[0 ] = ' \0 ' ;
920944 if (offset != NULL ) *offset = -1 ;
921945 return false ;
@@ -930,6 +954,16 @@ bool os::dll_address_to_library_name(address addr, char* buf,
930954 Dl_info dlinfo;
931955
932956 if (dladdr ((void *)addr, &dlinfo) != 0 ) {
957+ #if defined(__APPLE__)
958+ if (addr == (address)(intptr_t )-1 ) {
959+ // dladdr() in macOS12/Monterey returns success for -1, but that addr
960+ // value should not be allowed to work to avoid confusion.
961+ buf[0 ] = ' \0 ' ;
962+ if (offset) *offset = -1 ;
963+ return false ;
964+ }
965+ #endif
966+
933967 if (dlinfo.dli_fname != NULL ) {
934968 jio_snprintf (buf, buflen, " %s" , dlinfo.dli_fname );
935969 }
0 commit comments