@@ -34,6 +34,23 @@ typedef struct PipeMemoryReader {
34
34
int from_child [2 ];
35
35
} PipeMemoryReader ;
36
36
37
+ typedef struct RemoteSection {
38
+ uintptr_t StartAddress ;
39
+ uintptr_t Size ;
40
+ uintptr_t EndAddress ;
41
+ } RemoteSection ;
42
+
43
+ typedef struct RemoteReflectionInfo {
44
+ RemoteSection fieldmd ;
45
+ RemoteSection assocty ;
46
+ RemoteSection builtin ;
47
+ RemoteSection capture ;
48
+ RemoteSection typeref ;
49
+ RemoteSection reflstr ;
50
+ uintptr_t StartAddress ;
51
+ size_t TotalSize ;
52
+ } RemoteReflectionInfo ;
53
+
37
54
static void errorAndExit (const char * message ) {
38
55
fprintf (stderr , "%s\n" , message );
39
56
abort ();
@@ -44,6 +61,77 @@ static void errnoAndExit(const char *message) {
44
61
abort ();
45
62
}
46
63
64
+ static swift_reflection_section_t
65
+ makeLocalSection (void * Buffer , RemoteSection Section ,
66
+ RemoteReflectionInfo Info ) {
67
+ if (Section .Size == 0 ) {
68
+ swift_reflection_section_t LS = {NULL , NULL };
69
+ return LS ;
70
+ }
71
+
72
+ uintptr_t Base
73
+ = (uintptr_t )Buffer + Section .StartAddress - Info .StartAddress ;
74
+ swift_reflection_section_t LS = {
75
+ (void * )Base ,
76
+ (void * )(Base + Section .Size )
77
+ };
78
+ return LS ;
79
+ }
80
+
81
+ static
82
+ uintptr_t getStartAddress (const RemoteSection Sections [], size_t Count ) {
83
+ uintptr_t Start = 0 ;
84
+ for (size_t i = 0 ; i < Count ; ++ i ) {
85
+ if (Sections [i ].StartAddress != 0 ) {
86
+ if (Start != 0 )
87
+ Start = MIN (Start , Sections [i ].StartAddress );
88
+ else
89
+ Start = Sections [i ].StartAddress ;
90
+ }
91
+ }
92
+ return Start ;
93
+ }
94
+
95
+ static
96
+ uintptr_t getEndAddress (const RemoteSection Sections [], size_t Count ) {
97
+ uintptr_t End = 0 ;
98
+ for (size_t i = 0 ; i < Count ; ++ i ) {
99
+ if (Sections [i ].StartAddress != 0 )
100
+ End = MAX (End , Sections [i ].EndAddress );
101
+ }
102
+ return End ;
103
+ }
104
+
105
+ static
106
+ RemoteReflectionInfo makeRemoteReflectionInfo (RemoteSection fieldmd ,
107
+ RemoteSection assocty ,
108
+ RemoteSection builtin ,
109
+ RemoteSection capture ,
110
+ RemoteSection typeref ,
111
+ RemoteSection reflstr ) {
112
+ RemoteReflectionInfo Info = {
113
+ fieldmd ,
114
+ assocty ,
115
+ builtin ,
116
+ capture ,
117
+ typeref ,
118
+ reflstr ,
119
+ 0 ,
120
+ 0
121
+ };
122
+
123
+ const RemoteSection Sections [6 ] = {
124
+ fieldmd , assocty , builtin , capture , typeref , reflstr
125
+ };
126
+
127
+ Info .StartAddress = getStartAddress (Sections , 6 );
128
+
129
+ uintptr_t EndAddress = getEndAddress (Sections , 6 );
130
+ Info .TotalSize = EndAddress - Info .StartAddress ;
131
+
132
+ return Info ;
133
+ }
134
+
47
135
static const size_t ReadEnd = 0 ;
48
136
static const size_t WriteEnd = 1 ;
49
137
@@ -165,11 +253,24 @@ PipeMemoryReader createPipeMemoryReader() {
165
253
return Reader ;
166
254
}
167
255
256
+ static
257
+ RemoteSection makeRemoteSection (const PipeMemoryReader * Reader ) {
258
+ uintptr_t Start ;
259
+ size_t Size ;
260
+
261
+ PipeMemoryReader_collectBytesFromPipe (Reader , & Start , sizeof (Start ));
262
+ PipeMemoryReader_collectBytesFromPipe (Reader , & Size , sizeof (Size ));
263
+
264
+ RemoteSection RS = {Start , Size , Start + Size };
265
+ return RS ;
266
+ }
267
+
268
+ #if defined(__APPLE__ ) && defined(__MACH__ )
168
269
static void
169
- PipeMemoryReader_receiveReflectionInfo (SwiftReflectionContextRef RC ,
270
+ PipeMemoryReader_receiveImages (SwiftReflectionContextRef RC ,
170
271
const PipeMemoryReader * Reader ) {
171
272
int WriteFD = PipeMemoryReader_getParentWriteFD (Reader );
172
- write (WriteFD , REQUEST_REFLECTION_INFO , 2 );
273
+ write (WriteFD , REQUEST_IMAGES , 2 );
173
274
size_t NumReflectionInfos ;
174
275
PipeMemoryReader_collectBytesFromPipe (Reader , & NumReflectionInfos ,
175
276
sizeof (NumReflectionInfos ));
@@ -188,6 +289,64 @@ PipeMemoryReader_receiveReflectionInfo(SwiftReflectionContextRef RC,
188
289
189
290
free (Images );
190
291
}
292
+ #endif
293
+
294
+ static void
295
+ PipeMemoryReader_receiveReflectionInfo (SwiftReflectionContextRef RC ,
296
+ const PipeMemoryReader * Reader ) {
297
+ int WriteFD = PipeMemoryReader_getParentWriteFD (Reader );
298
+ write (WriteFD , REQUEST_REFLECTION_INFO , 2 );
299
+ size_t NumReflectionInfos ;
300
+ PipeMemoryReader_collectBytesFromPipe (Reader , & NumReflectionInfos ,
301
+ sizeof (NumReflectionInfos ));
302
+
303
+ if (NumReflectionInfos == 0 )
304
+ return ;
305
+
306
+ RemoteReflectionInfo * RemoteInfos = calloc (NumReflectionInfos ,
307
+ sizeof (RemoteReflectionInfo ));
308
+ if (RemoteInfos == NULL )
309
+ errnoAndExit ("malloc failed" );
310
+
311
+ for (size_t i = 0 ; i < NumReflectionInfos ; ++ i ) {
312
+ RemoteInfos [i ] = makeRemoteReflectionInfo (
313
+ makeRemoteSection (Reader ),
314
+ makeRemoteSection (Reader ),
315
+ makeRemoteSection (Reader ),
316
+ makeRemoteSection (Reader ),
317
+ makeRemoteSection (Reader ),
318
+ makeRemoteSection (Reader ));
319
+ }
320
+
321
+ // Now pull in the remote sections into our address space.
322
+
323
+ for (size_t i = 0 ; i < NumReflectionInfos ; ++ i ) {
324
+ RemoteReflectionInfo RemoteInfo = RemoteInfos [i ];
325
+
326
+ void * Buffer = malloc (RemoteInfo .TotalSize );
327
+
328
+ int Success = PipeMemoryReader_readBytes ((void * )Reader ,
329
+ RemoteInfo .StartAddress ,
330
+ Buffer ,
331
+ RemoteInfo .TotalSize );
332
+ if (!Success )
333
+ errorAndExit ("Couldn't read reflection information" );
334
+
335
+ swift_reflection_info_t Info = {
336
+ {makeLocalSection (Buffer , RemoteInfo .fieldmd , RemoteInfo ), 0 },
337
+ {makeLocalSection (Buffer , RemoteInfo .assocty , RemoteInfo ), 0 },
338
+ {makeLocalSection (Buffer , RemoteInfo .builtin , RemoteInfo ), 0 },
339
+ {makeLocalSection (Buffer , RemoteInfo .capture , RemoteInfo ), 0 },
340
+ {makeLocalSection (Buffer , RemoteInfo .typeref , RemoteInfo ), 0 },
341
+ {makeLocalSection (Buffer , RemoteInfo .reflstr , RemoteInfo ), 0 },
342
+ /*LocalStartAddress*/ (uintptr_t ) Buffer ,
343
+ /*RemoteStartAddress*/ RemoteInfo .StartAddress ,
344
+ };
345
+ swift_reflection_addReflectionInfo (RC , Info );
346
+ }
347
+
348
+ free (RemoteInfos );
349
+ }
191
350
192
351
uint64_t PipeMemoryReader_getStringLength (void * Context , swift_addr_t Address ) {
193
352
const PipeMemoryReader * Reader = (const PipeMemoryReader * )Context ;
@@ -291,7 +450,11 @@ int doDumpHeapInstance(const char *BinaryFilename) {
291
450
if (PointerSize != sizeof (uintptr_t ))
292
451
errorAndExit ("Child process had unexpected architecture" );
293
452
453
+ #if defined(__APPLE__ ) && defined(__MACH__ )
454
+ PipeMemoryReader_receiveImages (RC , & Pipe );
455
+ #else
294
456
PipeMemoryReader_receiveReflectionInfo (RC , & Pipe );
457
+ #endif
295
458
296
459
while (1 ) {
297
460
InstanceKind Kind = PipeMemoryReader_receiveInstanceKind (& Pipe );
0 commit comments