VirtualBox

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

Last change on this file since 78607 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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