-
Notifications
You must be signed in to change notification settings - Fork 0
/
pc_lookup_dwarf.c
1264 lines (1035 loc) · 31.8 KB
/
pc_lookup_dwarf.c
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- C -*-
mpiP MPI Profiler ( http://llnl.github.io/mpiP )
Please see COPYRIGHT AND LICENSE information at the end of this file.
-----
pc_lookup_dwarf.c -- functions that use libdwarf for symbol lookup
$Id$
*/
#ifndef lint
static char *svnid =
"$Id$";
#endif
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "mpiPi.h"
#include "mpiPconfig.h"
#if defined(USE_LIBDWARF)
#ifndef CEXTRACT
#include "elf.h"
#include "libelf.h"
#include "dwarf.h"
#include "libdwarf.h"
#endif
static Dwarf_Error dw_err;
/*----------------------------------------------------------------------------
* collection of unique file name strings
*
* In the address-to-source line information, we expect to see the same file
* name several times. Rather than allocate a separate string for each
* file name, we maintain a collection of unique file names.
*
* Unlike the function or address-to-source line information, we expect
* only a small number of unique file names. Whereas we want lookup
* to be quick, we also want the implementation to be simple, and because
* we don't have a nice STL-like collection of container classes available,
* we use a linked list with linear search for lookup for this collection.
* Because we expect a small number of unique names, the linear search
* should not be too much of a performance penalty.
*/
struct UniqueFileNameInfo
{
char *fileName;
};
struct UniqueFileNameListNode
{
struct UniqueFileNameInfo ufi;
struct UniqueFileNameListNode *next;
};
static struct UniqueFileNameListNode *uniqueFileNameList = NULL;
static char *
UniqueFileName_Get (const char *testFileName)
{
char *ret = NULL;
struct UniqueFileNameListNode *currInfo = NULL;
assert (testFileName != NULL);
currInfo = uniqueFileNameList;
while (currInfo != NULL)
{
if (strcmp (testFileName, currInfo->ufi.fileName) == 0)
{
/* we found a match */
break;
}
/* advance to the next file name */
currInfo = currInfo->next;
}
if (currInfo != NULL)
{
/*
* We found a match during our search -
* use the already-allocated string.
*/
ret = currInfo->ufi.fileName;
}
else
{
/*
* We didn't find a match during our search.
* Allocate a new string.
*/
currInfo = (struct UniqueFileNameListNode *)
malloc (sizeof (struct UniqueFileNameListNode));
if (currInfo == NULL)
{
mpiPi_abort ("malloc failed\n");
}
currInfo->ufi.fileName = strdup (testFileName);
if (currInfo->ufi.fileName == NULL)
{
mpiPi_abort ("malloc failed\n");
}
/*
* link the new entry into our list
* since the list is unordered, just link it in
* at the beginning for simplicity.
*/
currInfo->next = uniqueFileNameList;
uniqueFileNameList = currInfo;
ret = currInfo->ufi.fileName;
}
assert (ret != NULL);
return ret;
}
static void
UniqueFileName_Destroy (void)
{
struct UniqueFileNameListNode *currInfo = uniqueFileNameList;
while (currInfo != NULL)
{
struct UniqueFileNameListNode *nextInfo = currInfo->next;
free (currInfo->ufi.fileName);
free (currInfo);
currInfo = nextInfo;
}
}
/*----------------------------------------------------------------------------
* address range map
*
* Collection organized by non-overlapping address ranges.
* Organized as a red-black tree for quick insertion and quick lookup.
*/
enum AddrRangeMapNodeColor
{
AddrRangeMapNodeColor_Red,
AddrRangeMapNodeColor_Black
};
struct AddrRangeMapNode
{
enum AddrRangeMapNodeColor color;
Dwarf_Addr lowAddr;
Dwarf_Addr highAddr;
void *info;
struct AddrRangeMapNode *parent;
struct AddrRangeMapNode *leftChild;
struct AddrRangeMapNode *rightChild;
};
struct AddrRangeMap
{
struct AddrRangeMapNode *root;
};
static void
AddrRangeMap_Init (struct AddrRangeMap *map)
{
assert (map != NULL);
map->root = NULL;
}
static void
AddrRangeMap_DestroyAux (struct AddrRangeMapNode *node,
void (*nodeInfoFreeFunc) (void *))
{
assert (node != NULL);
/* destroy subtrees of node */
if (node->leftChild != NULL)
{
AddrRangeMap_DestroyAux (node->leftChild, nodeInfoFreeFunc);
node->leftChild = NULL;
}
if (node->rightChild != NULL)
{
AddrRangeMap_DestroyAux (node->rightChild, nodeInfoFreeFunc);
node->rightChild = NULL;
}
/* destroy node itself */
if ((nodeInfoFreeFunc != NULL) && (node->info != NULL))
{
nodeInfoFreeFunc (node->info);
}
free (node);
}
static void
AddrRangeMap_Destroy (struct AddrRangeMap *map,
void (*nodeInfoFreeFunc) (void *))
{
if (map != NULL)
{
if (map->root != NULL)
{
AddrRangeMap_DestroyAux (map->root, nodeInfoFreeFunc);
}
free (map);
}
}
static void
AddrRangeMap_LeftRotate (struct AddrRangeMap *map,
struct AddrRangeMapNode *node)
{
struct AddrRangeMapNode *oldRightChild = NULL;
assert (map != NULL);
assert (node != NULL);
assert (node->rightChild != NULL);
/*
* save old right child; will become new root of
* subtree currently rooted at node
*/
oldRightChild = node->rightChild;
node->rightChild = oldRightChild->leftChild;
if (oldRightChild->leftChild != NULL)
{
oldRightChild->leftChild->parent = node;
}
/* do the rotation */
oldRightChild->parent = node->parent;
if (node->parent == NULL)
{
map->root = oldRightChild;
}
else if (node == node->parent->leftChild)
{
node->parent->leftChild = oldRightChild;
}
else
{
node->parent->rightChild = oldRightChild;
}
oldRightChild->leftChild = node;
node->parent = oldRightChild;
}
static void
AddrRangeMap_RightRotate (struct AddrRangeMap *map,
struct AddrRangeMapNode *node)
{
struct AddrRangeMapNode *oldLeftChild = NULL;
assert (map != NULL);
assert (node != NULL);
assert (node->leftChild != NULL);
/*
* save old left child; will become new root of
* subtree currently rooted at node
*/
oldLeftChild = node->leftChild;
node->leftChild = oldLeftChild->rightChild;
if (oldLeftChild->rightChild != NULL)
{
oldLeftChild->rightChild->parent = node;
}
/* do the rotation */
oldLeftChild->parent = node->parent;
if (node->parent == NULL)
{
map->root = oldLeftChild;
}
else if (node == node->parent->rightChild)
{
node->parent->rightChild = oldLeftChild;
}
else
{
node->parent->leftChild = oldLeftChild;
}
oldLeftChild->rightChild = node;
node->parent = oldLeftChild;
}
static void
AddrRangeMap_Add (struct AddrRangeMap *map,
Dwarf_Addr lowAddr, Dwarf_Addr highAddr, void *info)
{
struct AddrRangeMapNode *currNode = NULL;
struct AddrRangeMapNode *newEntry = NULL;
mpiPi_msg_debug ("AddrRangeMap::Add: [0x%016llx,0x%016llx]\n",
lowAddr, highAddr);
/* build a new entry for this mapping */
newEntry =
(struct AddrRangeMapNode *) malloc (sizeof (struct AddrRangeMapNode));
if (newEntry == NULL)
{
mpiPi_abort ("malloc failed\n");
}
newEntry->color = AddrRangeMapNodeColor_Red;
assert (lowAddr <= highAddr);
newEntry->lowAddr = lowAddr;
newEntry->highAddr = highAddr;
newEntry->info = info;
newEntry->parent = NULL;
newEntry->leftChild = NULL;
newEntry->rightChild = NULL;
/* add the new node to our map */
if (map->root == NULL)
{
/* new node is the first node to be added */
map->root = newEntry;
}
else
{
/* new node is not the first node to be added */
currNode = map->root;
while (currNode != NULL)
{
if (highAddr <= currNode->lowAddr)
{
/* target address is below range covered by current node */
if (currNode->leftChild != NULL)
{
/* compare with ranges smaller than ours */
currNode = currNode->leftChild;
}
else
{
/* adopt new node as our left child */
currNode->leftChild = newEntry;
newEntry->parent = currNode;
break;
}
}
else if (lowAddr >= currNode->highAddr)
{
/* target address is above range covered by current node */
if (currNode->rightChild != NULL)
{
/* compare with ranges larger than ours */
currNode = currNode->rightChild;
}
else
{
/* adopt new node as our right child */
currNode->rightChild = newEntry;
newEntry->parent = currNode;
break;
}
}
else
{
/* new range overlaps with our range! */
mpiPi_abort
("new address node range [0x%016llx,0x%016llx] overlaps our range [0x%016llx,0x%016llx]\n",
lowAddr, highAddr, currNode->lowAddr, currNode->highAddr);
}
}
}
/*
* new node has been inserted, but its insertion
* may have unbalanced the tree -
* re-balance it.
* Based on red-black insert algorithm given in
* Cormen, Lieserson, Rivest "Introduction to Algorithms"
*/
currNode = newEntry;
while ((currNode != map->root) &&
(currNode->parent->color == AddrRangeMapNodeColor_Red))
{
struct AddrRangeMapNode *uncleNode = NULL;
if (currNode->parent == currNode->parent->parent->leftChild)
{
/* currNode's parent is its parent's left child */
uncleNode = currNode->parent->parent->rightChild;
if ((uncleNode != NULL) &&
(uncleNode->color == AddrRangeMapNodeColor_Red))
{
currNode->parent->color = AddrRangeMapNodeColor_Black;
uncleNode->color = AddrRangeMapNodeColor_Black;
currNode->parent->parent->color = AddrRangeMapNodeColor_Red;
currNode = currNode->parent->parent;
}
else
{
/* uncleNode is NULL (NULL is assumed black) or is black */
if (currNode == currNode->parent->rightChild)
{
currNode = currNode->parent;
AddrRangeMap_LeftRotate (map, currNode);
}
currNode->parent->color = AddrRangeMapNodeColor_Black;
currNode->parent->parent->color = AddrRangeMapNodeColor_Red;
AddrRangeMap_RightRotate (map, currNode->parent->parent);
}
}
else
{
/* currNode's parent is its parent's right child */
uncleNode = currNode->parent->parent->leftChild;
if ((uncleNode != NULL) &&
(uncleNode->color == AddrRangeMapNodeColor_Red))
{
currNode->parent->color = AddrRangeMapNodeColor_Black;
uncleNode->color = AddrRangeMapNodeColor_Black;
currNode->parent->parent->color = AddrRangeMapNodeColor_Red;
currNode = currNode->parent->parent;
}
else
{
/* uncleNode is NULL (NULL is assumed black) or is black */
if (currNode == currNode->parent->leftChild)
{
currNode = currNode->parent;
AddrRangeMap_RightRotate (map, currNode);
}
currNode->parent->color = AddrRangeMapNodeColor_Black;
currNode->parent->parent->color = AddrRangeMapNodeColor_Red;
AddrRangeMap_LeftRotate (map, currNode->parent->parent);
}
}
}
assert (map->root != NULL);
map->root->color = AddrRangeMapNodeColor_Black;
}
static const void *
AddrRangeMap_Find (struct AddrRangeMap *map, void *addr)
{
struct AddrRangeMapNode *currNode;
const void *ret = NULL;
mpiPi_msg_debug ("AddrRangeMap::Find: looking for %p\n", addr);
currNode = map->root;
while (currNode != NULL)
{
mpiPi_msg_debug
("AddrRangeMap::Find: comparing with [0x%016llx,0x%016llx]\n",
currNode->lowAddr, currNode->highAddr);
if (((Dwarf_Addr) addr) < currNode->lowAddr)
{
/* target address is below range covered by current node */
/* NOTE: we might not have a left child */
mpiPi_msg_debug ("AddrRangeMap::Find: going left\n");
currNode = currNode->leftChild;
}
else if (((Dwarf_Addr) addr) > currNode->highAddr)
{
/* target address is above range covered by current node */
/* NOTE: we might not have a right child */
mpiPi_msg_debug ("AddrRangeMap::Find: going right\n");
currNode = currNode->rightChild;
}
else
{
/* target address falls within range of current node's statement */
mpiPi_msg_debug ("AddrRangeMap::Find: found\n");
ret = currNode->info;
break;
}
}
return ret;
}
/*----------------------------------------------------------------------------
* address to source line map
*
* The DWARF library interface can provide address-to-source line mapping
* information. However, it does not necessarily define a mapping for
* each address in the executable.
*
* If address-to-source line information is available, we expect there
* to be a *lot* of it. We want to maintain this information in
* a data structure so that lookups are quick and inserts are reasonable.
* We don't modify or delete the mapping information once we've built
* the data structure (except to tear it all down at the end of the run),
* so we don't care so much about the cost of modifying or deleting mapping
* information.
*
*/
struct AddrToSourceInfo
{
char *fileName;
unsigned int lineNumber;
};
static struct AddrRangeMap *addrToSourceMap = NULL;
static void
AddrToSourceMap_Init (void)
{
addrToSourceMap =
(struct AddrRangeMap *) malloc (sizeof (struct AddrRangeMap));
if (addrToSourceMap == NULL)
{
mpiPi_abort ("malloc failed\n");
}
AddrRangeMap_Init (addrToSourceMap);
}
static void
AddrToSourceMap_Destroy (void)
{
if (addrToSourceMap != NULL)
{
AddrRangeMap_Destroy (addrToSourceMap, free);
addrToSourceMap = NULL;
}
}
static void
AddrToSourceMap_Add (Dwarf_Addr addr,
const char *fileName, unsigned int lineNumber)
{
/* build a new entry for this mapping */
struct AddrToSourceInfo *newEntry =
(struct AddrToSourceInfo *) malloc (sizeof (struct AddrToSourceInfo));
if (newEntry == NULL)
{
mpiPi_abort ("malloc failed\n");
}
newEntry->fileName = UniqueFileName_Get (fileName);
assert (newEntry->fileName != NULL);
newEntry->lineNumber = lineNumber;
mpiPi_msg_debug ("AddrToSourceMap::Add: 0x%016llx => %s: %d\n",
addr, newEntry->fileName, newEntry->lineNumber);
assert (addrToSourceMap != NULL);
AddrRangeMap_Add (addrToSourceMap, addr, addr, /* we will patch range ends later */
newEntry);
}
static Dwarf_Addr
AddrToSourceMap_PatchRangesAux (struct AddrRangeMapNode *node,
Dwarf_Addr maxAddress)
{
Dwarf_Addr ret;
assert (node != NULL);
/* check for address ranges smaller than ours */
if (node->leftChild != NULL)
{
/*
* patch ranges in our left subtree
* max address of any range below ours is our min address.
*/
ret = AddrToSourceMap_PatchRangesAux (node->leftChild, node->lowAddr);
}
else
{
/*
* we have no left child,
* so there are no ranges we know of that are smaller than ours.
*/
ret = node->lowAddr;
}
/* check for ranges greater than ours */
if (node->rightChild != NULL)
{
/*
* we have a right child,
* so there are ranges that are larger than ours
*/
maxAddress = AddrToSourceMap_PatchRangesAux (node->rightChild,
maxAddress);
}
/*
* limit our range according to our (possibly updated) idea of the
* minimum address larger than our range.
*/
mpiPi_msg_debug
("AddrRangeMap::PatchRanges: resetting range from [0x%016llx,0x%016llx] to [0x%016llx,0x%016llx]\n",
node->lowAddr, node->highAddr, node->lowAddr, (maxAddress - 1));
node->highAddr = (maxAddress - 1);
return ret;
}
static void
AddrToSourceMap_PatchRanges (void)
{
assert (addrToSourceMap != NULL);
if (addrToSourceMap->root != NULL)
{
/*
* set the overall max address
* we could do better using domain-specific information,
* such as the max address in the text section if we
* knew we were looking at functions in the text section.
*/
Dwarf_Addr maxAddress = ~(Dwarf_Addr) 0;
/* patch the address ranges in our map */
AddrToSourceMap_PatchRangesAux (addrToSourceMap->root, maxAddress);
}
}
static const struct AddrToSourceInfo *
AddrToSourceMap_Find (void *addr)
{
assert (addrToSourceMap != NULL);
return (const struct AddrToSourceInfo *) AddrRangeMap_Find (addrToSourceMap,
addr);
}
/*----------------------------------------------------------------------------
*
*
*/
struct FunctionInfo
{
char *name;
};
static struct AddrRangeMap *functionMap = NULL;
static void
FunctionMap_Init (void)
{
assert (functionMap == NULL);
functionMap = (struct AddrRangeMap *) malloc (sizeof (struct AddrRangeMap));
if (functionMap == NULL)
{
mpiPi_abort ("malloc failed\n");
}
AddrRangeMap_Init (functionMap);
}
static void
FunctionInfo_Destroy (void *node)
{
struct FunctionInfo *fi = (struct FunctionInfo *) node;
assert (fi != NULL);
free (fi->name);
free (fi);
}
static void
FunctionMap_Destroy (void)
{
if (functionMap != NULL)
{
AddrRangeMap_Destroy (functionMap, FunctionInfo_Destroy);
functionMap = NULL;
}
}
void
FunctionMap_Add (const char *funcName,
Dwarf_Addr lowAddress, Dwarf_Addr highAddress)
{
/* build a new entry for this function */
struct FunctionInfo *newEntry =
(struct FunctionInfo *) malloc (sizeof (struct FunctionInfo));
if (newEntry == NULL)
{
mpiPi_abort ("malloc failed\n");
}
mpiPi_msg_debug ("FunctionMap::Add: %s [0x%016llx,0x%016llx]\n",
funcName, lowAddress, highAddress);
newEntry->name = strdup (funcName);
if (newEntry->name == NULL)
{
mpiPi_abort ("malloc failed\n");
}
/* add the function to our ordered collection of functions */
assert (functionMap != NULL);
AddrRangeMap_Add (functionMap, lowAddress, highAddress, newEntry);
}
static const struct FunctionInfo *
FunctionMap_Find (void *addr)
{
assert (functionMap != NULL);
return (const struct FunctionInfo *) AddrRangeMap_Find (functionMap, addr);
}
/*----------------------------------------------------------------------------
* DWARF source lookup functions
*
*/
static Dwarf_Debug dwHandle = NULL; /* DWARF library session handle */
static int dwFd = -1; /* file descriptor for executable */
static void
HandleFunctionDIE (Dwarf_Debug dwHandle, Dwarf_Die currChildDIE)
{
char *funcName = NULL;
Dwarf_Addr lowAddress = 0;
Dwarf_Addr highAddress = 0;
int dwDieNameRet, dwDieLowAddrRet, dwDieHighAddrRet;
dwDieNameRet = dwarf_diename (currChildDIE,
&funcName,
&dw_err);
if (dwDieNameRet != DW_DLV_OK)
mpiPi_msg_debug("Failed to get DIE name : %s\n", dwarf_errmsg(dw_err));
dwDieLowAddrRet = dwarf_lowpc (currChildDIE,
&lowAddress,
&dw_err);
if (dwDieLowAddrRet != DW_DLV_OK)
mpiPi_msg_debug("Failed to get low PC : %s\n", dwarf_errmsg(dw_err));
dwDieHighAddrRet = dwarf_highpc (currChildDIE,
&highAddress,
&dw_err);
if (dwDieHighAddrRet != DW_DLV_OK)
mpiPi_msg_debug("Failed to get high PC : %s\n", dwarf_errmsg(dw_err));
if ((dwDieNameRet == DW_DLV_OK) &&
(dwDieLowAddrRet == DW_DLV_OK) && (dwDieHighAddrRet == DW_DLV_OK))
{
FunctionMap_Add (funcName, lowAddress, highAddress);
}
if ((dwDieNameRet == DW_DLV_OK) && (funcName != NULL))
{
dwarf_dealloc (dwHandle, funcName, DW_DLA_STRING);
}
}
int
open_dwarf_executable (char *fileName)
{
int dwStatus = -1;
mpiPi_msg_debug ("enter open_dwarf_executable\n");
if (fileName == NULL)
{
mpiPi_msg_warn ("Executable file name is NULL!\n");
mpiPi_msg_warn
("If this is a Fortran application, you may be using the incorrect mpiP library.\n");
}
/* open the executable */
assert (dwFd == -1);
mpiPi_msg_debug ("opening file %s\n", fileName);
dwFd = open (fileName, O_RDONLY);
if (dwFd == -1)
{
mpiPi_msg_warn ("could not open file %s\n", fileName);
return 0;
}
/* initialize the DWARF library */
assert (dwHandle == NULL);
dwStatus = dwarf_init (dwFd, /* exe file descriptor */
DW_DLC_READ, /* desired access */
NULL, /* error handler */
NULL, /* error argument */
&dwHandle, /* session handle */
&dw_err); /* error object */
if (dwStatus == DW_DLV_ERROR)
{
close (dwFd);
dwFd = -1;
mpiPi_abort ("could not initialize DWARF library : %s\n", dwarf_errmsg(dw_err));
}
if (dwStatus == DW_DLV_NO_ENTRY)
{
mpiPi_msg_warn ("No symbols in the executable\n");
return 0;
}
/* initialize our function and addr-to-source mappings */
AddrToSourceMap_Init ();
FunctionMap_Init ();
/* access symbol info */
while (1)
{
Dwarf_Unsigned nextCompilationUnitHeaderOffset = 0;
Dwarf_Die currCompileUnitDIE = NULL;
Dwarf_Line *lineEntries = NULL;
Dwarf_Signed nLineEntries = 0;
Dwarf_Half currDIETag;
Dwarf_Die currChildDIE = NULL;
Dwarf_Die nextChildDIE = NULL;
Dwarf_Die oldChildDIE = NULL;
/* access next compilation unit header */
dwStatus = dwarf_next_cu_header (dwHandle, NULL, /* cu_header_length */
NULL, /* version_stamp */
NULL, /* abbrev_offset */
NULL, /* address_size */
&nextCompilationUnitHeaderOffset, &dw_err); /* error object */
if (dwStatus != DW_DLV_OK)
{
if (dwStatus != DW_DLV_NO_ENTRY)
{
mpiPi_abort ("failed to access next DWARF cu header : %s\n", dwarf_errmsg(dw_err));
}
break;
}
/* access the first debug info entry (DIE) for this computation unit */
dwStatus = dwarf_siblingof (dwHandle, NULL, /* current DIE */
&currCompileUnitDIE, /* sibling DIE */
&dw_err); /* error object */
if (dwStatus != DW_DLV_OK)
{
mpiPi_abort ("failed to access first DWARF DIE : %s\n", dwarf_errmsg(dw_err));
}
/* get line number information for this compilation
* unit, if available
*/
dwStatus = dwarf_srclines (currCompileUnitDIE,
&lineEntries, &nLineEntries, &dw_err);
if (dwStatus == DW_DLV_OK)
{
unsigned int i;
/*
* Extract and save address-to-source line mapping.
*
* NOTE: At least on the Cray X1, we see line number
* information with the same address mapping to different lines.
* It seems like when there are multiple entries for a given
* address, the last one is the correct one. (Needs verification.)
* If we see multiple entries for a given address, we only
* save the last one.
*/
for (i = 0; i < nLineEntries; i++)
{
Dwarf_Unsigned lineNumber = 0;
Dwarf_Addr lineAddress = 0;
char *lineSourceFile = NULL;
int lineNoStatus, lineAddrStatus, lineSrcFileStatus;
lineNoStatus = dwarf_lineno (lineEntries[i],
&lineNumber,
&dw_err);
if (lineNoStatus != DW_DLV_OK)
mpiPi_msg_debug("Failed to get line number : %s\n", dwarf_errmsg(dw_err));
lineAddrStatus = dwarf_lineaddr (lineEntries[i],
&lineAddress,
&dw_err);
if (lineAddrStatus != DW_DLV_OK)
mpiPi_msg_debug("Failed to get line address : %s\n", dwarf_errmsg(dw_err));
lineSrcFileStatus = dwarf_linesrc (lineEntries[i],
&lineSourceFile,
&dw_err);
if (lineSrcFileStatus != DW_DLV_OK)
mpiPi_msg_debug("Failed to get source file status : %s\n", dwarf_errmsg(dw_err));
if ((lineNoStatus == DW_DLV_OK) &&
(lineAddrStatus == DW_DLV_OK)
&& (lineSrcFileStatus == DW_DLV_OK))
{
int saveCurrentEntry = 0; /* bool */
if (i < (nLineEntries - 1))
{
/*
* We're not on the last line number entry -
* check the address associated with the next
* entry to see if it differs from this one.
* Only save the entry if it the next address
* differs.
*/
Dwarf_Addr nextLineAddress = 0;
int nextLineAddrStatus =
dwarf_lineaddr (lineEntries[i + 1],
&nextLineAddress,
&dw_err);
assert (nextLineAddrStatus == DW_DLV_OK);
if (nextLineAddress != lineAddress)
{
saveCurrentEntry = 1;
}
}
else
{
/* we're on the last line number entry */
saveCurrentEntry = 1;
}
if (saveCurrentEntry)
{
/* save the mapping entry */
AddrToSourceMap_Add (lineAddress, lineSourceFile,
lineNumber);
}
}
if (lineSourceFile != NULL)
{
dwarf_dealloc (dwHandle, lineSourceFile, DW_DLA_STRING);
}
}