1 | /* $Id: strformatrt.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - IPRT String Formatter Extensions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /** @page pg_rt_str_format_rt The IPRT String Format Extensions
|
---|
32 | *
|
---|
33 | * The string formatter supports most of the non-float format types and flags.
|
---|
34 | * See RTStrFormatV() for the full tail there. In addition we've added a number
|
---|
35 | * of iprt specific format types for the iprt typedefs and other useful stuff.
|
---|
36 | * Note that several of these are similar to \%p and doesn't care much if you try
|
---|
37 | * add formating flags/width/precision.
|
---|
38 | *
|
---|
39 | *
|
---|
40 | * Group 1, the basic runtime typedefs (excluding those which obviously are pointer).
|
---|
41 | * - \%RTbool - Takes a bool value and prints 'true', 'false', or '!%d!'.
|
---|
42 | * - \%RTfile - Takes a #RTFILE value.
|
---|
43 | * - \%RTfmode - Takes a #RTFMODE value.
|
---|
44 | * - \%RTfoff - Takes a #RTFOFF value.
|
---|
45 | * - \%RTfp16 - Takes a #RTFAR16 value.
|
---|
46 | * - \%RTfp32 - Takes a #RTFAR32 value.
|
---|
47 | * - \%RTfp64 - Takes a #RTFAR64 value.
|
---|
48 | * - \%RTgid - Takes a #RTGID value.
|
---|
49 | * - \%RTino - Takes a #RTINODE value.
|
---|
50 | * - \%RTint - Takes a #RTINT value.
|
---|
51 | * - \%RTiop - Takes a #RTIOPORT value.
|
---|
52 | * - \%RTldrm - Takes a #RTLDRMOD value.
|
---|
53 | * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
|
---|
54 | * - \%RTproc - Takes a #RTPROCESS value.
|
---|
55 | * - \%RTptr - Takes a #RTINTPTR or #RTUINTPTR value (but not void *).
|
---|
56 | * - \%RTreg - Takes a #RTUINTREG value.
|
---|
57 | * - \%RTsel - Takes a #RTSEL value.
|
---|
58 | * - \%RTsem - Takes a #RTSEMEVENT, #RTSEMEVENTMULTI, #RTSEMMUTEX, #RTSEMFASTMUTEX, or #RTSEMRW value.
|
---|
59 | * - \%RTsock - Takes a #RTSOCKET value.
|
---|
60 | * - \%RTthrd - Takes a #RTTHREAD value.
|
---|
61 | * - \%RTuid - Takes a #RTUID value.
|
---|
62 | * - \%RTuint - Takes a #RTUINT value.
|
---|
63 | * - \%RTunicp - Takes a #RTUNICP value.
|
---|
64 | * - \%RTutf16 - Takes a #RTUTF16 value.
|
---|
65 | * - \%RTuuid - Takes a #PCRTUUID and will print the UUID as a string.
|
---|
66 | * - \%RTxuint - Takes a #RTUINT or #RTINT value, formatting it as hex.
|
---|
67 | * - \%RGi - Takes a #RTGCINT value.
|
---|
68 | * - \%RGp - Takes a #RTGCPHYS value.
|
---|
69 | * - \%RGr - Takes a #RTGCUINTREG value.
|
---|
70 | * - \%RGu - Takes a #RTGCUINT value.
|
---|
71 | * - \%RGv - Takes a #RTGCPTR, #RTGCINTPTR or #RTGCUINTPTR value.
|
---|
72 | * - \%RGx - Takes a #RTGCUINT or #RTGCINT value, formatting it as hex.
|
---|
73 | * - \%RHi - Takes a #RTHCINT value.
|
---|
74 | * - \%RHp - Takes a #RTHCPHYS value.
|
---|
75 | * - \%RHr - Takes a #RTHCUINTREG value.
|
---|
76 | * - \%RHu - Takes a #RTHCUINT value.
|
---|
77 | * - \%RHv - Takes a #RTHCPTR, #RTHCINTPTR or #RTHCUINTPTR value.
|
---|
78 | * - \%RHx - Takes a #RTHCUINT or #RTHCINT value, formatting it as hex.
|
---|
79 | * - \%RCi - Takes a #RTCCINT value.
|
---|
80 | * - \%RCp - Takes a #RTCCPHYS value.
|
---|
81 | * - \%RCr - Takes a #RTCCUINTREG value.
|
---|
82 | * - \%RCu - Takes a #RTUINT value.
|
---|
83 | * - \%RCv - Takes a #uintptr_t, #intptr_t, void * value.
|
---|
84 | * - \%RCx - Takes a #RTUINT or #RTINT value, formatting it as hex.
|
---|
85 | *
|
---|
86 | *
|
---|
87 | * Group 2, the generic integer types which are prefered over relying on what
|
---|
88 | * bit-count a 'long', 'short', or 'long long' has on a platform. This are
|
---|
89 | * highly prefered for the [u]intXX_t kind of types.
|
---|
90 | * - \%RI[8|16|32|64] - Signed integer value of the specifed bit count.
|
---|
91 | * - \%RU[8|16|32|64] - Unsigned integer value of the specifed bit count.
|
---|
92 | * - \%RX[8|16|32|64] - Hexadecimal integer value of the specifed bit count.
|
---|
93 | *
|
---|
94 | *
|
---|
95 | * Group 3, hex dumpers and other complex stuff which requires more than simple formatting.
|
---|
96 | * - \%Rhxd - Takes a pointer to the memory which is to be dumped in typical
|
---|
97 | * hex format. Use the width to specify the length, and the precision to
|
---|
98 | * set the number of bytes per line. Default width and precision is 16.
|
---|
99 | * - \%Rhxs - Takes a pointer to the memory to be displayed as a hex string,
|
---|
100 | * i.e. a series of space separated bytes formatted as two digit hex value.
|
---|
101 | * Use the width to specify the length. Default length is 16 bytes.
|
---|
102 | * - \%Rrc - Takes an integer iprt status code as argument. Will insert the
|
---|
103 | * status code define corresponding to the iprt status code.
|
---|
104 | * - \%Rrs - Takes an integer iprt status code as argument. Will insert the
|
---|
105 | * short description of the specified status code.
|
---|
106 | * - \%Rrf - Takes an integer iprt status code as argument. Will insert the
|
---|
107 | * full description of the specified status code.
|
---|
108 | * - \%Rra - Takes an integer iprt status code as argument. Will insert the
|
---|
109 | * status code define + full description.
|
---|
110 | * - \%Rt - Current thread (RTThreadSelf()), no arguments.
|
---|
111 | *
|
---|
112 | * - \%Rwc - Takes a long Windows error code as argument. Will insert the status
|
---|
113 | * code define corresponding to the Windows error code.
|
---|
114 | * - \%Rwf - Takes a long Windows error code as argument. Will insert the
|
---|
115 | * full description of the specified status code.
|
---|
116 | * - \%Rwa - Takes a long Windows error code as argument. Will insert the
|
---|
117 | * error code define + full description.
|
---|
118 | *
|
---|
119 | * On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
|
---|
120 | *
|
---|
121 | * @todo (r=dmik) Add a cross-platform \%Rcomr? that will fall back to \%Rw? on
|
---|
122 | * Win32 platforms and will interpret XPCOM result codes on all other.
|
---|
123 | *
|
---|
124 | *
|
---|
125 | * Group 4, structure dumpers.
|
---|
126 | *
|
---|
127 | * - \%RDtimespec - Takes a PCRTTIMESPEC.
|
---|
128 | *
|
---|
129 | *
|
---|
130 | */
|
---|
131 |
|
---|
132 | /*******************************************************************************
|
---|
133 | * Header Files *
|
---|
134 | *******************************************************************************/
|
---|
135 | #define LOG_GROUP RTLOGGROUP_STRING
|
---|
136 | #include <iprt/log.h>
|
---|
137 | #include <iprt/string.h>
|
---|
138 | #include <iprt/assert.h>
|
---|
139 | #include <iprt/string.h>
|
---|
140 | #include <iprt/stdarg.h>
|
---|
141 | #ifdef IN_RING3
|
---|
142 | # include <iprt/thread.h>
|
---|
143 | # include <iprt/err.h>
|
---|
144 | #endif
|
---|
145 | #include <iprt/time.h>
|
---|
146 | #include "internal/string.h"
|
---|
147 |
|
---|
148 |
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Callback to format iprt formatting extentions.
|
---|
152 | * See @ref pg_rt_str_format_rt for a reference on the format types.
|
---|
153 | *
|
---|
154 | * @returns The number of bytes formatted.
|
---|
155 | * @param pfnOutput Pointer to output function.
|
---|
156 | * @param pvArgOutput Argument for the output function.
|
---|
157 | * @param ppszFormat Pointer to the format string pointer. Advance this till the char
|
---|
158 | * after the format specifier.
|
---|
159 | * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
|
---|
160 | * @param cchWidth Format Width. -1 if not specified.
|
---|
161 | * @param cchPrecision Format Precision. -1 if not specified.
|
---|
162 | * @param fFlags Flags (RTSTR_NTFS_*).
|
---|
163 | * @param chArgSize The argument size specifier, 'l' or 'L'.
|
---|
164 | */
|
---|
165 | size_t rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs, int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize)
|
---|
166 | {
|
---|
167 | const char *pszFormatOrg = *ppszFormat;
|
---|
168 | char ch = *(*ppszFormat)++;
|
---|
169 | if (ch == 'R')
|
---|
170 | {
|
---|
171 | ch = *(*ppszFormat)++;
|
---|
172 | switch (ch)
|
---|
173 | {
|
---|
174 | /*
|
---|
175 | * Groups 1 and 2.
|
---|
176 | */
|
---|
177 | case 'T':
|
---|
178 | case 'G':
|
---|
179 | case 'H':
|
---|
180 | case 'C':
|
---|
181 | case 'I':
|
---|
182 | case 'X':
|
---|
183 | case 'U':
|
---|
184 | {
|
---|
185 | /*
|
---|
186 | * Interpret the type.
|
---|
187 | */
|
---|
188 | typedef enum { RTSF_INT, RTSF_INTW, RTSF_FP16, RTSF_FP32, RTSF_FP64, RTSF_UUID, RTSF_BOOL } RTSF;
|
---|
189 | static const struct
|
---|
190 | {
|
---|
191 | uint8_t cch; /**< the length of the string. */
|
---|
192 | char sz[10]; /**< the part following 'R'. */
|
---|
193 | uint8_t cb; /**< the size of the type. */
|
---|
194 | uint8_t u8Base; /**< the size of the type. */
|
---|
195 | RTSF enmFormat; /**< The way to format it. */
|
---|
196 | uint16_t fFlags; /**< additional RTSTR_F_* flags. */
|
---|
197 | }
|
---|
198 | /** Sorted array of types, looked up using binary search! */
|
---|
199 | s_aTypes[] =
|
---|
200 | {
|
---|
201 | #define STRMEM(str) sizeof(str) - 1, str
|
---|
202 | { STRMEM("Ci"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
203 | { STRMEM("Cp"), sizeof(RTGCPHYS), 16, RTSF_INTW, 0 },
|
---|
204 | { STRMEM("Cr"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
|
---|
205 | { STRMEM("Cu"), sizeof(RTUINT), 10, RTSF_INT, 0 },
|
---|
206 | { STRMEM("Cv"), sizeof(void *), 16, RTSF_INTW, 0 },
|
---|
207 | { STRMEM("Cx"), sizeof(RTUINT), 16, RTSF_INT, 0 },
|
---|
208 | { STRMEM("Gi"), sizeof(RTGCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
209 | { STRMEM("Gp"), sizeof(RTGCPHYS), 16, RTSF_INTW, 0 },
|
---|
210 | { STRMEM("Gr"), sizeof(RTGCUINTREG), 16, RTSF_INTW, 0 },
|
---|
211 | { STRMEM("Gu"), sizeof(RTGCUINT), 10, RTSF_INT, 0 },
|
---|
212 | { STRMEM("Gv"), sizeof(RTGCPTR), 16, RTSF_INTW, 0 },
|
---|
213 | { STRMEM("Gx"), sizeof(RTGCUINT), 16, RTSF_INT, 0 },
|
---|
214 | { STRMEM("Hi"), sizeof(RTHCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
215 | { STRMEM("Hp"), sizeof(RTHCPHYS), 16, RTSF_INTW, 0 },
|
---|
216 | { STRMEM("Hr"), sizeof(RTGCUINTREG), 16, RTSF_INTW, 0 },
|
---|
217 | { STRMEM("Hu"), sizeof(RTHCUINT), 10, RTSF_INT, 0 },
|
---|
218 | { STRMEM("Hv"), sizeof(RTHCPTR), 16, RTSF_INTW, 0 },
|
---|
219 | { STRMEM("Hx"), sizeof(RTHCUINT), 16, RTSF_INT, 0 },
|
---|
220 | { STRMEM("I16"), sizeof(int16_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
221 | { STRMEM("I32"), sizeof(int32_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
222 | { STRMEM("I64"), sizeof(int64_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
223 | { STRMEM("I8"), sizeof(int8_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
224 | { STRMEM("Tbool"), sizeof(bool), 10, RTSF_BOOL, 0 },
|
---|
225 | { STRMEM("Tfile"), sizeof(RTFILE), 10, RTSF_INT, 0 },
|
---|
226 | { STRMEM("Tfmode"), sizeof(RTFMODE), 16, RTSF_INTW, 0 },
|
---|
227 | { STRMEM("Tfoff"), sizeof(RTFOFF), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
228 | { STRMEM("Tfp16"), sizeof(RTFAR16), 16, RTSF_FP16, RTSTR_F_ZEROPAD },
|
---|
229 | { STRMEM("Tfp32"), sizeof(RTFAR32), 16, RTSF_FP32, RTSTR_F_ZEROPAD },
|
---|
230 | { STRMEM("Tfp64"), sizeof(RTFAR64), 16, RTSF_FP64, RTSTR_F_ZEROPAD },
|
---|
231 | { STRMEM("Tgid"), sizeof(RTGID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
232 | { STRMEM("Tino"), sizeof(RTINODE), 16, RTSF_INTW, 0 },
|
---|
233 | { STRMEM("Tint"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
234 | { STRMEM("Tiop"), sizeof(RTIOPORT), 16, RTSF_INTW, 0 },
|
---|
235 | { STRMEM("Tldrm"), sizeof(RTLDRMOD), 16, RTSF_INTW, 0 },
|
---|
236 | { STRMEM("Tnthrd"), sizeof(RTNATIVETHREAD), 16, RTSF_INTW, 0 },
|
---|
237 | { STRMEM("Tproc"), sizeof(RTPROCESS), 16, RTSF_INTW, 0 },
|
---|
238 | { STRMEM("Tptr"), sizeof(RTUINTPTR), 16, RTSF_INTW, 0 },
|
---|
239 | { STRMEM("Treg"), sizeof(RTUINTREG), 16, RTSF_INTW, 0 },
|
---|
240 | { STRMEM("Tsel"), sizeof(RTSEL), 16, RTSF_INTW, 0 },
|
---|
241 | { STRMEM("Tsem"), sizeof(RTSEMEVENT), 16, RTSF_INTW, 0 },
|
---|
242 | { STRMEM("Tsock"), sizeof(RTSOCKET), 10, RTSF_INT, 0 },
|
---|
243 | { STRMEM("Tthrd"), sizeof(RTTHREAD), 16, RTSF_INTW, 0 },
|
---|
244 | { STRMEM("Tuid"), sizeof(RTUID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
|
---|
245 | { STRMEM("Tuint"), sizeof(RTUINT), 10, RTSF_INT, 0 },
|
---|
246 | { STRMEM("Tunicp"), sizeof(RTUNICP), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
|
---|
247 | { STRMEM("Tutf16"), sizeof(RTUTF16), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
|
---|
248 | { STRMEM("Tuuid"), sizeof(PCRTUUID), 16, RTSF_UUID, 0 },
|
---|
249 | { STRMEM("Txint"), sizeof(RTUINT), 16, RTSF_INT, 0 },
|
---|
250 | { STRMEM("U16"), sizeof(uint16_t), 10, RTSF_INT, 0 },
|
---|
251 | { STRMEM("U32"), sizeof(uint32_t), 10, RTSF_INT, 0 },
|
---|
252 | { STRMEM("U64"), sizeof(uint64_t), 10, RTSF_INT, 0 },
|
---|
253 | { STRMEM("U8"), sizeof(uint8_t), 10, RTSF_INT, 0 },
|
---|
254 | { STRMEM("X16"), sizeof(uint16_t), 16, RTSF_INT, 0 },
|
---|
255 | { STRMEM("X32"), sizeof(uint32_t), 16, RTSF_INT, 0 },
|
---|
256 | { STRMEM("X64"), sizeof(uint64_t), 16, RTSF_INT, 0 },
|
---|
257 | { STRMEM("X8"), sizeof(uint8_t), 16, RTSF_INT, 0 },
|
---|
258 | #undef STRMEM
|
---|
259 | };
|
---|
260 | const char *pszType = *ppszFormat - 1;
|
---|
261 | int iStart = 0;
|
---|
262 | int iEnd = ELEMENTS(s_aTypes) - 1;
|
---|
263 | int i = ELEMENTS(s_aTypes) / 2;
|
---|
264 |
|
---|
265 | union
|
---|
266 | {
|
---|
267 | uint8_t u8;
|
---|
268 | uint16_t u16;
|
---|
269 | uint32_t u32;
|
---|
270 | uint64_t u64;
|
---|
271 | int8_t i8;
|
---|
272 | int16_t i16;
|
---|
273 | int32_t i32;
|
---|
274 | int64_t i64;
|
---|
275 | RTFAR16 fp16;
|
---|
276 | RTFAR32 fp32;
|
---|
277 | RTFAR64 fp64;
|
---|
278 | bool fBool;
|
---|
279 | PCRTUUID pUuid;
|
---|
280 | } u;
|
---|
281 | char szBuf[80];
|
---|
282 | unsigned cch;
|
---|
283 |
|
---|
284 | AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
|
---|
285 |
|
---|
286 | /*
|
---|
287 | * Lookup the type - binary search.
|
---|
288 | */
|
---|
289 | for (;;)
|
---|
290 | {
|
---|
291 | int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
|
---|
292 | if (!iDiff)
|
---|
293 | break;
|
---|
294 | if (iEnd == iStart)
|
---|
295 | {
|
---|
296 | AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
|
---|
297 | return 0;
|
---|
298 | }
|
---|
299 | if (iDiff < 0)
|
---|
300 | iEnd = i - 1;
|
---|
301 | else
|
---|
302 | iStart = i + 1;
|
---|
303 | if (iEnd < iStart)
|
---|
304 | {
|
---|
305 | AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
|
---|
306 | return 0;
|
---|
307 | }
|
---|
308 | i = iStart + (iEnd - iStart) / 2;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /*
|
---|
312 | * Advance the format string and merge flags.
|
---|
313 | */
|
---|
314 | *ppszFormat += s_aTypes[i].cch - 1;
|
---|
315 | fFlags |= s_aTypes[i].fFlags;
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * Fetch the argument.
|
---|
319 | * It's important that a signed value gets sign-extended up to 64-bit.
|
---|
320 | */
|
---|
321 | u.u64 = 0;
|
---|
322 | if (fFlags & RTSTR_F_VALSIGNED)
|
---|
323 | {
|
---|
324 | switch (s_aTypes[i].cb)
|
---|
325 | {
|
---|
326 | case sizeof(int8_t):
|
---|
327 | u.i64 = va_arg(*pArgs, /*int8_t*/int);
|
---|
328 | fFlags |= RTSTR_F_8BIT;
|
---|
329 | break;
|
---|
330 | case sizeof(int16_t):
|
---|
331 | u.i64 = va_arg(*pArgs, /*int16_t*/int);
|
---|
332 | fFlags |= RTSTR_F_16BIT;
|
---|
333 | break;
|
---|
334 | case sizeof(int32_t):
|
---|
335 | u.i64 = va_arg(*pArgs, int32_t);
|
---|
336 | fFlags |= RTSTR_F_32BIT;
|
---|
337 | break;
|
---|
338 | case sizeof(int64_t):
|
---|
339 | u.i64 = va_arg(*pArgs, int64_t);
|
---|
340 | fFlags |= RTSTR_F_64BIT;
|
---|
341 | break;
|
---|
342 | default:
|
---|
343 | AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
|
---|
344 | break;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | else
|
---|
348 | {
|
---|
349 | switch (s_aTypes[i].cb)
|
---|
350 | {
|
---|
351 | case sizeof(uint8_t):
|
---|
352 | u.u8 = va_arg(*pArgs, /*uint8_t*/unsigned);
|
---|
353 | fFlags |= RTSTR_F_8BIT;
|
---|
354 | break;
|
---|
355 | case sizeof(uint16_t):
|
---|
356 | u.u16 = va_arg(*pArgs, /*uint16_t*/unsigned);
|
---|
357 | fFlags |= RTSTR_F_16BIT;
|
---|
358 | break;
|
---|
359 | case sizeof(uint32_t):
|
---|
360 | u.u32 = va_arg(*pArgs, uint32_t);
|
---|
361 | fFlags |= RTSTR_F_32BIT;
|
---|
362 | break;
|
---|
363 | case sizeof(uint64_t):
|
---|
364 | u.u64 = va_arg(*pArgs, uint64_t);
|
---|
365 | fFlags |= RTSTR_F_64BIT;
|
---|
366 | break;
|
---|
367 | case sizeof(RTFAR32):
|
---|
368 | u.fp32 = va_arg(*pArgs, RTFAR32);
|
---|
369 | break;
|
---|
370 | case sizeof(RTFAR64):
|
---|
371 | u.fp64 = va_arg(*pArgs, RTFAR64);
|
---|
372 | break;
|
---|
373 | default:
|
---|
374 | AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
|
---|
375 | break;
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | /*
|
---|
380 | * Format the output.
|
---|
381 | */
|
---|
382 | switch (s_aTypes[i].enmFormat)
|
---|
383 | {
|
---|
384 | case RTSF_INT:
|
---|
385 | {
|
---|
386 | cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
|
---|
387 | break;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /* hex which defaults to max width. */
|
---|
391 | case RTSF_INTW:
|
---|
392 | {
|
---|
393 | Assert(s_aTypes[i].u8Base == 16);
|
---|
394 | if (cchWidth < 0)
|
---|
395 | {
|
---|
396 | cchWidth = s_aTypes[i].cb * 2 + (fFlags & RTSTR_F_SPECIAL ? 2 : 0);
|
---|
397 | fFlags |= RTSTR_F_ZEROPAD;
|
---|
398 | }
|
---|
399 | cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
|
---|
400 | break;
|
---|
401 | }
|
---|
402 |
|
---|
403 | case RTSF_FP16:
|
---|
404 | {
|
---|
405 | fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION);
|
---|
406 | cch = RTStrFormatNumber(&szBuf[0], u.fp16.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
|
---|
407 | Assert(cch == 4);
|
---|
408 | szBuf[4] = ':';
|
---|
409 | cch = RTStrFormatNumber(&szBuf[5], u.fp16.off, 16, 4, -1, fFlags | RTSTR_F_16BIT);
|
---|
410 | Assert(cch == 4);
|
---|
411 | cch = 4 + 1 + 4;
|
---|
412 | break;
|
---|
413 | }
|
---|
414 | case RTSF_FP32:
|
---|
415 | {
|
---|
416 | fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION);
|
---|
417 | cch = RTStrFormatNumber(&szBuf[0], u.fp32.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
|
---|
418 | Assert(cch == 4);
|
---|
419 | szBuf[4] = ':';
|
---|
420 | cch = RTStrFormatNumber(&szBuf[5], u.fp32.off, 16, 8, -1, fFlags | RTSTR_F_32BIT);
|
---|
421 | Assert(cch == 8);
|
---|
422 | cch = 4 + 1 + 8;
|
---|
423 | break;
|
---|
424 | }
|
---|
425 | case RTSF_FP64:
|
---|
426 | {
|
---|
427 | fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION);
|
---|
428 | cch = RTStrFormatNumber(&szBuf[0], u.fp64.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
|
---|
429 | Assert(cch == 4);
|
---|
430 | szBuf[4] = ':';
|
---|
431 | cch = RTStrFormatNumber(&szBuf[5], u.fp64.off, 16, 16, -1, fFlags | RTSTR_F_64BIT);
|
---|
432 | Assert(cch == 16);
|
---|
433 | cch = 4 + 1 + 16;
|
---|
434 | break;
|
---|
435 | }
|
---|
436 |
|
---|
437 | case RTSF_UUID:
|
---|
438 | {
|
---|
439 | static const char szNull[] = "<NULL>";
|
---|
440 |
|
---|
441 | if (VALID_PTR(u.pUuid))
|
---|
442 | {
|
---|
443 | /* cannot call RTUuidToStr because of GC/R0. */
|
---|
444 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
445 | "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
---|
446 | u.pUuid->Gen.u32TimeLow,
|
---|
447 | u.pUuid->Gen.u16TimeMid,
|
---|
448 | u.pUuid->Gen.u16TimeHiAndVersion,
|
---|
449 | u.pUuid->Gen.u16ClockSeq & 0xff,
|
---|
450 | u.pUuid->Gen.u16ClockSeq >> 8,
|
---|
451 | u.pUuid->Gen.au8Node[0],
|
---|
452 | u.pUuid->Gen.au8Node[1],
|
---|
453 | u.pUuid->Gen.au8Node[2],
|
---|
454 | u.pUuid->Gen.au8Node[3],
|
---|
455 | u.pUuid->Gen.au8Node[4],
|
---|
456 | u.pUuid->Gen.au8Node[5]);
|
---|
457 | }
|
---|
458 | return pfnOutput(pvArgOutput, szNull, sizeof(szNull) - 1);
|
---|
459 | }
|
---|
460 |
|
---|
461 | case RTSF_BOOL:
|
---|
462 | {
|
---|
463 | static const char szTrue[] = "true ";
|
---|
464 | static const char szFalse[] = "false";
|
---|
465 | if (u.u64 == 1)
|
---|
466 | return pfnOutput(pvArgOutput, szTrue, sizeof(szTrue) - 1);
|
---|
467 | if (u.u64 == 0)
|
---|
468 | return pfnOutput(pvArgOutput, szFalse, sizeof(szFalse) - 1);
|
---|
469 | /* invalid boolean value */
|
---|
470 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "!%lld!", u.u64);
|
---|
471 | }
|
---|
472 |
|
---|
473 | default:
|
---|
474 | AssertMsgFailed(("Internal error %d\n", s_aTypes[i].enmFormat));
|
---|
475 | return 0;
|
---|
476 | }
|
---|
477 |
|
---|
478 | /*
|
---|
479 | * Finally, output the formatted string and return.
|
---|
480 | */
|
---|
481 | return pfnOutput(pvArgOutput, szBuf, cch);
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | /* Group 3 */
|
---|
486 |
|
---|
487 | /*
|
---|
488 | * hex dumping.
|
---|
489 | */
|
---|
490 | case 'h':
|
---|
491 | {
|
---|
492 | char ch = *(*ppszFormat)++;
|
---|
493 | switch (ch)
|
---|
494 | {
|
---|
495 | /*
|
---|
496 | * Hex stuff.
|
---|
497 | */
|
---|
498 | case 'x':
|
---|
499 | {
|
---|
500 | uint8_t *pu8 = va_arg(*pArgs, uint8_t *);
|
---|
501 | if (cchWidth <= 0)
|
---|
502 | cchWidth = 16;
|
---|
503 | if (pu8)
|
---|
504 | {
|
---|
505 | ch = *(*ppszFormat)++;
|
---|
506 | switch (ch)
|
---|
507 | {
|
---|
508 | /*
|
---|
509 | * Regular hex dump.
|
---|
510 | */
|
---|
511 | case 'd':
|
---|
512 | {
|
---|
513 | size_t cch = 0;
|
---|
514 | int off = 0;
|
---|
515 |
|
---|
516 | if (cchPrecision <= 0)
|
---|
517 | cchPrecision = 16;
|
---|
518 |
|
---|
519 | while (off < cchWidth)
|
---|
520 | {
|
---|
521 | int i;
|
---|
522 | cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%0*x %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
|
---|
523 | for (i = 0; i < cchPrecision && off + i < cchWidth ; i++)
|
---|
524 | cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
525 | off + i < cchWidth ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pu8[i]);
|
---|
526 | while (i++ < cchPrecision)
|
---|
527 | cch += pfnOutput(pvArgOutput, " ", 3);
|
---|
528 |
|
---|
529 | cch += pfnOutput(pvArgOutput, " ", 1);
|
---|
530 |
|
---|
531 | for (i = 0; i < cchPrecision && off + i < cchWidth; i++)
|
---|
532 | {
|
---|
533 | uint8_t u8 = pu8[i];
|
---|
534 | cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
|
---|
535 | }
|
---|
536 |
|
---|
537 | /* next */
|
---|
538 | pu8 += cchPrecision;
|
---|
539 | off += cchPrecision;
|
---|
540 | }
|
---|
541 | return cch;
|
---|
542 | }
|
---|
543 |
|
---|
544 | /*
|
---|
545 | * Hex string.
|
---|
546 | */
|
---|
547 | case 's':
|
---|
548 | {
|
---|
549 | if (cchWidth-- > 0)
|
---|
550 | {
|
---|
551 | size_t cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++);
|
---|
552 | for (; cchWidth > 0; cchWidth--, pu8++)
|
---|
553 | cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8);
|
---|
554 | return cch;
|
---|
555 | }
|
---|
556 | break;
|
---|
557 | }
|
---|
558 |
|
---|
559 | default:
|
---|
560 | AssertMsgFailed(("Invalid status code format type '%.10s'!\n", ch, pszFormatOrg));
|
---|
561 | break;
|
---|
562 | }
|
---|
563 | }
|
---|
564 | else
|
---|
565 | return pfnOutput(pvArgOutput, "<null>", sizeof("<null>") - 1);
|
---|
566 | break;
|
---|
567 | }
|
---|
568 |
|
---|
569 | default:
|
---|
570 | AssertMsgFailed(("Invalid status code format type '%.10s'!\n", ch, pszFormatOrg));
|
---|
571 | return 0;
|
---|
572 |
|
---|
573 | }
|
---|
574 | break;
|
---|
575 | }
|
---|
576 |
|
---|
577 | /*
|
---|
578 | * iprt status code: %Vrc, %Vrs, %Vrf, %Vra.
|
---|
579 | */
|
---|
580 | case 'r':
|
---|
581 | {
|
---|
582 | int rc = va_arg(*pArgs, int);
|
---|
583 | char ch = *(*ppszFormat)++;
|
---|
584 | #ifdef IN_RING3 /* we don't want this anywhere else yet. */
|
---|
585 | PCRTSTATUSMSG pMsg = RTErrGet(rc);
|
---|
586 | switch (ch)
|
---|
587 | {
|
---|
588 | case 'c':
|
---|
589 | return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
|
---|
590 | case 's':
|
---|
591 | return pfnOutput(pvArgOutput, pMsg->pszMsgShort, strlen(pMsg->pszMsgShort));
|
---|
592 | case 'f':
|
---|
593 | return pfnOutput(pvArgOutput, pMsg->pszMsgFull, strlen(pMsg->pszMsgFull));
|
---|
594 | case 'a':
|
---|
595 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
|
---|
596 | default:
|
---|
597 | AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
|
---|
598 | return 0;
|
---|
599 | }
|
---|
600 | #else /* !IN_RING3 */
|
---|
601 | switch (ch)
|
---|
602 | {
|
---|
603 | case 'c':
|
---|
604 | case 's':
|
---|
605 | case 'f':
|
---|
606 | case 'a':
|
---|
607 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%d", rc);
|
---|
608 | default:
|
---|
609 | AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
|
---|
610 | return 0;
|
---|
611 | }
|
---|
612 | #endif /* !IN_RING3 */
|
---|
613 | break;
|
---|
614 | }
|
---|
615 |
|
---|
616 | #if defined(IN_RING3)
|
---|
617 | /*
|
---|
618 | * Windows status code: %Rwc, %Rwf, %Rwa
|
---|
619 | */
|
---|
620 | case 'w':
|
---|
621 | {
|
---|
622 | long rc = va_arg(*pArgs, long);
|
---|
623 | char ch = *(*ppszFormat)++;
|
---|
624 | #if defined(RT_OS_WINDOWS)
|
---|
625 | PCRTWINERRMSG pMsg = RTErrWinGet(rc);
|
---|
626 | #endif
|
---|
627 | switch (ch)
|
---|
628 | {
|
---|
629 | #if defined(RT_OS_WINDOWS)
|
---|
630 | case 'c':
|
---|
631 | return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
|
---|
632 | case 'f':
|
---|
633 | return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
|
---|
634 | case 'a':
|
---|
635 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
|
---|
636 | #else
|
---|
637 | case 'c':
|
---|
638 | case 'f':
|
---|
639 | case 'a':
|
---|
640 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "0x%08X", rc);
|
---|
641 | #endif
|
---|
642 | default:
|
---|
643 | AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
|
---|
644 | return 0;
|
---|
645 | }
|
---|
646 | break;
|
---|
647 | }
|
---|
648 | #endif /* IN_RING3 */
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * Group 4, structure dumpers.
|
---|
652 | */
|
---|
653 | case 'D':
|
---|
654 | {
|
---|
655 | /*
|
---|
656 | * Interpret the type.
|
---|
657 | */
|
---|
658 | typedef enum
|
---|
659 | {
|
---|
660 | RTST_TIMESPEC
|
---|
661 | } RTST;
|
---|
662 | /** Set if it's a pointer */
|
---|
663 | #define RTST_FLAGS_POINTER RT_BIT(0)
|
---|
664 | static const struct
|
---|
665 | {
|
---|
666 | uint8_t cch; /**< the length of the string. */
|
---|
667 | char sz[16-2]; /**< the part following 'R'. */
|
---|
668 | uint8_t cb; /**< the size of the argument. */
|
---|
669 | uint8_t fFlags; /**< RTST_FLAGS_* */
|
---|
670 | RTST enmType; /**< The structure type. */
|
---|
671 | }
|
---|
672 | /** Sorted array of types, looked up using binary search! */
|
---|
673 | s_aTypes[] =
|
---|
674 | {
|
---|
675 | #define STRMEM(str) sizeof(str) - 1, str
|
---|
676 | { STRMEM("Dtimespec"), sizeof(PCRTTIMESPEC), RTST_FLAGS_POINTER, RTST_TIMESPEC},
|
---|
677 | #undef STRMEM
|
---|
678 | };
|
---|
679 | const char *pszType = *ppszFormat - 1;
|
---|
680 | int iStart = 0;
|
---|
681 | int iEnd = ELEMENTS(s_aTypes) - 1;
|
---|
682 | int i = ELEMENTS(s_aTypes) / 2;
|
---|
683 |
|
---|
684 | union
|
---|
685 | {
|
---|
686 | const void *pv;
|
---|
687 | uint64_t u64;
|
---|
688 | PCRTTIMESPEC pTimeSpec;
|
---|
689 | } u;
|
---|
690 |
|
---|
691 | AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
|
---|
692 |
|
---|
693 | /*
|
---|
694 | * Lookup the type - binary search.
|
---|
695 | */
|
---|
696 | for (;;)
|
---|
697 | {
|
---|
698 | int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
|
---|
699 | if (!iDiff)
|
---|
700 | break;
|
---|
701 | if (iEnd == iStart)
|
---|
702 | {
|
---|
703 | AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
|
---|
704 | return 0;
|
---|
705 | }
|
---|
706 | if (iDiff < 0)
|
---|
707 | iEnd = i - 1;
|
---|
708 | else
|
---|
709 | iStart = i + 1;
|
---|
710 | if (iEnd < iStart)
|
---|
711 | {
|
---|
712 | AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
|
---|
713 | return 0;
|
---|
714 | }
|
---|
715 | i = iStart + (iEnd - iStart) / 2;
|
---|
716 | }
|
---|
717 | *ppszFormat += s_aTypes[i].cch - 1;
|
---|
718 |
|
---|
719 | /*
|
---|
720 | * Fetch the argument.
|
---|
721 | */
|
---|
722 | u.u64 = 0;
|
---|
723 | switch (s_aTypes[i].cb)
|
---|
724 | {
|
---|
725 | case sizeof(const void *):
|
---|
726 | u.pv = va_arg(*pArgs, const void *);
|
---|
727 | break;
|
---|
728 | default:
|
---|
729 | AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
|
---|
730 | break;
|
---|
731 | }
|
---|
732 |
|
---|
733 | /*
|
---|
734 | * If it's a pointer, we'll check if it's valid before going on.
|
---|
735 | */
|
---|
736 | if ((s_aTypes[i].fFlags & RTST_FLAGS_POINTER) && !VALID_PTR(u.pv))
|
---|
737 | return pfnOutput(pvArgOutput, "<null>", sizeof("<null>") - 1);
|
---|
738 |
|
---|
739 | /*
|
---|
740 | * Format the output.
|
---|
741 | */
|
---|
742 | switch (s_aTypes[i].enmType)
|
---|
743 | {
|
---|
744 | case RTST_TIMESPEC:
|
---|
745 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%lld ns", RTTimeSpecGetNano(u.pTimeSpec));
|
---|
746 |
|
---|
747 | default:
|
---|
748 | AssertMsgFailed(("Invalid/unhandled enmType=%d\n", s_aTypes[i].enmType));
|
---|
749 | break;
|
---|
750 | }
|
---|
751 | break;
|
---|
752 | }
|
---|
753 |
|
---|
754 | /*
|
---|
755 | * Invalid/Unknown. Bitch about it.
|
---|
756 | */
|
---|
757 | default:
|
---|
758 | AssertMsgFailed(("Invalid VBox format type '%.10s'!\n", pszFormatOrg));
|
---|
759 | break;
|
---|
760 | }
|
---|
761 | }
|
---|
762 | else
|
---|
763 | AssertMsgFailed(("Invalid VBox format type '%.10s'!\n", pszFormatOrg));
|
---|
764 |
|
---|
765 | NOREF(pszFormatOrg);
|
---|
766 | return 0;
|
---|
767 | }
|
---|
768 |
|
---|
769 |
|
---|