VirtualBox

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

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

strformatrt.cpp: We shouldn't need 16-bit ring-0 support here AFAIK.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 54.7 KB
Line 
1/* $Id: strformatrt.cpp 66299 2017-03-28 12:40:02Z 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#ifndef DEBUG
475 /*
476 * For now don't show the address.
477 */
478 cch = 0;
479 if (fFlags & RTSTR_F_OBFUSCATE_PTR)
480 {
481 if (fFlags & RTSTR_F_SPECIAL)
482 cch += pfnOutput(pvArgOutput, RT_STR_TUPLE("0x"));
483
484# if R0_ARCH_BITS == 32
485 cch += pfnOutput(pvArgOutput, RT_STR_TUPLE("XXXXXXXX"));
486# elif R0_ARCH_BITS == 64
487 cch += pfnOutput(pvArgOutput, RT_STR_TUPLE("XXXXXXXXXXXXXXXX"));
488# else
489# error implement me!
490# endif
491 return cch;
492 }
493#endif
494
495 /*
496 * Format the output.
497 */
498 switch (s_aTypes[i].enmFormat)
499 {
500 case RTSF_INT:
501 {
502 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
503 break;
504 }
505
506 /* hex which defaults to max width. */
507 case RTSF_INTW:
508 {
509 Assert(s_aTypes[i].u8Base == 16);
510 if (cchWidth < 0)
511 {
512 cchWidth = s_aTypes[i].cb * 2 + (fFlags & RTSTR_F_SPECIAL ? 2 : 0);
513 fFlags |= RTSTR_F_ZEROPAD;
514 }
515 cch = RTStrFormatNumber(szBuf, u.u64, s_aTypes[i].u8Base, cchWidth, cchPrecision, fFlags);
516 break;
517 }
518
519 case RTSF_BOOL:
520 {
521 static const char s_szTrue[] = "true ";
522 static const char s_szFalse[] = "false";
523 if (u.u64 == 1)
524 return pfnOutput(pvArgOutput, s_szTrue, sizeof(s_szTrue) - 1);
525 if (u.u64 == 0)
526 return pfnOutput(pvArgOutput, s_szFalse, sizeof(s_szFalse) - 1);
527 /* invalid boolean value */
528 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "!%lld!", u.u64);
529 }
530
531 case RTSF_FP16:
532 {
533 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
534 cch = RTStrFormatNumber(&szBuf[0], u.fp16.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
535 Assert(cch == 4);
536 szBuf[4] = ':';
537 cch = RTStrFormatNumber(&szBuf[5], u.fp16.off, 16, 4, -1, fFlags | RTSTR_F_16BIT);
538 Assert(cch == 4);
539 cch = 4 + 1 + 4;
540 break;
541 }
542 case RTSF_FP32:
543 {
544 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
545 cch = RTStrFormatNumber(&szBuf[0], u.fp32.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
546 Assert(cch == 4);
547 szBuf[4] = ':';
548 cch = RTStrFormatNumber(&szBuf[5], u.fp32.off, 16, 8, -1, fFlags | RTSTR_F_32BIT);
549 Assert(cch == 8);
550 cch = 4 + 1 + 8;
551 break;
552 }
553 case RTSF_FP64:
554 {
555 fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
556 cch = RTStrFormatNumber(&szBuf[0], u.fp64.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
557 Assert(cch == 4);
558 szBuf[4] = ':';
559 cch = RTStrFormatNumber(&szBuf[5], u.fp64.off, 16, 16, -1, fFlags | RTSTR_F_64BIT);
560 Assert(cch == 16);
561 cch = 4 + 1 + 16;
562 break;
563 }
564
565 case RTSF_IPV4:
566 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
567 "%u.%u.%u.%u",
568 u.Ipv4Addr.au8[0],
569 u.Ipv4Addr.au8[1],
570 u.Ipv4Addr.au8[2],
571 u.Ipv4Addr.au8[3]);
572
573 case RTSF_IPV6:
574 {
575 if (VALID_PTR(u.pIpv6Addr))
576 return rtstrFormatIPv6(pfnOutput, pvArgOutput, u.pIpv6Addr);
577 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
578 }
579
580 case RTSF_MAC:
581 {
582 if (VALID_PTR(u.pMac))
583 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
584 "%02x:%02x:%02x:%02x:%02x:%02x",
585 u.pMac->au8[0],
586 u.pMac->au8[1],
587 u.pMac->au8[2],
588 u.pMac->au8[3],
589 u.pMac->au8[4],
590 u.pMac->au8[5]);
591 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
592 }
593
594 case RTSF_NETADDR:
595 {
596 if (VALID_PTR(u.pNetAddr))
597 {
598 switch (u.pNetAddr->enmType)
599 {
600 case RTNETADDRTYPE_IPV4:
601 if (u.pNetAddr->uPort == RTNETADDR_PORT_NA)
602 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
603 "%u.%u.%u.%u",
604 u.pNetAddr->uAddr.IPv4.au8[0],
605 u.pNetAddr->uAddr.IPv4.au8[1],
606 u.pNetAddr->uAddr.IPv4.au8[2],
607 u.pNetAddr->uAddr.IPv4.au8[3]);
608 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
609 "%u.%u.%u.%u:%u",
610 u.pNetAddr->uAddr.IPv4.au8[0],
611 u.pNetAddr->uAddr.IPv4.au8[1],
612 u.pNetAddr->uAddr.IPv4.au8[2],
613 u.pNetAddr->uAddr.IPv4.au8[3],
614 u.pNetAddr->uPort);
615
616 case RTNETADDRTYPE_IPV6:
617 if (u.pNetAddr->uPort == RTNETADDR_PORT_NA)
618 return rtstrFormatIPv6(pfnOutput, pvArgOutput, &u.pNetAddr->uAddr.IPv6);
619
620 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
621 "[%RTnaipv6]:%u",
622 &u.pNetAddr->uAddr.IPv6,
623 u.pNetAddr->uPort);
624
625 case RTNETADDRTYPE_MAC:
626 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
627 "%02x:%02x:%02x:%02x:%02x:%02x",
628 u.pNetAddr->uAddr.Mac.au8[0],
629 u.pNetAddr->uAddr.Mac.au8[1],
630 u.pNetAddr->uAddr.Mac.au8[2],
631 u.pNetAddr->uAddr.Mac.au8[3],
632 u.pNetAddr->uAddr.Mac.au8[4],
633 u.pNetAddr->uAddr.Mac.au8[5]);
634
635 default:
636 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
637 "unsupported-netaddr-type=%u", u.pNetAddr->enmType);
638
639 }
640 }
641 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
642 }
643
644 case RTSF_UUID:
645 {
646 if (VALID_PTR(u.pUuid))
647 {
648 /* cannot call RTUuidToStr because of GC/R0. */
649 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
650 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
651 RT_H2LE_U32(u.pUuid->Gen.u32TimeLow),
652 RT_H2LE_U16(u.pUuid->Gen.u16TimeMid),
653 RT_H2LE_U16(u.pUuid->Gen.u16TimeHiAndVersion),
654 u.pUuid->Gen.u8ClockSeqHiAndReserved,
655 u.pUuid->Gen.u8ClockSeqLow,
656 u.pUuid->Gen.au8Node[0],
657 u.pUuid->Gen.au8Node[1],
658 u.pUuid->Gen.au8Node[2],
659 u.pUuid->Gen.au8Node[3],
660 u.pUuid->Gen.au8Node[4],
661 u.pUuid->Gen.au8Node[5]);
662 }
663 return pfnOutput(pvArgOutput, s_szNull, sizeof(s_szNull) - 1);
664 }
665
666 default:
667 AssertMsgFailed(("Internal error %d\n", s_aTypes[i].enmFormat));
668 return 0;
669 }
670
671 /*
672 * Finally, output the formatted string and return.
673 */
674 return pfnOutput(pvArgOutput, szBuf, cch);
675 }
676
677
678 /* Group 3 */
679
680 /*
681 * Base name printing.
682 */
683 case 'b':
684 {
685 switch (*(*ppszFormat)++)
686 {
687 case 'n':
688 {
689 const char *pszLastSep;
690 const char *psz = pszLastSep = va_arg(*pArgs, const char *);
691 if (!VALID_PTR(psz))
692 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
693
694 while ((ch = *psz) != '\0')
695 {
696 if (RTPATH_IS_SEP(ch))
697 {
698 do
699 psz++;
700 while ((ch = *psz) != '\0' && RTPATH_IS_SEP(ch));
701 if (!ch)
702 break;
703 pszLastSep = psz;
704 }
705 psz++;
706 }
707
708 return pfnOutput(pvArgOutput, pszLastSep, psz - pszLastSep);
709 }
710
711 default:
712 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
713 break;
714 }
715 break;
716 }
717
718
719 /*
720 * Pretty function / method name printing.
721 */
722 case 'f':
723 {
724 switch (*(*ppszFormat)++)
725 {
726 /*
727 * Pretty function / method name printing.
728 * This isn't 100% right (see classic signal prototype) and it assumes
729 * standardized names, but it'll do for today.
730 */
731 case 'n':
732 {
733 const char *pszStart;
734 const char *psz = pszStart = va_arg(*pArgs, const char *);
735 int cAngle = 0;
736
737 if (!VALID_PTR(psz))
738 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
739
740 while ((ch = *psz) != '\0' && ch != '(')
741 {
742 if (RT_C_IS_BLANK(ch))
743 {
744 psz++;
745 while ((ch = *psz) != '\0' && (RT_C_IS_BLANK(ch) || ch == '('))
746 psz++;
747 if (ch && cAngle == 0)
748 pszStart = psz;
749 }
750 else if (ch == '(')
751 break;
752 else if (ch == '<')
753 {
754 cAngle++;
755 psz++;
756 }
757 else if (ch == '>')
758 {
759 cAngle--;
760 psz++;
761 }
762 else
763 psz++;
764 }
765
766 return pfnOutput(pvArgOutput, pszStart, psz - pszStart);
767 }
768
769 default:
770 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
771 break;
772 }
773 break;
774 }
775
776
777 /*
778 * hex dumping and COM/XPCOM.
779 */
780 case 'h':
781 {
782 switch (*(*ppszFormat)++)
783 {
784 /*
785 * Hex stuff.
786 */
787 case 'x':
788 {
789 uint8_t *pu8 = va_arg(*pArgs, uint8_t *);
790 if (cchPrecision < 0)
791 cchPrecision = 16;
792 if (pu8)
793 {
794 switch (*(*ppszFormat)++)
795 {
796 /*
797 * Regular hex dump.
798 */
799 case 'd':
800 {
801 int off = 0;
802 cch = 0;
803
804 if (cchWidth <= 0)
805 cchWidth = 16;
806
807 while (off < cchPrecision)
808 {
809 int i;
810 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%0*p %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
811 for (i = 0; i < cchWidth && off + i < cchPrecision ; i++)
812 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
813 off + i < cchPrecision ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pu8[i]);
814 while (i++ < cchWidth)
815 cch += pfnOutput(pvArgOutput, " ", 3);
816
817 cch += pfnOutput(pvArgOutput, " ", 1);
818
819 for (i = 0; i < cchWidth && off + i < cchPrecision; i++)
820 {
821 uint8_t u8 = pu8[i];
822 cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
823 }
824
825 /* next */
826 pu8 += cchWidth;
827 off += cchWidth;
828 }
829 return cch;
830 }
831
832 /*
833 * Hex string.
834 */
835 case 's':
836 {
837 if (cchPrecision-- > 0)
838 {
839 cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++);
840 for (; cchPrecision > 0; cchPrecision--, pu8++)
841 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8);
842 return cch;
843 }
844 break;
845 }
846
847 default:
848 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
849 break;
850 }
851 }
852 else
853 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
854 break;
855 }
856
857
858#ifdef IN_RING3
859 /*
860 * XPCOM / COM status code: %Rhrc, %Rhrf, %Rhra
861 * ASSUMES: If Windows Then COM else XPCOM.
862 */
863 case 'r':
864 {
865 uint32_t hrc = va_arg(*pArgs, uint32_t);
866 PCRTCOMERRMSG pMsg = RTErrCOMGet(hrc);
867 switch (*(*ppszFormat)++)
868 {
869 case 'c':
870 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
871 case 'f':
872 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
873 case 'a':
874 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, hrc, pMsg->pszMsgFull);
875 default:
876 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
877 return 0;
878 }
879 break;
880 }
881#endif /* IN_RING3 */
882
883 default:
884 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
885 return 0;
886
887 }
888 break;
889 }
890
891 /*
892 * iprt status code: %Rrc, %Rrs, %Rrf, %Rra.
893 */
894 case 'r':
895 {
896 int rc = va_arg(*pArgs, int);
897#ifdef IN_RING3 /* we don't want this anywhere else yet. */
898 PCRTSTATUSMSG pMsg = RTErrGet(rc);
899 switch (*(*ppszFormat)++)
900 {
901 case 'c':
902 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
903 case 's':
904 return pfnOutput(pvArgOutput, pMsg->pszMsgShort, strlen(pMsg->pszMsgShort));
905 case 'f':
906 return pfnOutput(pvArgOutput, pMsg->pszMsgFull, strlen(pMsg->pszMsgFull));
907 case 'a':
908 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
909 default:
910 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
911 return 0;
912 }
913#else /* !IN_RING3 */
914 switch (*(*ppszFormat)++)
915 {
916 case 'c':
917 case 's':
918 case 'f':
919 case 'a':
920 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%d", rc);
921 default:
922 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
923 return 0;
924 }
925#endif /* !IN_RING3 */
926 break;
927 }
928
929#if defined(IN_RING3)
930 /*
931 * Windows status code: %Rwc, %Rwf, %Rwa
932 */
933 case 'w':
934 {
935 long rc = va_arg(*pArgs, long);
936# if defined(RT_OS_WINDOWS)
937 PCRTWINERRMSG pMsg = RTErrWinGet(rc);
938# endif
939 switch (*(*ppszFormat)++)
940 {
941# if defined(RT_OS_WINDOWS)
942 case 'c':
943 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
944 case 'f':
945 return pfnOutput(pvArgOutput, pMsg->pszMsgFull,strlen(pMsg->pszMsgFull));
946 case 'a':
947 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (0x%08X) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
948# else
949 case 'c':
950 case 'f':
951 case 'a':
952 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "0x%08X", rc);
953# endif
954 default:
955 AssertMsgFailed(("Invalid status code format type '%.10s'!\n", pszFormatOrg));
956 return 0;
957 }
958 break;
959 }
960#endif /* IN_RING3 */
961
962 /*
963 * Group 4, structure dumpers.
964 */
965 case 'D':
966 {
967 /*
968 * Interpret the type.
969 */
970 typedef enum
971 {
972 RTST_TIMESPEC
973 } RTST;
974/** Set if it's a pointer */
975#define RTST_FLAGS_POINTER RT_BIT(0)
976 static const struct
977 {
978 uint8_t cch; /**< the length of the string. */
979 char sz[16-2]; /**< the part following 'R'. */
980 uint8_t cb; /**< the size of the argument. */
981 uint8_t fFlags; /**< RTST_FLAGS_* */
982 RTST enmType; /**< The structure type. */
983 }
984 /** Sorted array of types, looked up using binary search! */
985 s_aTypes[] =
986 {
987#define STRMEM(str) sizeof(str) - 1, str
988 { STRMEM("Dtimespec"), sizeof(PCRTTIMESPEC), RTST_FLAGS_POINTER, RTST_TIMESPEC},
989#undef STRMEM
990 };
991 const char *pszType = *ppszFormat - 1;
992 int iStart = 0;
993 int iEnd = RT_ELEMENTS(s_aTypes) - 1;
994 int i = RT_ELEMENTS(s_aTypes) / 2;
995
996 union
997 {
998 const void *pv;
999 uint64_t u64;
1000 PCRTTIMESPEC pTimeSpec;
1001 } u;
1002
1003 AssertMsg(!chArgSize, ("Not argument size '%c' for RT types! '%.10s'\n", chArgSize, pszFormatOrg));
1004
1005 /*
1006 * Lookup the type - binary search.
1007 */
1008 for (;;)
1009 {
1010 int iDiff = strncmp(pszType, s_aTypes[i].sz, s_aTypes[i].cch);
1011 if (!iDiff)
1012 break;
1013 if (iEnd == iStart)
1014 {
1015 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
1016 return 0;
1017 }
1018 if (iDiff < 0)
1019 iEnd = i - 1;
1020 else
1021 iStart = i + 1;
1022 if (iEnd < iStart)
1023 {
1024 AssertMsgFailed(("Invalid format type '%.10s'!\n", pszFormatOrg));
1025 return 0;
1026 }
1027 i = iStart + (iEnd - iStart) / 2;
1028 }
1029 *ppszFormat += s_aTypes[i].cch - 1;
1030
1031 /*
1032 * Fetch the argument.
1033 */
1034 u.u64 = 0;
1035 switch (s_aTypes[i].cb)
1036 {
1037 case sizeof(const void *):
1038 u.pv = va_arg(*pArgs, const void *);
1039 break;
1040 default:
1041 AssertMsgFailed(("Invalid format error, size %d'!\n", s_aTypes[i].cb));
1042 break;
1043 }
1044
1045 /*
1046 * If it's a pointer, we'll check if it's valid before going on.
1047 */
1048 if ((s_aTypes[i].fFlags & RTST_FLAGS_POINTER) && !VALID_PTR(u.pv))
1049 return pfnOutput(pvArgOutput, RT_STR_TUPLE("<null>"));
1050
1051 /*
1052 * Format the output.
1053 */
1054 switch (s_aTypes[i].enmType)
1055 {
1056 case RTST_TIMESPEC:
1057 return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "%'lld ns", RTTimeSpecGetNano(u.pTimeSpec));
1058
1059 default:
1060 AssertMsgFailed(("Invalid/unhandled enmType=%d\n", s_aTypes[i].enmType));
1061 break;
1062 }
1063 break;
1064 }
1065
1066#ifdef IN_RING3
1067 /*
1068 * Group 5, XML / HTML escapers.
1069 */
1070 case 'M':
1071 {
1072 char chWhat = (*ppszFormat)[0];
1073 bool fAttr = chWhat == 'a';
1074 char chType = (*ppszFormat)[1];
1075 AssertMsgBreak(chWhat == 'a' || chWhat == 'e', ("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1076 *ppszFormat += 2;
1077 switch (chType)
1078 {
1079 case 's':
1080 {
1081 static const char s_szElemEscape[] = "<>&\"'";
1082 static const char s_szAttrEscape[] = "<>&\"\n\r"; /* more? */
1083 const char * const pszEscape = fAttr ? s_szAttrEscape : s_szElemEscape;
1084 size_t const cchEscape = (fAttr ? RT_ELEMENTS(s_szAttrEscape) : RT_ELEMENTS(s_szElemEscape)) - 1;
1085 size_t cchOutput = 0;
1086 const char *pszStr = va_arg(*pArgs, char *);
1087 ssize_t cchStr;
1088 ssize_t offCur;
1089 ssize_t offLast;
1090
1091 if (!VALID_PTR(pszStr))
1092 pszStr = "<NULL>";
1093 cchStr = RTStrNLen(pszStr, (unsigned)cchPrecision);
1094
1095 if (fAttr)
1096 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1097 if (!(fFlags & RTSTR_F_LEFT))
1098 while (--cchWidth >= cchStr)
1099 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1100
1101 offLast = offCur = 0;
1102 while (offCur < cchStr)
1103 {
1104 if (memchr(pszEscape, pszStr[offCur], cchEscape))
1105 {
1106 if (offLast < offCur)
1107 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1108 switch (pszStr[offCur])
1109 {
1110 case '<': cchOutput += pfnOutput(pvArgOutput, "&lt;", 4); break;
1111 case '>': cchOutput += pfnOutput(pvArgOutput, "&gt;", 4); break;
1112 case '&': cchOutput += pfnOutput(pvArgOutput, "&amp;", 5); break;
1113 case '\'': cchOutput += pfnOutput(pvArgOutput, "&apos;", 6); break;
1114 case '"': cchOutput += pfnOutput(pvArgOutput, "&quot;", 6); break;
1115 case '\n': cchOutput += pfnOutput(pvArgOutput, "&#xA;", 5); break;
1116 case '\r': cchOutput += pfnOutput(pvArgOutput, "&#xD;", 5); break;
1117 default:
1118 AssertFailed();
1119 }
1120 offLast = offCur + 1;
1121 }
1122 offCur++;
1123 }
1124 if (offLast < offCur)
1125 cchOutput += pfnOutput(pvArgOutput, &pszStr[offLast], offCur - offLast);
1126
1127 while (--cchWidth >= cchStr)
1128 cchOutput += pfnOutput(pvArgOutput, " ", 1);
1129 if (fAttr)
1130 cchOutput += pfnOutput(pvArgOutput, "\"", 1);
1131 return cchOutput;
1132 }
1133
1134 default:
1135 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1136 }
1137 break;
1138 }
1139#endif /* IN_RING3 */
1140
1141
1142 /*
1143 * Groups 6 - CPU Architecture Register Formatters.
1144 * "%RAarch[reg]"
1145 */
1146 case 'A':
1147 {
1148 char const * const pszArch = *ppszFormat;
1149 const char *pszReg = pszArch;
1150 size_t cchOutput = 0;
1151 int cPrinted = 0;
1152 size_t cchReg;
1153
1154 /* Parse out the */
1155 while ((ch = *pszReg++) && ch != '[')
1156 { /* nothing */ }
1157 AssertMsgBreak(ch == '[', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
1158
1159 cchReg = 0;
1160 while ((ch = pszReg[cchReg]) && ch != ']')
1161 cchReg++;
1162 AssertMsgBreak(ch == ']', ("Malformed IPRT architecture register format type '%.10s'!\n", pszFormatOrg));
1163
1164 *ppszFormat = &pszReg[cchReg + 1];
1165
1166
1167#define REG_EQUALS(a_szReg) (sizeof(a_szReg) - 1 == cchReg && !strncmp(a_szReg, pszReg, sizeof(a_szReg) - 1))
1168#define REG_OUT_BIT(a_uVal, a_fBitMask, a_szName) \
1169 do { \
1170 if ((a_uVal) & (a_fBitMask)) \
1171 { \
1172 if (!cPrinted++) \
1173 cchOutput += pfnOutput(pvArgOutput, "{" a_szName, sizeof(a_szName)); \
1174 else \
1175 cchOutput += pfnOutput(pvArgOutput, "," a_szName, sizeof(a_szName)); \
1176 (a_uVal) &= ~(a_fBitMask); \
1177 } \
1178 } while (0)
1179#define REG_OUT_CLOSE(a_uVal) \
1180 do { \
1181 if ((a_uVal)) \
1182 { \
1183 cchOutput += pfnOutput(pvArgOutput, !cPrinted ? "{unkn=" : ",unkn=", 6); \
1184 cch = RTStrFormatNumber(&szBuf[0], (a_uVal), 16, 1, -1, fFlags); \
1185 cchOutput += pfnOutput(pvArgOutput, szBuf, cch); \
1186 cPrinted++; \
1187 } \
1188 if (cPrinted) \
1189 cchOutput += pfnOutput(pvArgOutput, "}", 1); \
1190 } while (0)
1191
1192
1193 if (0)
1194 { /* dummy */ }
1195#ifdef STRFORMAT_WITH_X86
1196 /*
1197 * X86 & AMD64.
1198 */
1199 else if ( pszReg - pszArch == 3 + 1
1200 && pszArch[0] == 'x'
1201 && pszArch[1] == '8'
1202 && pszArch[2] == '6')
1203 {
1204 if (REG_EQUALS("cr0"))
1205 {
1206 uint64_t cr0 = va_arg(*pArgs, uint64_t);
1207 fFlags |= RTSTR_F_64BIT;
1208 cch = RTStrFormatNumber(&szBuf[0], cr0, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
1209 cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
1210 REG_OUT_BIT(cr0, X86_CR0_PE, "PE");
1211 REG_OUT_BIT(cr0, X86_CR0_MP, "MP");
1212 REG_OUT_BIT(cr0, X86_CR0_EM, "EM");
1213 REG_OUT_BIT(cr0, X86_CR0_TS, "DE");
1214 REG_OUT_BIT(cr0, X86_CR0_ET, "ET");
1215 REG_OUT_BIT(cr0, X86_CR0_NE, "NE");
1216 REG_OUT_BIT(cr0, X86_CR0_WP, "WP");
1217 REG_OUT_BIT(cr0, X86_CR0_AM, "AM");
1218 REG_OUT_BIT(cr0, X86_CR0_NW, "NW");
1219 REG_OUT_BIT(cr0, X86_CR0_CD, "CD");
1220 REG_OUT_BIT(cr0, X86_CR0_PG, "PG");
1221 REG_OUT_CLOSE(cr0);
1222 }
1223 else if (REG_EQUALS("cr4"))
1224 {
1225 uint64_t cr4 = va_arg(*pArgs, uint64_t);
1226 fFlags |= RTSTR_F_64BIT;
1227 cch = RTStrFormatNumber(&szBuf[0], cr4, 16, 8, -1, fFlags | RTSTR_F_ZEROPAD);
1228 cchOutput += pfnOutput(pvArgOutput, szBuf, cch);
1229 REG_OUT_BIT(cr4, X86_CR4_VME, "VME");
1230 REG_OUT_BIT(cr4, X86_CR4_PVI, "PVI");
1231 REG_OUT_BIT(cr4, X86_CR4_TSD, "TSD");
1232 REG_OUT_BIT(cr4, X86_CR4_DE, "DE");
1233 REG_OUT_BIT(cr4, X86_CR4_PSE, "PSE");
1234 REG_OUT_BIT(cr4, X86_CR4_PAE, "PAE");
1235 REG_OUT_BIT(cr4, X86_CR4_MCE, "MCE");
1236 REG_OUT_BIT(cr4, X86_CR4_PGE, "PGE");
1237 REG_OUT_BIT(cr4, X86_CR4_PCE, "PCE");
1238 REG_OUT_BIT(cr4, X86_CR4_OSFXSR, "OSFXSR");
1239 REG_OUT_BIT(cr4, X86_CR4_OSXMMEEXCPT, "OSXMMEEXCPT");
1240 REG_OUT_BIT(cr4, X86_CR4_VMXE, "VMXE");
1241 REG_OUT_BIT(cr4, X86_CR4_SMXE, "SMXE");
1242 REG_OUT_BIT(cr4, X86_CR4_PCIDE, "PCIDE");
1243 REG_OUT_BIT(cr4, X86_CR4_OSXSAVE, "OSXSAVE");
1244 REG_OUT_BIT(cr4, X86_CR4_SMEP, "SMEP");
1245 REG_OUT_BIT(cr4, X86_CR4_SMAP, "SMAP");
1246 REG_OUT_CLOSE(cr4);
1247 }
1248 else
1249 AssertMsgFailed(("Unknown x86 register specified in '%.10s'!\n", pszFormatOrg));
1250 }
1251#endif
1252 else
1253 AssertMsgFailed(("Unknown architecture specified in '%.10s'!\n", pszFormatOrg));
1254#undef REG_OUT_BIT
1255#undef REG_OUT_CLOSE
1256#undef REG_EQUALS
1257 return cchOutput;
1258 }
1259
1260 /*
1261 * Invalid/Unknown. Bitch about it.
1262 */
1263 default:
1264 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1265 break;
1266 }
1267 }
1268 else
1269 AssertMsgFailed(("Invalid IPRT format type '%.10s'!\n", pszFormatOrg));
1270
1271 NOREF(pszFormatOrg);
1272 return 0;
1273}
1274
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