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  private:
73   //! Number of bytes allocated for each buffer
74   const size_t kBuffSize;
75   //! Index that where a string will be inserted into current buffer
76   size_t mBuffPos;
77   //! Pointer to the current buffer
78   char *mCurrBuff;
79   //! List of allocated buffers for the debug dump session
80   DynamicVector<UniquePtr<char>> mBuffers;
81 
82   /**
83    * Set the current buffer to new buffer and append it to back of buffers.
84    *
85    * @return true if successfully allocated memory for new buffer.
86    */
87   bool allocNewBuffer();
88 
89   /**
90    * Insert a string onto the end of current buffer.
91    *
92    * @param formatStr The format string with format specifiers.
93    * @param argList The variable list of arguments to be inserted into
94    *    formatStr.
95    * @param sizeValid The pointer to a variable that will indicate whether
96    *    the value stored in sizeOfStr is valid.
97    * @param sizeOfStr The pointer to a variable that will be filled with the
98    *    size of the string, not including the null terminator.
99    *
100    * @return true on success.
101    */
102   bool insertString(const char *formatStr, va_list argList, bool *sizeValid,
103                     size_t *sizeOfStr);
104 };
105 
106 }  // namespace chre
107 
108 #endif  // CHRE_UTIL_SYSTEM_DEBUG_DUMP_H_
109