VirtualBox

source: vbox/trunk/src/VBox/Runtime/VBox/strformat-vbox.cpp@ 20374

Last change on this file since 20374 was 14061, checked in by vboxsync, 17 years ago

IPRT: strformat-vbox.cpp: More 64-bit MSC warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 20.0 KB
Line 
1/* $Id: strformat-vbox.cpp 14061 2008-11-10 23:12:54Z vboxsync $ */
2/** @file
3 * IPRT - VBox String Formatter extensions.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/** @page pg_rt_str_format_vbox The VBox String Format Extensions
32 *
33 * The string formatter supports most of the non-float format types and flags.
34 * See RTStrFormatV() for the full tail there. In addition we've added a number
35 * of VBox specific format types. Most of these doesn't work with any flags (like
36 * width, precision and such), but they should still be very useful.
37 *
38 * The types:
39 *
40 * - \%Vrc - Takes an integer iprt status code as argument. Will insert the
41 * status code define corresponding to the iprt status code.
42 * - \%Vrs - Takes an integer iprt status code as argument. Will insert the
43 * short description of the specified status code.
44 * - \%Vrf - Takes an integer iprt status code as argument. Will insert the
45 * full description of the specified status code.
46 * - \%Vra - Takes an integer iprt status code as argument. Will insert the
47 * status code define + full description.
48 * - \%Vt - Current thread (RTThreadSelf()).
49 * - \%Vhxd - Takes a pointer to the memory which is to be dumped in typical
50 * hex format. Use the width to specify the length, and the precision to
51 * set the number of bytes per line. Default width and precision is 16.
52 * - \%Vhxs - Takes a pointer to the memory to be displayed as a hex string,
53 * i.e. a series of space separated bytes formatted as two digit hex value.
54 * Use the width to specify the length. Default length is 16 bytes.
55 * - \%VGp - Guest context physical address.
56 * - \%VGv - Guest context virtual address.
57 * - \%VRv - 32 bits guest context virtual address.
58 * - \%VHp - Host context physical address.
59 * - \%VHv - Host context virtual address.
60 * - \%VI[8|16|32|64] - Signed integer value of the specifed bit count.
61 * - \%VU[8|16|32|64] - Unsigned integer value of the specifed bit count.
62 * - \%VX[8|16|32|64] - Hexadecimal integer value of the specifed bit count.
63 *
64 */
65
66/*******************************************************************************
67* Header Files *
68*******************************************************************************/
69#include <VBox/log.h>
70#include <iprt/string.h>
71#include <iprt/assert.h>
72#include <iprt/stdarg.h>
73#ifdef IN_RING3
74# include <iprt/thread.h>
75# include <iprt/err.h>
76#endif
77#include "internal/string.h"
78
79
80/**
81 * Callback to format VBox formatting extentions.
82 * See @ref pg_rt_str_format_vbox for a reference on the format types.
83 *
84 * @returns The number of bytes formatted.
85 * @param pfnOutput Pointer to output function.
86 * @param pvArgOutput Argument for the output function.
87 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
88 * after the format specifier.
89 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
90 * @param cchWidth Format Width. -1 if not specified.
91 * @param cchPrecision Format Precision. -1 if not specified.
92 * @param fFlags Flags (RTSTR_NTFS_*).
93 * @param chArgSize The argument size specifier, 'l' or 'L'.
94 */
95size_t rtstrFormatVBox(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, const char **ppszFormat, va_list *pArgs, int cchWidth, int cchPrecision, unsigned fFlags, char chArgSize)
96{
97 char ch = *(*ppszFormat)++;
98 if (ch == 'V')
99 {
100 ch = *(*ppszFormat)++;
101 switch (ch)
102 {
103 /*
104 * iprt status code: %Vrc, %Vrs, %Vrf, %Vra.
105 */
106 case 'r':
107 {
108 int rc = va_arg(*pArgs, int);
109 char ch = *(*ppszFormat)++;
110#ifdef IN_RING3 /* we don't want this anywhere else yet. */
111 PCRTSTATUSMSG pMsg = RTErrGet(rc);
112 switch (ch)
113 {
114 case 'c':
115 return pfnOutput(pvArgOutput, pMsg->pszDefine, strlen(pMsg->pszDefine));
116 case 's':
117 return pfnOutput(pvArgOutput, pMsg->pszMsgShort, strlen(pMsg->pszMsgShort));
118 case 'f':
119 return pfnOutput(pvArgOutput, pMsg->pszMsgFull, strlen(pMsg->pszMsgFull));
120 case 'a':
121 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s (%d) - %s", pMsg->pszDefine, rc, pMsg->pszMsgFull);
122 default:
123 AssertMsgFailed(("Invalid status code format type '%%Vr%c%.10s'!\n", ch, *ppszFormat));
124 return 0;
125 }
126#else /* !IN_RING3 */
127 switch (ch)
128 {
129 case 'c':
130 case 's':
131 case 'f':
132 case 'a':
133 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%d", rc);
134 default:
135 AssertMsgFailed(("Invalid status code format type '%%Vr%c%.10s'!\n", ch, *ppszFormat));
136 return 0;
137 }
138#endif /* !IN_RING3 */
139 break;
140 }
141
142 /*
143 * Current thread.
144 */
145 case 't':
146#if defined(IN_RING3) && !defined(RT_MINI) /* we don't want this anywhere else yet. */
147 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%#x\n", RTThreadSelf());
148#else /* !IN_RING3 || RT_MINI */
149 return pfnOutput(pvArgOutput, "0xffffffff", sizeof("0xffffffff") - 1);
150#endif /* !IN_RING3 || RT_MINI */
151
152 case 'h':
153 {
154 char ch = *(*ppszFormat)++;
155 switch (ch)
156 {
157 /*
158 * Hex stuff.
159 */
160 case 'x':
161 {
162 uint8_t *pu8 = va_arg(*pArgs, uint8_t *);
163 if (cchWidth <= 0)
164 cchWidth = 16;
165 if (pu8)
166 {
167 ch = *(*ppszFormat)++;
168 switch (ch)
169 {
170 /*
171 * Regular hex dump.
172 */
173 case 'd':
174 {
175 size_t cch = 0;
176 int off = 0;
177
178 if (cchPrecision <= 0)
179 cchPrecision = 16;
180
181 while (off < cchWidth)
182 {
183 int i;
184 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s%0*x %04x:", off ? "\n" : "", sizeof(pu8) * 2, (uintptr_t)pu8, off);
185 for (i = 0; i < cchPrecision && off + i < cchWidth ; i++)
186 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
187 off + i < cchWidth ? !(i & 7) && i ? "-%02x" : " %02x" : " ", pu8[i]);
188 while (i++ < cchPrecision)
189 cch += pfnOutput(pvArgOutput, " ", 3);
190
191 cch += pfnOutput(pvArgOutput, " ", 1);
192
193 for (i = 0; i < cchPrecision && off + i < cchWidth; i++)
194 {
195 uint8_t u8 = pu8[i];
196 cch += pfnOutput(pvArgOutput, u8 < 127 && u8 >= 32 ? (const char *)&u8 : ".", 1);
197 }
198
199 /* next */
200 pu8 += cchPrecision;
201 off += cchPrecision;
202 }
203 return cch;
204 }
205
206 /*
207 * Hex string.
208 */
209 case 's':
210 {
211 if (cchWidth-- > 0)
212 {
213 size_t cch = RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%02x", *pu8++);
214 for (; cchWidth > 0; cchWidth--, pu8++)
215 cch += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " %02x", *pu8);
216 return cch;
217 }
218 break;
219 }
220
221 default:
222 AssertMsgFailed(("Invalid status code format type '%%Vhx%c%.10s'!\n", ch, *ppszFormat));
223 break;
224 }
225 }
226 else
227 return pfnOutput(pvArgOutput, "<null>", sizeof("<null>") - 1);
228 break;
229 }
230
231 default:
232 AssertMsgFailed(("Invalid status code format type '%%Vh%c%.10s'!\n", ch, *ppszFormat));
233 return 0;
234
235 }
236 break;
237 }
238
239 /*
240 * Guest types.
241 */
242 case 'G':
243 {
244 /*
245 * Guest types.
246 */
247 ch = *(*ppszFormat)++;
248 switch (ch)
249 {
250 case 'p': /* Physical address. */
251 {
252 RTGCPHYS GCPhys = va_arg(*pArgs, RTGCPHYS);
253 switch (sizeof(GCPhys))
254 {
255 case 4: return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%08x", GCPhys);
256 case 8: return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%016llx", GCPhys);
257 default: AssertFailed(); return 0;
258 }
259 }
260
261 case 'v': /* Virtual address. */
262 {
263 RTGCPTR GCPtr = va_arg(*pArgs, RTGCPTR);
264 switch (sizeof(GCPtr))
265 {
266 case 4: return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%08x", GCPtr);
267 case 8: return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%016llx", GCPtr);
268 default: AssertFailed(); return 0;
269 }
270 }
271
272 default:
273 AssertMsgFailed(("Invalid status code format type '%%VG%c%.10s'!\n", ch, *ppszFormat));
274 break;
275 }
276 break;
277 }
278
279 /*
280 * Raw mode types.
281 */
282 case 'R':
283 {
284 /*
285 * Raw mode types.
286 */
287 ch = *(*ppszFormat)++;
288 switch (ch)
289 {
290 case 'v': /* Virtual address. */
291 {
292 RTRCPTR RCPtr = va_arg(*pArgs, RTRCPTR);
293 switch (sizeof(RCPtr))
294 {
295 case 4: return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%08x", RCPtr);
296 default: AssertFailed(); return 0;
297 }
298 }
299
300 default:
301 AssertMsgFailed(("Invalid status code format type '%%VR%c%.10s'!\n", ch, *ppszFormat));
302 break;
303 }
304 break;
305 }
306
307
308 /*
309 * Host types.
310 */
311 case 'H':
312 {
313 /*
314 * Host types.
315 */
316 ch = *(*ppszFormat)++;
317 switch (ch)
318 {
319 case 'p': /* Physical address. */
320 {
321 RTHCPHYS HCPhys = va_arg(*pArgs, RTHCPHYS);
322 switch (sizeof(HCPhys))
323 {
324 case 4: return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%08x", HCPhys);
325 case 8: return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%016llx", HCPhys);
326 default: AssertFailed(); return 0;
327 }
328 }
329
330 case 'v': /* Virtual address. */
331 {
332 RTHCPTR HCPtr = va_arg(*pArgs, RTHCPTR);
333 switch (sizeof(HCPtr))
334 {
335 case 4: return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%08x", HCPtr);
336 case 8: return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%016llx", HCPtr);
337 default: AssertFailed(); return 0;
338 }
339 }
340
341 default:
342 AssertMsgFailed(("Invalid status code format type '%%VH%c%.10s'!\n", ch, *ppszFormat));
343 break;
344 }
345 break;
346 }
347
348 /*
349 * Signed integer types: VI<bits>
350 */
351 case 'I':
352 {
353 /*
354 * Figure out number of bits.
355 */
356 int64_t i64 = 0;
357 char szNum[64];
358 int cch;
359
360 ch = *(*ppszFormat)++;
361 if (ch == '8')
362 i64 = (int8_t)va_arg(*pArgs, int);
363 else if (ch == '1')
364 {
365 ch = *(*ppszFormat)++;
366 if (ch == '6')
367 i64 = (int16_t)va_arg(*pArgs, int);
368 else
369 AssertMsgFailed(("Invalid format %%VI1%c.10s\n", ch, *ppszFormat));
370 }
371 else if (ch == '3')
372 {
373 ch = *(*ppszFormat)++;
374 if (ch == '2')
375 i64 = (int32_t)va_arg(*pArgs, int32_t);
376 else
377 AssertMsgFailed(("Invalid format %%VI1%c.10s\n", ch, *ppszFormat));
378 }
379 else if (ch == '6')
380 {
381 ch = *(*ppszFormat)++;
382 if (ch == '4')
383 i64 = (int64_t)va_arg(*pArgs, int64_t);
384 else
385 AssertMsgFailed(("Invalid format %%VI1%c.10s\n", ch, *ppszFormat));
386 }
387 else
388 AssertMsgFailed(("Invalid format %%VI%c.10s\n", ch, *ppszFormat));
389
390 cch = RTStrFormatNumber(szNum, i64, 10, cchWidth, cchPrecision, fFlags | RTSTR_F_VALSIGNED);
391 Assert(cch < (int)sizeof(szNum));
392 return pfnOutput(pvArgOutput, szNum, cch);
393 }
394
395 /*
396 * Unsigned integer types: VU<bits>
397 * Hex integer types : VX<bits>
398 */
399 case 'U':
400 case 'X':
401 {
402 int iBase = ch == 'X' ? 16 : 10;
403
404 /*
405 * Figure out number of bits and read the value.
406 */
407 uint64_t u64 = 0;
408 char szNum[64];
409 int cch;
410
411 ch = *(*ppszFormat)++;
412 if (ch == '8')
413 u64 = (uint8_t)va_arg(*pArgs, unsigned);
414 else if (ch == '1')
415 {
416 ch = *(*ppszFormat)++;
417 if (ch == '6')
418 u64 = (uint16_t)va_arg(*pArgs, unsigned);
419 else
420 AssertMsgFailed(("Invalid format %%VI1%c.10s\n", ch, *ppszFormat));
421 }
422 else if (ch == '3')
423 {
424 ch = *(*ppszFormat)++;
425 if (ch == '2')
426 u64 = (uint32_t)va_arg(*pArgs, uint32_t);
427 else
428 AssertMsgFailed(("Invalid format %%VI1%c.10s\n", ch, *ppszFormat));
429 }
430 else if (ch == '6')
431 {
432 ch = *(*ppszFormat)++;
433 if (ch == '4')
434 u64 = (uint64_t)va_arg(*pArgs, uint64_t);
435 else
436 AssertMsgFailed(("Invalid format %%VI1%c.10s\n", ch, *ppszFormat));
437 }
438 else
439 AssertMsgFailed(("Invalid format %%VI%c.10s\n", ch, *ppszFormat));
440
441 cch = RTStrFormatNumber(szNum, u64, iBase, cchWidth, cchPrecision, fFlags);
442 Assert(cch < (int)sizeof(szNum));
443 return pfnOutput(pvArgOutput, szNum, cch);
444 }
445
446 /*
447 * UUID.
448 */
449 case 'u':
450 if ( (*ppszFormat)[0] == 'u'
451 && (*ppszFormat)[1] == 'i'
452 && (*ppszFormat)[2] == 'd')
453 {
454 PRTUUID pUuid = va_arg(*pArgs, PRTUUID);
455 static const char szNull[] = "<NULL>";
456
457 (*ppszFormat) += 3;
458 if (VALID_PTR(pUuid))
459 {
460 /* cannot call RTUuidToStr because of GC/R0. */
461 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
462 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
463 pUuid->Gen.u32TimeLow,
464 pUuid->Gen.u16TimeMid,
465 pUuid->Gen.u16TimeHiAndVersion,
466 pUuid->Gen.u8ClockSeqHiAndReserved,
467 pUuid->Gen.u8ClockSeqLow,
468 pUuid->Gen.au8Node[0],
469 pUuid->Gen.au8Node[1],
470 pUuid->Gen.au8Node[2],
471 pUuid->Gen.au8Node[3],
472 pUuid->Gen.au8Node[4],
473 pUuid->Gen.au8Node[5]);
474 }
475
476 return pfnOutput(pvArgOutput, szNull, sizeof(szNull) - 1);
477 }
478 /* fall thru */
479
480 /*
481 * Invalid/Unknown. Bitch about it.
482 */
483 default:
484 AssertMsgFailed(("Invalid VBox format type '%%V%c%.10s'!\n", ch, *ppszFormat));
485 break;
486 }
487 }
488 else
489 AssertMsgFailed(("Invalid VBox format type '%%%c%.10s'!\n", ch, *ppszFormat));
490
491 return 0;
492}
493
Note: See TracBrowser for help on using the repository browser.

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