VirtualBox

source: vbox/trunk/src/VBox/Runtime/strformatrt.cpp@ 3392

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

RTTIMESPEC is signed.

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