• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/prelude/functions/create_function_internal.h"
18 
19 #include "perfetto/base/status.h"
20 #include "perfetto/ext/base/string_view.h"
21 #include "src/trace_processor/sqlite/sqlite_utils.h"
22 #include "src/trace_processor/util/status_macros.h"
23 
24 namespace perfetto {
25 namespace trace_processor {
26 
ParseFunctionName(base::StringView raw,base::StringView & out)27 base::Status ParseFunctionName(base::StringView raw, base::StringView& out) {
28   size_t function_name_end = raw.find('(');
29   if (function_name_end == base::StringView::npos)
30     return base::ErrStatus("unable to find bracket starting argument list");
31 
32   base::StringView function_name = raw.substr(0, function_name_end);
33   if (!sql_argument::IsValidName(function_name)) {
34     return base::ErrStatus("function name %s is not alphanumeric",
35                            function_name.ToStdString().c_str());
36   }
37   out = function_name;
38   return base::OkStatus();
39 }
40 
ParsePrototype(base::StringView raw,Prototype & out)41 base::Status ParsePrototype(base::StringView raw, Prototype& out) {
42   // Examples of function prototypes:
43   // ANDROID_SDK_LEVEL()
44   // STARTUP_SLICE(dur_ns INT)
45   // FIND_NEXT_SLICE_WITH_NAME(ts INT, name STRING)
46 
47   base::StringView function_name;
48   RETURN_IF_ERROR(ParseFunctionName(raw, function_name));
49 
50   size_t function_name_end = function_name.size();
51   size_t args_start = function_name_end + 1;
52   size_t args_end = raw.find(')', args_start);
53   if (args_end == base::StringView::npos)
54     return base::ErrStatus("unable to find bracket ending argument list");
55 
56   base::StringView args_str = raw.substr(args_start, args_end - args_start);
57   RETURN_IF_ERROR(sql_argument::ParseArgumentDefinitions(args_str.ToStdString(),
58                                                          out.arguments));
59 
60   out.function_name = function_name.ToStdString();
61   return base::OkStatus();
62 }
63 
SqliteRetToStatus(sqlite3 * db,const std::string & function_name,int ret)64 base::Status SqliteRetToStatus(sqlite3* db,
65                                const std::string& function_name,
66                                int ret) {
67   if (ret != SQLITE_ROW && ret != SQLITE_DONE) {
68     return base::ErrStatus("%s: SQLite error while executing function body: %s",
69                            function_name.c_str(), sqlite3_errmsg(db));
70   }
71   return base::OkStatus();
72 }
73 
MaybeBindArgument(sqlite3_stmt * stmt,const std::string & function_name,const sql_argument::ArgumentDefinition & arg,sqlite3_value * value)74 base::Status MaybeBindArgument(sqlite3_stmt* stmt,
75                                const std::string& function_name,
76                                const sql_argument::ArgumentDefinition& arg,
77                                sqlite3_value* value) {
78   int index = sqlite3_bind_parameter_index(stmt, arg.dollar_name().c_str());
79 
80   // If the argument is not in the query, this just means its an unused
81   // argument which we can just ignore.
82   if (index == 0)
83     return base::Status();
84 
85   int ret = sqlite3_bind_value(stmt, index, value);
86   if (ret != SQLITE_OK) {
87     return base::ErrStatus(
88         "%s: SQLite error while binding value to argument %s: %s",
89         function_name.c_str(), arg.name().c_str(),
90         sqlite3_errmsg(sqlite3_db_handle(stmt)));
91   }
92   return base::OkStatus();
93 }
94 
95 }  // namespace trace_processor
96 }  // namespace perfetto
97