VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/strformatrt.cpp@ 8479

Last change on this file since 8479 was 8479, checked in by vboxsync, 17 years ago

Added a new IPRT format type '%Rfn' that will drop the return type and parameter list from a PRETTY_FUNCTION.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 38.9 KB
Line 
1/* $Id: strformatrt.cpp 8479 2008-04-29 20:27:58Z vboxsync $ */
2/** @file
3 * IPRT - 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 * - \%Rhrc - Takes a COM/XPCOM status code as argument. Will insert the status
120 * code define corresponding to the Windows error code.
121 * - \%Rhrf - Takes a COM/XPCOM status code as argument. Will insert the
122 * full description of the specified status code.
123 * - \%Rhra - Takes a COM/XPCOM error code as argument. Will insert the
124 * error code define + full description.
125 *
126 * - \%Rfn - Pretty printing of a function or method. It drops the
127 * return code and parameter list.
128 *
129 * On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
130 *
131 *
132 * Group 4, structure dumpers.
133 *
134 * - \%RDtimespec - Takes a PCRTTIMESPEC.
135 *
136 *
137 */
138
139/*******************************************************************************
140* Header Files *
141*******************************************************************************/
142#define LOG_GROUP RTLOGGROUP_STRING
143#include <iprt/log.h>
144#include <iprt/string.h>
145#include <iprt/assert.h>
146#include <iprt/string.h>
147#include <iprt/stdarg.h>
148#ifdef IN_RING3
149# include <iprt/thread.h>
150# include <iprt/err.h>
151#endif
152#include <iprt/time.h>
153#include <iprt/ctype.h>
154#include "internal/string.h"
155
156
157
158/**
159 * Callback to format iprt formatting extentions.
160 * See @ref pg_rt_str_format_rt for a reference on the format types.
161 *
162 * @returns The number of bytes formatted.
163 * @param pfnOutput Pointer to output function.
164 * @param pvArgOutput Argument for the output function.
165 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
166 * after the format specifier.
167 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
168 * @param cchWidth Format Width. -1 if not specified.
169 * @param cchPrecision Format Precision. -1 if not specified.
170 * @param fFlags Flags (RTSTR_NTFS_*).
171 * @param chArgSize The argument size specifier, 'l' or 'L'.
172 */
173size_t rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs, int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize)
174{
175 const char *pszFormatOrg = *ppszFormat;
176 char ch = *(*ppszFormat)++;
177 if (ch == 'R')
178 {
179 ch = *(*ppszFormat)++;
180 switch (ch)
181 {
182 /*
183 * Groups 1 and 2.
184 */
185 case 'T':
186 case 'G':
187 case 'H':
188 case 'C':
189 case 'I':
190 case 'X':
191 case 'U':
192 {
193 /*
194 * Interpret the type.
195 */
196 typedef enum { RTSF_INT, RTSF_INTW, RTSF_FP16, RTSF_FP32, RTSF_FP64, RTSF_UUID, RTSF_BOOL } RTSF;
197 static const struct
198 {
199 uint8_t cch; /**< the length of the string. */
200 char sz[10]; /**< the part following 'R'. */
201 uint8_t cb; /**< the size of the type. */
202 uint8_t u8Base; /**< the size of the type. */
203 RTSF enmFormat; /**< The way to format it. */
204 uint16_t fFlags; /**< additional RTSTR_F_* flags. */
205 }
206 /** Sorted array of types, looked up using binary search! */
207 s_aTypes[] =
208 {
209#define STRMEM(str) sizeof(str) - 1, str
210 { STRMEM("Ci"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
211 { STRMEM("Cp"), sizeof(RTGCPHYS), 16, RTSF_INTW, 0 },
212 { STRMEM("Cr"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
213 { STRMEM("Cu"), sizeof(RTUINT), 10, RTSF_INT, 0 },
214 { STRMEM("Cv"), sizeof(void *), 16, RTSF_INTW, 0 },
215 { STRMEM("Cx"), sizeof(RTUINT), 16, RTSF_INT, 0 },
216 { STRMEM("Gi"), sizeof(RTGCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
217 { STRMEM("Gp"), sizeof(RTGCPHYS), 16, RTSF_INTW, 0 },
218 { STRMEM("Gr"), sizeof(RTGCUINTREG), 16, RTSF_INTW, 0 },
219 { STRMEM("Gu"), sizeof(RTGCUINT), 10, RTSF_INT, 0 },
220 { STRMEM("Gv"), sizeof(RTGCPTR), 16, RTSF_INTW, 0 },
221 { STRMEM("Gx"), sizeof(RTGCUINT), 16, RTSF_INT, 0 },
222 { STRMEM("Hi"), sizeof(RTHCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
223 { STRMEM("Hp"), sizeof(RTHCPHYS), 16, RTSF_INTW, 0 },
224 { STRMEM("Hr"), sizeof(RTGCUINTREG), 16, RTSF_INTW, 0 },
225 { STRMEM("Hu"), sizeof(RTHCUINT), 10, RTSF_INT, 0 },
226 { STRMEM("Hv"), sizeof(RTHCPTR), 16, RTSF_INTW, 0 },
227 { STRMEM("Hx"), sizeof(RTHCUINT), 16, RTSF_INT, 0 },
228 { STRMEM("I16"), sizeof(int16_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
229 { STRMEM("I32"), sizeof(int32_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
230 { STRMEM("I64"), sizeof(int64_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
231 { STRMEM("I8"), sizeof(int8_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
232 { STRMEM("Tbool"), sizeof(bool), 10, RTSF_BOOL, 0 },
233 { STRMEM("Tfile"), sizeof(RTFILE), 10, RTSF_INT, 0 },
234 { STRMEM("Tfmode"), sizeof(RTFMODE), 16, RTSF_INTW, 0 },
235 { STRMEM("Tfoff"), sizeof(RTFOFF), 10, RTSF_INT, RTSTR_F_VALSIGNED },
236 { STRMEM("Tfp16"), sizeof(RTFAR16), 16, RTSF_FP16, RTSTR_F_ZEROPAD },
237 { STRMEM("Tfp32"), sizeof(RTFAR32), 16, RTSF_FP32, RTSTR_F_ZEROPAD },
238 { STRMEM("Tfp64"), sizeof(RTFAR64), 16, RTSF_FP64, RTSTR_F_ZEROPAD },
239 { STRMEM("Tgid"), sizeof(RTGID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
240 { STRMEM("Tino"), sizeof(RTINODE), 16, RTSF_INTW, 0 },
241 { STRMEM("Tint"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
242 { STRMEM("Tiop"), sizeof(RTIOPORT), 16, RTSF_INTW, 0 },
243 { STRMEM("Tldrm"), sizeof(RTLDRMOD), 16, RTSF_INTW, 0 },
244 { STRMEM("Tnthrd"), sizeof(RTNATIVETHREAD), 16, RTSF_INTW, 0 },
245 { STRMEM("Tproc"), sizeof(RTPROCESS), 16, RTSF_INTW, 0 },
246 { STRMEM("Tptr"), sizeof(RTUINTPTR), 16, RTSF_INTW, 0 },
247 { STRMEM("Treg"), sizeof(RTUINTREG), 16, RTSF_INTW, 0 },
248 { STRMEM("Tsel"), sizeof(RTSEL), 16, RTSF_INTW, 0 },
249 { STRMEM("Tsem"), sizeof(RTSEMEVENT), 16, RTSF_INTW, 0 },
250 { STRMEM("Tsock"), sizeof(RTSOCKET), 10, RTSF_INT, 0 },
251 { STRMEM("Tthrd"), sizeof(RTTHREAD), 16, RTSF_INTW, 0 },
252 { STRMEM("Tuid"), sizeof(RTUID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
253 { STRMEM("Tuint"), sizeof(RTUINT), 10, RTSF_INT, 0 },
254 { STRMEM("Tunicp"), sizeof(RTUNICP), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
255 { STRMEM("Tutf16"), sizeof(RTUTF16), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
256 { STRMEM("Tuuid"), sizeof(PCRTUUID), 16, RTSF_UUID, 0 },
257 { STRMEM("Txint"), sizeof(RTUINT), 16, RTSF_INT, 0 },
258 { STRMEM("U16"), sizeof(uint16_t), 10, RTSF_INT, 0 },
259 { STRMEM("U32"), sizeof(uint32_t), 10, RTSF_INT, 0 },
260 { STRMEM("U64"), sizeof(uint64_t), 10, RTSF_INT, 0 },
261 { STRMEM("U8"), sizeof(uint8_t), 10, RTSF_INT, 0 },
262 { STRMEM("X16"), sizeof(uint16_t), 16, RTSF_INT, 0 },
263 { STRMEM("X32"), sizeof(uint32_t), 16, RTSF_INT, 0 },
264 { STRMEM("X64"), sizeof(uint64_t), 16, RTSF_INT, 0 },
265 { STRMEM("X8"), sizeof(uint8_t), 16, RTSF_INT, 0 },
266#undef STRMEM
267 };
268 const char *pszType = *ppszFormat - 1;
269 int iStart = 0;
270 int iEnd = ELEMENTS(s_aTypes) - 1;
271 int i = ELEMENTS(s_aTypes) / 2;
272
273 union
274 {
275 uint8_t u8;
276 uint16_t u16;
277 uint32_t u32;
278 uint64_t u64;
279 int8_t i8;
280 int16_t i16;
281 int32_t i32;
282 int64_t i64;
283 RTFAR16 fp16;
284 RTFAR32 fp32;
285 RTFAR64 fp64;
286 bool fBool;
287 PCRTUUID pUuid;
288 } u;
289 char szBuf[80];
290 unsigned cch;
291
292 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
293
294 /*
295 * Lookup the type - binary search.
296 */
297 for (;;)
298 {
299 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
300 if (!iDiff)
301 break;
302 if (iEnd == iStart)
303 {
304 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
305 return 0;
306 }
307 if (iDiff < 0)
308 iEnd = i - 1;
309 else
310 iStart = i + 1;
311 if (iEnd < iStart)
312 {
313 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
314 return 0;
315 }
316 i = iStart + (iEnd - iStart) / 2;
317 }
318
319 /*
320 * Advance the format string and merge flags.
321 */
322 *ppszFormat += s_aTypes[i].cch - 1;
323 fFlags |= s_aTypes[i].fFlags;
324
325 /*
326 * Fetch the argument.
327 * It's important that a signed value gets sign-extended up to 64-bit.
328 */
329 u.u64 = 0;
330 if (fFlags & RTSTR_F_VALSIGNED)
331 {
332 switch (s_aTypes[i].cb)
333 {
334 case sizeof(int8_t):
335 u.i64 = va_arg(*pArgs, /*int8_t*/int);
336 fFlags |= RTSTR_F_8BIT;
337 break;
338 case sizeof(int16_t):
339 u.i64 = va_arg(*pArgs, /*int16_t*/int);
340 fFlags |= RTSTR_F_16BIT;
341 break;
342 case sizeof(int32_t):
343 u.i64 = va_arg(*pArgs, int32_t);
344 fFlags |= RTSTR_F_32BIT;
345 break;
346 case sizeof(int64_t):
347 u.i64 = va_arg(*pArgs, int64_t);
348 fFlags |= RTSTR_F_64BIT;
349 break;
350 default:
351 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
352 break;
353 }
354 }
355 else
356 {
357 switch (s_aTypes[i].cb)
358 {
359 case sizeof(uint8_t):
360 u.u8 = va_arg(*pArgs, /*uint8_t*/unsigned);
361 fFlags |= RTSTR_F_8BIT;
362 break;
363 case sizeof(uint16_t):
364 u.u16 = va_arg(*pArgs, /*uint16_t*/unsigned);
365 fFlags |= RTSTR_F_16BIT;
366 break;
367 case sizeof(uint32_t):
368 u.u32 = va_arg(*pArgs, uint32_t);
369 fFlags |= RTSTR_F_32BIT;
370 break;
371 case sizeof(uint64_t):
372 u.u64 = va_arg(*pArgs, uint64_t);
373 fFlags |= RTSTR_F_64BIT;
374 break;
375 case sizeof(RTFAR32):
376 u.fp32 = va_arg(*pArgs, RTFAR32);
377 break;
378 case sizeof(RTFAR64):
379 u.fp64 = va_arg(*pArgs, RTFAR64);
380 break;
381 default:
382 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
383 break;
384 }
385 }
386
387 /*
388 * Format the output.
389 */
390 switch (s_aTypes[i].enmFormat)
391 {
392 case RTSF_INT:
393 {
394 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
395 break;
396 }
397
398 /* hex which defaults to max width. */
399 case RTSF_INTW:
400 {
401 Assert(s_aTypes[i].u8Base == 16);
402 if (cchWidth < 0)
403 {
404 cchWidth = s_aTypes[i].cb * 2 + (fFlags & RTSTR_F_SPECIAL ? 2 : 0);
405 fFlags |= RTSTR_F_ZEROPAD;
406 }
407 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
408 break;
409 }
410
411 case RTSF_FP16:
412 {
413 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION);
414 cch = RTStrFormatNumber(&szBuf[0], u.fp16.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
415 Assert(cch == 4);
416 szBuf[4] = ':';
417 cch = RTStrFormatNumber(&szBuf[5], u.fp16.off, 16, 4, -1, fFlags | RTSTR_F_16BIT);
418 Assert(cch == 4);
419 cch = 4 + 1 + 4;
420 break;
421 }
422 case RTSF_FP32:
423 {
424 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION);
425 cch = RTStrFormatNumber(&szBuf[0], u.fp32.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
426 Assert(cch == 4);
427 szBuf[4] = ':';
428 cch = RTStrFormatNumber(&szBuf[5], u.fp32.off, 16, 8, -1, fFlags | RTSTR_F_32BIT);
429 Assert(cch == 8);
430 cch = 4 + 1 + 8;
431 break;
432 }
433 case RTSF_FP64:
434 {
435 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION);
436 cch = RTStrFormatNumber(&szBuf[0], u.fp64.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
437 Assert(cch == 4);
438 szBuf[4] = ':';
439 cch = RTStrFormatNumber(&szBuf[5], u.fp64.off, 16, 16, -1, fFlags | RTSTR_F_64BIT);
440 Assert(cch == 16);
441 cch = 4 + 1 + 16;
442 break;
443 }
444
445 case RTSF_UUID:
446 {
447 static const char szNull[] = "<NULL>";
448
449 if (VALID_PTR(u.pUuid))
450 {
451 /* cannot call RTUuidToStr because of GC/R0. */
452 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
453 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
454 u.pUuid->Gen.u32TimeLow,
455 u.pUuid->Gen.u16TimeMid,
456 u.pUuid->Gen.u16TimeHiAndVersion,
457 u.pUuid->Gen.u16ClockSeq & 0xff,
458 u.pUuid->Gen.u16ClockSeq >> 8,
459 u.pUuid->Gen.au8Node[0],
460 u.pUuid->Gen.au8Node[1],
461 u.pUuid->Gen.au8Node[2],
462 u.pUuid->Gen.au8Node[3],
463 u.pUuid->Gen.au8Node[4],
464 u.pUuid->Gen.au8Node[5]);
465 }
466 return pfnOutput(pvArgOutput, szNull, sizeof(szNull) - 1);
467 }
468
469 case RTSF_BOOL:
470 {
471 static const char szTrue[] = "true ";
472 static const char szFalse[] = "false";
473 if (u.u64 == 1)
474 return pfnOutput(pvArgOutput, szTrue, sizeof(szTrue) - 1);
475 if (u.u64 == 0)
476 return pfnOutput(pvArgOutput, szFalse, sizeof(szFalse) - 1);
477 /* invalid boolean value */
478 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "!%lld!", u.u64);
479 }
480
481 default:
482 AssertMsgFailed(("Internal error %d\n", s_aTypes[i].enmFormat));
483 return 0;
484 }
485
486 /*
487 * Finally, output the formatted string and return.
488 */
489 return pfnOutput(pvArgOutput, szBuf, cch);
490 }
491
492
493 /* Group 3 */
494
495 /*
496 * Pretty function / method name printing.
497 */
498 case 'f':
499 {
500 char ch = *(*ppszFormat)++;
501 switch (ch)
502 {
503 /*
504 * Pretty function / method name printing.
505 * This isn't 100% right (see classic signal prototype) and it assumes
506 * standardized names, but it'll do for today.
507 */
508 case 'n':
509 {
510 const char *pszStart;
511 const char *psz = pszStart = va_arg(*pArgs, const char *);
512 if (!VALID_PTR(psz))
513 return pfnOutput(pvArgOutput, "<null>", sizeof("<null>") - 1);
514
515 while ((ch = *psz) != '\0' && ch != '(')
516 {
517 if (RT_C_IS_BLANK(ch))
518 {
519 psz++;
520 while ((ch = *psz) != '\0' && (RT_C_IS_BLANK(ch) || ch == '('))
521 psz++;
522 if (ch)
523 pszStart = psz;
524 }
525 else if (ch == '(')
526 break;
527 else
528 psz++;
529 }
530
531 return pfnOutput(pvArgOutput, pszStart, psz - pszStart);
532 }
533
534 default:
535 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", ch, pszFormatOrg));
536 break;
537 }
538 break;
539 }
540
541
542 /*
543 * hex dumping and COM/XPCOM.
544 */
545 case 'h':
546 {
547 char ch = *(*ppszFormat)++;
548 switch (ch)
549 {
550 /*
551 * Hex stuff.
552 */
553 case 'x':
554 {
555 uint8_t *pu8 = va_arg(*pArgs, uint8_t *);
556 if (cchWidth <= 0)
557 cchWidth = 16;
558 if (pu8)
559 {
560 ch = *(*ppszFormat)++;
561 switch (ch)
562 {
563 /*
564 * Regular hex dump.
565 */
566 case 'd':
567 {
568 size_t cch = 0;
569 int off = 0;
570
571 if (cchPrecision <= 0)
572 cchPrecision = 16;
573
574 while (off < cchWidth)
575 {
576 int i;
577 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%0*x %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
578 for (i = 0; i < cchPrecision && off + i < cchWidth ; i++)
579 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
580 off + i < cchWidth ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pu8[i]);
581 while (i++ < cchPrecision)
582 cch += pfnOutput(pvArgOutput, " ", 3);
583
584 cch += pfnOutput(pvArgOutput, " ", 1);
585
586 for (i = 0; i < cchPrecision && off + i < cchWidth; i++)
587 {
588 uint8_t u8 = pu8[i];
589 cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
590 }
591
592 /* next */
593 pu8 += cchPrecision;
594 off += cchPrecision;
595 }
596 return cch;
597 }
598
599 /*
600 * Hex string.
601 */
602 case 's':
603 {
604 if (cchWidth-- > 0)
605 {
606 size_t cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++);
607 for (; cchWidth > 0; cchWidth--, pu8++)
608 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8);
609 return cch;
610 }
611 break;
612 }
613
614 default:
615 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", ch, pszFormatOrg));
616 break;
617 }
618 }
619 else
620 return pfnOutput(pvArgOutput, "<null>", sizeof("<null>") - 1);
621 break;
622 }
623
624
625#ifdef IN_RING3
626 /*
627 * XPCOM / COM status code: %Rhrc, %Rhrf, %Rhra
628 * ASSUMES: If Windows Then COM else XPCOM.
629 */
630 case 'r':
631 {
632
633 char ch = *(*ppszFormat)++;
634 uint32_t hrc = va_arg(*pArgs, uint32_t);
635 PCRTCOMERRMSG pMsg = RTErrCOMGet(hrc);
636 switch (ch)
637 {
638 case 'c':
639 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
640 case 'f':
641 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
642 case 'a':
643 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, hrc, pMsg->pszMsgFull);
644 default:
645 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
646 return 0;
647 }
648 break;
649 }
650#endif /* IN_RING3 */
651
652 default:
653 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", ch, pszFormatOrg));
654 return 0;
655
656 }
657 break;
658 }
659
660 /*
661 * iprt status code: %Vrc, %Vrs, %Vrf, %Vra.
662 */
663 case 'r':
664 {
665 int rc = va_arg(*pArgs, int);
666 char ch = *(*ppszFormat)++;
667#ifdef IN_RING3 /* we don't want this anywhere else yet. */
668 PCRTSTATUSMSG pMsg = RTErrGet(rc);
669 switch (ch)
670 {
671 case 'c':
672 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
673 case 's':
674 return pfnOutput(pvArgOutput, pMsg->pszMsgShort, strlen(pMsg->pszMsgShort));
675 case 'f':
676 return pfnOutput(pvArgOutput, pMsg->pszMsgFull, strlen(pMsg->pszMsgFull));
677 case 'a':
678 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
679 default:
680 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
681 return 0;
682 }
683#else /* !IN_RING3 */
684 switch (ch)
685 {
686 case 'c':
687 case 's':
688 case 'f':
689 case 'a':
690 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%d", rc);
691 default:
692 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
693 return 0;
694 }
695#endif /* !IN_RING3 */
696 break;
697 }
698
699#if defined(IN_RING3)
700 /*
701 * Windows status code: %Rwc, %Rwf, %Rwa
702 */
703 case 'w':
704 {
705 long rc = va_arg(*pArgs, long);
706 char ch = *(*ppszFormat)++;
707# if defined(RT_OS_WINDOWS)
708 PCRTWINERRMSG pMsg = RTErrWinGet(rc);
709# endif
710 switch (ch)
711 {
712# if defined(RT_OS_WINDOWS)
713 case 'c':
714 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
715 case 'f':
716 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
717 case 'a':
718 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
719# else
720 case 'c':
721 case 'f':
722 case 'a':
723 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "0x%08X", rc);
724# endif
725 default:
726 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
727 return 0;
728 }
729 break;
730 }
731#endif /* IN_RING3 */
732
733 /*
734 * Group 4, structure dumpers.
735 */
736 case 'D':
737 {
738 /*
739 * Interpret the type.
740 */
741 typedef enum
742 {
743 RTST_TIMESPEC
744 } RTST;
745/** Set if it's a pointer */
746#define RTST_FLAGS_POINTER RT_BIT(0)
747 static const struct
748 {
749 uint8_t cch; /**< the length of the string. */
750 char sz[16-2]; /**< the part following 'R'. */
751 uint8_t cb; /**< the size of the argument. */
752 uint8_t fFlags; /**< RTST_FLAGS_* */
753 RTST enmType; /**< The structure type. */
754 }
755 /** Sorted array of types, looked up using binary search! */
756 s_aTypes[] =
757 {
758#define STRMEM(str) sizeof(str) - 1, str
759 { STRMEM("Dtimespec"), sizeof(PCRTTIMESPEC), RTST_FLAGS_POINTER, RTST_TIMESPEC},
760#undef STRMEM
761 };
762 const char *pszType = *ppszFormat - 1;
763 int iStart = 0;
764 int iEnd = ELEMENTS(s_aTypes) - 1;
765 int i = ELEMENTS(s_aTypes) / 2;
766
767 union
768 {
769 const void *pv;
770 uint64_t u64;
771 PCRTTIMESPEC pTimeSpec;
772 } u;
773
774 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
775
776 /*
777 * Lookup the type - binary search.
778 */
779 for (;;)
780 {
781 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
782 if (!iDiff)
783 break;
784 if (iEnd == iStart)
785 {
786 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
787 return 0;
788 }
789 if (iDiff < 0)
790 iEnd = i - 1;
791 else
792 iStart = i + 1;
793 if (iEnd < iStart)
794 {
795 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
796 return 0;
797 }
798 i = iStart + (iEnd - iStart) / 2;
799 }
800 *ppszFormat += s_aTypes[i].cch - 1;
801
802 /*
803 * Fetch the argument.
804 */
805 u.u64 = 0;
806 switch (s_aTypes[i].cb)
807 {
808 case sizeof(const void *):
809 u.pv = va_arg(*pArgs, const void *);
810 break;
811 default:
812 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
813 break;
814 }
815
816 /*
817 * If it's a pointer, we'll check if it's valid before going on.
818 */
819 if ((s_aTypes[i].fFlags & RTST_FLAGS_POINTER) && !VALID_PTR(u.pv))
820 return pfnOutput(pvArgOutput, "<null>", sizeof("<null>") - 1);
821
822 /*
823 * Format the output.
824 */
825 switch (s_aTypes[i].enmType)
826 {
827 case RTST_TIMESPEC:
828 return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%lld ns", RTTimeSpecGetNano(u.pTimeSpec));
829
830 default:
831 AssertMsgFailed(("Invalid/unhandled enmType=%d\n", s_aTypes[i].enmType));
832 break;
833 }
834 break;
835 }
836
837 /*
838 * Invalid/Unknown. Bitch about it.
839 */
840 default:
841 AssertMsgFailed(("Invalid VBox format type '%.10s'!\n", pszFormatOrg));
842 break;
843 }
844 }
845 else
846 AssertMsgFailed(("Invalid VBox format type '%.10s'!\n", pszFormatOrg));
847
848 NOREF(pszFormatOrg);
849 return 0;
850}
851
852
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette