VirtualBox

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

最後變更 在這個檔案從66285是 66285,由 vboxsync 提交於 8 年 前

Runtime: treat 'RKv' normal for debug builds

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

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