-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.cpp
More file actions
646 lines (594 loc) · 18.9 KB
/
execution.cpp
File metadata and controls
646 lines (594 loc) · 18.9 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
// ----------------------------------------------------------------------
// Representation of runtime execution
// stacks, threads, time
#include "execution.h"
unsigned int CCNode::m_ccnode_nextid = 1;
// ----------------------------------------------------------------------
// Calling context tree
CCNode* CCNode::Call(Method* m)
{
CCNode* result = 0;
auto p = m_callees.find(m->getId());
// TODO auto p = m_callees.find(m);
if (p == m_callees.end()) {
result = new CCNode( this, // parent
m, // method
this->m_output ); // file output
m_callees[m->getId()] = result;
// TODO m_callees[m] = result;
} else {
result = (*p).second;
}
NodeId_t parent = this->get_node_id();
NodeId_t child = result->get_node_id();
if (this->m_output.is_open()) {
this->m_output << parent << "," << child << endl;
}
return result;
}
CCNode* CCNode::Return(Method* m)
{
if (m_method != m) {
cout << "WEIRD: Returning from the wrong method " << m->info() << endl;
cout << "WEIRD: should be " << m_method->info() << endl;
}
return m_parent;
}
string CCNode::info()
{
stringstream ss;
if (m_method) {
ss << m_method->info(); // << "@" << m_startTime;
}
else {
ss << "ROOT";
}
return ss.str();
}
string CCNode::stacktrace()
{
stringstream ss;
CCNode* cur = this;
while (cur) {
ss << cur->info() << endl;
cur = cur->getParent();
}
return ss.str();
}
DequeId_t CCNode::stacktrace_using_id()
{
DequeId_t result;
CCNode* cur = this;
while (cur) {
result.push_front(this->getMethodId());
cur = cur->getParent();
}
return result;
}
// A version that returns a list/vector of
// - method pointers
deque<Method *> CCNode::simple_stacktrace()
{
if (this->isDone()) {
return this->m_simptrace;
}
deque<Method *> result;
// Relies on the fact that Method * are unique even
// if the CCNodes are different as long as they refer
// to the same method. A different CCNode will mean
// a different invocation.
Method *mptr = this->getMethod();
assert(mptr);
CCNode *parent_ptr = this->getParent();
if (parent_ptr) {
result = parent_ptr->simple_stacktrace();
}
result.push_front(mptr);
this->setDone();
this->m_simptrace = result;
return result;
}
bool CCNode::simple_cc_equal( CCNode &other )
{
// Relies on the fact that Method * are unique even
// if the CCNodes are different as long as they refer
// to the same method. A different CCNode will mean
// a different invocation.
if (this->getMethod() != other.getMethod()) {
return false;
}
CCNode *self_ptr = this->getParent();
CCNode *other_ptr = other.getParent();
if ((self_ptr == NULL || other_ptr == NULL)) {
return (self_ptr == other_ptr);
}
return (self_ptr->simple_cc_equal(*other_ptr));
}
// ----------------------------------------------------------------------
// Thread representation
// (Essentially, the stack)
// -- Call method m
void Thread::Call(Method *m)
{
if (this->m_kind == ExecMode::Full) {
CCNode* cur = this->TopCC();
this->m_curcc = cur->Call(m);
// this->nodefile << child << "," << m->getName() << endl;
}
if (this->m_kind == ExecMode::StackOnly) {
// Save (old_top, new_top) of m_methods
// NOT SURE IF THIS IS STILL NEEDED:
ContextPair cpair;
if (m_methods.size() > 0) {
cpair = std::make_pair( m_methods.back(), m );
} else {
cpair = std::make_pair( (Method *) NULL, m );
}
this->setContextPair( cpair, CPairType::CP_Call );
// TODO this->debug_cpair( this->getContextPair(), "call" );
// END NOT NEEDED NOTE
// -------------------
m_methods.push_back(m);
// m_methods, m_locals, and m_deadlocals must be synched in pushing
// TODO: Do we need to check for m existing in map?
// Ideally no, but not really sure what is possible in Elephant
// Tracks.
m_locals.push_back(new LocalVarSet());
m_deadlocals.push_back(new LocalVarSet());
// Count 2 level contexts
MethodDeque top2meth = this->top_N_methods(2);
this->m_exec.IncCallContext( top2meth );
}
}
// -- Return from method m
void Thread::Return(Method* m)
{
if (this->m_kind == ExecMode::Full) {
CCNode* cur = this->TopCC();
if (cur->getMethod()) {
this->m_curcc = cur->Return(m);
} else {
cout << "WARNING: Return from " << m->info() << " at top context" << endl;
m_curcc = cur;
}
}
if (this->m_kind == ExecMode::StackOnly) {
if ( !m_methods.empty()) {
Method *cur = m_methods.back();
m_methods.pop_back();
// if (cur != m) {
// cerr << "WARNING: Return from method " << m->info() << " does not match stack top " << cur->info() << endl;
// }
// m_methods, m_locals, and m_deadlocals must be synched in popping
// NOTE: Maybe refactor. See same code in Thread::Call
// Save (old_top, new_top) of m_methods
ContextPair cpair;
if (m_methods.size() > 0) {
// TODO: What if m != cur?
// It seems reasonable to simply use the m that's passed to us rather than
// rely on the call stack being correct. TODO: Verify.
cpair = std::make_pair( cur, m_methods.back() );
} else {
cpair = std::make_pair( cur, (Method *) NULL );
}
this->setContextPair( cpair, CPairType::CP_Return );
// TODO this->debug_cpair( this->getContextPair(), "return" );
// TODO TODO: Save type (Call vs Return) -- See similar code above.
// Locals
LocalVarSet *localvars = m_locals.back();
m_locals.pop_back();
LocalVarSet *deadvars = m_deadlocals.back();
m_deadlocals.pop_back();
delete localvars;
delete deadvars;
} else {
cout << "ERROR: Stack empty at return " << m->info() << endl;
}
}
}
// -- Get current CC
CCNode* Thread::TopCC()
{
if (this->m_kind == ExecMode::Full) {
assert(m_curcc);
// TODO // -- Create a root context if necessary
// TODO if (m_curcc == 0) {
// TODO m_curcc = new CCNode();
// TODO }
return m_curcc;
}
if (this->m_kind == ExecMode::StackOnly) {
cout << "ERROR: Asking for calling context in stack mode" << endl;
return 0;
}
cout << "ERROR: Unkown mode " << this->m_kind << endl;
return 0;
}
// -- Get current method
Method *Thread::TopMethod()
{
if (this->m_kind == ExecMode::Full) {
return TopCC()->getMethod();
}
if (this->m_kind == ExecMode::StackOnly) {
if ( ! m_methods.empty()) {
return m_methods.back();
} else {
// cout << "ERROR: Asking for top of empty stack" << endl;
return NULL;
}
}
cout << "ERROR: Unkown mode " << m_kind << endl;
return NULL;
}
// -- Get method 'num' from the top (zero-based)
Method *Thread::TopMethod( unsigned int num )
{
if (this->m_kind == ExecMode::Full) {
assert(false);
return TopCC()->getMethod();
}
if (this->m_kind == ExecMode::StackOnly) {
unsigned int mysize = this->m_methods.size();
if (mysize > num) {
// return this->m_methods[num + 1];
return this->m_methods[mysize - num - 1];
} else {
// cout << "ERROR: Asking for " << num
// << "-th from top but stack is smaller." << endl;
return NULL;
}
}
cout << "ERROR: Unkown mode " << m_kind << endl;
return NULL;
}
// -- Get N current methods. Only makes sense if N > 1.
// For N == 1, use TopMethod()
MethodDeque Thread::top_N_methods(unsigned int N)
{
assert( N > 1 );
MethodDeque result;
if (this->m_kind == ExecMode::Full) {
// TODO: NOT IMPLEMENTED YET FOR FULL MODE
assert(false);
// TODO DELETE return TopCC()->getMethod();
}
else if (this->m_kind == ExecMode::StackOnly) {
unsigned int count = 0;
unsigned int stacksize = this->m_methods.size();
while ((count < N) && count < stacksize) {
result.push_back(this->TopMethod(count));
count++;
}
while (count < N ) {
result.push_back(NULL);
count++;
}
} else {
cout << "ERROR: Unkown mode " << m_kind << endl;
assert(result.size() == 0);
}
return result;
// NOTHING GOES HERE.
}
// -- Get full context of methods.
MethodDeque Thread::full_method_stack()
{
MethodDeque result;
if (this->m_kind == ExecMode::Full) {
// TODO: NOT IMPLEMENTED YET FOR FULL MODE
// Maybe should be called at all from FULL mode.
assert(false);
} else if (this->m_kind == ExecMode::StackOnly) {
unsigned int count = 0;
unsigned int stacksize = this->m_methods.size();
while (count < stacksize) {
result.push_back(this->TopMethod(count));
count++;
}
} else {
cout << "ERROR: Unkown mode " << m_kind << endl;
assert(result.size() == 0);
}
return result;
// NOTHING GOES HERE.
}
//-----------------------------------------------------------------------------
// Private helper function for determining whether
// a function is a Java library.
// If the function name starts with:
// - 'java/'
// - 'sun/'
// - 'com/sun/'
// - 'com/ibm/'
// then it's a Java library function.
bool is_javalib_method( string &methname )
{
return ( (methname.substr(0,5) == "java/") ||
(methname.substr(0,4) == "sun/") ||
(methname.substr(0,8) == "com/sun/") ||
(methname.substr(0,8) == "com/ibm/") );
}
//-----------------------------------------------------------------------------
// -- Get all Java library methods from top of call stack +
// first non Java method
MethodDeque Thread::top_javalib_methods()
{
MethodDeque result;
if (this->m_kind == ExecMode::Full) {
// TODO: NOT IMPLEMENTED YET FOR FULL MODE
assert(false);
} else if (this->m_kind == ExecMode::StackOnly) {
// TODO Some testing code. Should move these out into a unit testing
// framework.
// string test1("java/lang/Shutdown.shutdown");
// string test2("SHOULD FAIL");
// string test3("");
// string test4("sun/some/method");
// string test5("com/sun/some/method");
// string test6("com/ibm/some/method");
// assert( is_javalib_method(test1) );
// assert( !is_javalib_method(test2) );
// assert( !is_javalib_method(test3) );
// assert( is_javalib_method(test4) );
// assert( is_javalib_method(test5) );
// assert( is_javalib_method(test6) );
unsigned int count = 0;
unsigned int stacksize = this->m_methods.size();
while (count < stacksize) {
Method *mptr = this->TopMethod(count);
string mname = (mptr ? mptr->getName() : "NULL_METHOD");
result.push_back(mptr);
count++;
if (!is_javalib_method(mname)) {
// This check and break statement is placed after
// the push onto the result deque. This means that
// at least one non Java library method may be included
// in the result.
break;
}
}
if (result.size() == 0) {
result.push_back(NULL);
}
} else {
cout << "ERROR: Unkown mode " << m_kind << endl;
assert(result.size() == 0);
}
return result;
// NOTHING GOES HERE.
}
// -- Get current dead locals
LocalVarSet * Thread::TopLocalVarSet()
{
if (this->m_kind == ExecMode::Full) {
// TODO
return NULL;
}
else if (this->m_kind == ExecMode::StackOnly) {
return ((!m_deadlocals.empty()) ? m_deadlocals.back() : NULL);
}
}
// -- Get a stack trace
string Thread::stacktrace()
{
if (this->m_kind == ExecMode::Full) {
return TopCC()->stacktrace();
}
if (this->m_kind == ExecMode::StackOnly) {
if ( ! m_methods.empty()) {
stringstream ss;
for ( auto p = m_methods.begin();
p != m_methods.end();
p++ ) {
Method* m =*p;
ss << m->info() << endl;
}
return ss.str();
} else {
return "<empty>";
}
}
cout << "ERROR: Unkown mode " << m_kind << endl;
return "ERROR";
}
// -- Get a stack trace in method ids
DequeId_t Thread::stacktrace_using_id()
{
if (this->m_kind == ExecMode::Full) {
return TopCC()->stacktrace_using_id();
}
if (this->m_kind == ExecMode::StackOnly) {
assert(false);
// TODO if ( ! m_methods.empty()) {
// TODO stringstream ss;
// TODO for ( auto p = m_methods.begin();
// TODO p != m_methods.end();
// TODO p++ ) {
// TODO Method* m =*p;
// TODO ss << m->info() << endl;
// TODO }
// TODO return ss.str();
// TODO } else {
// TODO return "<empty>";
// TODO }
DequeId_t result;
return result;
}
assert(false);
}
// -- Object is a root
void Thread::objectRoot(Object *object)
{
if (!m_locals.empty()) {
LocalVarSet *localvars = m_locals.back();
localvars->insert(object);
} else {
cout << "[objectRoot] ERROR: Stack empty at ROOT event." << endl;
}
}
// -- Check dead object if root
bool Thread::isLocalVariable(Object *object)
{
if (!m_locals.empty()) {
LocalVarSet *localvars = m_locals.back();
auto it = localvars->find(object);
return (it != localvars->end());
} else {
cout << "[isLocalVariable] ERROR: Stack empty at ROOT event." << endl;
return false;
}
}
// ----------------------------------------------------------------------
// Execution state
// -- Look up or create a thread
Thread* ExecState::getThread(unsigned int threadid)
{
Thread* result = 0;
auto p = m_threads.find(threadid);
if (p == m_threads.end()) {
// -- Not there, make a new one
result = new Thread( threadid,
this->m_kind,
this->m_allocCountmap,
this->m_deathPairCountMap,
this->m_contextTypeMap,
*this,
*this->m_output,
*this->m_nodefile );
m_threads[threadid] = result;
} else {
result = (*p).second;
}
return result;
}
// -- Call method m in thread t
void ExecState::Call(Method* m, unsigned int threadid)
{
this->IncMethodTime();
Thread *t = getThread(threadid);
if (t) {
t->Call(m);
this->m_thread_stack.push_back(t);
}
}
// -- Return from method m in thread t
void ExecState::Return(Method* m, unsigned int threadid)
{
this->IncMethodExitTime();
if (this->m_thread_stack.size() > 0) {
this->m_thread_stack.pop_back();
}
getThread(threadid)->Return(m);
}
// -- Get the top method in thread t
Method* ExecState::TopMethod(unsigned int threadid)
{
Thread *thread = getThread(threadid);
if (thread) {
return thread->TopMethod();
} else {
return NULL;
}
}
// -- Get the top method in thread t
Method *ExecState::TopMethod( unsigned int threadid,
unsigned int num )
{
Thread *thread = getThread(threadid);
if (thread) {
return thread->TopMethod(num);
} else {
return NULL;
}
}
// -- Get the top calling context in thread t
CCNode* ExecState::TopCC(unsigned int threadid)
{
return getThread(threadid)->TopCC();
}
// Update the Object pointer to simple Death context pair map
void ExecState::UpdateObj2DeathContext( Object *obj,
MethodDeque &methdeque )
{
unsigned int count = 0;
bool nonjava_flag = false;
string nonjava_method;
MethodDeque top_2_methods;
while ( (count < 2) &&
(methdeque.size() > 0) ) {
Method *next_method = methdeque.front();
methdeque.pop_front();
string next_name;
count += 1;
next_name = (next_method ? next_method->getName() : "NULL_METHOD");
obj->setDeathSite(next_method, count);
obj->setDeathContextSiteName(next_name, count);
if (count == 1) {
this->m_objDeath2cmap[obj] = next_name;
top_2_methods.push_back(next_method);
} else if (count == 2) {
top_2_methods.push_back(next_method);
}
if (!nonjava_flag && !is_javalib_method(next_name)) {
obj->set_nonJavaLib_death_context(next_name);
nonjava_flag = true;
break;
}
}
if (!nonjava_flag && (count < 2)) {
while (count < 2) {
string next_name("NULL_METHOD");
count += 1;
obj->setDeathSite(NULL, count);
obj->setDeathContextSiteName(next_name, count);
if (count == 1) {
this->m_objDeath2cmap[obj] = next_name;
top_2_methods.push_back(NULL);
} else if (count == 2) {
top_2_methods.push_back(NULL);
}
if (!nonjava_flag && !is_javalib_method(next_name)) {
obj->set_nonJavaLib_death_context(next_name);
nonjava_flag = true;
break;
}
}
}
if (!nonjava_flag) {
string last_name;
if (methdeque.size() > 0) {
Method *last_method = methdeque.back();
last_name = ( last_method ? last_method->getName() : "NULL_METHOD" );
} else {
last_name = "NULL_METHOD";
}
obj->set_nonJavaLib_death_context(last_name);
nonjava_flag = true; // probably don't need to set the flag, but just in
// case someone adds some code later on.
}
ContextPair cpair = std::make_pair( top_2_methods[0], top_2_methods[1] );
auto iter = this->m_deathPairCountMap.find(cpair);
if (iter == this->m_deathPairCountMap.end()) {
this->m_deathPairCountMap[cpair] = 0;
}
this->m_deathPairCountMap[cpair]++;
}
// -
// Utility files
//
std::ostream& operator << ( std::ostream &out, const ExecMode &value )
{
switch(value)
{
case ExecMode::Full:
out << "Full";
break;
case ExecMode::StackOnly:
out << "StackOnly";
break;
default:
out << "<UNKNOWN>";
}
return out;
}