• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Sasha Goldshtein
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 #include <linux/bpf.h>
17 #include <linux/version.h>
18 #include <sys/utsname.h>
19 #include <unistd.h>
20 
21 #include <fstream>
22 #include <regex>
23 
24 #include <clang/AST/ASTConsumer.h>
25 #include <clang/AST/ASTContext.h>
26 #include <clang/AST/RecordLayout.h>
27 #include <clang/Frontend/CompilerInstance.h>
28 #include <clang/Frontend/MultiplexConsumer.h>
29 #include <clang/Rewrite/Core/Rewriter.h>
30 
31 #include "frontend_action_common.h"
32 #include "tp_frontend_action.h"
33 #include "common.h"
34 
35 namespace ebpf {
36 
37 using std::map;
38 using std::set;
39 using std::string;
40 using std::to_string;
41 using std::unique_ptr;
42 using std::vector;
43 using std::regex;
44 using std::smatch;
45 using std::regex_search;
46 using std::istream;
47 using std::ifstream;
48 using namespace clang;
49 
TracepointTypeVisitor(ASTContext & C,Rewriter & rewriter)50 TracepointTypeVisitor::TracepointTypeVisitor(ASTContext &C, Rewriter &rewriter)
51     : diag_(C.getDiagnostics()), rewriter_(rewriter), out_(llvm::errs()) {
52 }
53 
GenerateTracepointStruct(SourceLocation loc,string const & category,string const & event)54 string TracepointTypeVisitor::GenerateTracepointStruct(
55     SourceLocation loc, string const& category, string const& event) {
56   string format_file = "/sys/kernel/debug/tracing/events/" +
57     category + "/" + event + "/format";
58   ifstream input(format_file.c_str());
59   if (!input)
60     return "";
61 
62   return ebpf::parse_tracepoint(input, category, event);
63 }
64 
_is_tracepoint_struct_type(string const & type_name,string & tp_category,string & tp_event)65 static inline bool _is_tracepoint_struct_type(string const& type_name,
66                                               string& tp_category,
67                                               string& tp_event) {
68   // We are looking to roughly match the regex:
69   //    (?:struct|class)\s+tracepoint__(\S+)__(\S+)
70   // Not using std::regex because older versions of GCC don't support it yet.
71   // E.g., the libstdc++ that ships with Ubuntu 14.04.
72 
73   auto first_space_pos = type_name.find_first_of("\t ");
74   if (first_space_pos == string::npos)
75     return false;
76   auto first_tok = type_name.substr(0, first_space_pos);
77   if (first_tok != "struct" && first_tok != "class")
78     return false;
79 
80   auto non_space_pos = type_name.find_first_not_of("\t ", first_space_pos);
81   auto second_space_pos = type_name.find_first_of("\t ", non_space_pos);
82   auto second_tok = type_name.substr(non_space_pos,
83                                      second_space_pos - non_space_pos);
84   if (second_tok.find("tracepoint__") != 0)
85     return false;
86 
87   auto tp_event_pos = second_tok.rfind("__");
88   if (tp_event_pos == string::npos)
89     return false;
90   tp_event = second_tok.substr(tp_event_pos + 2);
91 
92   auto tp_category_pos = second_tok.find("__");
93   if (tp_category_pos == tp_event_pos)
94     return false;
95   tp_category = second_tok.substr(tp_category_pos + 2,
96                                   tp_event_pos - tp_category_pos - 2);
97   return true;
98 }
99 
100 
VisitFunctionDecl(FunctionDecl * D)101 bool TracepointTypeVisitor::VisitFunctionDecl(FunctionDecl *D) {
102   if (D->isExternallyVisible() && D->hasBody()) {
103     // If this function has a tracepoint structure as an argument,
104     // add that structure declaration based on the structure name.
105     for (auto it = D->param_begin(); it != D->param_end(); ++it) {
106       auto arg = *it;
107       auto type = arg->getType();
108       if (type->isPointerType() &&
109           type->getPointeeType()->isStructureOrClassType()) {
110         auto type_name = type->getPointeeType().getAsString();
111         string tp_cat, tp_evt;
112         if (_is_tracepoint_struct_type(type_name, tp_cat, tp_evt)) {
113           string tp_struct = GenerateTracepointStruct(
114               GET_BEGINLOC(D), tp_cat, tp_evt);
115           // Get the actual function declaration point (the macro instantiation
116           // point if using the TRACEPOINT_PROBE macro instead of the macro
117           // declaration point in bpf_helpers.h).
118           auto insert_loc = GET_BEGINLOC(D);
119           insert_loc = rewriter_.getSourceMgr().getFileLoc(insert_loc);
120           rewriter_.InsertText(insert_loc, tp_struct);
121         }
122       }
123     }
124   }
125   return true;
126 }
127 
TracepointTypeConsumer(ASTContext & C,Rewriter & rewriter)128 TracepointTypeConsumer::TracepointTypeConsumer(ASTContext &C, Rewriter &rewriter)
129     : visitor_(C, rewriter) {
130 }
131 
HandleTopLevelDecl(DeclGroupRef Group)132 bool TracepointTypeConsumer::HandleTopLevelDecl(DeclGroupRef Group) {
133   for (auto D : Group)
134     visitor_.TraverseDecl(D);
135   return true;
136 }
137 
TracepointFrontendAction(llvm::raw_ostream & os)138 TracepointFrontendAction::TracepointFrontendAction(llvm::raw_ostream &os)
139     : os_(os), rewriter_(new Rewriter) {
140 }
141 
EndSourceFileAction()142 void TracepointFrontendAction::EndSourceFileAction() {
143   rewriter_->getEditBuffer(rewriter_->getSourceMgr().getMainFileID()).write(os_);
144   os_.flush();
145 }
146 
CreateASTConsumer(CompilerInstance & Compiler,llvm::StringRef InFile)147 unique_ptr<ASTConsumer> TracepointFrontendAction::CreateASTConsumer(
148         CompilerInstance &Compiler, llvm::StringRef InFile) {
149   rewriter_->setSourceMgr(Compiler.getSourceManager(), Compiler.getLangOpts());
150   return unique_ptr<ASTConsumer>(new TracepointTypeConsumer(
151               Compiler.getASTContext(), *rewriter_));
152 }
153 
154 }
155