VirtualBox

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

Last change on this file since 66284 was 66284, checked in by vboxsync, 8 years ago

Runtime: introduced 'RKv' format specifier

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 54.7 KB
Line 
1/* $Id: strformatrt.cpp 66284 2017-03-28 09:04:15Z vboxsync $ */
2/** @file
3 * IPRT - IPRT String Formatter Extensions.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
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
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_STRING
32#include <iprt/string.h>
33#ifndef RT_NO_EXPORT_SYMBOL
34# define RT_NO_EXPORT_SYMBOL /* don't slurp <linux/module.h> which then again
35 slurps arch-specific headers defining symbols */
36#endif
37#include "internal/iprt.h"
38
39#include <iprt/log.h>
40#include <iprt/assert.h>
41#include <iprt/string.h>
42#include <iprt/stdarg.h>
43#ifdef IN_RING3
44# include <iprt/thread.h>
45# include <iprt/err.h>
46#endif
47#include <iprt/ctype.h>
48#include <iprt/time.h>
49#include <iprt/net.h>
50#include <iprt/path.h>
51#include <iprt/asm.h>
52#define STRFORMAT_WITH_X86
53#ifdef STRFORMAT_WITH_X86
54# include <iprt/x86.h>
55#endif
56#include "internal/string.h"
57
58
59/*********************************************************************************************************************************
60* Global Variables *
61*********************************************************************************************************************************/
62static char g_szHexDigits[17] = "0123456789abcdef";
63
64
65/**
66 * Helper that formats a 16-bit hex word in a IPv6 address.
67 *
68 * @returns Length in chars.
69 * @param pszDst The output buffer. Written from the start.
70 * @param uWord The word to format as hex.
71 */
72static size_t rtstrFormatIPv6HexWord(char *pszDst, uint16_t uWord)
73{
74 size_t off;
75 uint16_t cDigits;
76
77 if (uWord & UINT16_C(0xff00))
78 cDigits = uWord & UINT16_C(0xf000) ? 4 : 3;
79 else
80 cDigits = uWord & UINT16_C(0x00f0) ? 2 : 1;
81
82 off = 0;
83 switch (cDigits)
84 {
85 case 4: pszDst[off++] = g_szHexDigits[(uWord >> 12) & 0xf]; /* fall thru */
86 case 3: pszDst[off++] = g_szHexDigits[(uWord >> 8) & 0xf]; /* fall thru */
87 case 2: pszDst[off++] = g_szHexDigits[(uWord >> 4) & 0xf]; /* fall thru */
88 case 1: pszDst[off++] = g_szHexDigits[(uWord >> 0) & 0xf];
89 break;
90 }
91 pszDst[off] = '\0';
92 return off;
93}
94
95
96/**
97 * Helper function to format IPv6 address according to RFC 5952.
98 *
99 * @returns The number of bytes formatted.
100 * @param pfnOutput Pointer to output function.
101 * @param pvArgOutput Argument for the output function.
102 * @param pIpv6Addr IPv6 address
103 */
104static size_t rtstrFormatIPv6(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PCRTNETADDRIPV6 pIpv6Addr)
105{
106 size_t cch; /* result */
107 bool fEmbeddedIpv4;
108 size_t cwHexPart;
109 size_t cwLongestZeroRun;
110 size_t iLongestZeroStart;
111 size_t idx;
112 char szHexWord[8];
113
114 Assert(pIpv6Addr != NULL);
115
116 /*
117 * Check for embedded IPv4 address.
118 *
119 * IPv4-compatible - ::11.22.33.44 (obsolete)
120 * IPv4-mapped - ::ffff:11.22.33.44
121 * IPv4-translated - ::ffff:0:11.22.33.44 (RFC 2765)
122 */
123 fEmbeddedIpv4 = false;
124 cwHexPart = RT_ELEMENTS(pIpv6Addr->au16);
125 if ( pIpv6Addr->au64[0] == 0
126 && ( ( pIpv6Addr->au32[2] == 0
127 && pIpv6Addr->au32[3] != 0
128 && pIpv6Addr->au32[3] != RT_H2BE_U32_C(1) )
129 || pIpv6Addr->au32[2] == RT_H2BE_U32_C(0x0000ffff)
130 || pIpv6Addr->au32[2] == RT_H2BE_U32_C(0xffff0000) ) )
131 {
132 fEmbeddedIpv4 = true;
133 cwHexPart -= 2;
134 }
135
136 /*
137 * Find the longest sequences of two or more zero words.
138 */
139 cwLongestZeroRun = 0;
140 iLongestZeroStart = 0;
141 for (idx = 0; idx < cwHexPart; idx++)
142 if (pIpv6Addr->au16[idx] == 0)
143 {
144 size_t iZeroStart = idx;
145 size_t cwZeroRun;
146 do
147 idx++;
148 while (idx < cwHexPart && pIpv6Addr->au16[idx] == 0);
149 cwZeroRun = idx - iZeroStart;
150 if (cwZeroRun > 1 && cwZeroRun > cwLongestZeroRun)
151 {
152 cwLongestZeroRun = cwZeroRun;
153 iLongestZeroStart = iZeroStart;
154 if (cwZeroRun >= cwHexPart - idx)
155 break;
156 }
157 }
158
159 /*
160 * Do the formatting.
161 */
162 cch = 0;
163 if (cwLongestZeroRun == 0)
164 {
165 for (idx = 0; idx < cwHexPart; ++idx)
166 {
167 if (idx > 0)
168 cch += pfnOutput(pvArgOutput, ":", 1);
169 cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
170 }
171
172 if (fEmbeddedIpv4)
173 cch += pfnOutput(pvArgOutput, ":", 1);
174 }
175 else
176 {
177 const size_t iLongestZeroEnd = iLongestZeroStart + cwLongestZeroRun;
178
179 if (iLongestZeroStart == 0)
180 cch += pfnOutput(pvArgOutput, ":", 1);
181 else
182 for (idx = 0; idx < iLongestZeroStart; ++idx)
183 {
184 cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
185 cch += pfnOutput(pvArgOutput, ":", 1);
186 }
187
188 if (iLongestZeroEnd == cwHexPart)
189 cch += pfnOutput(pvArgOutput, ":", 1);
190 else
191 {
192 for (idx = iLongestZeroEnd; idx < cwHexPart; ++idx)
193 {
194 cch += pfnOutput(pvArgOutput, ":", 1);
195 cch += pfnOutput(pvArgOutput, szHexWord, rtstrFormatIPv6HexWord(szHexWord, RT_BE2H_U16(pIpv6Addr->au16[idx])));
196 }
197
198 if (fEmbeddedIpv4)
199 cch += pfnOutput(pvArgOutput, ":", 1);
200 }
201 }
202
203 if (fEmbeddedIpv4)
204 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
205 "%u.%u.%u.%u",
206 pIpv6Addr->au8[12],
207 pIpv6Addr->au8[13],
208 pIpv6Addr->au8[14],
209 pIpv6Addr->au8[15]);
210
211 return cch;
212}
213
214
215/**
216 * Callback to format iprt formatting extentions.
217 * See @ref pg_rt_str_format for a reference on the format types.
218 *
219 * @returns The number of bytes formatted.
220 * @param pfnOutput Pointer to output function.
221 * @param pvArgOutput Argument for the output function.
222 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
223 * after the format specifier.
224 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
225 * @param cchWidth Format Width. -1 if not specified.
226 * @param cchPrecision Format Precision. -1 if not specified.
227 * @param fFlags Flags (RTSTR_NTFS_*).
228 * @param chArgSize The argument size specifier, 'l' or 'L'.
229 */
230DECLHIDDEN(size_t) rtstrFormatRt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs,
231 int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize)
232{
233 const char *pszFormatOrg = *ppszFormat;
234 char ch = *(*ppszFormat)++;
235 size_t cch;
236 char szBuf[80];
237
238 if (ch == 'R')
239 {
240 ch = *(*ppszFormat)++;
241 switch (ch)
242 {
243 /*
244 * Groups 1 and 2.
245 */
246 case 'T':
247 case 'G':
248 case 'H':
249 case 'R':
250 case 'C':
251 case 'I':
252 case 'X':
253 case 'U':
254 case 'K':
255 {
256 /*
257 * Interpret the type.
258 */
259 typedef enum
260 {
261 RTSF_INT,
262 RTSF_INTW,
263 RTSF_BOOL,
264 RTSF_FP16,
265 RTSF_FP32,
266 RTSF_FP64,
267 RTSF_IPV4,
268 RTSF_IPV6,
269 RTSF_MAC,
270 RTSF_NETADDR,
271 RTSF_UUID
272 } RTSF;
273 static const struct
274 {
275 uint8_t cch; /**< the length of the string. */
276 char sz[10]; /**< the part following 'R'. */
277 uint8_t cb; /**< the size of the type. */
278 uint8_t u8Base; /**< the size of the type. */
279 RTSF enmFormat; /**< The way to format it. */
280 uint16_t fFlags; /**< additional RTSTR_F_* flags. */
281 }
282 /** Sorted array of types, looked up using binary search! */
283 s_aTypes[] =
284 {
285#define STRMEM(str) sizeof(str) - 1, str
286 { STRMEM("Ci"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
287 { STRMEM("Cp"), sizeof(RTCCPHYS), 16, RTSF_INTW, 0 },
288 { STRMEM("Cr"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
289 { STRMEM("Cu"), sizeof(RTUINT), 10, RTSF_INT, 0 },
290 { STRMEM("Cv"), sizeof(void *), 16, RTSF_INTW, 0 },
291 { STRMEM("Cx"), sizeof(RTUINT), 16, RTSF_INT, 0 },
292 { STRMEM("Gi"), sizeof(RTGCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
293 { STRMEM("Gp"), sizeof(RTGCPHYS), 16, RTSF_INTW, 0 },
294 { STRMEM("Gr"), sizeof(RTGCUINTREG), 16, RTSF_INTW, 0 },
295 { STRMEM("Gu"), sizeof(RTGCUINT), 10, RTSF_INT, 0 },
296 { STRMEM("Gv"), sizeof(RTGCPTR), 16, RTSF_INTW, 0 },
297 { STRMEM("Gx"), sizeof(RTGCUINT), 16, RTSF_INT, 0 },
298 { STRMEM("Hi"), sizeof(RTHCINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
299 { STRMEM("Hp"), sizeof(RTHCPHYS), 16, RTSF_INTW, 0 },
300 { STRMEM("Hr"), sizeof(RTHCUINTREG), 16, RTSF_INTW, 0 },
301 { STRMEM("Hu"), sizeof(RTHCUINT), 10, RTSF_INT, 0 },
302 { STRMEM("Hv"), sizeof(RTHCPTR), 16, RTSF_INTW, 0 },
303 { STRMEM("Hx"), sizeof(RTHCUINT), 16, RTSF_INT, 0 },
304 { STRMEM("I16"), sizeof(int16_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
305 { STRMEM("I32"), sizeof(int32_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
306 { STRMEM("I64"), sizeof(int64_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
307 { STRMEM("I8"), sizeof(int8_t), 10, RTSF_INT, RTSTR_F_VALSIGNED },
308 { STRMEM("Kv"), sizeof(RTHCPTR), 16, RTSF_INT, RTSTR_F_OBFUSCATE_PTR },
309 { STRMEM("Rv"), sizeof(RTRCPTR), 16, RTSF_INTW, 0 },
310 { STRMEM("Tbool"), sizeof(bool), 10, RTSF_BOOL, 0 },
311 { STRMEM("Tfile"), sizeof(RTFILE), 10, RTSF_INT, 0 },
312 { STRMEM("Tfmode"), sizeof(RTFMODE), 16, RTSF_INTW, 0 },
313 { STRMEM("Tfoff"), sizeof(RTFOFF), 10, RTSF_INT, RTSTR_F_VALSIGNED },
314 { STRMEM("Tfp16"), sizeof(RTFAR16), 16, RTSF_FP16, RTSTR_F_ZEROPAD },
315 { STRMEM("Tfp32"), sizeof(RTFAR32), 16, RTSF_FP32, RTSTR_F_ZEROPAD },
316 { STRMEM("Tfp64"), sizeof(RTFAR64), 16, RTSF_FP64, RTSTR_F_ZEROPAD },
317 { STRMEM("Tgid"), sizeof(RTGID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
318 { STRMEM("Tino"), sizeof(RTINODE), 16, RTSF_INTW, 0 },
319 { STRMEM("Tint"), sizeof(RTINT), 10, RTSF_INT, RTSTR_F_VALSIGNED },
320 { STRMEM("Tiop"), sizeof(RTIOPORT), 16, RTSF_INTW, 0 },
321 { STRMEM("Tldrm"), sizeof(RTLDRMOD), 16, RTSF_INTW, 0 },
322 { STRMEM("Tmac"), sizeof(PCRTMAC), 16, RTSF_MAC, 0 },
323 { STRMEM("Tnaddr"), sizeof(PCRTNETADDR), 10, RTSF_NETADDR,0 },
324 { STRMEM("Tnaipv4"), sizeof(RTNETADDRIPV4), 10, RTSF_IPV4, 0 },
325 { STRMEM("Tnaipv6"), sizeof(PCRTNETADDRIPV6),16, RTSF_IPV6, 0 },
326 { STRMEM("Tnthrd"), sizeof(RTNATIVETHREAD), 16, RTSF_INTW, 0 },
327 { STRMEM("Tproc"), sizeof(RTPROCESS), 16, RTSF_INTW, 0 },
328 { STRMEM("Tptr"), sizeof(RTUINTPTR), 16, RTSF_INTW, 0 },
329 { STRMEM("Treg"), sizeof(RTCCUINTREG), 16, RTSF_INTW, 0 },
330 { STRMEM("Tsel"), sizeof(RTSEL), 16, RTSF_INTW, 0 },
331 { STRMEM("Tsem"), sizeof(RTSEMEVENT), 16, RTSF_INTW, 0 },
332 { STRMEM("Tsock"), sizeof(RTSOCKET), 10, RTSF_INT, 0 },
333 { STRMEM("Tthrd"), sizeof(RTTHREAD), 16, RTSF_INTW, 0 },
334 { STRMEM("Tuid"), sizeof(RTUID), 10, RTSF_INT, RTSTR_F_VALSIGNED },
335 { STRMEM("Tuint"), sizeof(RTUINT), 10, RTSF_INT, 0 },
336 { STRMEM("Tunicp"), sizeof(RTUNICP), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
337 { STRMEM("Tutf16"), sizeof(RTUTF16), 16, RTSF_INTW, RTSTR_F_ZEROPAD },
338 { STRMEM("Tuuid"), sizeof(PCRTUUID), 16, RTSF_UUID, 0 },
339 { STRMEM("Txint"), sizeof(RTUINT), 16, RTSF_INT, 0 },
340 { STRMEM("U16"), sizeof(uint16_t), 10, RTSF_INT, 0 },
341 { STRMEM("U32"), sizeof(uint32_t), 10, RTSF_INT, 0 },
342 { STRMEM("U64"), sizeof(uint64_t), 10, RTSF_INT, 0 },
343 { STRMEM("U8"), sizeof(uint8_t), 10, RTSF_INT, 0 },
344 { STRMEM("X16"), sizeof(uint16_t), 16, RTSF_INT, 0 },
345 { STRMEM("X32"), sizeof(uint32_t), 16, RTSF_INT, 0 },
346 { STRMEM("X64"), sizeof(uint64_t), 16, RTSF_INT, 0 },
347 { STRMEM("X8"), sizeof(uint8_t), 16, RTSF_INT, 0 },
348#undef STRMEM
349 };
350 static const char s_szNull[] = "<NULL>";
351
352 const char *pszType = *ppszFormat - 1;
353 int iStart = 0;
354 int iEnd = RT_ELEMENTS(s_aTypes) - 1;
355 int i = RT_ELEMENTS(s_aTypes) / 2;
356
357 union
358 {
359 uint8_t u8;
360 uint16_t u16;
361 uint32_t u32;
362 uint64_t u64;
363 int8_t i8;
364 int16_t i16;
365 int32_t i32;
366 int64_t i64;
367 RTFAR16 fp16;
368 RTFAR32 fp32;
369 RTFAR64 fp64;
370 bool fBool;
371 PCRTMAC pMac;
372 RTNETADDRIPV4 Ipv4Addr;
373 PCRTNETADDRIPV6 pIpv6Addr;
374 PCRTNETADDR pNetAddr;
375 PCRTUUID pUuid;
376 } u;
377
378 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
379 RT_NOREF_PV(chArgSize);
380
381 /*
382 * Lookup the type - binary search.
383 */
384 for (;;)
385 {
386 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
387 if (!iDiff)
388 break;
389 if (iEnd == iStart)
390 {
391 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
392 return 0;
393 }
394 if (iDiff < 0)
395 iEnd = i - 1;
396 else
397 iStart = i + 1;
398 if (iEnd < iStart)
399 {
400 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
401 return 0;
402 }
403 i = iStart + (iEnd - iStart) / 2;
404 }
405
406 /*
407 * Advance the format string and merge flags.
408 */
409 *ppszFormat += s_aTypes[i].cch - 1;
410 fFlags |= s_aTypes[i].fFlags;
411
412 /*
413 * Fetch the argument.
414 * It's important that a signed value gets sign-extended up to 64-bit.
415 */
416 RT_ZERO(u);
417 if (fFlags & RTSTR_F_VALSIGNED)
418 {
419 switch (s_aTypes[i].cb)
420 {
421 case sizeof(int8_t):
422 u.i64 = va_arg(*pArgs, /*int8_t*/int);
423 fFlags |= RTSTR_F_8BIT;
424 break;
425 case sizeof(int16_t):
426 u.i64 = va_arg(*pArgs, /*int16_t*/int);
427 fFlags |= RTSTR_F_16BIT;
428 break;
429 case sizeof(int32_t):
430 u.i64 = va_arg(*pArgs, int32_t);
431 fFlags |= RTSTR_F_32BIT;
432 break;
433 case sizeof(int64_t):
434 u.i64 = va_arg(*pArgs, int64_t);
435 fFlags |= RTSTR_F_64BIT;
436 break;
437 default:
438 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
439 break;
440 }
441 }
442 else
443 {
444 switch (s_aTypes[i].cb)
445 {
446 case sizeof(uint8_t):
447 u.u8 = va_arg(*pArgs, /*uint8_t*/unsigned);
448 fFlags |= RTSTR_F_8BIT;
449 break;
450 case sizeof(uint16_t):
451 u.u16 = va_arg(*pArgs, /*uint16_t*/unsigned);
452 fFlags |= RTSTR_F_16BIT;
453 break;
454 case sizeof(uint32_t):
455 u.u32 = va_arg(*pArgs, uint32_t);
456 fFlags |= RTSTR_F_32BIT;
457 break;
458 case sizeof(uint64_t):
459 u.u64 = va_arg(*pArgs, uint64_t);
460 fFlags |= RTSTR_F_64BIT;
461 break;
462 case sizeof(RTFAR32):
463 u.fp32 = va_arg(*pArgs, RTFAR32);
464 break;
465 case sizeof(RTFAR64):
466 u.fp64 = va_arg(*pArgs, RTFAR64);
467 break;
468 default:
469 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
470 break;
471 }
472 }
473
474 /*
475 * For now don't show the address.
476 */
477 cch = 0;
478 if (fFlags & RTSTR_F_OBFUSCATE_PTR)
479 {
480 if (fFlags & RTSTR_F_SPECIAL)
481 cch += pfnOutput(pvArgOutput, RT_STR_TUPLE("0x"));
482
483#ifdef RT_ARCH_X86
484 cch += pfnOutput(pvArgOutput, RT_STR_TUPLE("XXXXXXXX"));
485#else
486 cch += pfnOutput(pvArgOutput, RT_STR_TUPLE("XXXXXXXXXXXXXXXX"));
487#endif
488 return cch;
489 }
490
491 /*
492 * Format the output.
493 */
494 switch (s_aTypes[i].enmFormat)
495 {
496 case RTSF_INT:
497 {
498 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
499 break;
500 }
501
502 /* hex which defaults to max width. */
503 case RTSF_INTW:
504 {
505 Assert(s_aTypes[i].u8Base == 16);
506 if (cchWidth < 0)
507 {
508 cchWidth = s_aTypes[i].cb * 2 + (fFlags & RTSTR_F_SPECIAL ? 2 : 0);
509 fFlags |= RTSTR_F_ZEROPAD;
510 }
511 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
512 break;
513 }
514
515 case RTSF_BOOL:
516 {
517 static const char s_szTrue[] = "true ";
518 static const char s_szFalse[] = "false";
519 if (u.u64 == 1)
520 return pfnOutput(pvArgOutput, s_szTrue, sizeof(s_szTrue) - 1);
521 if (u.u64 == 0)
522 return pfnOutput(pvArgOutput, s_szFalse, sizeof(s_szFalse) - 1);
523 /* invalid boolean value */
524 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "!%lld!", u.u64);
525 }
526
527 case RTSF_FP16:
528 {
529 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
530 cch = RTStrFormatNumber(&szBuf[0], u.fp16.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
531 Assert(cch == 4);
532 szBuf[4] = ':';
533 cch = RTStrFormatNumber(&szBuf[5], u.fp16.off, 16, 4, -1, fFlags | RTSTR_F_16BIT);
534 Assert(cch == 4);
535 cch = 4 + 1 + 4;
536 break;
537 }
538 case RTSF_FP32:
539 {
540 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
541 cch = RTStrFormatNumber(&szBuf[0], u.fp32.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
542 Assert(cch == 4);
543 szBuf[4] = ':';
544 cch = RTStrFormatNumber(&szBuf[5], u.fp32.off, 16, 8, -1, fFlags | RTSTR_F_32BIT);
545 Assert(cch == 8);
546 cch = 4 + 1 + 8;
547 break;
548 }
549 case RTSF_FP64:
550 {
551 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
552 cch = RTStrFormatNumber(&szBuf[0], u.fp64.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
553 Assert(cch == 4);
554 szBuf[4] = ':';
555 cch = RTStrFormatNumber(&szBuf[5], u.fp64.off, 16, 16, -1, fFlags | RTSTR_F_64BIT);
556 Assert(cch == 16);
557 cch = 4 + 1 + 16;
558 break;
559 }
560
561 case RTSF_IPV4:
562 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
563 "%u.%u.%u.%u",
564 u.Ipv4Addr.au8[0],
565 u.Ipv4Addr.au8[1],
566 u.Ipv4Addr.au8[2],
567 u.Ipv4Addr.au8[3]);
568
569 case RTSF_IPV6:
570 {
571 if (VALID_PTR(u.pIpv6Addr))
572 return rtstrFormatIPv6(pfnOutput, pvArgOutput, u.pIpv6Addr);
573 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
574 }
575
576 case RTSF_MAC:
577 {
578 if (VALID_PTR(u.pMac))
579 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
580 "%02x:%02x:%02x:%02x:%02x:%02x",
581 u.pMac->au8[0],
582 u.pMac->au8[1],
583 u.pMac->au8[2],
584 u.pMac->au8[3],
585 u.pMac->au8[4],
586 u.pMac->au8[5]);
587 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
588 }
589
590 case RTSF_NETADDR:
591 {
592 if (VALID_PTR(u.pNetAddr))
593 {
594 switch (u.pNetAddr->enmType)
595 {
596 case RTNETADDRTYPE_IPV4:
597 if (u.pNetAddr->uPort == RTNETADDR_PORT_NA)
598 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
599 "%u.%u.%u.%u",
600 u.pNetAddr->uAddr.IPv4.au8[0],
601 u.pNetAddr->uAddr.IPv4.au8[1],
602 u.pNetAddr->uAddr.IPv4.au8[2],
603 u.pNetAddr->uAddr.IPv4.au8[3]);
604 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
605 "%u.%u.%u.%u:%u",
606 u.pNetAddr->uAddr.IPv4.au8[0],
607 u.pNetAddr->uAddr.IPv4.au8[1],
608 u.pNetAddr->uAddr.IPv4.au8[2],
609 u.pNetAddr->uAddr.IPv4.au8[3],
610 u.pNetAddr->uPort);
611
612 case RTNETADDRTYPE_IPV6:
613 if (u.pNetAddr->uPort == RTNETADDR_PORT_NA)
614 return rtstrFormatIPv6(pfnOutput, pvArgOutput, &u.pNetAddr->uAddr.IPv6);
615
616 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
617 "[%RTnaipv6]:%u",
618 &u.pNetAddr->uAddr.IPv6,
619 u.pNetAddr->uPort);
620
621 case RTNETADDRTYPE_MAC:
622 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
623 "%02x:%02x:%02x:%02x:%02x:%02x",
624 u.pNetAddr->uAddr.Mac.au8[0],
625 u.pNetAddr->uAddr.Mac.au8[1],
626 u.pNetAddr->uAddr.Mac.au8[2],
627 u.pNetAddr->uAddr.Mac.au8[3],
628 u.pNetAddr->uAddr.Mac.au8[4],
629 u.pNetAddr->uAddr.Mac.au8[5]);
630
631 default:
632 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
633 "unsupported-netaddr-type=%u", u.pNetAddr->enmType);
634
635 }
636 }
637 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
638 }
639
640 case RTSF_UUID:
641 {
642 if (VALID_PTR(u.pUuid))
643 {
644 /* cannot call RTUuidToStr because of GC/R0. */
645 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
646 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
647 RT_H2LE_U32(u.pUuid->Gen.u32TimeLow),
648 RT_H2LE_U16(u.pUuid->Gen.u16TimeMid),
649 RT_H2LE_U16(u.pUuid->Gen.u16TimeHiAndVersion),
650 u.pUuid->Gen.u8ClockSeqHiAndReserved,
651 u.pUuid->Gen.u8ClockSeqLow,
652 u.pUuid->Gen.au8Node[0],
653 u.pUuid->Gen.au8Node[1],
654 u.pUuid->Gen.au8Node[2],
655 u.pUuid->Gen.au8Node[3],
656 u.pUuid->Gen.au8Node[4],
657 u.pUuid->Gen.au8Node[5]);
658 }
659 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
660 }
661
662 default:
663 AssertMsgFailed(("Internal error %d\n", s_aTypes[i].enmFormat));
664 return 0;
665 }
666
667 /*
668 * Finally, output the formatted string and return.
669 */
670 return pfnOutput(pvArgOutput, szBuf, cch);
671 }
672
673
674 /* Group 3 */
675
676 /*
677 * Base name printing.
678 */
679 case 'b':
680 {
681 switch (*(*ppszFormat)++)
682 {
683 case 'n':
684 {
685 const char *pszLastSep;
686 const char *psz = pszLastSep = va_arg(*pArgs, const char *);
687 if (!VALID_PTR(psz))
688 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
689
690 while ((ch = *psz) != '\0')
691 {
692 if (RTPATH_IS_SEP(ch))
693 {
694 do
695 psz++;
696 while ((ch = *psz) != '\0' && RTPATH_IS_SEP(ch));
697 if (!ch)
698 break;
699 pszLastSep = psz;
700 }
701 psz++;
702 }
703
704 return pfnOutput(pvArgOutput, pszLastSep, psz - pszLastSep);
705 }
706
707 default:
708 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
709 break;
710 }
711 break;
712 }
713
714
715 /*
716 * Pretty function / method name printing.
717 */
718 case 'f':
719 {
720 switch (*(*ppszFormat)++)
721 {
722 /*
723 * Pretty function / method name printing.
724 * This isn't 100% right (see classic signal prototype) and it assumes
725 * standardized names, but it'll do for today.
726 */
727 case 'n':
728 {
729 const char *pszStart;
730 const char *psz = pszStart = va_arg(*pArgs, const char *);
731 int cAngle = 0;
732
733 if (!VALID_PTR(psz))
734 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
735
736 while ((ch = *psz) != '\0' && ch != '(')
737 {
738 if (RT_C_IS_BLANK(ch))
739 {
740 psz++;
741 while ((ch = *psz) != '\0' && (RT_C_IS_BLANK(ch) || ch == '('))
742 psz++;
743 if (ch && cAngle == 0)
744 pszStart = psz;
745 }
746 else if (ch == '(')
747 break;
748 else if (ch == '<')
749 {
750 cAngle++;
751 psz++;
752 }
753 else if (ch == '>')
754 {
755 cAngle--;
756 psz++;
757 }
758 else
759 psz++;
760 }
761
762 return pfnOutput(pvArgOutput, pszStart, psz - pszStart);
763 }
764
765 default:
766 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
767 break;
768 }
769 break;
770 }
771
772
773 /*
774 * hex dumping and COM/XPCOM.
775 */
776 case 'h':
777 {
778 switch (*(*ppszFormat)++)
779 {
780 /*
781 * Hex stuff.
782 */
783 case 'x':
784 {
785 uint8_t *pu8 = va_arg(*pArgs, uint8_t *);
786 if (cchPrecision < 0)
787 cchPrecision = 16;
788 if (pu8)
789 {
790 switch (*(*ppszFormat)++)
791 {
792 /*
793 * Regular hex dump.
794 */
795 case 'd':
796 {
797 int off = 0;
798 cch = 0;
799
800 if (cchWidth <= 0)
801 cchWidth = 16;
802
803 while (off < cchPrecision)
804 {
805 int i;
806 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%0*p %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
807 for (i = 0; i < cchWidth && off + i < cchPrecision ; i++)
808 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
809 off + i < cchPrecision ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pu8[i]);
810 while (i++ < cchWidth)
811 cch += pfnOutput(pvArgOutput, " ", 3);
812
813 cch += pfnOutput(pvArgOutput, " ", 1);
814
815 for (i = 0; i < cchWidth && off + i < cchPrecision; i++)
816 {
817 uint8_t u8 = pu8[i];
818 cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
819 }
820
821 /* next */
822 pu8 += cchWidth;
823 off += cchWidth;
824 }
825 return cch;
826 }
827
828 /*
829 * Hex string.
830 */
831 case 's':
832 {
833 if (cchPrecision-- > 0)
834 {
835 cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++);
836 for (; cchPrecision > 0; cchPrecision--, pu8++)
837 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8);
838 return cch;
839 }
840 break;
841 }
842
843 default:
844 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
845 break;
846 }
847 }
848 else
849 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
850 break;
851 }
852
853
854#ifdef IN_RING3
855 /*
856 * XPCOM / COM status code: %Rhrc, %Rhrf, %Rhra
857 * ASSUMES: If Windows Then COM else XPCOM.
858 */
859 case 'r':
860 {
861 uint32_t hrc = va_arg(*pArgs, uint32_t);
862 PCRTCOMERRMSG pMsg = RTErrCOMGet(hrc);
863 switch (*(*ppszFormat)++)
864 {
865 case 'c':
866 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
867 case 'f':
868 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
869 case 'a':
870 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, hrc, pMsg->pszMsgFull);
871 default:
872 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
873 return 0;
874 }
875 break;
876 }
877#endif /* IN_RING3 */
878
879 default:
880 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
881 return 0;
882
883 }
884 break;
885 }
886
887 /*
888 * iprt status code: %Rrc, %Rrs, %Rrf, %Rra.
889 */
890 case 'r':
891 {
892 int rc = va_arg(*pArgs, int);
893#ifdef IN_RING3 /* we don't want this anywhere else yet. */
894 PCRTSTATUSMSG pMsg = RTErrGet(rc);
895 switch (*(*ppszFormat)++)
896 {
897 case 'c':
898 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
899 case 's':
900 return pfnOutput(pvArgOutput, pMsg->pszMsgShort, strlen(pMsg->pszMsgShort));
901 case 'f':
902 return pfnOutput(pvArgOutput, pMsg->pszMsgFull, strlen(pMsg->pszMsgFull));
903 case 'a':
904 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
905 default:
906 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
907 return 0;
908 }
909#else /* !IN_RING3 */
910 switch (*(*ppszFormat)++)
911 {
912 case 'c':
913 case 's':
914 case 'f':
915 case 'a':
916 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%d", rc);
917 default:
918 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
919 return 0;
920 }
921#endif /* !IN_RING3 */
922 break;
923 }
924
925#if defined(IN_RING3)
926 /*
927 * Windows status code: %Rwc, %Rwf, %Rwa
928 */
929 case 'w':
930 {
931 long rc = va_arg(*pArgs, long);
932# if defined(RT_OS_WINDOWS)
933 PCRTWINERRMSG pMsg = RTErrWinGet(rc);
934# endif
935 switch (*(*ppszFormat)++)
936 {
937# if defined(RT_OS_WINDOWS)
938 case 'c':
939 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
940 case 'f':
941 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
942 case 'a':
943 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
944# else
945 case 'c':
946 case 'f':
947 case 'a':
948 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "0x%08X", rc);
949# endif
950 default:
951 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
952 return 0;
953 }
954 break;
955 }
956#endif /* IN_RING3 */
957
958 /*
959 * Group 4, structure dumpers.
960 */
961 case 'D':
962 {
963 /*
964 * Interpret the type.
965 */
966 typedef enum
967 {
968 RTST_TIMESPEC
969 } RTST;
970/** Set if it's a pointer */
971#define RTST_FLAGS_POINTER RT_BIT(0)
972 static const struct
973 {
974 uint8_t cch; /**< the length of the string. */
975 char sz[16-2]; /**< the part following 'R'. */
976 uint8_t cb; /**< the size of the argument. */
977 uint8_t fFlags; /**< RTST_FLAGS_* */
978 RTST enmType; /**< The structure type. */
979 }
980 /** Sorted array of types, looked up using binary search! */
981 s_aTypes[] =
982 {
983#define STRMEM(str) sizeof(str) - 1, str
984 { STRMEM("Dtimespec"), sizeof(PCRTTIMESPEC), RTST_FLAGS_POINTER, RTST_TIMESPEC},
985#undef STRMEM
986 };
987 const char *pszType = *ppszFormat - 1;
988 int iStart = 0;
989 int iEnd = RT_ELEMENTS(s_aTypes) - 1;
990 int i = RT_ELEMENTS(s_aTypes) / 2;
991
992 union
993 {
994 const void *pv;
995 uint64_t u64;
996 PCRTTIMESPEC pTimeSpec;
997 } u;
998
999 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
1000
1001 /*
1002 * Lookup the type - binary search.
1003 */
1004 for (;;)
1005 {
1006 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
1007 if (!iDiff)
1008 break;
1009 if (iEnd == iStart)
1010 {
1011 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
1012 return 0;
1013 }
1014 if (iDiff < 0)
1015 iEnd = i - 1;
1016 else
1017 iStart = i + 1;
1018 if (iEnd < iStart)
1019 {
1020 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
1021 return 0;
1022 }
1023 i = iStart + (iEnd - iStart) / 2;
1024 }
1025 *ppszFormat += s_aTypes[i].cch - 1;
1026
1027 /*
1028 * Fetch the argument.
1029 */
1030 u.u64 = 0;
1031 switch (s_aTypes[i].cb)
1032 {
1033 case sizeof(const void *):
1034 u.pv = va_arg(*pArgs, const void *);
1035 break;
1036 default:
1037 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
1038 break;
1039 }
1040
1041 /*
1042 * If it's a pointer, we'll check if it's valid before going on.
1043 */
1044 if ((s_aTypes[i].fFlags & RTST_FLAGS_POINTER) && !VALID_PTR(u.pv))
1045 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
1046
1047 /*
1048 * Format the output.
1049 */
1050 switch (s_aTypes[i].enmType)
1051 {
1052 case RTST_TIMESPEC:
1053 return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%'lld ns", RTTimeSpecGetNano(u.pTimeSpec));
1054
1055 default:
1056 AssertMsgFailed(("Invalid/unhandled enmType=%d\n", s_aTypes[i].enmType));
1057 break;
1058 }
1059 break;
1060 }
1061
1062#ifdef IN_RING3
1063 /*
1064 * Group 5, XML / HTML escapers.
1065 */
1066 case 'M':
1067 {
1068 char chWhat = (*ppszFormat)[0];
1069 bool fAttr = chWhat == 'a';
1070 char chType = (*ppszFormat)[1];
1071 AssertMsgBreak(chWhat == 'a' || chWhat == 'e', ("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1072 *ppszFormat += 2;
1073 switch (chType)
1074 {
1075 case 's':
1076 {
1077 static const char s_szElemEscape[] = "<>&\"'";
1078 static const char s_szAttrEscape[] = "<>&\"\n\r"; /* more? */
1079 const char * const pszEscape = fAttr ? s_szAttrEscape : s_szElemEscape;
1080 size_t const cchEscape = (fAttr ? RT_ELEMENTS(s_szAttrEscape) : RT_ELEMENTS(s_szElemEscape)) - 1;
1081 size_t cchOutput = 0;
1082 const char *pszStr = va_arg(*pArgs, char *);
1083 ssize_t cchStr;
1084 ssize_t offCur;
1085 ssize_t offLast;
1086
1087 if (!VALID_PTR(pszStr))
1088 pszStr = "<NULL>";
1089 cchStr = RTStrNLen(pszStr, (unsigned)cchPrecision);
1090
1091 if (fAttr)
1092 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1093 if (!(fFlags & RTSTR_F_LEFT))
1094 while (--cchWidth >= cchStr)
1095 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1096
1097 offLast = offCur = 0;
1098 while (offCur < cchStr)
1099 {
1100 if (memchr(pszEscape, pszStr[offCur], cchEscape))
1101 {
1102 if (offLast < offCur)
1103 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1104 switch (pszStr[offCur])
1105 {
1106 case '<': cchOutput += pfnOutput(pvArgOutput, "&lt;", 4); break;
1107 case '>': cchOutput += pfnOutput(pvArgOutput, "&gt;", 4); break;
1108 case '&': cchOutput += pfnOutput(pvArgOutput, "&amp;", 5); break;
1109 case '\'': cchOutput += pfnOutput(pvArgOutput, "&apos;", 6); break;
1110 case '"': cchOutput += pfnOutput(pvArgOutput, "&quot;", 6); break;
1111 case '\n': cchOutput += pfnOutput(pvArgOutput, "&#xA;", 5); break;
1112 case '\r': cchOutput += pfnOutput(pvArgOutput, "&#xD;", 5); break;
1113 default:
1114 AssertFailed();
1115 }
1116 offLast = offCur + 1;
1117 }
1118 offCur++;
1119 }
1120 if (offLast < offCur)
1121 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1122
1123 while (--cchWidth >= cchStr)
1124 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1125 if (fAttr)
1126 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1127 return cchOutput;
1128 }
1129
1130 default:
1131 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1132 }
1133 break;
1134 }
1135#endif /* IN_RING3 */
1136
1137
1138 /*
1139 * Groups 6 - CPU Architecture Register Formatters.
1140 * "%RAarch[reg]"
1141 */
1142 case 'A':
1143 {
1144 char const * const pszArch = *ppszFormat;
1145 const char *pszReg = pszArch;
1146 size_t cchOutput = 0;
1147 int cPrinted = 0;
1148 size_t cchReg;
1149
1150 /* Parse out the */
1151 while ((ch = *pszReg++) && ch != '[')
1152 { /* nothing */ }
1153 AssertMsgBreak(ch == '[', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
1154
1155 cchReg = 0;
1156 while ((ch = pszReg[cchReg]) && ch != ']')
1157 cchReg++;
1158 AssertMsgBreak(ch == ']', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
1159
1160 *ppszFormat = &pszReg[cchReg + 1];
1161
1162
1163#define REG_EQUALS(a_szReg) (sizeof(a_szReg) - 1 == cchReg && !strncmp(a_szReg, pszReg, sizeof(a_szReg) - 1))
1164#define REG_OUT_BIT(a_uVal, a_fBitMask, a_szName) \
1165 do { \
1166 if ((a_uVal) & (a_fBitMask)) \
1167 { \
1168 if (!cPrinted++) \
1169 cchOutput += pfnOutput(pvArgOutput, "{" a_szName, sizeof(a_szName)); \
1170 else \
1171 cchOutput += pfnOutput(pvArgOutput, "," a_szName, sizeof(a_szName)); \
1172 (a_uVal) &= ~(a_fBitMask); \
1173 } \
1174 } while (0)
1175#define REG_OUT_CLOSE(a_uVal) \
1176 do { \
1177 if ((a_uVal)) \
1178 { \
1179 cchOutput += pfnOutput(pvArgOutput, !cPrinted ? "{unkn=" : ",unkn=", 6); \
1180 cch = RTStrFormatNumber(&szBuf[0], (a_uVal), 16, 1, -1, fFlags); \
1181 cchOutput += pfnOutput(pvArgOutput, szBuf, cch); \
1182 cPrinted++; \
1183 } \
1184 if (cPrinted) \
1185 cchOutput += pfnOutput(pvArgOutput, "}", 1); \
1186 } while (0)
1187
1188
1189 if (0)
1190 { /* dummy */ }
1191#ifdef STRFORMAT_WITH_X86
1192 /*
1193 * X86 & AMD64.
1194 */
1195 else if ( pszReg - pszArch == 3 + 1
1196 && pszArch[0] == 'x'
1197 && pszArch[1] == '8'
1198 && pszArch[2] == '6')
1199 {
1200 if (REG_EQUALS("cr0"))
1201 {
1202 uint64_t cr0 = va_arg(*pArgs, uint64_t);
1203 fFlags |= RTSTR_F_64BIT;
1204 cch = RTStrFormatNumber(&szBuf[0], cr0, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
1205 cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
1206 REG_OUT_BIT(cr0, X86_CR0_PE, "PE");
1207 REG_OUT_BIT(cr0, X86_CR0_MP, "MP");
1208 REG_OUT_BIT(cr0, X86_CR0_EM, "EM");
1209 REG_OUT_BIT(cr0, X86_CR0_TS, "DE");
1210 REG_OUT_BIT(cr0, X86_CR0_ET, "ET");
1211 REG_OUT_BIT(cr0, X86_CR0_NE, "NE");
1212 REG_OUT_BIT(cr0, X86_CR0_WP, "WP");
1213 REG_OUT_BIT(cr0, X86_CR0_AM, "AM");
1214 REG_OUT_BIT(cr0, X86_CR0_NW, "NW");
1215 REG_OUT_BIT(cr0, X86_CR0_CD, "CD");
1216 REG_OUT_BIT(cr0, X86_CR0_PG, "PG");
1217 REG_OUT_CLOSE(cr0);
1218 }
1219 else if (REG_EQUALS("cr4"))
1220 {
1221 uint64_t cr4 = va_arg(*pArgs, uint64_t);
1222 fFlags |= RTSTR_F_64BIT;
1223 cch = RTStrFormatNumber(&szBuf[0], cr4, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
1224 cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
1225 REG_OUT_BIT(cr4, X86_CR4_VME, "VME");
1226 REG_OUT_BIT(cr4, X86_CR4_PVI, "PVI");
1227 REG_OUT_BIT(cr4, X86_CR4_TSD, "TSD");
1228 REG_OUT_BIT(cr4, X86_CR4_DE, "DE");
1229 REG_OUT_BIT(cr4, X86_CR4_PSE, "PSE");
1230 REG_OUT_BIT(cr4, X86_CR4_PAE, "PAE");
1231 REG_OUT_BIT(cr4, X86_CR4_MCE, "MCE");
1232 REG_OUT_BIT(cr4, X86_CR4_PGE, "PGE");
1233 REG_OUT_BIT(cr4, X86_CR4_PCE, "PCE");
1234 REG_OUT_BIT(cr4, X86_CR4_OSFXSR, "OSFXSR");
1235 REG_OUT_BIT(cr4, X86_CR4_OSXMMEEXCPT, "OSXMMEEXCPT");
1236 REG_OUT_BIT(cr4, X86_CR4_VMXE, "VMXE");
1237 REG_OUT_BIT(cr4, X86_CR4_SMXE, "SMXE");
1238 REG_OUT_BIT(cr4, X86_CR4_PCIDE, "PCIDE");
1239 REG_OUT_BIT(cr4, X86_CR4_OSXSAVE, "OSXSAVE");
1240 REG_OUT_BIT(cr4, X86_CR4_SMEP, "SMEP");
1241 REG_OUT_BIT(cr4, X86_CR4_SMAP, "SMAP");
1242 REG_OUT_CLOSE(cr4);
1243 }
1244 else
1245 AssertMsgFailed(("Unknown x86 register specified in '%.10s'!\n", pszFormatOrg));
1246 }
1247#endif
1248 else
1249 AssertMsgFailed(("Unknown architecture specified in '%.10s'!\n", pszFormatOrg));
1250#undef REG_OUT_BIT
1251#undef REG_OUT_CLOSE
1252#undef REG_EQUALS
1253 return cchOutput;
1254 }
1255
1256 /*
1257 * Invalid/Unknown. Bitch about it.
1258 */
1259 default:
1260 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1261 break;
1262 }
1263 }
1264 else
1265 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1266
1267 NOREF(pszFormatOrg);
1268 return 0;
1269}
1270
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