Vadim Iosevich | e76832c | 2017-11-05 09:09:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #ifndef _LOG_COLLECTOR_H |
| 31 | #define _LOG_COLLECTOR_H |
| 32 | #pragma once |
| 33 | |
| 34 | #include <chrono> |
| 35 | |
| 36 | #include "LogCollectorDefinitions.h" |
| 37 | #include "LogCollectorConfiguration.h" |
| 38 | #include "OsHandler.h" |
| 39 | |
| 40 | class Device; |
| 41 | |
| 42 | namespace log_collector |
| 43 | { |
| 44 | |
| 45 | class LogCollector |
| 46 | { |
| 47 | public: |
| 48 | // initialize a new instance of log collector with a refernce to the device its belongs to |
| 49 | LogCollector(Device* device, CpuType tracerType); |
| 50 | |
| 51 | ~LogCollector() { if (!m_log_buf) free(m_log_buf); } |
| 52 | |
| 53 | /* |
| 54 | * raise a flag for stop collecting logs |
| 55 | */ |
| 56 | Status StopCollectingLogs(); |
| 57 | Status StartCollectingLogs(); |
| 58 | /******************** end of APIs for offline use (DmTools doesn't request for logs) *************/ |
| 59 | |
| 60 | /******************** APIs for online use (DmTools sends requests) ******************************/ |
| 61 | /* |
| 62 | * Perform all required operations before collecting logs considering the given configuration, including "cleaning" the buffer (distibute the obsolete logs) |
| 63 | * @param: configuration for this collection session |
| 64 | * @return: operation status |
| 65 | */ |
| 66 | Status PrepareLogCollection(); |
| 67 | |
| 68 | bool CollectionIsNeeded(); |
| 69 | |
| 70 | bool IsInitialized() const { return m_initialized; } |
| 71 | |
| 72 | /* |
| 73 | * distribute next logs chunck (without log lines that were already read) |
| 74 | */ |
| 75 | Status GetNextLogs(std::vector<RawLogLine>& rawLogLines); |
| 76 | |
| 77 | /* |
| 78 | * Perform all required operations after collecting logs (e.g. close the log file if applicable) |
| 79 | */ |
| 80 | Status FinishLogCollection(); |
| 81 | |
| 82 | bool SetConfigurationParamerter(const string& parameter, const string& value); |
| 83 | string GetConfigurationParameterValue(const string& parameter, bool& success); |
| 84 | string GetConfigurationDump(); |
| 85 | |
| 86 | /******************** end of APIs for online use (DmTools sends requests) ***********************/ |
| 87 | |
| 88 | private: |
| 89 | // returns the size of logs buffer in fw/ucode |
| 90 | size_t log_size(size_t entry_num) { return sizeof(struct log_table_header) + entry_num * 4; } |
| 91 | |
| 92 | bool ComputeLogStartAddress(); |
| 93 | |
| 94 | // configurations |
| 95 | bool SetModuleVerbosity(); |
| 96 | void ParseModuelLevel(string moduleString); |
| 97 | string GetModuleVerbosityConfigurationValue(); |
| 98 | bool ReadConfigFile(); |
| 99 | bool ParseConfigLine(string line); |
| 100 | |
| 101 | // OS agnostic read log function |
| 102 | bool ReadLogBuffer(void* logBuffer, size_t size); |
| 103 | bool ReadLogWritePointer(void* logBuffer); |
| 104 | |
| 105 | int ParseLog(void* log_buf, size_t log_buf_entries, std::vector<RawLogLine>& rawLogLines); |
| 106 | |
| 107 | // output file methods |
| 108 | string GetNextOutputFileFullName(); |
| 109 | bool CreateNewOutputFile(); |
| 110 | void CloseOutputFile(); |
| 111 | void WriteToOutputFile(const stringstream& logLine); |
| 112 | |
| 113 | //****************** attributes ******************/ |
| 114 | |
| 115 | // log collector's context |
| 116 | LogCollectorConfiguration m_configuration; |
| 117 | Device* m_device; |
| 118 | string m_deviceName; |
| 119 | CpuType m_tracerType; |
| 120 | string m_logErrorMessagePrefix; |
| 121 | |
| 122 | // managing collection |
| 123 | bool m_continueCollectingLogs; |
| 124 | std::chrono::system_clock::time_point m_lastPollingTimestamp; |
| 125 | bool m_initialized; |
| 126 | bool m_ongoingRecording; |
| 127 | |
| 128 | // output file |
| 129 | ofstream m_outputFile; |
| 130 | long m_currentOutputFileSize = 0; |
| 131 | |
| 132 | // log buffer members |
| 133 | unique_ptr<log_table_header> m_logHeader; // content of log buffer header |
| 134 | int m_logAddress; // log buffer start address |
| 135 | void* m_log_buf; // log buffer content |
| 136 | u32 m_rptr; // last read address |
| 137 | u32 m_lastWptr; // last write ptr address (used for detecting buffer overflow) |
| 138 | //****************** end of attributes ************/ |
| 139 | }; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | #endif // !_LOG_COLLECTOR_H |
| 144 | |
| 145 | |