3636#include "BSG_KSObjC.h"
3737#include "BSG_KSSignalInfo.h"
3838#include "BSG_KSString.h"
39+ #include "BSG_KSMachHeaders.h"
3940
4041//#define BSG_kSLogger_LocalLevel TRACE
4142#include "BSG_KSLogger.h"
@@ -1084,70 +1085,23 @@ int bsg_kscrw_i_threadIndex(const thread_t thread) {
10841085 *
10851086 * @param key The object key, if needed.
10861087 *
1087- * @param index Which image to write about .
1088+ * @param index Cached info about the binary image .
10881089 */
10891090void bsg_kscrw_i_writeBinaryImage (const BSG_KSCrashReportWriter * const writer ,
1090- const char * const key , const uint32_t index ) {
1091- const struct mach_header * header = _dyld_get_image_header (index );
1092- if (header == NULL ) {
1093- return ;
1094- }
1095-
1096- uintptr_t cmdPtr = bsg_ksdlfirstCmdAfterHeader (header );
1097- if (cmdPtr == 0 ) {
1098- return ;
1099- }
1100-
1101- // Look for the TEXT segment to get the image size.
1102- // Also look for a UUID command.
1103- uint64_t imageSize = 0 ;
1104- uint64_t imageVmAddr = 0 ;
1105- uint8_t * uuid = NULL ;
1106-
1107- for (uint32_t iCmd = 0 ; iCmd < header -> ncmds ; iCmd ++ ) {
1108- struct load_command * loadCmd = (struct load_command * )cmdPtr ;
1109- switch (loadCmd -> cmd ) {
1110- case LC_SEGMENT : {
1111- struct segment_command * segCmd = (struct segment_command * )cmdPtr ;
1112- if (strcmp (segCmd -> segname , SEG_TEXT ) == 0 ) {
1113- imageSize = segCmd -> vmsize ;
1114- imageVmAddr = segCmd -> vmaddr ;
1115- }
1116- break ;
1117- }
1118- case LC_SEGMENT_64 : {
1119- struct segment_command_64 * segCmd =
1120- (struct segment_command_64 * )cmdPtr ;
1121- if (strcmp (segCmd -> segname , SEG_TEXT ) == 0 ) {
1122- imageSize = segCmd -> vmsize ;
1123- imageVmAddr = segCmd -> vmaddr ;
1124- }
1125- break ;
1126- }
1127- case LC_UUID : {
1128- struct uuid_command * uuidCmd = (struct uuid_command * )cmdPtr ;
1129- uuid = uuidCmd -> uuid ;
1130- break ;
1131- }
1132- }
1133- cmdPtr += loadCmd -> cmdsize ;
1134- }
1135-
1091+ const char * const key ,
1092+ const uint32_t index )
1093+ {
1094+ BSG_Mach_Binary_Image_Info info = * bsg_dyld_get_image_info (index );
1095+
11361096 writer -> beginObject (writer , key );
11371097 {
1138- writer -> addUIntegerElement (writer , BSG_KSCrashField_ImageAddress ,
1139- (uintptr_t )header );
1140- writer -> addUIntegerElement (writer , BSG_KSCrashField_ImageVmAddress ,
1141- imageVmAddr );
1142- writer -> addUIntegerElement (writer , BSG_KSCrashField_ImageSize ,
1143- imageSize );
1144- writer -> addStringElement (writer , BSG_KSCrashField_Name ,
1145- _dyld_get_image_name (index ));
1146- writer -> addUUIDElement (writer , BSG_KSCrashField_UUID , uuid );
1147- writer -> addIntegerElement (writer , BSG_KSCrashField_CPUType ,
1148- header -> cputype );
1149- writer -> addIntegerElement (writer , BSG_KSCrashField_CPUSubType ,
1150- header -> cpusubtype );
1098+ writer -> addUIntegerElement (writer , BSG_KSCrashField_ImageAddress , (uintptr_t )info .header );
1099+ writer -> addUIntegerElement (writer , BSG_KSCrashField_ImageVmAddress , info .imageVmAddr );
1100+ writer -> addUIntegerElement (writer , BSG_KSCrashField_ImageSize , info .imageSize );
1101+ writer -> addStringElement (writer , BSG_KSCrashField_Name , info .name );
1102+ writer -> addUUIDElement (writer , BSG_KSCrashField_UUID , info .uuid );
1103+ writer -> addIntegerElement (writer , BSG_KSCrashField_CPUType , info .header -> cputype );
1104+ writer -> addIntegerElement (writer , BSG_KSCrashField_CPUSubType , info .header -> cpusubtype );
11511105 }
11521106 writer -> endContainer (writer );
11531107}
@@ -1159,12 +1113,14 @@ void bsg_kscrw_i_writeBinaryImage(const BSG_KSCrashReportWriter *const writer,
11591113 * @param key The object key, if needed.
11601114 */
11611115void bsg_kscrw_i_writeBinaryImages (const BSG_KSCrashReportWriter * const writer ,
1162- const char * const key ) {
1163- const uint32_t imageCount = _dyld_image_count ();
1116+ const char * const key )
1117+ {
1118+ const uint32_t imageCount = bsg_dyld_image_count ();
11641119
11651120 writer -> beginArray (writer , key );
11661121 {
11671122 for (uint32_t iImg = 0 ; iImg < imageCount ; iImg ++ ) {
1123+ // Threads are suspended at this point; no need to synchronise/lock
11681124 bsg_kscrw_i_writeBinaryImage (writer , NULL , iImg );
11691125 }
11701126 }
@@ -1658,9 +1614,6 @@ void bsg_kscrashreport_writeStandardReport(
16581614
16591615 bsg_ksjsonendEncode (bsg_getJsonContext (writer ));
16601616
1661- if (!bsg_ksfuflushWriteBuffer (fd )) {
1662- BSG_KSLOG_ERROR ("Failed to flush write buffer" );
1663- }
16641617 close (fd );
16651618}
16661619
0 commit comments