xref: /aosp_15_r20/external/perfetto/src/trace_processor/perfetto_sql/intrinsics/functions/import.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2021 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/perfetto_sql/intrinsics/functions/import.h"
18 
19 #include <cstddef>
20 
21 #include "perfetto/base/status.h"
22 #include "perfetto/ext/base/string_utils.h"
23 #include "perfetto/trace_processor/basic_types.h"
24 #include "src/trace_processor/sqlite/sql_source.h"
25 #include "src/trace_processor/sqlite/sqlite_utils.h"
26 
27 namespace perfetto::trace_processor {
28 
Run(Import::Context * ctx,size_t argc,sqlite3_value ** argv,SqlValue &,Destructors &)29 base::Status Import::Run(Import::Context* ctx,
30                          size_t argc,
31                          sqlite3_value** argv,
32                          SqlValue&,
33                          Destructors&) {
34   if (argc != 1) {
35     return base::ErrStatus(
36         "IMPORT: invalid number of args; expected 1, received "
37         "%zu",
38         argc);
39   }
40   sqlite3_value* import_val = argv[0];
41 
42   // Type check
43   {
44     base::Status status = sqlite::utils::TypeCheckSqliteValue(
45         import_val, SqlValue::Type::kString);
46     if (!status.ok()) {
47       return base::ErrStatus("IMPORT(%s): %s", sqlite3_value_text(import_val),
48                              status.c_message());
49     }
50   }
51 
52   const char* import_key =
53       reinterpret_cast<const char*>(sqlite3_value_text(import_val));
54   base::StackString<1024> create("INCLUDE PERFETTO MODULE %s;", import_key);
55   return ctx->engine
56       ->Execute(
57           SqlSource::FromTraceProcessorImplementation(create.ToStdString()))
58       .status();
59 }
60 
61 }  // namespace perfetto::trace_processor
62