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 <map> 22 #include <memory> 23 #include <set> 24 #include <string> 25 #include <vector> 26 #include "sqlite3.h" 27 #include "sqllite_prepar_cache_data.h" 28 29 struct ElfSymbolTable { 30 uint64_t filePathIndex; 31 uint64_t textVaddr; 32 uint32_t textOffset; 33 uint32_t symEntSize; 34 std::string strTable; 35 std::string symTable; 36 }; 37 38 namespace SysTuning { 39 namespace TraceStreamer { 40 constexpr int32_t DATABASE_BASE = (1U << 20); 41 class TraceDataDB { 42 public: 43 TraceDataDB(); 44 TraceDataDB(const TraceDataDB&) = delete; 45 TraceDataDB& operator=(const TraceDataDB&) = delete; 46 virtual ~TraceDataDB(); 47 void Prepare(); 48 49 public: 50 using ResultCallBack = std::function<void(const std::string& /* json or proto result */, int32_t)>; 51 int32_t ExportDatabase(const std::string& outputName, ResultCallBack resultCallBack = nullptr); 52 int32_t BatchExportDatabase(const std::string& outputName); 53 int32_t CreatEmptyBatchDB(const std::string& outputName); 54 void RevertTableName(const std::string& outputName); 55 void CloseBatchDB(); 56 std::vector<std::string> SearchData(); 57 int32_t OperateDatabase(const std::string& sql); 58 int32_t SearchDatabase(const std::string& sql, ResultCallBack resultCallBack); 59 int32_t SearchDatabase(const std::string& sql, uint8_t* out, int32_t outLen); 60 int32_t SearchDatabase(std::string& sql, bool print); 61 int32_t SearchDatabaseToProto(const std::string& data, SqllitePreparCacheData::TLVResultCallBack resultCallBack); 62 std::string SearchDatabase(const std::string& sql); 63 void SetCancel(bool cancel); 64 void AppendNewTable(std::string tableName); 65 void EnableMetaTable(bool enabled); Cancel()66 bool Cancel() const 67 { 68 return cancelQuery_; 69 } 70 71 public: 72 sqlite3* db_; 73 74 private: 75 void ExecuteSql(const std::string_view& sql); 76 void SendDatabase(ResultCallBack resultCallBack); 77 void ParseCommandLine(std::string& option, std::string line, std::vector<std::string>& values); 78 void PrintSearchResult(std::string line, bool printResult); 79 int32_t HandleColumnNames(sqlite3_stmt* stmt, char* res, int32_t outLen, int32_t pos, int32_t colCount); 80 int32_t HandleRowData(sqlite3_stmt* stmt, char* res, int32_t outLen, int32_t pos, int32_t colCount); 81 static void GetRowString(sqlite3_stmt* stmt, int32_t colCount, std::string& rowStr); 82 static void SqliteFinalize(sqlite3_stmt* ptr); 83 84 private: 85 std::list<std::string> internalTables_ = {}; 86 bool exportMetaTable_ = true; 87 bool pared_ = false; 88 bool cancelQuery_ = false; 89 std::string wasmDBName_; 90 SqllitePreparCacheData sqlPreparCacheData_; 91 std::set<std::string> needClearTable_ = {"data_type", "device_info", "data_dict", "meta", "stat", 92 "symbols", "thread", "process", "trace_range", "args_view"}; 93 }; 94 } // namespace TraceStreamer 95 } // namespace SysTuning 96 #endif 97