1 /* 2 * Copyright (C) 2017 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 #ifndef CHRE_UTIL_SYSTEM_DEBUG_DUMP_H_ 18 #define CHRE_UTIL_SYSTEM_DEBUG_DUMP_H_ 19 20 #include <cstdarg> 21 #include <cstddef> 22 23 #include <chre/toolchain.h> 24 25 #include "chre/util/dynamic_vector.h" 26 #include "chre/util/unique_ptr.h" 27 28 namespace chre { 29 30 /** 31 * Class to hold information about debug dump buffers so that 32 * multiple debug dump commits can be called on buffers. 33 */ 34 class DebugDumpWrapper { 35 public: DebugDumpWrapper(size_t bufferSize)36 explicit DebugDumpWrapper(size_t bufferSize) 37 : kBuffSize(bufferSize), mCurrBuff(nullptr) {} 38 39 /** 40 * Add formatted string to buffers handling allocating a new buffer if 41 * necessary. 42 * 43 * @param formatStr String that should be formatted using the variable arg 44 * list. 45 * 46 * "this" is the first param in print, so CHRE_PRINTF_ATTR needs to point to 47 * the second and third params. 48 */ 49 CHRE_PRINTF_ATTR(2, 3) 50 void print(const char *formatStr, ...); 51 52 /** 53 * A version of print that takes arguments as a variable list. 54 */ 55 void printVaList(const char *formatStr, va_list argList); 56 57 /** 58 * @return The buffers collected that total up to the full debug dump. 59 */ getBuffers()60 const DynamicVector<UniquePtr<char>> &getBuffers() const { 61 return mBuffers; 62 } 63 64 /** 65 * Clear all the debug dump buffers. 66 */ clear()67 void clear() { 68 mCurrBuff = nullptr; 69 mBuffers.clear(); 70 } 71 72 /** 73 * Print API error distribution histogram to debug_dump 74 * 75 * @param histogram pointer the error distribution histogram. 76 * @param histogramLength The number of chre error types 77 */ 78 void logErrorHistogram(const uint32_t *histogram, uint8_t histogramLength); 79 80 private: 81 //! Number of bytes allocated for each buffer 82 const size_t kBuffSize; 83 //! Index that where a string will be inserted into current buffer 84 size_t mBuffPos; 85 //! Pointer to the current buffer 86 char *mCurrBuff; 87 //! List of allocated buffers for the debug dump session 88 DynamicVector<UniquePtr<char>> mBuffers; 89 90 /** 91 * Set the current buffer to new buffer and append it to back of buffers. 92 * 93 * @return true if successfully allocated memory for new buffer. 94 */ 95 bool allocNewBuffer(); 96 97 /** 98 * Insert a string onto the end of current buffer. 99 * 100 * @param formatStr The format string with format specifiers. 101 * @param argList The variable list of arguments to be inserted into 102 * formatStr. 103 * @param sizeValid The pointer to a variable that will indicate whether 104 * the value stored in sizeOfStr is valid. 105 * @param sizeOfStr The pointer to a variable that will be filled with the 106 * size of the string, not including the null terminator. 107 * 108 * @return true on success. 109 */ 110 bool insertString(const char *formatStr, va_list argList, bool *sizeValid, 111 size_t *sizeOfStr); 112 }; 113 114 } // namespace chre 115 116 #endif // CHRE_UTIL_SYSTEM_DEBUG_DUMP_H_ 117