VirtualBox

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

Last change on this file since 47144 was 46862, checked in by vboxsync, 11 years ago

IMP2 build fix (should fix mac osx, hopefully without breaking linux and solaris).

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