xref: /aosp_15_r20/external/stg/input.cc (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2022-2023 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License.  You may obtain a copy of the License at
9 //
10 //     https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Giuliano Procida
19 
20 #include "input.h"
21 
22 #include <memory>
23 #include <sstream>
24 
25 #include "abigail_reader.h"
26 #include "btf_reader.h"
27 #include "elf_dwarf_handle.h"
28 #include "elf_reader.h"
29 #include "error.h"
30 #include "filter.h"
31 #include "graph.h"
32 #include "proto_reader.h"
33 #include "reader_options.h"
34 #include "runtime.h"
35 
36 namespace stg {
37 
38 namespace {
39 
ReadInternal(Runtime & runtime,Graph & graph,InputFormat format,const char * input,ReadOptions options,const std::unique_ptr<Filter> & file_filter)40 Id ReadInternal(Runtime& runtime, Graph& graph, InputFormat format,
41                 const char* input, ReadOptions options,
42                 const std::unique_ptr<Filter>& file_filter) {
43   switch (format) {
44     case InputFormat::ABI: {
45       const Time read(runtime, "read ABI");
46       return abixml::Read(runtime, graph, input);
47     }
48     case InputFormat::BTF: {
49       const Time read(runtime, "read BTF");
50       return btf::ReadFile(graph, input, options);
51     }
52     case InputFormat::ELF: {
53       const Time read(runtime, "read ELF");
54       ElfDwarfHandle elf_dwarf_handle(input);
55       return elf::Read(runtime, graph, elf_dwarf_handle, options, file_filter);
56     }
57     case InputFormat::STG: {
58       const Time read(runtime, "read STG");
59       return proto::Read(runtime, graph, input);
60     }
61   }
62 }
63 
64 }  // namespace
65 
Read(Runtime & runtime,Graph & graph,InputFormat format,const char * input,ReadOptions options,const std::unique_ptr<Filter> & file_filter)66 Id Read(Runtime& runtime, Graph& graph, InputFormat format, const char* input,
67         ReadOptions options, const std::unique_ptr<Filter>& file_filter) {
68   try {
69     return ReadInternal(runtime, graph, format, input, options, file_filter);
70   } catch (Exception& e) {
71     std::ostringstream os;
72     os << "processing file '" << input << '\'';
73     e.Add(os.str());
74     throw;
75   }
76 }
77 
78 }  // namespace stg
79