1 /*
2 * Copyright (C) 2024 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 #include "src/trace_processor/importers/etm/file_tracker.h"
18
19 #include <cstdint>
20 #include <optional>
21
22 #include "perfetto/base/logging.h"
23 #include "perfetto/base/status.h"
24 #include "perfetto/trace_processor/trace_blob_view.h"
25
26 #include "src/trace_processor/importers/etm/elf_tracker.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28 #include "src/trace_processor/tables/metadata_tables_py.h"
29 #include "src/trace_processor/types/trace_processor_context.h"
30 #include "src/trace_processor/util/build_id.h"
31
32 namespace perfetto::trace_processor::etm {
33
34 FileTracker::~FileTracker() = default;
35
AddFile(const std::string & name,TraceBlobView data)36 base::Status FileTracker::AddFile(const std::string& name, TraceBlobView data) {
37 StringId name_id = context_->storage->InternString(name);
38 auto it = files_by_path_.Find(name_id);
39 if (it) {
40 return base::ErrStatus("Duplicate file: %s", name.c_str());
41 }
42 auto file_id = context_->storage->mutable_file_table()
43 ->Insert({name_id, static_cast<int64_t>(data.size())})
44 .id;
45 files_by_path_.Insert(name_id, file_id);
46
47 PERFETTO_CHECK(file_content_.size() == file_id.value);
48 file_content_.push_back(std::move(data));
49
50 IndexFileType(file_id, file_content_.back());
51
52 return base::OkStatus();
53 }
54
IndexFileType(tables::FileTable::Id file_id,const TraceBlobView & content)55 void FileTracker::IndexFileType(tables::FileTable::Id file_id,
56 const TraceBlobView& content) {
57 if (ElfTracker::GetOrCreate(context_)->ProcessFile(file_id, content)) {
58 return;
59 }
60 }
61
62 } // namespace perfetto::trace_processor::etm
63