1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef TRACE_DATA_DB_H
17 #define TRACE_DATA_DB_H
18 
19 #include <functional>
20 #include <list>
21 #include <map>
22 #include <memory>
23 #include <set>
24 #include <string>
25 #include <unordered_map>
26 #include <vector>
27 #include "sqlite3.h"
28 #include "sqllite_prepar_cache_data.h"
29 
30 struct ElfSymbolTable {
31     uint64_t filePathIndex;
32     uint64_t textVaddr;
33     uint32_t textOffset;
34     uint32_t symEntSize;
35     std::string strTable;
36     std::string symTable;
37 };
38 
39 namespace SysTuning {
40 namespace TraceStreamer {
41 constexpr int32_t DATABASE_BASE = (1U << 20);
42 class TraceDataDB {
43 public:
44     TraceDataDB();
45     TraceDataDB(const TraceDataDB &) = delete;
46     TraceDataDB &operator=(const TraceDataDB &) = delete;
47     virtual ~TraceDataDB();
48     void Prepare();
49 
50 public:
51     using ResultCallBack = std::function<void(const std::string & /* json or proto result */, int32_t)>;
52     int32_t ExportDatabase(const std::string &outputName, ResultCallBack resultCallBack = nullptr);
53     int32_t BatchExportDatabase(const std::string &outputName);
54     int32_t CreatEmptyBatchDB(const std::string &outputName);
55     void RevertTableName(const std::string &outputName);
56     void CloseBatchDB();
57     std::vector<std::string> SearchData();
58     int32_t OperateDatabase(const std::string &sql);
59     int32_t SearchDatabase(const std::string &sql, ResultCallBack resultCallBack);
60     int32_t SearchDatabase(const std::string &sql, uint8_t *out, int32_t outLen);
61     int32_t SearchDatabase(std::string &sql, bool print);
62     int32_t SearchDatabaseToProto(const std::string &data, SqllitePreparCacheData::TLVResultCallBack resultCallBack);
63     std::string SearchDatabase(const std::string &sql);
64     void SetCancel(bool cancel);
65     void AppendNewTable(std::string tableName);
66     void EnableMetaTable(bool enabled);
Cancel() const67     bool Cancel() const
68     {
69         return cancelQuery_;
70     }
71 
72 public:
73     sqlite3 *db_;
74 
75 protected:
76     std::unordered_map<std::string, size_t> tableToCompletedSize_;
77 
78 private:
79     void ExecuteSql(const std::string_view &sql);
80     void SendDatabase(ResultCallBack resultCallBack);
81     void ParseCommandLine(std::string &option, std::string line, std::vector<std::string> &values);
82     void PrintSearchResult(std::string line, bool printResult);
83     int32_t HandleColumnNames(sqlite3_stmt *stmt, char *res, int32_t outLen, int32_t pos, int32_t colCount);
84     int32_t HandleRowData(sqlite3_stmt *stmt, char *res, int32_t outLen, int32_t pos, int32_t colCount);
85     static void GetRowString(sqlite3_stmt *stmt, int32_t colCount, std::string &rowStr);
86     static void SqliteFinalize(sqlite3_stmt *ptr);
87     void InitTableToCompletedSize();
88 
89 private:
90     std::list<std::string> internalTables_ = {};
91     bool exportMetaTable_ = true;
92     bool pared_ = false;
93     bool cancelQuery_ = false;
94     std::string wasmDBName_;
95     SqllitePreparCacheData sqlPreparCacheData_;
96     std::set<std::string> needClearTable_ = {"data_type", "device_info",  "data_dict", "meta",    "clock_snapshot",
97                                              "callstack", "thread_state", "stat",      "symbols", "thread",
98                                              "process",   "trace_range",  "args_view"};
99 };
100 } // namespace TraceStreamer
101 } // namespace SysTuning
102 #endif
103