• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
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 <memory>
22 #include <string>
23 #include <vector>
24 #include "sqlite3.h"
25 
26 struct ElfSymbolTable {
27     uint64_t filePathIndex;
28     uint64_t textVaddr;
29     uint32_t textOffset;
30     uint32_t symEntSize;
31     std::string strTable;
32     std::string symTable;
33 };
34 
35 namespace SysTuning {
36 namespace TraceStreamer {
37 const int32_t SEND_CONTINUE = 0;
38 const int32_t SEND_FINISH = 1;
39 constexpr int32_t DATABASE_BASE = (1U << 20);
40 class TraceDataDB {
41 public:
42     TraceDataDB();
43     TraceDataDB(const TraceDataDB&) = delete;
44     TraceDataDB& operator=(const TraceDataDB&) = delete;
45     virtual ~TraceDataDB();
46     virtual void InitDB() = 0;
47     void Prepare();
48 
49 public:
50     using ResultCallBack = std::function<void(const std::string /* json result */, int32_t)>;
51     int32_t ExportDatabase(const std::string& outputName, ResultCallBack resultCallBack = nullptr);
52     std::vector<std::string> SearchData();
53     int32_t OperateDatabase(const std::string& sql);
54     int32_t SearchDatabase(const std::string& sql, ResultCallBack resultCallBack);
55     int32_t SearchDatabase(const std::string& sql, uint8_t* out, int32_t outLen);
56     int32_t SearchDatabase(const std::string& sql, bool print);
57     void SetCancel(bool cancel);
58     void AppendNewTable(std::string tableName);
59     void EnableMetaTable(bool enabled);
Cancel()60     bool Cancel() const
61     {
62         return cancelQuery_;
63     }
64 
65 public:
66     sqlite3* db_;
67 
68 private:
69     void ExecuteSql(const std::string_view& sql);
70     void SendDatabase(ResultCallBack resultCallBack);
71     static void GetRowString(sqlite3_stmt* stmt, int32_t colCount, std::string& rowStr);
72     std::list<std::string> internalTables_ = {};
73     bool exportMetaTable_ = true;
74     bool pared_ = false;
75     bool cancelQuery_ = false;
76     std::string wasmDBName_;
77 };
78 } // namespace TraceStreamer
79 } // namespace SysTuning
80 #endif
81