VirtualBox

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

Last change on this file since 24285 was 23961, checked in by vboxsync, 15 years ago

IPRT: Three new format types: %RTmac, %RTnaipv4 and %RTnapiv6.

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