VirtualBox

source: vbox/trunk/src/bldprogs/VBoxTpG.cpp@ 42414

Last change on this file since 42414 was 41362, checked in by vboxsync, 13 years ago

VBoxTpG: attempt to fix 32-bit builds

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 92.9 KB
Line 
1/* $Id: VBoxTpG.cpp 41362 2012-05-21 11:47:16Z vboxsync $ */
2/** @file
3 * VBox Build Tool - VBox Tracepoint Generator.
4 */
5
6/*
7 * Copyright (C) 2012 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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <VBox/VBoxTpG.h>
23
24#include <iprt/alloca.h>
25#include <iprt/assert.h>
26#include <iprt/ctype.h>
27#include <iprt/env.h>
28#include <iprt/err.h>
29#include <iprt/file.h>
30#include <iprt/getopt.h>
31#include <iprt/initterm.h>
32#include <iprt/list.h>
33#include <iprt/mem.h>
34#include <iprt/message.h>
35#include <iprt/path.h>
36#include <iprt/process.h>
37#include <iprt/stream.h>
38#include <iprt/string.h>
39#include <iprt/uuid.h>
40
41#include "scmstream.h"
42
43
44/*******************************************************************************
45* Structures and Typedefs *
46*******************************************************************************/
47
48typedef struct VTGATTRS
49{
50 kVTGStability enmCode;
51 kVTGStability enmData;
52 kVTGClass enmDataDep;
53} VTGATTRS;
54typedef VTGATTRS *PVTGATTRS;
55
56
57typedef struct VTGARG
58{
59 RTLISTNODE ListEntry;
60 /** The argument name. (heap) */
61 char *pszName;
62 /** The type presented to the tracer (in string table). */
63 const char *pszTracerType;
64 /** The argument type used in the probe method in that context. (heap) */
65 char *pszCtxType;
66 /** Argument passing format string. First and only argument is the name.
67 * (const string) */
68 const char *pszArgPassingFmt;
69 /** The type flags. */
70 uint32_t fType;
71} VTGARG;
72typedef VTGARG *PVTGARG;
73
74typedef struct VTGPROBE
75{
76 RTLISTNODE ListEntry;
77 char *pszMangledName;
78 const char *pszUnmangledName;
79 RTLISTANCHOR ArgHead;
80 uint32_t cArgs;
81 bool fHaveLargeArgs;
82 uint32_t offArgList;
83 uint32_t iProbe;
84} VTGPROBE;
85typedef VTGPROBE *PVTGPROBE;
86
87typedef struct VTGPROVIDER
88{
89 RTLISTNODE ListEntry;
90 const char *pszName;
91
92 uint16_t iFirstProbe;
93 uint16_t cProbes;
94
95 VTGATTRS AttrSelf;
96 VTGATTRS AttrModules;
97 VTGATTRS AttrFunctions;
98 VTGATTRS AttrName;
99 VTGATTRS AttrArguments;
100
101 RTLISTANCHOR ProbeHead;
102} VTGPROVIDER;
103typedef VTGPROVIDER *PVTGPROVIDER;
104
105/**
106 * A string table string.
107 */
108typedef struct VTGSTRING
109{
110 /** The string space core. */
111 RTSTRSPACECORE Core;
112 /** The string table offset. */
113 uint32_t offStrTab;
114 /** The actual string. */
115 char szString[1];
116} VTGSTRING;
117typedef VTGSTRING *PVTGSTRING;
118
119
120/*******************************************************************************
121* Global Variables *
122*******************************************************************************/
123/** The string space organizing the string table strings. Each node is a VTGSTRING. */
124static RTSTRSPACE g_StrSpace = NULL;
125/** Used by the string table enumerator to set VTGSTRING::offStrTab. */
126static uint32_t g_offStrTab;
127/** List of providers created by the parser. */
128static RTLISTANCHOR g_ProviderHead;
129/** The number of type errors. */
130static uint32_t g_cTypeErrors = 0;
131
132
133/** @name Options
134 * @{ */
135static enum
136{
137 kVBoxTpGAction_Nothing,
138 kVBoxTpGAction_GenerateHeader,
139 kVBoxTpGAction_GenerateWrapperHeader,
140 kVBoxTpGAction_GenerateObject
141} g_enmAction = kVBoxTpGAction_Nothing;
142static uint32_t g_cBits = HC_ARCH_BITS;
143static uint32_t g_cHostBits = HC_ARCH_BITS;
144static uint32_t g_fTypeContext = VTG_TYPE_CTX_R0;
145static const char *g_pszContextDefine = "IN_RING0";
146static bool g_fApplyCpp = false;
147static uint32_t g_cVerbosity = 0;
148static const char *g_pszOutput = NULL;
149static const char *g_pszScript = NULL;
150static const char *g_pszTempAsm = NULL;
151#ifdef RT_OS_DARWIN
152static const char *g_pszAssembler = "yasm";
153static const char *g_pszAssemblerFmtOpt = "-f";
154static const char g_szAssemblerFmtVal32[] = "macho32";
155static const char g_szAssemblerFmtVal64[] = "macho64";
156static const char g_szAssemblerOsDef[] = "RT_OS_DARWIN";
157#elif defined(RT_OS_OS2)
158static const char *pszAssembler = "nasm.exe";
159static const char *pszAssemblerFmtOpt = "-f";
160static const char g_szAssemblerFmtVal32[] = "obj";
161static const char g_szAssemblerFmtVal64[] = "elf64";
162static const char g_szAssemblerOsDef[] = "RT_OS_OS2";
163#elif defined(RT_OS_WINDOWS)
164static const char *g_pszAssembler = "yasm.exe";
165static const char *g_pszAssemblerFmtOpt = "-f";
166static const char g_szAssemblerFmtVal32[] = "win32";
167static const char g_szAssemblerFmtVal64[] = "win64";
168static const char g_szAssemblerOsDef[] = "RT_OS_WINDOWS";
169#else
170static const char *g_pszAssembler = "yasm";
171static const char *g_pszAssemblerFmtOpt = "-f";
172static const char g_szAssemblerFmtVal32[] = "elf32";
173static const char g_szAssemblerFmtVal64[] = "elf64";
174# ifdef RT_OS_FREEBSD
175static const char g_szAssemblerOsDef[] = "RT_OS_FREEBSD";
176# elif defined(RT_OS_NETBSD)
177static const char g_szAssemblerOsDef[] = "RT_OS_NETBSD";
178# elif defined(RT_OS_OPENBSD)
179static const char g_szAssemblerOsDef[] = "RT_OS_OPENBSD";
180# elif defined(RT_OS_LINUX)
181static const char g_szAssemblerOsDef[] = "RT_OS_LINUX";
182# elif defined(RT_OS_SOLARIS)
183static const char g_szAssemblerOsDef[] = "RT_OS_SOLARIS";
184# else
185# error "Port me!"
186# endif
187#endif
188static const char *g_pszAssemblerFmtVal = RT_CONCAT(g_szAssemblerFmtVal, HC_ARCH_BITS);
189static const char *g_pszAssemblerDefOpt = "-D";
190static const char *g_pszAssemblerIncOpt = "-I";
191static char g_szAssemblerIncVal[RTPATH_MAX];
192static const char *g_pszAssemblerIncVal = __FILE__ "/../../../include/";
193static const char *g_pszAssemblerOutputOpt = "-o";
194static unsigned g_cAssemblerOptions = 0;
195static const char *g_apszAssemblerOptions[32];
196static const char *g_pszProbeFnName = "SUPR0TracerFireProbe";
197static bool g_fProbeFnImported = true;
198static bool g_fPic = false;
199/** @} */
200
201
202
203
204/**
205 * Inserts a string into the string table, reusing any matching existing string
206 * if possible.
207 *
208 * @returns Read only string.
209 * @param pch The string to insert (need not be terminated).
210 * @param cch The length of the string.
211 */
212static const char *strtabInsertN(const char *pch, size_t cch)
213{
214 PVTGSTRING pStr = (PVTGSTRING)RTStrSpaceGetN(&g_StrSpace, pch, cch);
215 if (pStr)
216 return pStr->szString;
217
218 /*
219 * Create a new entry.
220 */
221 pStr = (PVTGSTRING)RTMemAlloc(RT_OFFSETOF(VTGSTRING, szString[cch + 1]));
222 if (!pStr)
223 return NULL;
224
225 pStr->Core.pszString = pStr->szString;
226 memcpy(pStr->szString, pch, cch);
227 pStr->szString[cch] = '\0';
228 pStr->offStrTab = UINT32_MAX;
229
230 bool fRc = RTStrSpaceInsert(&g_StrSpace, &pStr->Core);
231 Assert(fRc); NOREF(fRc);
232 return pStr->szString;
233}
234
235
236/**
237 * Retrieves the string table offset of the given string table string.
238 *
239 * @returns String table offset.
240 * @param pszStrTabString The string table string.
241 */
242static uint32_t strtabGetOff(const char *pszStrTabString)
243{
244 PVTGSTRING pStr = RT_FROM_MEMBER(pszStrTabString, VTGSTRING, szString[0]);
245 Assert(pStr->Core.pszString == pszStrTabString);
246 return pStr->offStrTab;
247}
248
249
250/**
251 * Invokes the assembler.
252 *
253 * @returns Exit code.
254 * @param pszOutput The output file.
255 * @param pszTempAsm The source file.
256 */
257static RTEXITCODE generateInvokeAssembler(const char *pszOutput, const char *pszTempAsm)
258{
259 const char *apszArgs[64];
260 unsigned iArg = 0;
261
262 apszArgs[iArg++] = g_pszAssembler;
263 apszArgs[iArg++] = g_pszAssemblerFmtOpt;
264 apszArgs[iArg++] = g_pszAssemblerFmtVal;
265 apszArgs[iArg++] = g_pszAssemblerDefOpt;
266 if (!strcmp(g_pszAssemblerFmtVal, "macho32") || !strcmp(g_pszAssemblerFmtVal, "macho64"))
267 apszArgs[iArg++] = "ASM_FORMAT_MACHO";
268 else if (!strcmp(g_pszAssemblerFmtVal, "obj") || !strcmp(g_pszAssemblerFmtVal, "omf"))
269 apszArgs[iArg++] = "ASM_FORMAT_OMF";
270 else if ( !strcmp(g_pszAssemblerFmtVal, "win32")
271 || !strcmp(g_pszAssemblerFmtVal, "win64")
272 || !strcmp(g_pszAssemblerFmtVal, "pe32")
273 || !strcmp(g_pszAssemblerFmtVal, "pe64")
274 || !strcmp(g_pszAssemblerFmtVal, "pe") )
275 apszArgs[iArg++] = "ASM_FORMAT_PE";
276 else if ( !strcmp(g_pszAssemblerFmtVal, "elf32")
277 || !strcmp(g_pszAssemblerFmtVal, "elf64")
278 || !strcmp(g_pszAssemblerFmtVal, "elf"))
279 apszArgs[iArg++] = "ASM_FORMAT_ELF";
280 else
281 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unknown assembler format '%s'", g_pszAssemblerFmtVal);
282 apszArgs[iArg++] = g_pszAssemblerDefOpt;
283 if (g_cBits == 32)
284 apszArgs[iArg++] = "ARCH_BITS=32";
285 else
286 apszArgs[iArg++] = "ARCH_BITS=64";
287 apszArgs[iArg++] = g_pszAssemblerDefOpt;
288 if (g_cHostBits == 32)
289 apszArgs[iArg++] = "HC_ARCH_BITS=32";
290 else
291 apszArgs[iArg++] = "HC_ARCH_BITS=64";
292 apszArgs[iArg++] = g_pszAssemblerDefOpt;
293 if (g_cBits == 32)
294 apszArgs[iArg++] = "RT_ARCH_X86";
295 else
296 apszArgs[iArg++] = "RT_ARCH_AMD64";
297 apszArgs[iArg++] = g_pszAssemblerDefOpt;
298 apszArgs[iArg++] = g_pszContextDefine;
299 if (g_szAssemblerOsDef[0])
300 {
301 apszArgs[iArg++] = g_pszAssemblerDefOpt;
302 apszArgs[iArg++] = g_szAssemblerOsDef;
303 }
304 apszArgs[iArg++] = g_pszAssemblerIncOpt;
305 apszArgs[iArg++] = g_pszAssemblerIncVal;
306 apszArgs[iArg++] = g_pszAssemblerOutputOpt;
307 apszArgs[iArg++] = pszOutput;
308 for (unsigned i = 0; i < g_cAssemblerOptions; i++)
309 apszArgs[iArg++] = g_apszAssemblerOptions[i];
310 apszArgs[iArg++] = pszTempAsm;
311 apszArgs[iArg] = NULL;
312
313 if (g_cVerbosity > 1)
314 {
315 RTMsgInfo("Starting assmbler '%s' with arguments:\n", g_pszAssembler);
316 for (unsigned i = 0; i < iArg; i++)
317 RTMsgInfo(" #%02u: '%s'\n", i, apszArgs[i]);
318 }
319
320 RTPROCESS hProc;
321 int rc = RTProcCreate(apszArgs[0], apszArgs, RTENV_DEFAULT, RTPROC_FLAGS_SEARCH_PATH, &hProc);
322 if (RT_FAILURE(rc))
323 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to start '%s' (assembler): %Rrc", apszArgs[0], rc);
324
325 RTPROCSTATUS Status;
326 rc = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &Status);
327 if (RT_FAILURE(rc))
328 {
329 RTProcTerminate(hProc);
330 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTProcWait failed: %Rrc", rc);
331 }
332 if (Status.enmReason == RTPROCEXITREASON_SIGNAL)
333 return RTMsgErrorExit(RTEXITCODE_FAILURE, "The assembler failed: signal %d", Status.iStatus);
334 if (Status.enmReason != RTPROCEXITREASON_NORMAL)
335 return RTMsgErrorExit(RTEXITCODE_FAILURE, "The assembler failed: abend");
336 if (Status.iStatus != 0)
337 return RTMsgErrorExit((RTEXITCODE)Status.iStatus, "The assembler failed: exit code %d", Status.iStatus);
338
339 return RTEXITCODE_SUCCESS;
340}
341
342
343/**
344 * Worker that does the boring bits when generating a file.
345 *
346 * @returns Exit code.
347 * @param pszOutput The name of the output file.
348 * @param pszWhat What kind of file it is.
349 * @param pfnGenerator The callback function that provides the contents
350 * of the file.
351 */
352static RTEXITCODE generateFile(const char *pszOutput, const char *pszWhat,
353 RTEXITCODE (*pfnGenerator)(PSCMSTREAM))
354{
355 SCMSTREAM Strm;
356 int rc = ScmStreamInitForWriting(&Strm, NULL);
357 if (RT_FAILURE(rc))
358 return RTMsgErrorExit(RTEXITCODE_FAILURE, "ScmStreamInitForWriting returned %Rrc when generating the %s file",
359 rc, pszWhat);
360
361 RTEXITCODE rcExit = pfnGenerator(&Strm);
362 if (RT_FAILURE(ScmStreamGetStatus(&Strm)))
363 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Stream error %Rrc generating the %s file",
364 ScmStreamGetStatus(&Strm), pszWhat);
365 if (rcExit == RTEXITCODE_SUCCESS)
366 {
367 rc = ScmStreamWriteToFile(&Strm, "%s", pszOutput);
368 if (RT_FAILURE(rc))
369 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "ScmStreamWriteToFile returned %Rrc when writing '%s' (%s)",
370 rc, pszOutput, pszWhat);
371 if (rcExit == RTEXITCODE_SUCCESS)
372 {
373 if (g_cVerbosity > 0)
374 RTMsgInfo("Successfully generated '%s'.", pszOutput);
375 if (g_cVerbosity > 1)
376 {
377 RTMsgInfo("================ %s - start ================", pszWhat);
378 ScmStreamRewindForReading(&Strm);
379 const char *pszLine;
380 size_t cchLine;
381 SCMEOL enmEol;
382 while ((pszLine = ScmStreamGetLine(&Strm, &cchLine, &enmEol)) != NULL)
383 RTPrintf("%.*s\n", cchLine, pszLine);
384 RTMsgInfo("================ %s - end ================", pszWhat);
385 }
386 }
387 }
388 ScmStreamDelete(&Strm);
389 return rcExit;
390}
391
392
393/**
394 * @callback_method_impl{FNRTSTRSPACECALLBACK, Writes the string table strings.}
395 */
396static DECLCALLBACK(int) generateAssemblyStrTabCallback(PRTSTRSPACECORE pStr, void *pvUser)
397{
398 PVTGSTRING pVtgStr = (PVTGSTRING)pStr;
399 PSCMSTREAM pStrm = (PSCMSTREAM)pvUser;
400
401 pVtgStr->offStrTab = g_offStrTab;
402 g_offStrTab += (uint32_t)pVtgStr->Core.cchString + 1;
403
404 ScmStreamPrintf(pStrm,
405 " db '%s', 0 ; off=%u len=%zu\n",
406 pVtgStr->szString, pVtgStr->offStrTab, pVtgStr->Core.cchString);
407 return VINF_SUCCESS;
408}
409
410
411/**
412 * Generate assembly source that can be turned into an object file.
413 *
414 * (This is a generateFile callback.)
415 *
416 * @returns Exit code.
417 * @param pStrm The output stream.
418 */
419static RTEXITCODE generateAssembly(PSCMSTREAM pStrm)
420{
421 PVTGPROVIDER pProvider;
422 PVTGPROBE pProbe;
423 PVTGARG pArg;
424
425
426 if (g_cVerbosity > 0)
427 RTMsgInfo("Generating assembly code...");
428
429 /*
430 * Write the file header.
431 */
432 ScmStreamPrintf(pStrm,
433 "; $Id: VBoxTpG.cpp 41362 2012-05-21 11:47:16Z vboxsync $ \n"
434 ";; @file\n"
435 "; Automatically generated from %s. Do NOT edit!\n"
436 ";\n"
437 "\n"
438 "%%include \"iprt/asmdefs.mac\"\n"
439 "\n"
440 "\n"
441 ";"
442 "; We put all the data in a dedicated section / segment.\n"
443 ";\n"
444 "; In order to find the probe location specifiers, we do the necessary\n"
445 "; trickery here, ASSUMING that this object comes in first in the link\n"
446 "; editing process.\n"
447 ";\n"
448 "%%ifdef ASM_FORMAT_OMF\n"
449 " %%macro VTG_GLOBAL 2\n"
450 " global NAME(%%1)\n"
451 " NAME(%%1):\n"
452 " %%endmacro\n"
453 " segment VTG.Obj public CLASS=VTG align=4096 use32\n"
454 "\n"
455 "%%elifdef ASM_FORMAT_MACHO\n"
456 " %%macro VTG_GLOBAL 2\n"
457 " global NAME(%%1)\n"
458 " NAME(%%1):\n"
459 " %%endmacro\n"
460 " ; Section order hack!\n"
461 " ; With the ld64-97.17 linker there was a problem with it determin the section\n"
462 " ; order based on symbol references. The references to the start and end of the\n"
463 " ; __VTGPrLc section forced it in front of __VTGObj.\n"
464 " extern section$start$__VTG$__VTGObj\n"
465 " extern section$end$__VTG$__VTGObj\n"
466 " [section __VTG __VTGObj align=1024]\n"
467 "\n"
468 "%%elifdef ASM_FORMAT_PE\n"
469 " %%macro VTG_GLOBAL 2\n"
470 " global NAME(%%1)\n"
471 " NAME(%%1):\n"
472 " %%endmacro\n"
473 " [section VTGPrLc.Begin data align=64]\n"
474 /*" times 16 db 0xcc\n"*/
475 "VTG_GLOBAL g_aVTGPrLc, data\n"
476 " [section VTGPrLc.Data data align=4]\n"
477 " [section VTGPrLc.End data align=4]\n"
478 "VTG_GLOBAL g_aVTGPrLc_End, data\n"
479 /*" times 16 db 0xcc\n"*/
480 " [section VTGObj data align=32]\n"
481 "\n"
482 "%%elifdef ASM_FORMAT_ELF\n"
483 " %%macro VTG_GLOBAL 2\n"
484 " global NAME(%%1):%%2 hidden\n"
485 " NAME(%%1):\n"
486 " %%endmacro\n"
487 " [section .VTGData progbits alloc noexec write align=4096]\n"
488 " [section .VTGPrLc.Begin progbits alloc noexec write align=32]\n"
489 " dd 0,0,0,0, 0,0,0,0\n"
490 "VTG_GLOBAL g_aVTGPrLc, data\n"
491 " [section .VTGPrLc progbits alloc noexec write align=1]\n"
492 " [section .VTGPrLc.End progbits alloc noexec write align=1]\n"
493 "VTG_GLOBAL g_aVTGPrLc_End, data\n"
494 " dd 0,0,0,0, 0,0,0,0\n"
495 " [section .VTGData]\n"
496 "\n"
497 "%%else\n"
498 " %%error \"ASM_FORMAT_XXX is not defined\"\n"
499 "%%endif\n"
500 "\n"
501 "\n"
502 "VTG_GLOBAL g_VTGObjHeader, data\n"
503 " ;0 1 2 3\n"
504 " ;012345678901234567890123456789012\n"
505 " db 'VTG Object Header v1.5', 0, 0\n"
506 " dd %u\n"
507 " dd NAME(g_acVTGProbeEnabled_End) - NAME(g_VTGObjHeader)\n"
508 " dd NAME(g_achVTGStringTable) - NAME(g_VTGObjHeader)\n"
509 " dd NAME(g_achVTGStringTable_End) - NAME(g_achVTGStringTable)\n"
510 " dd NAME(g_aVTGArgLists) - NAME(g_VTGObjHeader)\n"
511 " dd NAME(g_aVTGArgLists_End) - NAME(g_aVTGArgLists)\n"
512 " dd NAME(g_aVTGProbes) - NAME(g_VTGObjHeader)\n"
513 " dd NAME(g_aVTGProbes_End) - NAME(g_aVTGProbes)\n"
514 " dd NAME(g_aVTGProviders) - NAME(g_VTGObjHeader)\n"
515 " dd NAME(g_aVTGProviders_End) - NAME(g_aVTGProviders)\n"
516 " dd NAME(g_acVTGProbeEnabled) - NAME(g_VTGObjHeader)\n"
517 " dd NAME(g_acVTGProbeEnabled_End) - NAME(g_acVTGProbeEnabled)\n"
518 " dd 0\n"
519 " dd 0\n"
520 "%%ifdef ASM_FORMAT_MACHO ; Apple has a real decent linker!\n"
521 "extern section$start$__VTG$__VTGPrLc\n"
522 " RTCCPTR_DEF section$start$__VTG$__VTGPrLc\n"
523 " %%if ARCH_BITS == 32\n"
524 " dd 0\n"
525 " %%endif\n"
526 "extern section$end$__VTG$__VTGPrLc\n"
527 " RTCCPTR_DEF section$end$__VTG$__VTGPrLc\n"
528 " %%if ARCH_BITS == 32\n"
529 " dd 0\n"
530 " %%endif\n"
531 "%%else\n"
532 " RTCCPTR_DEF NAME(g_aVTGPrLc)\n"
533 " %%if ARCH_BITS == 32\n"
534 " dd 0\n"
535 " %%endif\n"
536 " RTCCPTR_DEF NAME(g_aVTGPrLc_End)\n"
537 " %%if ARCH_BITS == 32\n"
538 " dd 0\n"
539 " %%endif\n"
540 "%%endif\n"
541 ,
542 g_pszScript, g_cBits);
543 RTUUID Uuid;
544 int rc = RTUuidCreate(&Uuid);
545 if (RT_FAILURE(rc))
546 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTUuidCreate failed: %Rrc", rc);
547 ScmStreamPrintf(pStrm,
548 " dd 0%08xh, 0%08xh, 0%08xh, 0%08xh\n"
549 "%%ifdef ASM_FORMAT_MACHO\n"
550 " RTCCPTR_DEF section$start$__VTG$__VTGObj\n"
551 " %%if ARCH_BITS == 32\n"
552 " dd 0\n"
553 " %%endif\n"
554 "%%else\n"
555 " dd 0, 0\n"
556 "%%endif\n"
557 " dd 0, 0\n"
558 , Uuid.au32[0], Uuid.au32[1], Uuid.au32[2], Uuid.au32[3]);
559
560 /*
561 * Dump the string table before we start using the strings.
562 */
563 ScmStreamPrintf(pStrm,
564 "\n"
565 ";\n"
566 "; The string table.\n"
567 ";\n"
568 "VTG_GLOBAL g_achVTGStringTable, data\n");
569 g_offStrTab = 0;
570 RTStrSpaceEnumerate(&g_StrSpace, generateAssemblyStrTabCallback, pStrm);
571 ScmStreamPrintf(pStrm,
572 "VTG_GLOBAL g_achVTGStringTable_End, data\n");
573
574 /*
575 * Write out the argument lists before we use them.
576 */
577 ScmStreamPrintf(pStrm,
578 "\n"
579 ";\n"
580 "; The argument lists.\n"
581 ";\n"
582 "ALIGNDATA(16)\n"
583 "VTG_GLOBAL g_aVTGArgLists, data\n");
584 uint32_t off = 0;
585 RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
586 {
587 RTListForEach(&pProvider->ProbeHead, pProbe, VTGPROBE, ListEntry)
588 {
589 if (pProbe->offArgList != UINT32_MAX)
590 continue;
591
592 /* Write it. */
593 pProbe->offArgList = off;
594 ScmStreamPrintf(pStrm,
595 " ; off=%u\n"
596 " db %2u ; Argument count\n"
597 " db %u ; fHaveLargeArgs\n"
598 " db 0, 0 ; Reserved\n"
599 , off, pProbe->cArgs, (int)pProbe->fHaveLargeArgs);
600 off += 4;
601 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
602 {
603 ScmStreamPrintf(pStrm,
604 " dd %8u ; type '%s' (name '%s')\n"
605 " dd 0%08xh ; type flags\n",
606 strtabGetOff(pArg->pszTracerType), pArg->pszTracerType, pArg->pszName,
607 pArg->fType);
608 off += 8;
609 }
610
611 /* Look for matching argument lists (lazy bird walks the whole list). */
612 PVTGPROVIDER pProv2;
613 RTListForEach(&g_ProviderHead, pProv2, VTGPROVIDER, ListEntry)
614 {
615 PVTGPROBE pProbe2;
616 RTListForEach(&pProvider->ProbeHead, pProbe2, VTGPROBE, ListEntry)
617 {
618 if (pProbe2->offArgList != UINT32_MAX)
619 continue;
620 if (pProbe2->cArgs != pProbe->cArgs)
621 continue;
622
623 PVTGARG pArg2;
624 pArg = RTListNodeGetNext(&pProbe->ArgHead, VTGARG, ListEntry);
625 pArg2 = RTListNodeGetNext(&pProbe2->ArgHead, VTGARG, ListEntry);
626 int32_t cArgs = pProbe->cArgs;
627 while ( cArgs-- > 0
628 && pArg2->pszTracerType == pArg->pszTracerType
629 && pArg2->fType == pArg->fType)
630 {
631 pArg = RTListNodeGetNext(&pArg->ListEntry, VTGARG, ListEntry);
632 pArg2 = RTListNodeGetNext(&pArg2->ListEntry, VTGARG, ListEntry);
633 }
634 if (cArgs >= 0)
635 continue;
636 pProbe2->offArgList = pProbe->offArgList;
637 }
638 }
639 }
640 }
641 ScmStreamPrintf(pStrm,
642 "VTG_GLOBAL g_aVTGArgLists_End, data\n");
643
644
645 /*
646 * Probe definitions.
647 */
648 ScmStreamPrintf(pStrm,
649 "\n"
650 ";\n"
651 "; Prob definitions.\n"
652 ";\n"
653 "ALIGNDATA(16)\n"
654 "VTG_GLOBAL g_aVTGProbes, data\n"
655 "\n");
656 uint32_t iProvider = 0;
657 uint32_t iProbe = 0;
658 RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
659 {
660 pProvider->iFirstProbe = iProbe;
661 RTListForEach(&pProvider->ProbeHead, pProbe, VTGPROBE, ListEntry)
662 {
663 ScmStreamPrintf(pStrm,
664 "VTG_GLOBAL g_VTGProbeData_%s_%s, data ; idx=#%4u\n"
665 " dd %6u ; offName\n"
666 " dd %6u ; offArgList\n"
667 " dw (NAME(g_cVTGProbeEnabled_%s_%s) - NAME(g_acVTGProbeEnabled)) / 4 ; idxEnabled\n"
668 " dw %6u ; idxProvider\n"
669 " dd NAME(g_VTGObjHeader) - NAME(g_VTGProbeData_%s_%s) ; offObjHdr\n"
670 ,
671 pProvider->pszName, pProbe->pszMangledName, iProbe,
672 strtabGetOff(pProbe->pszUnmangledName),
673 pProbe->offArgList,
674 pProvider->pszName, pProbe->pszMangledName,
675 iProvider,
676 pProvider->pszName, pProbe->pszMangledName
677 );
678 pProbe->iProbe = iProbe;
679 iProbe++;
680 }
681 pProvider->cProbes = iProbe - pProvider->iFirstProbe;
682 iProvider++;
683 }
684 ScmStreamPrintf(pStrm, "VTG_GLOBAL g_aVTGProbes_End, data\n");
685
686 /*
687 * The provider data.
688 */
689 ScmStreamPrintf(pStrm,
690 "\n"
691 ";\n"
692 "; Provider data.\n"
693 ";\n"
694 "ALIGNDATA(16)\n"
695 "VTG_GLOBAL g_aVTGProviders, data\n");
696 iProvider = 0;
697 RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
698 {
699 ScmStreamPrintf(pStrm,
700 " ; idx=#%4u - %s\n"
701 " dd %6u ; name\n"
702 " dw %6u ; index of first probe\n"
703 " dw %6u ; count of probes\n"
704 " db %d, %d, %d ; AttrSelf\n"
705 " db %d, %d, %d ; AttrModules\n"
706 " db %d, %d, %d ; AttrFunctions\n"
707 " db %d, %d, %d ; AttrName\n"
708 " db %d, %d, %d ; AttrArguments\n"
709 " db 0 ; reserved\n"
710 ,
711 iProvider, pProvider->pszName,
712 strtabGetOff(pProvider->pszName),
713 pProvider->iFirstProbe,
714 pProvider->cProbes,
715 pProvider->AttrSelf.enmCode, pProvider->AttrSelf.enmData, pProvider->AttrSelf.enmDataDep,
716 pProvider->AttrModules.enmCode, pProvider->AttrModules.enmData, pProvider->AttrModules.enmDataDep,
717 pProvider->AttrFunctions.enmCode, pProvider->AttrFunctions.enmData, pProvider->AttrFunctions.enmDataDep,
718 pProvider->AttrName.enmCode, pProvider->AttrName.enmData, pProvider->AttrName.enmDataDep,
719 pProvider->AttrArguments.enmCode, pProvider->AttrArguments.enmData, pProvider->AttrArguments.enmDataDep);
720 iProvider++;
721 }
722 ScmStreamPrintf(pStrm, "VTG_GLOBAL g_aVTGProviders_End, data\n");
723
724 /*
725 * Declare the probe enable flags.
726 *
727 * These must be placed at the end so they'll end up adjacent to the probe
728 * locations. This is important for reducing the amount of memory we need
729 * to lock down for user mode modules.
730 */
731 ScmStreamPrintf(pStrm,
732 ";\n"
733 "; Probe enabled flags.\n"
734 ";\n"
735 "ALIGNDATA(16)\n"
736 "VTG_GLOBAL g_acVTGProbeEnabled, data\n"
737 );
738 uint32_t cProbes = 0;
739 RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
740 {
741 RTListForEach(&pProvider->ProbeHead, pProbe, VTGPROBE, ListEntry)
742 {
743 ScmStreamPrintf(pStrm,
744 "VTG_GLOBAL g_cVTGProbeEnabled_%s_%s, data\n"
745 " dd 0\n",
746 pProvider->pszName, pProbe->pszMangledName);
747 cProbes++;
748 }
749 }
750 ScmStreamPrintf(pStrm, "VTG_GLOBAL g_acVTGProbeEnabled_End, data\n");
751 if (cProbes >= _32K)
752 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Too many probes: %u (max %u)", cProbes, _32K - 1);
753
754
755 /*
756 * Emit code for the stub functions.
757 */
758 bool const fWin64 = g_cBits == 64 && (!strcmp(g_pszAssemblerFmtVal, "win64") || !strcmp(g_pszAssemblerFmtVal, "pe64"));
759 bool const fMachO64 = g_cBits == 64 && !strcmp(g_pszAssemblerFmtVal, "macho64");
760 bool const fMachO32 = g_cBits == 32 && !strcmp(g_pszAssemblerFmtVal, "macho32");
761 ScmStreamPrintf(pStrm,
762 "\n"
763 ";\n"
764 "; Prob stubs.\n"
765 ";\n"
766 "BEGINCODE\n"
767 "extern %sNAME(%s)\n",
768 g_fProbeFnImported ? "IMP" : "",
769 g_pszProbeFnName);
770 if (fMachO64 && g_fProbeFnImported && !g_fPic)
771 ScmStreamPrintf(pStrm,
772 "g_pfnVtgProbeFn:\n"
773 " dq NAME(%s)\n",
774 g_pszProbeFnName);
775
776 RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
777 {
778 RTListForEach(&pProvider->ProbeHead, pProbe, VTGPROBE, ListEntry)
779 {
780 ScmStreamPrintf(pStrm,
781 "\n"
782 "VTG_GLOBAL VTGProbeStub_%s_%s, function; (VBOXTPGPROBELOC pVTGProbeLoc",
783 pProvider->pszName, pProbe->pszMangledName);
784 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
785 {
786 ScmStreamPrintf(pStrm, ", %s %s", pArg->pszTracerType, pArg->pszName);
787 }
788 ScmStreamPrintf(pStrm,
789 ");\n");
790
791 /*
792 * Check if the probe in question is enabled.
793 */
794 if (g_cBits == 32)
795 ScmStreamPrintf(pStrm,
796 " mov eax, [esp + 4]\n"
797 " test byte [eax+3], 0x80 ; fEnabled == true?\n"
798 " jz .return ; jump on false\n");
799 else if (fWin64)
800 ScmStreamPrintf(pStrm,
801 " test byte [rcx+3], 0x80 ; fEnabled == true?\n"
802 " jz .return ; jump on false\n");
803 else
804 ScmStreamPrintf(pStrm,
805 " test byte [rdi+3], 0x80 ; fEnabled == true?\n"
806 " jz .return ; jump on false\n");
807
808 /*
809 * Jump to the fire-probe function.
810 */
811 if (g_cBits == 32)
812 ScmStreamPrintf(pStrm, g_fPic ?
813 " jmp %s wrt ..plt\n"
814 : g_fProbeFnImported ?
815 " mov ecx, IMP2(%s)\n"
816 " jmp ecx\n"
817 :
818 " jmp NAME(%s)\n"
819 , g_pszProbeFnName);
820 else if (fWin64)
821 ScmStreamPrintf(pStrm, g_fProbeFnImported ?
822 " mov rax, IMP2(%s)\n"
823 " jmp rax\n"
824 :
825 " jmp NAME(%s)\n"
826 , g_pszProbeFnName);
827 else if (fMachO64 && g_fProbeFnImported)
828 ScmStreamPrintf(pStrm,
829 " jmp [g_pfnVtgProbeFn wrt rip]\n");
830 else
831 ScmStreamPrintf(pStrm, g_fPic ?
832 " jmp [rel %s wrt ..got]\n"
833 : g_fProbeFnImported ?
834 " lea rax, [IMP2(%s)]\n"
835 " jmp rax\n"
836 :
837 " jmp NAME(%s)\n"
838 , g_pszProbeFnName);
839
840 ScmStreamPrintf(pStrm,
841 ".return:\n"
842 " ret ; The probe was disabled, return\n"
843 "\n");
844 }
845 }
846
847 return RTEXITCODE_SUCCESS;
848}
849
850
851static RTEXITCODE generateObject(const char *pszOutput, const char *pszTempAsm)
852{
853 if (!pszTempAsm)
854 {
855 size_t cch = strlen(pszOutput);
856 char *psz = (char *)alloca(cch + sizeof(".asm"));
857 memcpy(psz, pszOutput, cch);
858 memcpy(psz + cch, ".asm", sizeof(".asm"));
859 pszTempAsm = psz;
860 }
861
862 RTEXITCODE rcExit = generateFile(pszTempAsm, "assembly", generateAssembly);
863 if (rcExit == RTEXITCODE_SUCCESS)
864 rcExit = generateInvokeAssembler(pszOutput, pszTempAsm);
865 RTFileDelete(pszTempAsm);
866 return rcExit;
867}
868
869
870static RTEXITCODE generateProbeDefineName(char *pszBuf, size_t cbBuf, const char *pszProvider, const char *pszProbe)
871{
872 size_t cbMax = strlen(pszProvider) + 1 + strlen(pszProbe) + 1;
873 if (cbMax > cbBuf || cbMax > 80)
874 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Probe '%s' in provider '%s' ends up with a too long defined\n", pszProbe, pszProvider);
875
876 while (*pszProvider)
877 *pszBuf++ = RT_C_TO_UPPER(*pszProvider++);
878
879 *pszBuf++ = '_';
880
881 while (*pszProbe)
882 {
883 if (pszProbe[0] == '_' && pszProbe[1] == '_')
884 pszProbe++;
885 *pszBuf++ = RT_C_TO_UPPER(*pszProbe++);
886 }
887
888 *pszBuf = '\0';
889 return RTEXITCODE_SUCCESS;
890}
891
892
893/**
894 * Called via generateFile to generate the header file.
895 *
896 * @returns Exit code status.
897 * @param pStrm The output stream.
898 */
899static RTEXITCODE generateHeader(PSCMSTREAM pStrm)
900{
901 /*
902 * Calc the double inclusion blocker define and then write the file header.
903 */
904 char szTmp[4096];
905 const char *pszName = RTPathFilename(g_pszScript);
906 size_t cchName = strlen(pszName);
907 if (cchName >= sizeof(szTmp) - 64)
908 return RTMsgErrorExit(RTEXITCODE_FAILURE, "File name is too long '%s'", pszName);
909 szTmp[0] = '_';
910 szTmp[1] = '_';
911 szTmp[2] = '_';
912 memcpy(&szTmp[3], pszName, cchName);
913 szTmp[3 + cchName + 0] = '_';
914 szTmp[3 + cchName + 1] = '_';
915 szTmp[3 + cchName + 2] = '_';
916 szTmp[3 + cchName + 3] = '\0';
917 char *psz = &szTmp[3];
918 while (*psz)
919 {
920 if (!RT_C_IS_ALNUM(*psz) && *psz != '_')
921 *psz = '_';
922 psz++;
923 }
924
925 ScmStreamPrintf(pStrm,
926 "/* $Id: VBoxTpG.cpp 41362 2012-05-21 11:47:16Z vboxsync $ */\n"
927 "/** @file\n"
928 " * Automatically generated from %s. Do NOT edit!\n"
929 " */\n"
930 "\n"
931 "#ifndef %s\n"
932 "#define %s\n"
933 "\n"
934 "#include <VBox/VBoxTpG.h>\n"
935 "\n"
936 "#ifndef %s\n"
937 "# error \"Expected '%s' to be defined\"\n"
938 "#endif\n"
939 "\n"
940 "RT_C_DECLS_BEGIN\n"
941 "\n"
942 "#ifdef VBOX_WITH_DTRACE\n"
943 "\n"
944 "# ifdef _MSC_VER\n"
945 "# pragma data_seg(VTG_LOC_SECT)\n"
946 "# pragma data_seg()\n"
947 "# endif\n"
948 "\n"
949 ,
950 g_pszScript,
951 szTmp,
952 szTmp,
953 g_pszContextDefine,
954 g_pszContextDefine);
955
956 /*
957 * Declare data, code and macros for each probe.
958 */
959 PVTGPROVIDER pProv;
960 PVTGPROBE pProbe;
961 PVTGARG pArg;
962 RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
963 {
964 RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
965 {
966 PVTGARG const pFirstArg = RTListGetFirst(&pProbe->ArgHead, VTGARG, ListEntry);
967
968 ScmStreamPrintf(pStrm,
969 "extern uint32_t g_cVTGProbeEnabled_%s_%s;\n"
970 "extern VTGDESCPROBE g_VTGProbeData_%s_%s;\n"
971 "DECLASM(void) VTGProbeStub_%s_%s(PVTGPROBELOC",
972 pProv->pszName, pProbe->pszMangledName,
973 pProv->pszName, pProbe->pszMangledName,
974 pProv->pszName, pProbe->pszMangledName);
975 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
976 {
977 ScmStreamPrintf(pStrm, ", %s", pArg->pszCtxType);
978 }
979 generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
980 ScmStreamPrintf(pStrm,
981 ");\n"
982 "# define %s_ENABLED() \\\n"
983 " (RT_UNLIKELY(g_cVTGProbeEnabled_%s_%s)) \n"
984 "# define %s("
985 , szTmp,
986 pProv->pszName, pProbe->pszMangledName,
987 szTmp);
988 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
989 {
990 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
991 ScmStreamPrintf(pStrm, "%s", pArg->pszName);
992 else
993 ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
994 }
995 ScmStreamPrintf(pStrm,
996 ") \\\n"
997 " do { \\\n"
998 " if (RT_UNLIKELY(g_cVTGProbeEnabled_%s_%s)) \\\n"
999 " { \\\n"
1000 " VTG_DECL_VTGPROBELOC(s_VTGProbeLoc) = \\\n"
1001 " { __LINE__, 0, 0, __FUNCTION__, &g_VTGProbeData_%s_%s }; \\\n"
1002 " VTGProbeStub_%s_%s(&s_VTGProbeLoc",
1003 pProv->pszName, pProbe->pszMangledName,
1004 pProv->pszName, pProbe->pszMangledName,
1005 pProv->pszName, pProbe->pszMangledName);
1006 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1007 {
1008 ScmStreamPrintf(pStrm, pArg->pszArgPassingFmt, pArg->pszName);
1009 }
1010 ScmStreamPrintf(pStrm,
1011 "); \\\n"
1012 " } \\\n"
1013 " { \\\n" );
1014 uint32_t iArg = 0;
1015 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1016 {
1017 if ((pArg->fType & (VTG_TYPE_FIXED_SIZED | VTG_TYPE_AUTO_CONV_PTR)) == VTG_TYPE_FIXED_SIZED)
1018 ScmStreamPrintf(pStrm,
1019 " AssertCompile(sizeof(%s) == %u); \\\n"
1020 " AssertCompile(sizeof(%s) <= %u); \\\n",
1021 pArg->pszTracerType, pArg->fType & VTG_TYPE_SIZE_MASK,
1022 pArg->pszName, pArg->fType & VTG_TYPE_SIZE_MASK);
1023 else if (pArg->fType & (VTG_TYPE_POINTER | VTG_TYPE_HC_ARCH_SIZED))
1024 ScmStreamPrintf(pStrm,
1025 " AssertCompile(sizeof(%s) <= sizeof(uintptr_t)); \\\n"
1026 " AssertCompile(sizeof(%s) <= sizeof(uintptr_t)); \\\n",
1027 pArg->pszName,
1028 pArg->pszTracerType);
1029 iArg++;
1030 }
1031 ScmStreamPrintf(pStrm,
1032 " } \\\n"
1033 " } while (0)\n"
1034 "\n");
1035 }
1036 }
1037
1038 ScmStreamPrintf(pStrm,
1039 "\n"
1040 "#else\n"
1041 "\n");
1042 RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
1043 {
1044 RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
1045 {
1046 generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
1047 ScmStreamPrintf(pStrm,
1048 "# define %s_ENABLED() (false)\n"
1049 "# define %s("
1050 , szTmp, szTmp);
1051 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1052 {
1053 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1054 ScmStreamPrintf(pStrm, "%s", pArg->pszName);
1055 else
1056 ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
1057 }
1058 ScmStreamPrintf(pStrm,
1059 ") do { } while (0)\n");
1060 }
1061 }
1062
1063 ScmStreamWrite(pStrm, RT_STR_TUPLE("\n"
1064 "#endif\n"
1065 "\n"
1066 "RT_C_DECLS_END\n"
1067 "#endif\n"));
1068 return RTEXITCODE_SUCCESS;
1069}
1070
1071
1072/**
1073 * Called via generateFile to generate the wrapper header file.
1074 *
1075 * @returns Exit code status.
1076 * @param pStrm The output stream.
1077 */
1078static RTEXITCODE generateWrapperHeader(PSCMSTREAM pStrm)
1079{
1080 /*
1081 * Calc the double inclusion blocker define and then write the file header.
1082 */
1083 char szTmp[4096];
1084 const char *pszName = RTPathFilename(g_pszScript);
1085 size_t cchName = strlen(pszName);
1086 if (cchName >= sizeof(szTmp) - 64)
1087 return RTMsgErrorExit(RTEXITCODE_FAILURE, "File name is too long '%s'", pszName);
1088 szTmp[0] = '_';
1089 szTmp[1] = '_';
1090 szTmp[2] = '_';
1091 memcpy(&szTmp[3], pszName, cchName);
1092 strcpy(&szTmp[3 + cchName ], "___WRAPPER___");
1093 char *psz = &szTmp[3];
1094 while (*psz)
1095 {
1096 if (!RT_C_IS_ALNUM(*psz) && *psz != '_')
1097 *psz = '_';
1098 psz++;
1099 }
1100
1101 ScmStreamPrintf(pStrm,
1102 "/* $Id: VBoxTpG.cpp 41362 2012-05-21 11:47:16Z vboxsync $ */\n"
1103 "/** @file\n"
1104 " * Automatically generated from %s. Do NOT edit!\n"
1105 " */\n"
1106 "\n"
1107 "#ifndef %s\n"
1108 "#define %s\n"
1109 "\n"
1110 "#include <VBox/VBoxTpG.h>\n"
1111 "\n"
1112 "#ifndef %s\n"
1113 "# error \"Expected '%s' to be defined\"\n"
1114 "#endif\n"
1115 "\n"
1116 "#ifdef VBOX_WITH_DTRACE\n"
1117 "\n"
1118 ,
1119 g_pszScript,
1120 szTmp,
1121 szTmp,
1122 g_pszContextDefine,
1123 g_pszContextDefine);
1124
1125 /*
1126 * Declare macros for each probe.
1127 */
1128 PVTGPROVIDER pProv;
1129 PVTGPROBE pProbe;
1130 PVTGARG pArg;
1131 RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
1132 {
1133 RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
1134 {
1135 PVTGARG const pFirstArg = RTListGetFirst(&pProbe->ArgHead, VTGARG, ListEntry);
1136
1137 generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
1138 ScmStreamPrintf(pStrm,
1139 "# define %s("
1140 , szTmp);
1141 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1142 {
1143 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1144 ScmStreamPrintf(pStrm, "%s", pArg->pszName);
1145 else
1146 ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
1147 }
1148 ScmStreamPrintf(pStrm,
1149 ") \\\n"
1150 " do { \\\n"
1151 " if (RT_UNLIKELY(%s_ENABLED())) \\\n"
1152 " { \\\n"
1153 " %s_ORIGINAL("
1154 , szTmp, szTmp);
1155 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1156 {
1157 const char *pszFmt = pArg->pszArgPassingFmt;
1158 if (pArg->fType & VTG_TYPE_AUTO_CONV_PTR)
1159 {
1160 /* Casting is required. ASSUMES sizeof(RTR0PTR) == sizeof(RTR3PTR) - safe! */
1161 pszFmt += sizeof(", ") - 1;
1162 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1163 ScmStreamPrintf(pStrm, "(%s)%M", pArg->pszTracerType, pszFmt, pArg->pszName);
1164 else
1165 ScmStreamPrintf(pStrm, ", (%s)%M", pArg->pszTracerType, pszFmt, pArg->pszName);
1166 }
1167 else
1168 {
1169 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1170 ScmStreamPrintf(pStrm, pArg->pszArgPassingFmt + sizeof(", ") - 1, pArg->pszName);
1171 else
1172 ScmStreamPrintf(pStrm, pArg->pszArgPassingFmt, pArg->pszName);
1173 }
1174 }
1175 ScmStreamPrintf(pStrm,
1176 "); \\\n"
1177 " } \\\n"
1178 " } while (0)\n"
1179 "\n");
1180 }
1181 }
1182
1183 ScmStreamPrintf(pStrm,
1184 "\n"
1185 "#else\n"
1186 "\n");
1187 RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
1188 {
1189 RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
1190 {
1191 generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
1192 ScmStreamPrintf(pStrm,
1193 "# define %s("
1194 , szTmp);
1195 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1196 {
1197 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1198 ScmStreamPrintf(pStrm, "%s", pArg->pszName);
1199 else
1200 ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
1201 }
1202 ScmStreamPrintf(pStrm,
1203 ") do { } while (0)\n");
1204 }
1205 }
1206
1207 ScmStreamWrite(pStrm, RT_STR_TUPLE("\n"
1208 "#endif\n"
1209 "\n"
1210 "#endif\n"));
1211 return RTEXITCODE_SUCCESS;
1212}
1213
1214
1215/**
1216 * Parser error with line and position.
1217 *
1218 * @returns RTEXITCODE_FAILURE.
1219 * @param pStrm The stream.
1220 * @param cb The offset from the current position to the
1221 * point of failure.
1222 * @param pszMsg The message to display.
1223 */
1224static RTEXITCODE parseError(PSCMSTREAM pStrm, size_t cb, const char *pszMsg)
1225{
1226 if (cb)
1227 ScmStreamSeekRelative(pStrm, -(ssize_t)cb);
1228 size_t const off = ScmStreamTell(pStrm);
1229 size_t const iLine = ScmStreamTellLine(pStrm);
1230 ScmStreamSeekByLine(pStrm, iLine);
1231 size_t const offLine = ScmStreamTell(pStrm);
1232
1233 RTPrintf("%s:%d:%zd: error: %s.\n", g_pszScript, iLine + 1, off - offLine + 1, pszMsg);
1234
1235 size_t cchLine;
1236 SCMEOL enmEof;
1237 const char *pszLine = ScmStreamGetLineByNo(pStrm, iLine, &cchLine, &enmEof);
1238 if (pszLine)
1239 RTPrintf(" %.*s\n"
1240 " %*s^\n",
1241 cchLine, pszLine, off - offLine, "");
1242 return RTEXITCODE_FAILURE;
1243}
1244
1245
1246/**
1247 * Parser error with line and position.
1248 *
1249 * @returns RTEXITCODE_FAILURE.
1250 * @param pStrm The stream.
1251 * @param cb The offset from the current position to the
1252 * point of failure.
1253 * @param pszMsg The message to display.
1254 */
1255static RTEXITCODE parseErrorAbs(PSCMSTREAM pStrm, size_t off, const char *pszMsg)
1256{
1257 ScmStreamSeekAbsolute(pStrm, off);
1258 return parseError(pStrm, 0, pszMsg);
1259}
1260
1261/**
1262 * Handles a C++ one line comment.
1263 *
1264 * @returns Exit code.
1265 * @param pStrm The stream.
1266 */
1267static RTEXITCODE parseOneLineComment(PSCMSTREAM pStrm)
1268{
1269 ScmStreamSeekByLine(pStrm, ScmStreamTellLine(pStrm) + 1);
1270 return RTEXITCODE_SUCCESS;
1271}
1272
1273/**
1274 * Handles a multi-line C/C++ comment.
1275 *
1276 * @returns Exit code.
1277 * @param pStrm The stream.
1278 */
1279static RTEXITCODE parseMultiLineComment(PSCMSTREAM pStrm)
1280{
1281 unsigned ch;
1282 while ((ch = ScmStreamGetCh(pStrm)) != ~(unsigned)0)
1283 {
1284 if (ch == '*')
1285 {
1286 do
1287 ch = ScmStreamGetCh(pStrm);
1288 while (ch == '*');
1289 if (ch == '/')
1290 return RTEXITCODE_SUCCESS;
1291 }
1292 }
1293
1294 parseError(pStrm, 1, "Expected end of comment, got end of file");
1295 return RTEXITCODE_FAILURE;
1296}
1297
1298
1299/**
1300 * Skips spaces and comments.
1301 *
1302 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
1303 * @param pStrm The stream..
1304 */
1305static RTEXITCODE parseSkipSpacesAndComments(PSCMSTREAM pStrm)
1306{
1307 unsigned ch;
1308 while ((ch = ScmStreamPeekCh(pStrm)) != ~(unsigned)0)
1309 {
1310 if (!RT_C_IS_SPACE(ch) && ch != '/')
1311 return RTEXITCODE_SUCCESS;
1312 unsigned ch2 = ScmStreamGetCh(pStrm); AssertBreak(ch == ch2); NOREF(ch2);
1313 if (ch == '/')
1314 {
1315 ch = ScmStreamGetCh(pStrm);
1316 RTEXITCODE rcExit;
1317 if (ch == '*')
1318 rcExit = parseMultiLineComment(pStrm);
1319 else if (ch == '/')
1320 rcExit = parseOneLineComment(pStrm);
1321 else
1322 rcExit = parseError(pStrm, 2, "Unexpected character");
1323 if (rcExit != RTEXITCODE_SUCCESS)
1324 return rcExit;
1325 }
1326 }
1327
1328 return parseError(pStrm, 0, "Unexpected end of file");
1329}
1330
1331
1332/**
1333 * Skips spaces and comments, returning the next character.
1334 *
1335 * @returns Next non-space-non-comment character. ~(unsigned)0 on EOF or
1336 * failure.
1337 * @param pStrm The stream.
1338 */
1339static unsigned parseGetNextNonSpaceNonCommentCh(PSCMSTREAM pStrm)
1340{
1341 unsigned ch;
1342 while ((ch = ScmStreamGetCh(pStrm)) != ~(unsigned)0)
1343 {
1344 if (!RT_C_IS_SPACE(ch) && ch != '/')
1345 return ch;
1346 if (ch == '/')
1347 {
1348 ch = ScmStreamGetCh(pStrm);
1349 RTEXITCODE rcExit;
1350 if (ch == '*')
1351 rcExit = parseMultiLineComment(pStrm);
1352 else if (ch == '/')
1353 rcExit = parseOneLineComment(pStrm);
1354 else
1355 rcExit = parseError(pStrm, 2, "Unexpected character");
1356 if (rcExit != RTEXITCODE_SUCCESS)
1357 return ~(unsigned)0;
1358 }
1359 }
1360
1361 parseError(pStrm, 0, "Unexpected end of file");
1362 return ~(unsigned)0;
1363}
1364
1365
1366/**
1367 * Get the next non-space-non-comment character on a preprocessor line.
1368 *
1369 * @returns The next character. On error message and ~(unsigned)0.
1370 * @param pStrm The stream.
1371 */
1372static unsigned parseGetNextNonSpaceNonCommentChOnPpLine(PSCMSTREAM pStrm)
1373{
1374 size_t off = ScmStreamTell(pStrm) - 1;
1375 unsigned ch;
1376 while ((ch = ScmStreamGetCh(pStrm)) != ~(unsigned)0)
1377 {
1378 if (RT_C_IS_SPACE(ch))
1379 {
1380 if (ch == '\n' || ch == '\r')
1381 {
1382 parseErrorAbs(pStrm, off, "Invalid preprocessor statement");
1383 break;
1384 }
1385 }
1386 else if (ch == '\\')
1387 {
1388 size_t off2 = ScmStreamTell(pStrm) - 1;
1389 ch = ScmStreamGetCh(pStrm);
1390 if (ch == '\r')
1391 ch = ScmStreamGetCh(pStrm);
1392 if (ch != '\n')
1393 {
1394 parseErrorAbs(pStrm, off2, "Expected new line");
1395 break;
1396 }
1397 }
1398 else
1399 return ch;
1400 }
1401 return ~(unsigned)0;
1402}
1403
1404
1405
1406/**
1407 * Skips spaces and comments.
1408 *
1409 * @returns Same as ScmStreamCGetWord
1410 * @param pStrm The stream..
1411 * @param pcchWord Where to return the length.
1412 */
1413static const char *parseGetNextCWord(PSCMSTREAM pStrm, size_t *pcchWord)
1414{
1415 if (parseSkipSpacesAndComments(pStrm) != RTEXITCODE_SUCCESS)
1416 return NULL;
1417 return ScmStreamCGetWord(pStrm, pcchWord);
1418}
1419
1420
1421
1422/**
1423 * Parses interface stability.
1424 *
1425 * @returns Interface stability if parsed correctly, otherwise error message and
1426 * kVTGStability_Invalid.
1427 * @param pStrm The stream.
1428 * @param ch The first character in the stability spec.
1429 */
1430static kVTGStability parseStability(PSCMSTREAM pStrm, unsigned ch)
1431{
1432 switch (ch)
1433 {
1434 case 'E':
1435 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("External")))
1436 return kVTGStability_External;
1437 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Evolving")))
1438 return kVTGStability_Evolving;
1439 break;
1440 case 'I':
1441 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Internal")))
1442 return kVTGStability_Internal;
1443 break;
1444 case 'O':
1445 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Obsolete")))
1446 return kVTGStability_Obsolete;
1447 break;
1448 case 'P':
1449 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Private")))
1450 return kVTGStability_Private;
1451 break;
1452 case 'S':
1453 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Stable")))
1454 return kVTGStability_Stable;
1455 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Standard")))
1456 return kVTGStability_Standard;
1457 break;
1458 case 'U':
1459 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Unstable")))
1460 return kVTGStability_Unstable;
1461 break;
1462 }
1463 parseError(pStrm, 1, "Unknown stability specifier");
1464 return kVTGStability_Invalid;
1465}
1466
1467
1468/**
1469 * Parses data depndency class.
1470 *
1471 * @returns Data dependency class if parsed correctly, otherwise error message
1472 * and kVTGClass_Invalid.
1473 * @param pStrm The stream.
1474 * @param ch The first character in the stability spec.
1475 */
1476static kVTGClass parseDataDepClass(PSCMSTREAM pStrm, unsigned ch)
1477{
1478 switch (ch)
1479 {
1480 case 'C':
1481 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Common")))
1482 return kVTGClass_Common;
1483 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Cpu")))
1484 return kVTGClass_Cpu;
1485 break;
1486 case 'G':
1487 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Group")))
1488 return kVTGClass_Group;
1489 break;
1490 case 'I':
1491 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Isa")))
1492 return kVTGClass_Isa;
1493 break;
1494 case 'P':
1495 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Platform")))
1496 return kVTGClass_Platform;
1497 break;
1498 case 'U':
1499 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Unknown")))
1500 return kVTGClass_Unknown;
1501 break;
1502 }
1503 parseError(pStrm, 1, "Unknown data dependency class specifier");
1504 return kVTGClass_Invalid;
1505}
1506
1507/**
1508 * Parses a pragma D attributes statement.
1509 *
1510 * @returns Suitable exit code, errors message already written on failure.
1511 * @param pStrm The stream.
1512 */
1513static RTEXITCODE parsePragmaDAttributes(PSCMSTREAM pStrm)
1514{
1515 /*
1516 * "CodeStability/DataStability/DataDepClass" - no spaces allowed.
1517 */
1518 unsigned ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
1519 if (ch == ~(unsigned)0)
1520 return RTEXITCODE_FAILURE;
1521
1522 kVTGStability enmCode = parseStability(pStrm, ch);
1523 if (enmCode == kVTGStability_Invalid)
1524 return RTEXITCODE_FAILURE;
1525 ch = ScmStreamGetCh(pStrm);
1526 if (ch != '/')
1527 return parseError(pStrm, 1, "Expected '/' following the code stability specifier");
1528
1529 kVTGStability enmData = parseStability(pStrm, ScmStreamGetCh(pStrm));
1530 if (enmData == kVTGStability_Invalid)
1531 return RTEXITCODE_FAILURE;
1532 ch = ScmStreamGetCh(pStrm);
1533 if (ch != '/')
1534 return parseError(pStrm, 1, "Expected '/' following the data stability specifier");
1535
1536 kVTGClass enmDataDep = parseDataDepClass(pStrm, ScmStreamGetCh(pStrm));
1537 if (enmDataDep == kVTGClass_Invalid)
1538 return RTEXITCODE_FAILURE;
1539
1540 /*
1541 * Expecting 'provider' followed by the name of an provider defined earlier.
1542 */
1543 ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
1544 if (ch == ~(unsigned)0)
1545 return RTEXITCODE_FAILURE;
1546 if (ch != 'p' || !ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("provider")))
1547 return parseError(pStrm, 1, "Expected 'provider'");
1548
1549 size_t cchName;
1550 const char *pszName = parseGetNextCWord(pStrm, &cchName);
1551 if (!pszName)
1552 return parseError(pStrm, 1, "Expected provider name");
1553
1554 PVTGPROVIDER pProv;
1555 RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
1556 {
1557 if ( !strncmp(pProv->pszName, pszName, cchName)
1558 && pProv->pszName[cchName] == '\0')
1559 break;
1560 }
1561 if (RTListNodeIsDummy(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry))
1562 return parseError(pStrm, cchName, "Provider not found");
1563
1564 /*
1565 * Which aspect of the provider?
1566 */
1567 size_t cchAspect;
1568 const char *pszAspect = parseGetNextCWord(pStrm, &cchAspect);
1569 if (!pszAspect)
1570 return parseError(pStrm, 1, "Expected provider aspect");
1571
1572 PVTGATTRS pAttrs;
1573 if (cchAspect == 8 && !memcmp(pszAspect, "provider", 8))
1574 pAttrs = &pProv->AttrSelf;
1575 else if (cchAspect == 8 && !memcmp(pszAspect, "function", 8))
1576 pAttrs = &pProv->AttrFunctions;
1577 else if (cchAspect == 6 && !memcmp(pszAspect, "module", 6))
1578 pAttrs = &pProv->AttrModules;
1579 else if (cchAspect == 4 && !memcmp(pszAspect, "name", 4))
1580 pAttrs = &pProv->AttrName;
1581 else if (cchAspect == 4 && !memcmp(pszAspect, "args", 4))
1582 pAttrs = &pProv->AttrArguments;
1583 else
1584 return parseError(pStrm, cchAspect, "Unknown aspect");
1585
1586 if (pAttrs->enmCode != kVTGStability_Invalid)
1587 return parseError(pStrm, cchAspect, "You have already specified these attributes");
1588
1589 pAttrs->enmCode = enmCode;
1590 pAttrs->enmData = enmData;
1591 pAttrs->enmDataDep = enmDataDep;
1592 return RTEXITCODE_SUCCESS;
1593}
1594
1595/**
1596 * Parses a D pragma statement.
1597 *
1598 * @returns Suitable exit code, errors message already written on failure.
1599 * @param pStrm The stream.
1600 */
1601static RTEXITCODE parsePragma(PSCMSTREAM pStrm)
1602{
1603 RTEXITCODE rcExit;
1604 unsigned ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
1605 if (ch == ~(unsigned)0)
1606 rcExit = RTEXITCODE_FAILURE;
1607 else if (ch == 'D' && ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("D")))
1608 {
1609 ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
1610 if (ch == ~(unsigned)0)
1611 rcExit = RTEXITCODE_FAILURE;
1612 else if (ch == 'a' && ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("attributes")))
1613 rcExit = parsePragmaDAttributes(pStrm);
1614 else
1615 rcExit = parseError(pStrm, 1, "Unknown pragma D");
1616 }
1617 else
1618 rcExit = parseError(pStrm, 1, "Unknown pragma");
1619 return rcExit;
1620}
1621
1622
1623/**
1624 * Classifies the given type expression.
1625 *
1626 * @return Type flags.
1627 * @param pszType The type expression.
1628 */
1629static uint32_t parseTypeExpression(const char *pszType)
1630{
1631 size_t cchType = strlen(pszType);
1632#define MY_STRMATCH(a_sz) (cchType == sizeof(a_sz) - 1 && !memcmp(a_sz, pszType, sizeof(a_sz) - 1))
1633
1634 /*
1635 * Try detect pointers.
1636 */
1637 if (pszType[cchType - 1] == '*') return VTG_TYPE_POINTER;
1638 if (pszType[cchType - 1] == '&')
1639 {
1640 RTMsgWarning("Please avoid using references like '%s' for probe arguments!", pszType);
1641 return VTG_TYPE_POINTER;
1642 }
1643
1644 /*
1645 * Standard integer types and IPRT variants.
1646 * It's important that we catch all types larger than 32-bit here or we'll
1647 * screw up the probe argument handling.
1648 */
1649 if (MY_STRMATCH("int")) return VTG_TYPE_FIXED_SIZED | sizeof(int) | VTG_TYPE_SIGNED;
1650 if (MY_STRMATCH("uintptr_t")) return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_UNSIGNED;
1651 if (MY_STRMATCH("intptr_t")) return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_SIGNED;
1652
1653 //if (MY_STRMATCH("uint128_t")) return VTG_TYPE_FIXED_SIZED | sizeof(uint128_t) | VTG_TYPE_UNSIGNED;
1654 if (MY_STRMATCH("uint64_t")) return VTG_TYPE_FIXED_SIZED | sizeof(uint64_t) | VTG_TYPE_UNSIGNED;
1655 if (MY_STRMATCH("uint32_t")) return VTG_TYPE_FIXED_SIZED | sizeof(uint32_t) | VTG_TYPE_UNSIGNED;
1656 if (MY_STRMATCH("uint16_t")) return VTG_TYPE_FIXED_SIZED | sizeof(uint16_t) | VTG_TYPE_UNSIGNED;
1657 if (MY_STRMATCH("uint8_t")) return VTG_TYPE_FIXED_SIZED | sizeof(uint8_t) | VTG_TYPE_UNSIGNED;
1658
1659 //if (MY_STRMATCH("int128_t")) return VTG_TYPE_FIXED_SIZED | sizeof(int128_t) | VTG_TYPE_SIGNED;
1660 if (MY_STRMATCH("int64_t")) return VTG_TYPE_FIXED_SIZED | sizeof(int64_t) | VTG_TYPE_SIGNED;
1661 if (MY_STRMATCH("int32_t")) return VTG_TYPE_FIXED_SIZED | sizeof(int32_t) | VTG_TYPE_SIGNED;
1662 if (MY_STRMATCH("int16_t")) return VTG_TYPE_FIXED_SIZED | sizeof(int16_t) | VTG_TYPE_SIGNED;
1663 if (MY_STRMATCH("int8_t")) return VTG_TYPE_FIXED_SIZED | sizeof(int8_t) | VTG_TYPE_SIGNED;
1664
1665 if (MY_STRMATCH("RTUINT64U")) return VTG_TYPE_FIXED_SIZED | sizeof(uint64_t) | VTG_TYPE_UNSIGNED;
1666 if (MY_STRMATCH("RTUINT32U")) return VTG_TYPE_FIXED_SIZED | sizeof(uint32_t) | VTG_TYPE_UNSIGNED;
1667 if (MY_STRMATCH("RTUINT16U")) return VTG_TYPE_FIXED_SIZED | sizeof(uint16_t) | VTG_TYPE_UNSIGNED;
1668
1669 if (MY_STRMATCH("RTMSINTERVAL")) return VTG_TYPE_FIXED_SIZED | sizeof(RTMSINTERVAL) | VTG_TYPE_UNSIGNED;
1670 if (MY_STRMATCH("RTTIMESPEC")) return VTG_TYPE_FIXED_SIZED | sizeof(RTTIMESPEC) | VTG_TYPE_SIGNED;
1671 if (MY_STRMATCH("RTPROCESS")) return VTG_TYPE_FIXED_SIZED | sizeof(RTPROCESS) | VTG_TYPE_UNSIGNED;
1672 if (MY_STRMATCH("RTHCPHYS")) return VTG_TYPE_FIXED_SIZED | sizeof(RTHCPHYS) | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS;
1673
1674 if (MY_STRMATCH("RTR3PTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3;
1675 if (MY_STRMATCH("RTR0PTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0;
1676 if (MY_STRMATCH("RTRCPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_RC;
1677 if (MY_STRMATCH("RTHCPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0;
1678
1679 if (MY_STRMATCH("RTR3UINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_UNSIGNED;
1680 if (MY_STRMATCH("RTR0UINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0 | VTG_TYPE_UNSIGNED;
1681 if (MY_STRMATCH("RTRCUINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_RC | VTG_TYPE_UNSIGNED;
1682 if (MY_STRMATCH("RTHCUINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_UNSIGNED;
1683
1684 if (MY_STRMATCH("RTR3INTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_SIGNED;
1685 if (MY_STRMATCH("RTR0INTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0 | VTG_TYPE_SIGNED;
1686 if (MY_STRMATCH("RTRCINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_RC | VTG_TYPE_SIGNED;
1687 if (MY_STRMATCH("RTHCINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_SIGNED;
1688
1689 if (MY_STRMATCH("RTUINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_CTX_RC | VTG_TYPE_UNSIGNED;
1690 if (MY_STRMATCH("RTINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_CTX_RC | VTG_TYPE_SIGNED;
1691
1692 if (MY_STRMATCH("RTHCUINTREG")) return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_UNSIGNED;
1693 if (MY_STRMATCH("RTR3UINTREG")) return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_CTX_R3 | VTG_TYPE_UNSIGNED;
1694 if (MY_STRMATCH("RTR0UINTREG")) return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_CTX_R3 | VTG_TYPE_UNSIGNED;
1695
1696 if (MY_STRMATCH("RTGCUINTREG")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCUINTREG) | VTG_TYPE_UNSIGNED | VTG_TYPE_CTX_GST;
1697 if (MY_STRMATCH("RTGCPTR")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPTR) | VTG_TYPE_UNSIGNED | VTG_TYPE_CTX_GST;
1698 if (MY_STRMATCH("RTGCINTPTR")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCUINTPTR) | VTG_TYPE_SIGNED | VTG_TYPE_CTX_GST;
1699 if (MY_STRMATCH("RTGCPTR32")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPTR32) | VTG_TYPE_SIGNED | VTG_TYPE_CTX_GST;
1700 if (MY_STRMATCH("RTGCPTR64")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPTR64) | VTG_TYPE_SIGNED | VTG_TYPE_CTX_GST;
1701 if (MY_STRMATCH("RTGCPHYS")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPHYS) | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS | VTG_TYPE_CTX_GST;
1702 if (MY_STRMATCH("RTGCPHYS32")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPHYS32) | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS | VTG_TYPE_CTX_GST;
1703 if (MY_STRMATCH("RTGCPHYS64")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPHYS64) | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS | VTG_TYPE_CTX_GST;
1704
1705 /*
1706 * The special VBox types.
1707 */
1708 if (MY_STRMATCH("PVM")) return VTG_TYPE_POINTER;
1709 if (MY_STRMATCH("PVMCPU")) return VTG_TYPE_POINTER;
1710 if (MY_STRMATCH("PCPUMCTX")) return VTG_TYPE_POINTER;
1711
1712 /*
1713 * Preaching time.
1714 */
1715 if ( MY_STRMATCH("unsigned long")
1716 || MY_STRMATCH("unsigned long long")
1717 || MY_STRMATCH("signed long")
1718 || MY_STRMATCH("signed long long")
1719 || MY_STRMATCH("long")
1720 || MY_STRMATCH("long long")
1721 || MY_STRMATCH("char")
1722 || MY_STRMATCH("signed char")
1723 || MY_STRMATCH("unsigned char")
1724 || MY_STRMATCH("double")
1725 || MY_STRMATCH("long double")
1726 || MY_STRMATCH("float")
1727 )
1728 {
1729 RTMsgError("Please do NOT use the type '%s' for probe arguments!", pszType);
1730 g_cTypeErrors++;
1731 return 0;
1732 }
1733
1734 if ( MY_STRMATCH("unsigned")
1735 || MY_STRMATCH("signed")
1736 || MY_STRMATCH("signed int")
1737 || MY_STRMATCH("unsigned int")
1738 || MY_STRMATCH("short")
1739 || MY_STRMATCH("signed short")
1740 || MY_STRMATCH("unsigned short")
1741 )
1742 RTMsgWarning("Please avoid using the type '%s' for probe arguments!", pszType);
1743 if (MY_STRMATCH("unsigned")) return VTG_TYPE_FIXED_SIZED | sizeof(int) | VTG_TYPE_UNSIGNED;
1744 if (MY_STRMATCH("unsigned int")) return VTG_TYPE_FIXED_SIZED | sizeof(int) | VTG_TYPE_UNSIGNED;
1745 if (MY_STRMATCH("signed")) return VTG_TYPE_FIXED_SIZED | sizeof(int) | VTG_TYPE_SIGNED;
1746 if (MY_STRMATCH("signed int")) return VTG_TYPE_FIXED_SIZED | sizeof(int) | VTG_TYPE_SIGNED;
1747 if (MY_STRMATCH("short")) return VTG_TYPE_FIXED_SIZED | sizeof(short) | VTG_TYPE_SIGNED;
1748 if (MY_STRMATCH("signed short")) return VTG_TYPE_FIXED_SIZED | sizeof(short) | VTG_TYPE_SIGNED;
1749 if (MY_STRMATCH("unsigned short")) return VTG_TYPE_FIXED_SIZED | sizeof(short) | VTG_TYPE_UNSIGNED;
1750
1751 /*
1752 * What we haven't caught by now is either unknown to us or wrong.
1753 */
1754 if (pszType[0] == 'P')
1755 {
1756 RTMsgError("Type '%s' looks like a pointer typedef, please do NOT use those "
1757 "but rather the non-pointer typedef or struct with '*'",
1758 pszType);
1759 g_cTypeErrors++;
1760 return VTG_TYPE_POINTER;
1761 }
1762
1763 RTMsgError("Don't know '%s' - please change or fix VBoxTpG", pszType);
1764 g_cTypeErrors++;
1765
1766#undef MY_STRCMP
1767 return 0;
1768}
1769
1770
1771/**
1772 * Initializes the members of an argument.
1773 *
1774 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE+msg.
1775 * @param pProbe The probe.
1776 * @param pArg The argument.
1777 * @param pStrm The input stream (for errors).
1778 * @param pchType The type.
1779 * @param cchType The type length.
1780 * @param pchName The name.
1781 * @param cchName The name length.
1782 */
1783static RTEXITCODE parseInitArgument(PVTGPROBE pProbe, PVTGARG pArg, PSCMSTREAM pStrm,
1784 char *pchType, size_t cchType, char *pchName, size_t cchName)
1785{
1786 Assert(!pArg->pszName); Assert(!pArg->pszTracerType); Assert(!pArg->pszCtxType); Assert(!pArg->fType);
1787
1788 pArg->pszArgPassingFmt = ", %s";
1789 pArg->pszName = RTStrDupN(pchName, cchName);
1790 pArg->pszTracerType = strtabInsertN(pchType, cchType);
1791 if (!pArg->pszTracerType || !pArg->pszName)
1792 return parseError(pStrm, 1, "Out of memory");
1793 pArg->fType = parseTypeExpression(pArg->pszTracerType);
1794
1795 if ( (pArg->fType & VTG_TYPE_POINTER)
1796 && !(g_fTypeContext & VTG_TYPE_CTX_R0) )
1797 {
1798 pArg->fType &= ~VTG_TYPE_POINTER;
1799 if ( !strcmp(pArg->pszTracerType, "struct VM *") || !strcmp(pArg->pszTracerType, "PVM")
1800 || !strcmp(pArg->pszTracerType, "struct VMCPU *") || !strcmp(pArg->pszTracerType, "PVMCPU")
1801 || !strcmp(pArg->pszTracerType, "struct CPUMCTX *") || !strcmp(pArg->pszTracerType, "PCPUMCTX")
1802 )
1803 {
1804 pArg->fType |= VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0
1805 | VTG_TYPE_FIXED_SIZED | (g_cHostBits / 8)
1806 | VTG_TYPE_AUTO_CONV_PTR;
1807 pArg->pszCtxType = RTStrDup("RTR0PTR");
1808
1809 if (!strcmp(pArg->pszTracerType, "struct VM *") || !strcmp(pArg->pszTracerType, "PVM"))
1810 pArg->pszArgPassingFmt = ", VTG_VM_TO_R0(%s)";
1811 else if (!strcmp(pArg->pszTracerType, "struct VMCPU *") || !strcmp(pArg->pszTracerType, "PVMCPU"))
1812 pArg->pszArgPassingFmt = ", VTG_VMCPU_TO_R0(%s)";
1813 else
1814 {
1815 PVTGARG pFirstArg = RTListGetFirst(&pProbe->ArgHead, VTGARG, ListEntry);
1816 if ( !pFirstArg
1817 || pFirstArg == pArg
1818 || strcmp(pFirstArg->pszName, "a_pVCpu")
1819 || ( strcmp(pFirstArg->pszTracerType, "struct VMCPU *")
1820 && strcmp(pFirstArg->pszTracerType, "PVMCPU *")) )
1821 return parseError(pStrm, 1, "The automatic ring-0 pointer conversion requires 'a_pVCpu' with type 'struct VMCPU *' as the first argument");
1822
1823 if (!strcmp(pArg->pszTracerType, "struct CPUMCTX *")|| !strcmp(pArg->pszTracerType, "PCPUMCTX"))
1824 pArg->pszArgPassingFmt = ", VTG_CPUMCTX_TO_R0(a_pVCpu, %s)";
1825 else
1826 pArg->pszArgPassingFmt = ", VBoxTpG-Is-Buggy!!";
1827 }
1828 }
1829 else
1830 {
1831 pArg->fType |= VTG_TYPE_CTX_POINTER | g_fTypeContext | VTG_TYPE_FIXED_SIZED | (g_cBits / 8);
1832 pArg->pszCtxType = RTStrDupN(pchType, cchType);
1833 }
1834 }
1835 else
1836 pArg->pszCtxType = RTStrDupN(pchType, cchType);
1837 if (!pArg->pszCtxType)
1838 return parseError(pStrm, 1, "Out of memory");
1839
1840 return RTEXITCODE_SUCCESS;
1841}
1842
1843
1844/**
1845 * Unmangles the probe name.
1846 *
1847 * This involves translating double underscore to dash.
1848 *
1849 * @returns Pointer to the unmangled name in the string table.
1850 * @param pszMangled The mangled name.
1851 */
1852static const char *parseUnmangleProbeName(const char *pszMangled)
1853{
1854 size_t cchMangled = strlen(pszMangled);
1855 char *pszTmp = (char *)alloca(cchMangled + 2);
1856 const char *pszSrc = pszMangled;
1857 char *pszDst = pszTmp;
1858
1859 while (*pszSrc)
1860 {
1861 if (pszSrc[0] == '_' && pszSrc[1] == '_' && pszSrc[2] != '_')
1862 {
1863 *pszDst++ = '-';
1864 pszSrc += 2;
1865 }
1866 else
1867 *pszDst++ = *pszSrc++;
1868 }
1869 *pszDst = '\0';
1870
1871 return strtabInsertN(pszTmp, pszDst - pszTmp);
1872}
1873
1874
1875/**
1876 * Parses a D probe statement.
1877 *
1878 * @returns Suitable exit code, errors message already written on failure.
1879 * @param pStrm The stream.
1880 * @param pProv The provider being parsed.
1881 */
1882static RTEXITCODE parseProbe(PSCMSTREAM pStrm, PVTGPROVIDER pProv)
1883{
1884 /*
1885 * Next up is a name followed by an opening parenthesis.
1886 */
1887 size_t cchProbe;
1888 const char *pszProbe = parseGetNextCWord(pStrm, &cchProbe);
1889 if (!pszProbe)
1890 return parseError(pStrm, 1, "Expected a probe name starting with an alphabetical character");
1891 unsigned ch = parseGetNextNonSpaceNonCommentCh(pStrm);
1892 if (ch != '(')
1893 return parseError(pStrm, 1, "Expected '(' after the probe name");
1894
1895 /*
1896 * Create a probe instance.
1897 */
1898 PVTGPROBE pProbe = (PVTGPROBE)RTMemAllocZ(sizeof(*pProbe));
1899 if (!pProbe)
1900 return parseError(pStrm, 0, "Out of memory");
1901 RTListInit(&pProbe->ArgHead);
1902 RTListAppend(&pProv->ProbeHead, &pProbe->ListEntry);
1903 pProbe->offArgList = UINT32_MAX;
1904 pProbe->pszMangledName = RTStrDupN(pszProbe, cchProbe);
1905 if (!pProbe->pszMangledName)
1906 return parseError(pStrm, 0, "Out of memory");
1907 pProbe->pszUnmangledName = parseUnmangleProbeName(pProbe->pszMangledName);
1908 if (!pProbe->pszUnmangledName)
1909 return parseError(pStrm, 0, "Out of memory");
1910
1911 /*
1912 * Parse loop for the argument.
1913 */
1914 PVTGARG pArg = NULL;
1915 size_t cchName = 0;
1916 size_t cchArg = 0;
1917 char szArg[4096];
1918 for (;;)
1919 {
1920 ch = parseGetNextNonSpaceNonCommentCh(pStrm);
1921 switch (ch)
1922 {
1923 case ')':
1924 case ',':
1925 {
1926 /* commit the argument */
1927 if (pArg)
1928 {
1929 if (!cchName)
1930 return parseError(pStrm, 1, "Argument has no name");
1931 if (cchArg - cchName - 1 >= 128)
1932 return parseError(pStrm, 1, "Argument type too long");
1933 RTEXITCODE rcExit = parseInitArgument(pProbe, pArg, pStrm,
1934 szArg, cchArg - cchName - 1,
1935 &szArg[cchArg - cchName], cchName);
1936 if (rcExit != RTEXITCODE_SUCCESS)
1937 return rcExit;
1938 if (VTG_TYPE_IS_LARGE(pArg->fType))
1939 pProbe->fHaveLargeArgs = true;
1940 pArg = NULL;
1941 cchName = cchArg = 0;
1942 }
1943 if (ch == ')')
1944 {
1945 size_t off = ScmStreamTell(pStrm);
1946 ch = parseGetNextNonSpaceNonCommentCh(pStrm);
1947 if (ch != ';')
1948 return parseErrorAbs(pStrm, off, "Expected ';'");
1949 return RTEXITCODE_SUCCESS;
1950 }
1951 break;
1952 }
1953
1954 default:
1955 {
1956 size_t cchWord;
1957 const char *pszWord = ScmStreamCGetWordM1(pStrm, &cchWord);
1958 if (!pszWord)
1959 return parseError(pStrm, 0, "Expected argument");
1960 if (!pArg)
1961 {
1962 pArg = (PVTGARG)RTMemAllocZ(sizeof(*pArg));
1963 if (!pArg)
1964 return parseError(pStrm, 1, "Out of memory");
1965 RTListAppend(&pProbe->ArgHead, &pArg->ListEntry);
1966 pProbe->cArgs++;
1967
1968 if (cchWord + 1 > sizeof(szArg))
1969 return parseError(pStrm, 1, "Too long parameter declaration");
1970 memcpy(szArg, pszWord, cchWord);
1971 szArg[cchWord] = '\0';
1972 cchArg = cchWord;
1973 cchName = 0;
1974 }
1975 else
1976 {
1977 if (cchArg + 1 + cchWord + 1 > sizeof(szArg))
1978 return parseError(pStrm, 1, "Too long parameter declaration");
1979
1980 szArg[cchArg++] = ' ';
1981 memcpy(&szArg[cchArg], pszWord, cchWord);
1982 cchArg += cchWord;
1983 szArg[cchArg] = '\0';
1984 cchName = cchWord;
1985 }
1986 break;
1987 }
1988
1989 case '*':
1990 {
1991 if (!pArg)
1992 return parseError(pStrm, 1, "A parameter type does not start with an asterix");
1993 if (cchArg + sizeof(" *") >= sizeof(szArg))
1994 return parseError(pStrm, 1, "Too long parameter declaration");
1995 szArg[cchArg++] = ' ';
1996 szArg[cchArg++] = '*';
1997 szArg[cchArg ] = '\0';
1998 cchName = 0;
1999 break;
2000 }
2001
2002 case ~(unsigned)0:
2003 return parseError(pStrm, 0, "Missing closing ')' on probe");
2004 }
2005 }
2006}
2007
2008/**
2009 * Parses a D provider statement.
2010 *
2011 * @returns Suitable exit code, errors message already written on failure.
2012 * @param pStrm The stream.
2013 */
2014static RTEXITCODE parseProvider(PSCMSTREAM pStrm)
2015{
2016 /*
2017 * Next up is a name followed by a curly bracket. Ignore comments.
2018 */
2019 RTEXITCODE rcExit = parseSkipSpacesAndComments(pStrm);
2020 if (rcExit != RTEXITCODE_SUCCESS)
2021 return parseError(pStrm, 1, "Expected a provider name starting with an alphabetical character");
2022 size_t cchName;
2023 const char *pszName = ScmStreamCGetWord(pStrm, &cchName);
2024 if (!pszName)
2025 return parseError(pStrm, 0, "Bad provider name");
2026 if (RT_C_IS_DIGIT(pszName[cchName - 1]))
2027 return parseError(pStrm, 1, "A provider name cannot end with digit");
2028
2029 unsigned ch = parseGetNextNonSpaceNonCommentCh(pStrm);
2030 if (ch != '{')
2031 return parseError(pStrm, 1, "Expected '{' after the provider name");
2032
2033 /*
2034 * Create a provider instance.
2035 */
2036 PVTGPROVIDER pProv = (PVTGPROVIDER)RTMemAllocZ(sizeof(*pProv));
2037 if (!pProv)
2038 return parseError(pStrm, 0, "Out of memory");
2039 RTListInit(&pProv->ProbeHead);
2040 RTListAppend(&g_ProviderHead, &pProv->ListEntry);
2041 pProv->pszName = strtabInsertN(pszName, cchName);
2042 if (!pProv->pszName)
2043 return parseError(pStrm, 0, "Out of memory");
2044
2045 /*
2046 * Parse loop.
2047 */
2048 for (;;)
2049 {
2050 ch = parseGetNextNonSpaceNonCommentCh(pStrm);
2051 switch (ch)
2052 {
2053 case 'p':
2054 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("probe")))
2055 rcExit = parseProbe(pStrm, pProv);
2056 else
2057 rcExit = parseError(pStrm, 1, "Unexpected character");
2058 break;
2059
2060 case '}':
2061 {
2062 size_t off = ScmStreamTell(pStrm);
2063 ch = parseGetNextNonSpaceNonCommentCh(pStrm);
2064 if (ch == ';')
2065 return RTEXITCODE_SUCCESS;
2066 rcExit = parseErrorAbs(pStrm, off, "Expected ';'");
2067 break;
2068 }
2069
2070 case ~(unsigned)0:
2071 rcExit = parseError(pStrm, 0, "Missing closing '}' on provider");
2072 break;
2073
2074 default:
2075 rcExit = parseError(pStrm, 1, "Unexpected character");
2076 break;
2077 }
2078 if (rcExit != RTEXITCODE_SUCCESS)
2079 return rcExit;
2080 }
2081}
2082
2083
2084static RTEXITCODE parseScript(const char *pszScript)
2085{
2086 SCMSTREAM Strm;
2087 int rc = ScmStreamInitForReading(&Strm, pszScript);
2088 if (RT_FAILURE(rc))
2089 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to open & read '%s' into memory: %Rrc", pszScript, rc);
2090 if (g_cVerbosity > 0)
2091 RTMsgInfo("Parsing '%s'...", pszScript);
2092
2093 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
2094 unsigned ch;
2095 while ((ch = ScmStreamGetCh(&Strm)) != ~(unsigned)0)
2096 {
2097 if (RT_C_IS_SPACE(ch))
2098 continue;
2099 switch (ch)
2100 {
2101 case '/':
2102 ch = ScmStreamGetCh(&Strm);
2103 if (ch == '*')
2104 rcExit = parseMultiLineComment(&Strm);
2105 else if (ch == '/')
2106 rcExit = parseOneLineComment(&Strm);
2107 else
2108 rcExit = parseError(&Strm, 2, "Unexpected character");
2109 break;
2110
2111 case 'p':
2112 if (ScmStreamCMatchingWordM1(&Strm, RT_STR_TUPLE("provider")))
2113 rcExit = parseProvider(&Strm);
2114 else
2115 rcExit = parseError(&Strm, 1, "Unexpected character");
2116 break;
2117
2118 case '#':
2119 {
2120 ch = parseGetNextNonSpaceNonCommentChOnPpLine(&Strm);
2121 if (ch == ~(unsigned)0)
2122 rcExit = RTEXITCODE_FAILURE;
2123 else if (ch == 'p' && ScmStreamCMatchingWordM1(&Strm, RT_STR_TUPLE("pragma")))
2124 rcExit = parsePragma(&Strm);
2125 else
2126 rcExit = parseError(&Strm, 1, "Unsupported preprocessor directive");
2127 break;
2128 }
2129
2130 default:
2131 rcExit = parseError(&Strm, 1, "Unexpected character");
2132 break;
2133 }
2134 if (rcExit != RTEXITCODE_SUCCESS)
2135 return rcExit;
2136 }
2137
2138 ScmStreamDelete(&Strm);
2139 if (g_cVerbosity > 0 && rcExit == RTEXITCODE_SUCCESS)
2140 RTMsgInfo("Successfully parsed '%s'.", pszScript);
2141 return rcExit;
2142}
2143
2144
2145/**
2146 * Parses the arguments.
2147 */
2148static RTEXITCODE parseArguments(int argc, char **argv)
2149{
2150 /*
2151 * Set / Adjust defaults.
2152 */
2153 int rc = RTPathAbs(g_pszAssemblerIncVal, g_szAssemblerIncVal, sizeof(g_szAssemblerIncVal) - 1);
2154 if (RT_FAILURE(rc))
2155 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPathAbs failed: %Rrc", rc);
2156 strcat(g_szAssemblerIncVal, "/");
2157 g_pszAssemblerIncVal = g_szAssemblerIncVal;
2158
2159 /*
2160 * Option config.
2161 */
2162 enum
2163 {
2164 kVBoxTpGOpt_32Bit = 1000,
2165 kVBoxTpGOpt_64Bit,
2166 kVBoxTpGOpt_GenerateWrapperHeader,
2167 kVBoxTpGOpt_Assembler,
2168 kVBoxTpGOpt_AssemblerFmtOpt,
2169 kVBoxTpGOpt_AssemblerFmtVal,
2170 kVBoxTpGOpt_AssemblerOutputOpt,
2171 kVBoxTpGOpt_AssemblerOption,
2172 kVBoxTpGOpt_Pic,
2173 kVBoxTpGOpt_ProbeFnName,
2174 kVBoxTpGOpt_ProbeFnImported,
2175 kVBoxTpGOpt_ProbeFnNotImported,
2176 kVBoxTpGOpt_Host32Bit,
2177 kVBoxTpGOpt_Host64Bit,
2178 kVBoxTpGOpt_RawModeContext,
2179 kVBoxTpGOpt_Ring0Context,
2180 kVBoxTpGOpt_Ring3Context,
2181 kVBoxTpGOpt_End
2182 };
2183
2184 static RTGETOPTDEF const s_aOpts[] =
2185 {
2186 /* dtrace w/ long options */
2187 { "-32", kVBoxTpGOpt_32Bit, RTGETOPT_REQ_NOTHING },
2188 { "-64", kVBoxTpGOpt_64Bit, RTGETOPT_REQ_NOTHING },
2189 { "--apply-cpp", 'C', RTGETOPT_REQ_NOTHING },
2190 { "--generate-obj", 'G', RTGETOPT_REQ_NOTHING },
2191 { "--generate-header", 'h', RTGETOPT_REQ_NOTHING },
2192 { "--output", 'o', RTGETOPT_REQ_STRING },
2193 { "--script", 's', RTGETOPT_REQ_STRING },
2194 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
2195 /* our stuff */
2196 { "--generate-wrapper-header", kVBoxTpGOpt_GenerateWrapperHeader, RTGETOPT_REQ_NOTHING },
2197 { "--assembler", kVBoxTpGOpt_Assembler, RTGETOPT_REQ_STRING },
2198 { "--assembler-fmt-opt", kVBoxTpGOpt_AssemblerFmtOpt, RTGETOPT_REQ_STRING },
2199 { "--assembler-fmt-val", kVBoxTpGOpt_AssemblerFmtVal, RTGETOPT_REQ_STRING },
2200 { "--assembler-output-opt", kVBoxTpGOpt_AssemblerOutputOpt, RTGETOPT_REQ_STRING },
2201 { "--assembler-option", kVBoxTpGOpt_AssemblerOption, RTGETOPT_REQ_STRING },
2202 { "--pic", kVBoxTpGOpt_Pic, RTGETOPT_REQ_NOTHING },
2203 { "--probe-fn-name", kVBoxTpGOpt_ProbeFnName, RTGETOPT_REQ_STRING },
2204 { "--probe-fn-imported", kVBoxTpGOpt_ProbeFnImported, RTGETOPT_REQ_NOTHING },
2205 { "--probe-fn-not-imported", kVBoxTpGOpt_ProbeFnNotImported, RTGETOPT_REQ_NOTHING },
2206 { "--host-32-bit", kVBoxTpGOpt_Host32Bit, RTGETOPT_REQ_NOTHING },
2207 { "--host-64-bit", kVBoxTpGOpt_Host64Bit, RTGETOPT_REQ_NOTHING },
2208 { "--raw-mode-context", kVBoxTpGOpt_RawModeContext, RTGETOPT_REQ_NOTHING },
2209 { "--ring-0-context", kVBoxTpGOpt_Ring0Context, RTGETOPT_REQ_NOTHING },
2210 { "--ring-3-context", kVBoxTpGOpt_Ring3Context, RTGETOPT_REQ_NOTHING },
2211 /** @todo We're missing a bunch of assembler options! */
2212 };
2213
2214 RTGETOPTUNION ValueUnion;
2215 RTGETOPTSTATE GetOptState;
2216 rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2217 AssertReleaseRCReturn(rc, RTEXITCODE_FAILURE);
2218
2219 /*
2220 * Process the options.
2221 */
2222 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
2223 {
2224 switch (rc)
2225 {
2226 /*
2227 * DTrace compatible options.
2228 */
2229 case kVBoxTpGOpt_32Bit:
2230 g_cHostBits = g_cBits = 32;
2231 g_pszAssemblerFmtVal = g_szAssemblerFmtVal32;
2232 break;
2233
2234 case kVBoxTpGOpt_64Bit:
2235 g_cHostBits = g_cBits = 64;
2236 g_pszAssemblerFmtVal = g_szAssemblerFmtVal64;
2237 break;
2238
2239 case 'C':
2240 g_fApplyCpp = true;
2241 RTMsgWarning("Ignoring the -C option - no preprocessing of the D script will be performed");
2242 break;
2243
2244 case 'G':
2245 if ( g_enmAction != kVBoxTpGAction_Nothing
2246 && g_enmAction != kVBoxTpGAction_GenerateObject)
2247 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "-G does not mix with -h or --generate-wrapper-header");
2248 g_enmAction = kVBoxTpGAction_GenerateObject;
2249 break;
2250
2251 case 'h':
2252 if (!strcmp(GetOptState.pDef->pszLong, "--generate-header"))
2253 {
2254 if ( g_enmAction != kVBoxTpGAction_Nothing
2255 && g_enmAction != kVBoxTpGAction_GenerateHeader)
2256 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "-h does not mix with -G or --generate-wrapper-header");
2257 g_enmAction = kVBoxTpGAction_GenerateHeader;
2258 }
2259 else
2260 {
2261 /* --help or similar */
2262 RTPrintf("VirtualBox Tracepoint Generator\n"
2263 "\n"
2264 "Usage: %s [options]\n"
2265 "\n"
2266 "Options:\n", RTProcShortName());
2267 for (size_t i = 0; i < RT_ELEMENTS(s_aOpts); i++)
2268 if ((unsigned)s_aOpts[i].iShort < 128)
2269 RTPrintf(" -%c,%s\n", s_aOpts[i].iShort, s_aOpts[i].pszLong);
2270 else
2271 RTPrintf(" %s\n", s_aOpts[i].pszLong);
2272 return RTEXITCODE_SUCCESS;
2273 }
2274 break;
2275
2276 case 'o':
2277 if (g_pszOutput)
2278 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Output file is already set to '%s'", g_pszOutput);
2279 g_pszOutput = ValueUnion.psz;
2280 break;
2281
2282 case 's':
2283 if (g_pszScript)
2284 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Script file is already set to '%s'", g_pszScript);
2285 g_pszScript = ValueUnion.psz;
2286 break;
2287
2288 case 'v':
2289 g_cVerbosity++;
2290 break;
2291
2292 case 'V':
2293 {
2294 /* The following is assuming that svn does it's job here. */
2295 static const char s_szRev[] = "$Revision: 41362 $";
2296 const char *psz = RTStrStripL(strchr(s_szRev, ' '));
2297 RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
2298 return RTEXITCODE_SUCCESS;
2299 }
2300
2301 case VINF_GETOPT_NOT_OPTION:
2302 if (g_enmAction == kVBoxTpGAction_GenerateObject)
2303 break; /* object files, ignore them. */
2304 return RTGetOptPrintError(rc, &ValueUnion);
2305
2306
2307 /*
2308 * Our options.
2309 */
2310 case kVBoxTpGOpt_GenerateWrapperHeader:
2311 if ( g_enmAction != kVBoxTpGAction_Nothing
2312 && g_enmAction != kVBoxTpGAction_GenerateWrapperHeader)
2313 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--generate-wrapper-header does not mix with -h or -G");
2314 g_enmAction = kVBoxTpGAction_GenerateWrapperHeader;
2315 break;
2316
2317 case kVBoxTpGOpt_Assembler:
2318 g_pszAssembler = ValueUnion.psz;
2319 break;
2320
2321 case kVBoxTpGOpt_AssemblerFmtOpt:
2322 g_pszAssemblerFmtOpt = ValueUnion.psz;
2323 break;
2324
2325 case kVBoxTpGOpt_AssemblerFmtVal:
2326 g_pszAssemblerFmtVal = ValueUnion.psz;
2327 break;
2328
2329 case kVBoxTpGOpt_AssemblerOutputOpt:
2330 g_pszAssemblerOutputOpt = ValueUnion.psz;
2331 break;
2332
2333 case kVBoxTpGOpt_AssemblerOption:
2334 if (g_cAssemblerOptions >= RT_ELEMENTS(g_apszAssemblerOptions))
2335 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Too many assembly options (max %u)", RT_ELEMENTS(g_apszAssemblerOptions));
2336 g_apszAssemblerOptions[g_cAssemblerOptions] = ValueUnion.psz;
2337 g_cAssemblerOptions++;
2338 break;
2339
2340 case kVBoxTpGOpt_Pic:
2341 g_fPic = true;
2342 break;
2343
2344 case kVBoxTpGOpt_ProbeFnName:
2345 g_pszProbeFnName = ValueUnion.psz;
2346 break;
2347
2348 case kVBoxTpGOpt_ProbeFnImported:
2349 g_fProbeFnImported = true;
2350 break;
2351
2352 case kVBoxTpGOpt_ProbeFnNotImported:
2353 g_fProbeFnImported = false;
2354 break;
2355
2356 case kVBoxTpGOpt_Host32Bit:
2357 g_cHostBits = 32;
2358 break;
2359
2360 case kVBoxTpGOpt_Host64Bit:
2361 g_cHostBits = 64;
2362 break;
2363
2364 case kVBoxTpGOpt_RawModeContext:
2365 g_fTypeContext = VTG_TYPE_CTX_RC;
2366 g_pszContextDefine = "IN_RC";
2367 break;
2368
2369 case kVBoxTpGOpt_Ring0Context:
2370 g_fTypeContext = VTG_TYPE_CTX_R0;
2371 g_pszContextDefine = "IN_RING0";
2372 break;
2373
2374 case kVBoxTpGOpt_Ring3Context:
2375 g_fTypeContext = VTG_TYPE_CTX_R3;
2376 g_pszContextDefine = "IN_RING3";
2377 break;
2378
2379
2380 /*
2381 * Errors and bugs.
2382 */
2383 default:
2384 return RTGetOptPrintError(rc, &ValueUnion);
2385 }
2386 }
2387
2388 /*
2389 * Check that we've got all we need.
2390 */
2391 if (g_enmAction == kVBoxTpGAction_Nothing)
2392 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No action specified (-h, -G or --generate-wrapper-header)");
2393 if (!g_pszScript)
2394 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No script file specified (-s)");
2395 if (!g_pszOutput)
2396 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No output file specified (-o)");
2397
2398 return RTEXITCODE_SUCCESS;
2399}
2400
2401
2402int main(int argc, char **argv)
2403{
2404 int rc = RTR3InitExe(argc, &argv, 0);
2405 if (RT_FAILURE(rc))
2406 return 1;
2407
2408 RTEXITCODE rcExit = parseArguments(argc, argv);
2409 if (rcExit == RTEXITCODE_SUCCESS)
2410 {
2411 /*
2412 * Parse the script.
2413 */
2414 RTListInit(&g_ProviderHead);
2415 rcExit = parseScript(g_pszScript);
2416 if (rcExit == RTEXITCODE_SUCCESS)
2417 {
2418 /*
2419 * Take action.
2420 */
2421 if (g_enmAction == kVBoxTpGAction_GenerateHeader)
2422 rcExit = generateFile(g_pszOutput, "header", generateHeader);
2423 else if (g_enmAction == kVBoxTpGAction_GenerateWrapperHeader)
2424 rcExit = generateFile(g_pszOutput, "wrapper header", generateWrapperHeader);
2425 else
2426 rcExit = generateObject(g_pszOutput, g_pszTempAsm);
2427 }
2428 }
2429
2430 if (rcExit == RTEXITCODE_SUCCESS && g_cTypeErrors > 0)
2431 rcExit = RTEXITCODE_FAILURE;
2432 return rcExit;
2433}
2434
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