1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_
18 #define ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_
19 
20 #include <dirent.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 
24 #include <cstring>
25 #include <memory>
26 #include <set>
27 #include <string>
28 
29 #include "base/os.h"
30 #include "base/unix_file/fd_file.h"
31 #include "common_compiler_test.h"
32 #include "elf/elf_builder.h"
33 #include "gtest/gtest.h"
34 #include "stream/file_output_stream.h"
35 
36 namespace art {
37 namespace dwarf {
38 
39 #define DW_CHECK(substring) Check(substring, false, __FILE__, __LINE__)
40 #define DW_CHECK_NEXT(substring) Check(substring, true, __FILE__, __LINE__)
41 
42 class DwarfTest : public CommonCompilerTest {
43  public:
44   static constexpr bool kPrintObjdumpOutput = false;  // debugging.
45 
46   struct ExpectedLine {
47     std::string substring;
48     bool next;
49     const char* at_file;
50     int at_line;
51   };
52 
53   // Check that the objdump output contains given output.
54   // If next is true, it must be the next line.  Otherwise lines are skipped.
Check(const char * substr,bool next,const char * at_file,int at_line)55   void Check(const char* substr, bool next, const char* at_file, int at_line) {
56     expected_lines_.push_back(ExpectedLine {substr, next, at_file, at_line});
57   }
58 
59   // Pretty-print the generated DWARF data using objdump.
60   template<typename ElfTypes>
Objdump(const char * args)61   std::vector<std::string> Objdump(const char* args) {
62     // Write simple elf file with just the DWARF sections.
63     InstructionSet isa =
64         (sizeof(typename ElfTypes::Addr) == 8) ? InstructionSet::kX86_64 : InstructionSet::kX86;
65     ScratchFile file;
66     FileOutputStream output_stream(file.GetFile());
67     ElfBuilder<ElfTypes> builder(isa, &output_stream);
68     builder.Start();
69     if (!debug_info_data_.empty()) {
70       builder.WriteSection(".debug_info", &debug_info_data_);
71     }
72     if (!debug_abbrev_data_.empty()) {
73       builder.WriteSection(".debug_abbrev", &debug_abbrev_data_);
74     }
75     if (!debug_str_data_.empty()) {
76       builder.WriteSection(".debug_str", &debug_str_data_);
77     }
78     if (!debug_line_data_.empty()) {
79       builder.WriteSection(".debug_line", &debug_line_data_);
80     }
81     if (!debug_frame_data_.empty()) {
82       builder.WriteSection(".debug_frame", &debug_frame_data_);
83     }
84     builder.End();
85     EXPECT_TRUE(builder.Good());
86 
87     // Read the elf file back using objdump.
88     std::vector<std::string> lines;
89     std::string cmd = GetAndroidTool("llvm-dwarfdump");
90     cmd = cmd + " " + args + " " + file.GetFilename() + " 2>&1";
91     FILE* output = popen(cmd.data(), "r");
92     char buffer[1024];
93     const char* line;
94     while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
95       if (kPrintObjdumpOutput) {
96         printf("%s", line);
97       }
98       if (line[0] != '\0' && line[0] != '\n') {
99         EXPECT_TRUE(strstr(line, "error:") == nullptr) << line;
100         EXPECT_TRUE(strstr(line, "warning:") == nullptr) << line;
101         std::string str(line);
102         if (str.back() == '\n') {
103           str.pop_back();
104         }
105         std::replace(str.begin(), str.end(), '\t', ' ');
106         lines.push_back(str);
107       }
108     }
109     pclose(output);
110     return lines;
111   }
112 
Objdump(bool is64bit,const char * args)113   std::vector<std::string> Objdump(bool is64bit, const char* args) {
114     if (is64bit) {
115       return Objdump<ElfTypes64>(args);
116     } else {
117       return Objdump<ElfTypes32>(args);
118     }
119   }
120 
121   // Compare objdump output to the recorded checks.
CheckObjdumpOutput(bool is64bit,const char * args)122   void CheckObjdumpOutput(bool is64bit, const char* args) {
123     std::vector<std::string> actual_lines = Objdump(is64bit, args);
124     auto actual_line = actual_lines.begin();
125     bool failed = false;
126     for (const ExpectedLine& expected : expected_lines_) {
127       const std::string& substring = expected.substring;
128       auto it = std::find_if(actual_line, actual_lines.end(),
129           [&](const std::string& line) { return line.find(substring) != std::string::npos; });
130       if (it == actual_lines.end()) {
131         failed = true;
132         ADD_FAILURE_AT(expected.at_file, expected.at_line) << "'" << substring << "' not found.";
133       } else {
134         if (expected.next && it != actual_line) {
135           failed = true;
136           ADD_FAILURE_AT(expected.at_file, expected.at_line)
137               << "'" << substring << "' found, but not on the immediate next line as expected.";
138         }
139         actual_line = ++it;
140       }
141     }
142     if (failed) {
143       LOG(ERROR) << "objdump output:";
144       for (const std::string& it : actual_lines) {
145         LOG(ERROR) << it;
146       }
147     }
148   }
149 
150   // Buffers which are going to assembled into ELF file and passed to objdump.
151   std::vector<uint8_t> debug_frame_data_;
152   std::vector<uint8_t> debug_info_data_;
153   std::vector<uint8_t> debug_abbrev_data_;
154   std::vector<uint8_t> debug_str_data_;
155   std::vector<uint8_t> debug_line_data_;
156 
157   // The expected output of objdump.
158   std::vector<ExpectedLine> expected_lines_;
159 };
160 
161 }  // namespace dwarf
162 }  // namespace art
163 
164 #endif  // ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_
165