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 #if defined(_MSC_VER) && (_MSC_VER < 1900)
39 /** VS2010 does not support inttypes - remove when VS2010 support is dropped */
40 #define __PRI64_PREFIX "ll"
41 #define PRIX64 __PRI64_PREFIX "X"
42 #define PRIu64 __PRI64_PREFIX "u"
43 #define PRIu32 "u"
44 #else
45 #include <cinttypes>
46 #endif
47
getValStr(std::string & valStr,const int valTotalBitSize,const int valValidBits,const uint64_t value,const bool asHex,const int updateBits)48 void trcPrintableElem::getValStr(std::string &valStr, const int valTotalBitSize, const int valValidBits, const uint64_t value, const bool asHex /* = true*/, const int updateBits /* = 0*/)
49 {
50 static char szStrBuffer[128];
51 static char szFormatBuffer[32];
52
53 assert((valTotalBitSize >= 4) && (valTotalBitSize <= 64));
54
55 uint64_t LimitMask = ~0ULL;
56 LimitMask >>= 64-valTotalBitSize;
57 valStr = "0x";
58
59 if(asHex)
60 {
61 int numHexChars = valTotalBitSize / 4;
62 numHexChars += ((valTotalBitSize % 4) > 0) ? 1 : 0;
63
64 int validChars = valValidBits / 4;
65 if((valValidBits % 4) > 0) validChars++;
66 int QM = numHexChars - validChars;
67 while(QM)
68 {
69 QM--;
70 valStr += "?";
71 }
72 if(valValidBits > 32)
73 {
74 sprintf(szFormatBuffer,"%%0%dllX",validChars); // create the format
75 sprintf(szStrBuffer,szFormatBuffer,value); // fill the buffer
76 }
77 else
78 {
79 sprintf(szFormatBuffer,"%%0%dlX",validChars); // create the format
80 sprintf(szStrBuffer,szFormatBuffer,(uint32_t)value); // fill the buffer
81 }
82 valStr+=szStrBuffer;
83 if(valValidBits < valTotalBitSize)
84 {
85 sprintf(szStrBuffer," (%d:0)", valValidBits-1);
86 valStr+=szStrBuffer;
87 }
88
89 if(updateBits)
90 {
91 uint64_t updateMask = ~0ULL;
92 updateMask >>= 64-updateBits;
93 sprintf(szStrBuffer," ~[0x%" PRIX64 "]",value & updateMask);
94 valStr+=szStrBuffer;
95 }
96 }
97 else
98 {
99 valStr = "";
100 if(valValidBits < valTotalBitSize)
101 valStr += "??";
102 if(valValidBits > 32)
103 {
104 sprintf(szStrBuffer,"%" PRIu64 ,value);
105 }
106 else
107 {
108 sprintf(szStrBuffer,"%" PRIu32 ,(uint32_t)value);
109 }
110 valStr += szStrBuffer;
111 if(valValidBits < valTotalBitSize)
112 {
113 sprintf(szStrBuffer," (%d:0)", valValidBits-1);
114 valStr+=szStrBuffer;
115 }
116 }
117 }
118
119
120 /* End of File trc_printable_elem.cpp */
121