xref: /aosp_15_r20/external/OpenCSD/decoder/source/trc_printable_elem.cpp (revision 02ca8ccacfba7e0df68f3332a95f3180334d6649)
1 /*
2  * \file       trc_printable_elem.cpp
3  * \brief      OpenCSD :
4  *
5  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6  */
7 
8 /*
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "common/trc_printable_elem.h"
36 #include <cassert>
37 #include <cstring>
38 #include <cinttypes>
39 
getValStr(std::string & valStr,const int valTotalBitSize,const int valValidBits,const uint64_t value,const bool asHex,const int updateBits)40 void trcPrintableElem::getValStr(std::string &valStr, const int valTotalBitSize, const int valValidBits, const uint64_t value, const bool asHex /* = true*/, const int updateBits /* = 0*/)
41 {
42     static char szStrBuffer[128];
43     static char szFormatBuffer[32];
44 
45     assert((valTotalBitSize >= 4) && (valTotalBitSize <= 64));
46 
47     valStr = "0x";
48 
49     if(asHex)
50     {
51         int numHexChars = valTotalBitSize / 4;
52         numHexChars += ((valTotalBitSize % 4) > 0) ? 1 : 0;
53 
54         int validChars = valValidBits / 4;
55         if((valValidBits % 4) > 0) validChars++;
56 		if (validChars < numHexChars)
57 		{
58 			int QM = numHexChars - validChars;
59 			while (QM)
60 			{
61 				QM--;
62 				valStr += "?";
63 			}
64 		}
65         if(valValidBits > 32)
66         {
67             sprintf(szFormatBuffer,"%%0%dllX",validChars);  // create the format
68             sprintf(szStrBuffer,szFormatBuffer,value); // fill the buffer
69         }
70         else
71         {
72             sprintf(szFormatBuffer,"%%0%dlX",validChars);  // create the format
73             sprintf(szStrBuffer,szFormatBuffer,(uint32_t)value); // fill the buffer
74         }
75         valStr+=szStrBuffer;
76         if(valValidBits < valTotalBitSize)
77         {
78             sprintf(szStrBuffer," (%d:0)", valValidBits-1);
79             valStr+=szStrBuffer;
80         }
81 
82         if(updateBits)
83         {
84             uint64_t updateMask = ~0ULL;
85             updateMask >>= 64-updateBits;
86             sprintf(szStrBuffer," ~[0x%" PRIX64 "]",value & updateMask);
87             valStr+=szStrBuffer;
88         }
89     }
90     else
91     {
92         valStr = "";
93         if(valValidBits < valTotalBitSize)
94             valStr += "??";
95         if(valValidBits > 32)
96         {
97             sprintf(szStrBuffer,"%" PRIu64 ,value);
98         }
99         else
100         {
101             sprintf(szStrBuffer,"%" PRIu32 ,(uint32_t)value);
102         }
103         valStr +=  szStrBuffer;
104         if(valValidBits < valTotalBitSize)
105         {
106             sprintf(szStrBuffer," (%d:0)", valValidBits-1);
107             valStr+=szStrBuffer;
108         }
109     }
110 }
111 
112 
113 /* End of File trc_printable_elem.cpp */
114