1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
//===-- ProfileObjectiveC.cpp -----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Created by Greg Clayton on 10/4/07.
//
//===----------------------------------------------------------------------===//
#include "ProfileObjectiveC.h"
#include "DNB.h"
#include <objc/objc-runtime.h>
#include <map>
#if defined (__powerpc__) || defined (__ppc__)
#define OBJC_MSG_SEND_PPC32_COMM_PAGE_ADDR ((nub_addr_t)0xfffeff00)
#endif
//----------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------
ProfileObjectiveC::ProfileObjectiveC() :
m_pid(INVALID_NUB_PROCESS),
m_objcStats(),
m_hit_count(0),
m_dump_count(0xffff)
{
memset(&m_begin_time, 0, sizeof(m_begin_time));
}
//----------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------
ProfileObjectiveC::~ProfileObjectiveC()
{
}
//----------------------------------------------------------------------
// Clear any counts that we may have had
//----------------------------------------------------------------------
void
ProfileObjectiveC::Clear()
{
if (m_pid != INVALID_NUB_PROCESS)
{
DNBBreakpointClear(m_pid, m_objc_msgSend.breakID);
DNBBreakpointClear(m_pid, m_objc_msgSendSuper.breakID);
#if defined (__powerpc__) || defined (__ppc__)
DNBBreakpointClear(m_pid, m_objc_msgSend_rtp.breakID);
#endif
}
m_objc_msgSend.Clear();
m_objc_msgSendSuper.Clear();
#if defined (__powerpc__) || defined (__ppc__)
memset(m_objc_msgSend_opcode, 0, k_opcode_size);
m_objc_msgSend_rtp.Clear();
#endif
memset(&m_begin_time, 0, sizeof(m_begin_time));
m_objcStats.clear();
}
void
ProfileObjectiveC::Initialize(nub_process_t pid)
{
Clear();
m_pid = pid;
}
void
ProfileObjectiveC::ProcessStateChanged(nub_state_t state)
{
//printf("ProfileObjectiveC::%s(%s)\n", __FUNCTION__, DNBStateAsString(state));
switch (state)
{
case eStateInvalid:
case eStateUnloaded:
case eStateExited:
case eStateDetached:
Clear();
break;
case eStateStopped:
#if defined (__powerpc__) || defined (__ppc__)
if (NUB_BREAK_ID_IS_VALID(m_objc_msgSend.breakID) && !NUB_BREAK_ID_IS_VALID(m_objc_msgSend_rtp.breakID))
{
nub_thread_t tid = DNBProcessGetCurrentThread(m_pid);
DNBRegisterValue pc_value;
if (DNBThreadGetRegisterValueByName(m_pid, tid, REGISTER_SET_ALL, "srr0" , &pc_value))
{
nub_addr_t pc = pc_value.value.uint32;
if (pc == OBJC_MSG_SEND_PPC32_COMM_PAGE_ADDR)
{
// Restore previous first instruction to 0xfffeff00 in comm page
DNBProcessMemoryWrite(m_pid, OBJC_MSG_SEND_PPC32_COMM_PAGE_ADDR, k_opcode_size, m_objc_msgSend_opcode);
//printf("Setting breakpoint on _objc_msgSend_rtp...\n");
m_objc_msgSend_rtp.breakID = DNBBreakpointSet(m_pid, OBJC_MSG_SEND_PPC32_COMM_PAGE_ADDR);
if (NUB_BREAK_ID_IS_VALID(m_objc_msgSend_rtp.breakID))
{
DNBBreakpointSetCallback(m_pid, m_objc_msgSend_rtp.breakID, ProfileObjectiveC::MessageSendBreakpointCallback, this);
}
}
}
}
#endif
DumpStats(m_pid, stdout);
break;
case eStateAttaching:
case eStateLaunching:
case eStateRunning:
case eStateStepping:
case eStateCrashed:
case eStateSuspended:
break;
default:
break;
}
}
void
ProfileObjectiveC::SharedLibraryStateChanged(DNBExecutableImageInfo *image_infos, nub_size_t num_image_infos)
{
//printf("ProfileObjectiveC::%s(%p, %u)\n", __FUNCTION__, image_infos, num_image_infos);
if (m_objc_msgSend.IsValid() && m_objc_msgSendSuper.IsValid())
return;
if (image_infos)
{
nub_process_t pid = m_pid;
nub_size_t i;
for (i = 0; i < num_image_infos; i++)
{
if (strcmp(image_infos[i].name, "/usr/lib/libobjc.A.dylib") == 0)
{
if (!NUB_BREAK_ID_IS_VALID(m_objc_msgSend.breakID))
{
m_objc_msgSend.addr = DNBProcessLookupAddress(pid, "_objc_msgSend", image_infos[i].name);
if (m_objc_msgSend.addr != INVALID_NUB_ADDRESS)
{
#if defined (__powerpc__) || defined (__ppc__)
if (DNBProcessMemoryRead(pid, m_objc_msgSend.addr, k_opcode_size, m_objc_msgSend_opcode) != k_opcode_size)
memset(m_objc_msgSend_opcode, 0, sizeof(m_objc_msgSend_opcode));
#endif
m_objc_msgSend.breakID = DNBBreakpointSet(pid, m_objc_msgSend.addr, 4, false);
if (NUB_BREAK_ID_IS_VALID(m_objc_msgSend.breakID))
DNBBreakpointSetCallback(pid, m_objc_msgSend.breakID, ProfileObjectiveC::MessageSendBreakpointCallback, this);
}
}
if (!NUB_BREAK_ID_IS_VALID(m_objc_msgSendSuper.breakID))
{
m_objc_msgSendSuper.addr = DNBProcessLookupAddress(pid, "_objc_msgSendSuper", image_infos[i].name);
if (m_objc_msgSendSuper.addr != INVALID_NUB_ADDRESS)
{
m_objc_msgSendSuper.breakID = DNBBreakpointSet(pid, m_objc_msgSendSuper.addr, 4, false);
if (NUB_BREAK_ID_IS_VALID(m_objc_msgSendSuper.breakID))
DNBBreakpointSetCallback(pid, m_objc_msgSendSuper.breakID, ProfileObjectiveC::MessageSendSuperBreakpointCallback, this);
}
}
break;
}
}
}
}
void
ProfileObjectiveC::SetStartTime()
{
gettimeofday(&m_begin_time, NULL);
}
void
ProfileObjectiveC::SelectorHit(objc_class_ptr_t isa, objc_selector_t sel)
{
m_objcStats[isa][sel]++;
}
nub_bool_t
ProfileObjectiveC::MessageSendBreakpointCallback(nub_process_t pid, nub_thread_t tid, nub_break_t breakID, void *userData)
{
ProfileObjectiveC *profile_objc = (ProfileObjectiveC*)userData;
uint32_t hit_count = profile_objc->IncrementHitCount();
if (hit_count == 1)
profile_objc->SetStartTime();
objc_class_ptr_t objc_self = 0;
objc_selector_t objc_selector = 0;
#if defined (__i386__)
DNBRegisterValue esp;
if (DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, "esp", &esp))
{
uint32_t uval32[2];
if (DNBProcessMemoryRead(pid, esp.value.uint32 + 4, 8, &uval32) == 8)
{
objc_self = uval32[0];
objc_selector = uval32[1];
}
}
#elif defined (__powerpc__) || defined (__ppc__)
DNBRegisterValue r3;
DNBRegisterValue r4;
if (DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, "r3", &r3) &&
DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, "r4", &r4))
{
objc_self = r3.value.uint32;
objc_selector = r4.value.uint32;
}
#elif defined (__arm__)
DNBRegisterValue r0;
DNBRegisterValue r1;
if (DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, "r0", &r0) &&
DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, "r1", &r1))
{
objc_self = r0.value.uint32;
objc_selector = r1.value.uint32;
}
#else
#error undefined architecture
#endif
if (objc_selector != 0)
{
uint32_t isa = 0;
if (objc_self == 0)
{
profile_objc->SelectorHit(0, objc_selector);
}
else
if (DNBProcessMemoryRead(pid, (nub_addr_t)objc_self, sizeof(isa), &isa) == sizeof(isa))
{
if (isa)
{
profile_objc->SelectorHit(isa, objc_selector);
}
else
{
profile_objc->SelectorHit(0, objc_selector);
}
}
}
// Dump stats if we are supposed to
if (profile_objc->ShouldDumpStats())
{
profile_objc->DumpStats(pid, stdout);
return true;
}
// Just let the target run again by returning false;
return false;
}
nub_bool_t
ProfileObjectiveC::MessageSendSuperBreakpointCallback(nub_process_t pid, nub_thread_t tid, nub_break_t breakID, void *userData)
{
ProfileObjectiveC *profile_objc = (ProfileObjectiveC*)userData;
uint32_t hit_count = profile_objc->IncrementHitCount();
if (hit_count == 1)
profile_objc->SetStartTime();
// printf("BreakID %u hit count is = %u\n", breakID, hc);
objc_class_ptr_t objc_super = 0;
objc_selector_t objc_selector = 0;
#if defined (__i386__)
DNBRegisterValue esp;
if (DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, "esp", &esp))
{
uint32_t uval32[2];
if (DNBProcessMemoryRead(pid, esp.value.uint32 + 4, 8, &uval32) == 8)
{
objc_super = uval32[0];
objc_selector = uval32[1];
}
}
#elif defined (__powerpc__) || defined (__ppc__)
DNBRegisterValue r3;
DNBRegisterValue r4;
if (DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, "r3", &r3) &&
DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, "r4", &r4))
{
objc_super = r3.value.uint32;
objc_selector = r4.value.uint32;
}
#elif defined (__arm__)
DNBRegisterValue r0;
DNBRegisterValue r1;
if (DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, "r0", &r0) &&
DNBThreadGetRegisterValueByName(pid, tid, REGISTER_SET_ALL, "r1", &r1))
{
objc_super = r0.value.uint32;
objc_selector = r1.value.uint32;
}
#else
#error undefined architecture
#endif
if (objc_selector != 0)
{
uint32_t isa = 0;
if (objc_super == 0)
{
profile_objc->SelectorHit(0, objc_selector);
}
else
if (DNBProcessMemoryRead(pid, (nub_addr_t)objc_super + 4, sizeof(isa), &isa) == sizeof(isa))
{
if (isa)
{
profile_objc->SelectorHit(isa, objc_selector);
}
else
{
profile_objc->SelectorHit(0, objc_selector);
}
}
}
// Dump stats if we are supposed to
if (profile_objc->ShouldDumpStats())
{
profile_objc->DumpStats(pid, stdout);
return true;
}
// Just let the target run again by returning false;
return false;
}
void
ProfileObjectiveC::DumpStats(nub_process_t pid, FILE *f)
{
if (f == NULL)
return;
if (m_hit_count == 0)
return;
ClassStatsMap::iterator class_pos;
ClassStatsMap::iterator class_end = m_objcStats.end();
struct timeval end_time;
gettimeofday(&end_time, NULL);
int64_t elapsed_usec = ((int64_t)(1000*1000))*((int64_t)end_time.tv_sec - (int64_t)m_begin_time.tv_sec) + ((int64_t)end_time.tv_usec - (int64_t)m_begin_time.tv_usec);
fprintf(f, "%u probe hits for %.2f hits/sec)\n", m_hit_count, (double)m_hit_count / (((double)elapsed_usec)/(1000000.0)));
for (class_pos = m_objcStats.begin(); class_pos != class_end; ++class_pos)
{
SelectorHitCount::iterator sel_pos;
SelectorHitCount::iterator sel_end = class_pos->second.end();
for (sel_pos = class_pos->second.begin(); sel_pos != sel_end; ++sel_pos)
{
struct objc_class objc_class;
uint32_t isa = class_pos->first;
uint32_t sel = sel_pos->first;
uint32_t sel_hit_count = sel_pos->second;
if (isa != 0 && DNBProcessMemoryRead(pid, isa, sizeof(objc_class), &objc_class) == sizeof(objc_class))
{
/* fprintf(f, "%#.8x\n isa = %p\n super_class = %p\n name = %p\n version = %lx\n info = %lx\ninstance_size = %lx\n ivars = %p\n methodLists = %p\n cache = %p\n protocols = %p\n",
arg1.value.pointer,
objc_class.isa,
objc_class.super_class,
objc_class.name,
objc_class.version,
objc_class.info,
objc_class.instance_size,
objc_class.ivars,
objc_class.methodLists,
objc_class.cache,
objc_class.protocols); */
// Print the class name
fprintf(f, "%6u hits for %c[", sel_hit_count, (objc_class.super_class == objc_class.isa ? '+' : '-'));
DNBPrintf(pid, INVALID_NUB_THREAD, (nub_addr_t)objc_class.name, f, "%s ");
}
else
{
fprintf(f, "%6u hits for [<nil> ", sel_hit_count);
}
DNBPrintf(pid, INVALID_NUB_THREAD, sel, f, "%s]\n");
}
}
}
|