@@ -15,32 +15,10 @@ void DataAccessPerfReader::parsePerfTraces() {
1515 parsePerfTrace (PerfTraceFilename);
1616}
1717
18- static void testPerfSampleRecordRegex () {
19- std::regex logRegex (
20- R"( ^.*?PERF_RECORD_SAMPLE\(.*?\):\s*(\d+)\/(\d+):\s*(0x[0-9a-fA-F]+)\s+period:\s*\d+\s+addr:\s*(0x[0-9a-fA-F]+)$)" );
21-
22- std::smatch testMatch;
23- const std::string testLine =
24- " 2193330181938979 0xa88 [0x48]: PERF_RECORD_SAMPLE(IP, 0x4002): "
25- " 1807344/1807344: 0x260b45 period: 100 addr: 0x200630" ;
26- if (std::regex_search (testLine, testMatch, logRegex)) {
27- if (testMatch.size () != 5 ) {
28- exitWithError (" Regex did not match expected number of groups." );
29- }
30- for (size_t i = 0 ; i < testMatch.size (); ++i) {
31- errs () << " Group " << i << " : " << testMatch[i] << " \n " ;
32- }
33- // errs() << "Test line matched successfully.\n";
34- } else {
35- exitWithError (" Test line did not match regex." );
36- }
37- }
38-
3918// Ignore mmap events.
4019void DataAccessPerfReader::parsePerfTrace (StringRef PerfTrace) {
4120 std::regex logRegex (
4221 R"( ^.*?PERF_RECORD_SAMPLE\(.*?\):\s*(\d+)\/(\d+):\s*(0x[0-9a-fA-F]+)\s+period:\s*\d+\s+addr:\s*(0x[0-9a-fA-F]+)$)" );
43- uint64_t UnmatchedLine = 0 , MatchedLine = 0 ;
4422
4523 auto BufferOrErr = MemoryBuffer::getFile (PerfTrace);
4624 std::error_code EC = BufferOrErr.getError ();
@@ -51,18 +29,23 @@ void DataAccessPerfReader::parsePerfTrace(StringRef PerfTrace) {
5129 for (; !LineIt.is_at_eof (); ++LineIt) {
5230 StringRef Line = *LineIt;
5331
32+ // Parse MMAP event from perf trace.
5433 // Parse MMAP event from perf trace.
5534 // Construct a binary from the binary file path.
5635 PerfScriptReader::MMapEvent MMap;
5736 if (Line.contains (" PERF_RECORD_MMAP2" )) {
5837 if (PerfScriptReader::extractMMapEventForBinary (Binary, Line, MMap)) {
59- errs () << " MMap event found: "
60- << " PID: " << MMap.PID
61- << " , Address: " << format (" 0x%llx" , MMap.Address )
62- << " , Size: " << MMap.Size << " , Offset: " << MMap.Offset
63- << " , Binary Path: " << MMap.BinaryPath << " \n " ;
38+ // TODO: This is a hack to avoid mapping binary address for data section
39+ // mappings.
6440 if (MMap.Offset == 0 ) {
6541 updateBinaryAddress (MMap);
42+ errs () << " Binary base address is "
43+ << format (" 0x%" PRIx64, Binary->getBaseAddress ())
44+ << " and preferred base address is "
45+ << format (" 0x%" PRIx64, Binary->getPreferredBaseAddress ())
46+ << " and first loadable address is "
47+ << format (" 0x%" PRIx64, Binary->getFirstLoadableAddress ())
48+ << " \n " ;
6649 }
6750 }
6851 continue ;
@@ -72,14 +55,6 @@ void DataAccessPerfReader::parsePerfTrace(StringRef PerfTrace) {
7255 // Skip lines that do not contain "PERF_RECORD_SAMPLE".
7356 continue ;
7457 }
75- // errs() << "Processing line: " << Line << "\n";
76-
77- // if (IPSampleRegex.match(Line, &Matches)) {
78- // errs() << "IP Captured: " << Matches.size() << "\n";
79- // }
80- // if (DataAddressRegex.match(Line, &Matches)) {
81- // errs() << "Data Address Captured: " << Matches.size() << "\n";
82- // }
8358
8459 std::smatch matches;
8560 const std::string LineStr = Line.str ();
@@ -89,41 +64,29 @@ void DataAccessPerfReader::parsePerfTrace(StringRef PerfTrace) {
8964 continue ;
9065
9166 uint64_t DataAddress = std::stoull (matches[4 ].str (), nullptr , 16 );
92- uint64_t IP = std::stoull (matches[3 ].str (), nullptr , 16 );
67+
68+ // Skip addresses out of the specified PT_LOAD section for data.
69+ if (DataAddress < DataMMap.Address ||
70+ DataAddress >= DataMMap.Address + DataMMap.Size )
71+ continue ;
72+
9373 int32_t PID = std::stoi (matches[1 ].str ());
94- // if (DataAddress == 0x200630) {
95- // errs() << "Find data address at 0x200630, IP: " << format("0x%llx",
96- // IP)
97- // << " pid is " << PID << "\n";
98- // }
99-
100- // errs() << matches.size() << " matches found in line: " << LineStr <<
101- // "\n"; for (const auto &Match : matches) {
102- // errs() << "Match: " << Match.str() << "\n";
103- // }
10474 // Check if the PID matches the filter.
10575
10676 if (PIDFilter && *PIDFilter != PID) {
10777 continue ;
10878 }
10979
80+ uint64_t IP = std::stoull (matches[3 ].str (), nullptr , 16 );
11081 // Extract the address and count.
111-
11282 uint64_t CanonicalDataAddress =
113- Binary->canonicalizeVirtualAddress (DataAddress);
114- // errs() << "Data address is " << format("0x" PRIx64 ":", DataAddress)
115- // << " Canonical data address is "
116- // << format("0x" PRIx64 ":", CanonicalDataAddress) << "\n";
117- AddressToCount[CanonicalDataAddress] += 1 ;
118- MatchedLine++;
119- } else {
120- // errs() << "\tNo match found for line: " << Line << "\n";
121- UnmatchedLine++;
83+ canonicalizeDataAddress (DataAddress, *Binary, DataMMap, DataSegment);
84+
85+ uint64_t CanonicalIPAddress = Binary->canonicalizeVirtualAddress (IP);
86+
87+ AddressMap[CanonicalIPAddress][CanonicalDataAddress] += 1 ;
12288 }
12389 }
124-
125- errs () << " Total unmatched lines: " << UnmatchedLine << " \t "
126- << " Matched lines: " << MatchedLine << " \n " ;
12790}
12891
12992} // namespace llvm
0 commit comments