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/macros.h"
30 #include "base/os.h"
31 #include "base/unix_file/fd_file.h"
32 #include "common_compiler_test.h"
33 #include "elf/elf_builder.h"
34 #include "gtest/gtest.h"
35 #include "stream/file_output_stream.h"
36 
37 namespace art HIDDEN {
38 namespace dwarf {
39 
40 #define DW_CHECK(substring) Check(substring, false, __FILE__, __LINE__)
41 #define DW_CHECK_NEXT(substring) Check(substring, true, __FILE__, __LINE__)
42 
43 class DwarfTest : public CommonCompilerTest {
44  public:
45   static constexpr bool kPrintObjdumpOutput = false;  // debugging.
46 
47   struct ExpectedLine {
48     std::string substring;
49     bool next;
50     const char* at_file;
51     int at_line;
52   };
53 
54   // Check that the objdump output contains given output.
55   // 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)56   void Check(const char* substr, bool next, const char* at_file, int at_line) {
57     expected_lines_.push_back(ExpectedLine {substr, next, at_file, at_line});
58   }
59 
60   // Pretty-print the generated DWARF data using objdump.
61   template<typename ElfTypes>
Objdump(const char * args)62   std::vector<std::string> Objdump(const char* args) {
63     // Write simple elf file with just the DWARF sections.
64     InstructionSet isa =
65         (sizeof(typename ElfTypes::Addr) == 8) ? InstructionSet::kX86_64 : InstructionSet::kX86;
66     ScratchFile file;
67     FileOutputStream output_stream(file.GetFile());
68     ElfBuilder<ElfTypes> builder(isa, &output_stream);
69     builder.Start();
70     if (!debug_info_data_.empty()) {
71       builder.WriteSection(".debug_info", &debug_info_data_);
72     }
73     if (!debug_abbrev_data_.empty()) {
74       builder.WriteSection(".debug_abbrev", &debug_abbrev_data_);
75     }
76     if (!debug_str_data_.empty()) {
77       builder.WriteSection(".debug_str", &debug_str_data_);
78     }
79     if (!debug_line_data_.empty()) {
80       builder.WriteSection(".debug_line", &debug_line_data_);
81     }
82     if (!debug_frame_data_.empty()) {
83       builder.WriteSection(".debug_frame", &debug_frame_data_);
84     }
85     builder.End();
86     EXPECT_TRUE(builder.Good());
87 
88     // Read the elf file back using objdump.
89     std::vector<std::string> lines;
90     std::string cmd = GetAndroidTool("llvm-dwarfdump");
91     cmd = cmd + " " + args + " " + file.GetFilename() + " 2>&1";
92     FILE* output = popen(cmd.data(), "r");
93     char buffer[1024];
94     const char* line;
95     while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
96       if (kPrintObjdumpOutput) {
97         printf("%s", line);
98       }
99       if (line[0] != '\0' && line[0] != '\n') {
100         EXPECT_TRUE(strstr(line, "error:") == nullptr) << line;
101         EXPECT_TRUE(strstr(line, "warning:") == nullptr) << line;
102         std::string str(line);
103         if (str.back() == '\n') {
104           str.pop_back();
105         }
106         std::replace(str.begin(), str.end(), '\t', ' ');
107         lines.push_back(str);
108       }
109     }
110     pclose(output);
111     return lines;
112   }
113 
Objdump(bool is64bit,const char * args)114   std::vector<std::string> Objdump(bool is64bit, const char* args) {
115     if (is64bit) {
116       return Objdump<ElfTypes64>(args);
117     } else {
118       return Objdump<ElfTypes32>(args);
119     }
120   }
121 
122   // Compare objdump output to the recorded checks.
CheckObjdumpOutput(bool is64bit,const char * args)123   void CheckObjdumpOutput(bool is64bit, const char* args) {
124     std::vector<std::string> actual_lines = Objdump(is64bit, args);
125     auto actual_line = actual_lines.begin();
126     bool failed = false;
127     for (const ExpectedLine& expected : expected_lines_) {
128       const std::string& substring = expected.substring;
129       auto it = std::find_if(actual_line, actual_lines.end(),
130           [&](const std::string& line) { return line.find(substring) != std::string::npos; });
131       if (it == actual_lines.end()) {
132         failed = true;
133         ADD_FAILURE_AT(expected.at_file, expected.at_line) << "'" << substring << "' not found.";
134       } else {
135         if (expected.next && it != actual_line) {
136           failed = true;
137           ADD_FAILURE_AT(expected.at_file, expected.at_line)
138               << "'" << substring << "' found, but not on the immediate next line as expected.";
139         }
140         actual_line = ++it;
141       }
142     }
143     if (failed) {
144       LOG(ERROR) << "objdump output:";
145       for (const std::string& it : actual_lines) {
146         LOG(ERROR) << it;
147       }
148     }
149   }
150 
151   // Buffers which are going to assembled into ELF file and passed to objdump.
152   std::vector<uint8_t> debug_frame_data_;
153   std::vector<uint8_t> debug_info_data_;
154   std::vector<uint8_t> debug_abbrev_data_;
155   std::vector<uint8_t> debug_str_data_;
156   std::vector<uint8_t> debug_line_data_;
157 
158   // The expected output of objdump.
159   std::vector<ExpectedLine> expected_lines_;
160 };
161 
162 }  // namespace dwarf
163 }  // namespace art
164 
165 #endif  // ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_
166