VirtualBox

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

Last change on this file since 55715 was 54067, checked in by vboxsync, 10 years ago

try address the 'char *' conversion warnings on solaris.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 95.1 KB
Line 
1/* $Id: VBoxTpG.cpp 54067 2015-02-03 11:12:16Z vboxsync $ */
2/** @file
3 * VBox Build Tool - VBox Tracepoint Generator.
4 */
5
6/*
7 * Copyright (C) 2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <VBox/VBoxTpG.h>
23
24#include <iprt/alloca.h>
25#include <iprt/assert.h>
26#include <iprt/ctype.h>
27#include <iprt/env.h>
28#include <iprt/err.h>
29#include <iprt/file.h>
30#include <iprt/getopt.h>
31#include <iprt/initterm.h>
32#include <iprt/list.h>
33#include <iprt/mem.h>
34#include <iprt/message.h>
35#include <iprt/path.h>
36#include <iprt/process.h>
37#include <iprt/stream.h>
38#include <iprt/string.h>
39#include <iprt/uuid.h>
40
41#include "scmstream.h"
42
43
44/*******************************************************************************
45* Structures and Typedefs *
46*******************************************************************************/
47
48typedef struct VTGATTRS
49{
50 kVTGStability enmCode;
51 kVTGStability enmData;
52 kVTGClass enmDataDep;
53} VTGATTRS;
54typedef VTGATTRS *PVTGATTRS;
55
56
57typedef struct VTGARG
58{
59 RTLISTNODE ListEntry;
60 /** The argument name. (heap) */
61 char *pszName;
62 /** The type presented to the tracer (in string table). */
63 const char *pszTracerType;
64 /** The argument type used in the probe method in that context. (heap) */
65 char *pszCtxType;
66 /** Argument passing format string. First and only argument is the name.
67 * (const string) */
68 const char *pszArgPassingFmt;
69 /** The type flags. */
70 uint32_t fType;
71} VTGARG;
72typedef VTGARG *PVTGARG;
73
74typedef struct VTGPROBE
75{
76 RTLISTNODE ListEntry;
77 char *pszMangledName;
78 const char *pszUnmangledName;
79 RTLISTANCHOR ArgHead;
80 uint32_t cArgs;
81 bool fHaveLargeArgs;
82 uint32_t offArgList;
83 uint32_t iProbe;
84} VTGPROBE;
85typedef VTGPROBE *PVTGPROBE;
86
87typedef struct VTGPROVIDER
88{
89 RTLISTNODE ListEntry;
90 const char *pszName;
91
92 uint16_t iFirstProbe;
93 uint16_t cProbes;
94
95 VTGATTRS AttrSelf;
96 VTGATTRS AttrModules;
97 VTGATTRS AttrFunctions;
98 VTGATTRS AttrName;
99 VTGATTRS AttrArguments;
100
101 RTLISTANCHOR ProbeHead;
102} VTGPROVIDER;
103typedef VTGPROVIDER *PVTGPROVIDER;
104
105/**
106 * A string table string.
107 */
108typedef struct VTGSTRING
109{
110 /** The string space core. */
111 RTSTRSPACECORE Core;
112 /** The string table offset. */
113 uint32_t offStrTab;
114 /** The actual string. */
115 char szString[1];
116} VTGSTRING;
117typedef VTGSTRING *PVTGSTRING;
118
119
120/*******************************************************************************
121* Global Variables *
122*******************************************************************************/
123/** The string space organizing the string table strings. Each node is a VTGSTRING. */
124static RTSTRSPACE g_StrSpace = NULL;
125/** Used by the string table enumerator to set VTGSTRING::offStrTab. */
126static uint32_t g_offStrTab;
127/** List of providers created by the parser. */
128static RTLISTANCHOR g_ProviderHead;
129/** The number of type errors. */
130static uint32_t g_cTypeErrors = 0;
131
132
133/** @name Options
134 * @{ */
135static enum
136{
137 kVBoxTpGAction_Nothing,
138 kVBoxTpGAction_GenerateHeader,
139 kVBoxTpGAction_GenerateWrapperHeader,
140 kVBoxTpGAction_GenerateObject
141} g_enmAction = kVBoxTpGAction_Nothing;
142static uint32_t g_cBits = HC_ARCH_BITS;
143static uint32_t g_cHostBits = HC_ARCH_BITS;
144static uint32_t g_fTypeContext = VTG_TYPE_CTX_R0;
145static const char *g_pszContextDefine = "IN_RING0";
146static 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 54067 2015-02-03 11:12:16Z 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.5', 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 ,
741 iProvider, pProvider->pszName,
742 strtabGetOff(pProvider->pszName),
743 pProvider->iFirstProbe,
744 pProvider->cProbes,
745 pProvider->AttrSelf.enmCode, pProvider->AttrSelf.enmData, pProvider->AttrSelf.enmDataDep,
746 pProvider->AttrModules.enmCode, pProvider->AttrModules.enmData, pProvider->AttrModules.enmDataDep,
747 pProvider->AttrFunctions.enmCode, pProvider->AttrFunctions.enmData, pProvider->AttrFunctions.enmDataDep,
748 pProvider->AttrName.enmCode, pProvider->AttrName.enmData, pProvider->AttrName.enmDataDep,
749 pProvider->AttrArguments.enmCode, pProvider->AttrArguments.enmData, pProvider->AttrArguments.enmDataDep);
750 iProvider++;
751 }
752 ScmStreamPrintf(pStrm, "VTG_GLOBAL g_aVTGProviders_End, data\n");
753
754 /*
755 * Declare the probe enable flags.
756 *
757 * These must be placed at the end so they'll end up adjacent to the probe
758 * locations. This is important for reducing the amount of memory we need
759 * to lock down for user mode modules.
760 */
761 ScmStreamPrintf(pStrm,
762 ";\n"
763 "; Probe enabled flags.\n"
764 ";\n"
765 "ALIGNDATA(16)\n"
766 "VTG_GLOBAL g_acVTGProbeEnabled, data\n"
767 );
768 uint32_t cProbes = 0;
769 RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
770 {
771 RTListForEach(&pProvider->ProbeHead, pProbe, VTGPROBE, ListEntry)
772 {
773 ScmStreamPrintf(pStrm,
774 "VTG_GLOBAL g_cVTGProbeEnabled_%s_%s, data\n"
775 " dd 0\n",
776 pProvider->pszName, pProbe->pszMangledName);
777 cProbes++;
778 }
779 }
780 ScmStreamPrintf(pStrm, "VTG_GLOBAL g_acVTGProbeEnabled_End, data\n");
781 if (cProbes >= _32K)
782 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Too many probes: %u (max %u)", cProbes, _32K - 1);
783
784
785 /*
786 * Emit code for the stub functions.
787 */
788 bool const fWin64 = g_cBits == 64 && (!strcmp(g_pszAssemblerFmtVal, "win64") || !strcmp(g_pszAssemblerFmtVal, "pe64"));
789 bool const fElf = !strcmp(g_pszAssemblerFmtVal, "elf32") || !strcmp(g_pszAssemblerFmtVal, "elf64");
790 ScmStreamPrintf(pStrm,
791 "\n"
792 ";\n"
793 "; Prob stubs.\n"
794 ";\n"
795 "BEGINCODE\n"
796 );
797 if (g_fProbeFnImported)
798 ScmStreamPrintf(pStrm,
799 "EXTERN_IMP2 %s\n"
800 "BEGINCODE ; EXTERN_IMP2 changes section\n",
801 g_pszProbeFnName);
802 else
803 ScmStreamPrintf(pStrm, "extern NAME(%s)\n", g_pszProbeFnName);
804
805 RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
806 {
807 RTListForEach(&pProvider->ProbeHead, pProbe, VTGPROBE, ListEntry)
808 {
809 ScmStreamPrintf(pStrm,
810 "\n"
811 "VTG_GLOBAL VTGProbeStub_%s_%s, function; (VBOXTPGPROBELOC pVTGProbeLoc",
812 pProvider->pszName, pProbe->pszMangledName);
813 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
814 {
815 ScmStreamPrintf(pStrm, ", %s %s", pArg->pszTracerType, pArg->pszName);
816 }
817 ScmStreamPrintf(pStrm,
818 ");\n");
819
820 /*
821 * Check if the probe in question is enabled.
822 */
823 if (g_cBits == 32)
824 ScmStreamPrintf(pStrm,
825 " mov eax, [esp + 4]\n"
826 " test byte [eax+3], 0x80 ; fEnabled == true?\n"
827 " jz .return ; jump on false\n");
828 else if (fWin64)
829 ScmStreamPrintf(pStrm,
830 " test byte [rcx+3], 0x80 ; fEnabled == true?\n"
831 " jz .return ; jump on false\n");
832 else
833 ScmStreamPrintf(pStrm,
834 " test byte [rdi+3], 0x80 ; fEnabled == true?\n"
835 " jz .return ; jump on false\n");
836
837 /*
838 * Jump to the fire-probe function.
839 */
840 if (g_cBits == 32)
841 ScmStreamPrintf(pStrm, g_fPic && fElf ?
842 " jmp %s wrt ..plt\n"
843 : g_fProbeFnImported ?
844 " mov ecx, IMP2(%s)\n"
845 " jmp ecx\n"
846 :
847 " jmp NAME(%s)\n"
848 , g_pszProbeFnName);
849 else
850 ScmStreamPrintf(pStrm, g_fPic && fElf ?
851 " jmp [rel %s wrt ..got]\n"
852 : g_fProbeFnImported ?
853 " jmp IMP2(%s)\n"
854 :
855 " jmp NAME(%s)\n"
856 , g_pszProbeFnName);
857
858 ScmStreamPrintf(pStrm,
859 ".return:\n"
860 " ret ; The probe was disabled, return\n"
861 "\n");
862 }
863 }
864
865 return RTEXITCODE_SUCCESS;
866}
867
868
869static RTEXITCODE generateObject(const char *pszOutput, const char *pszTempAsm)
870{
871 if (!pszTempAsm)
872 {
873 size_t cch = strlen(pszOutput);
874 char *psz = (char *)alloca(cch + sizeof(".asm"));
875 memcpy(psz, pszOutput, cch);
876 memcpy(psz + cch, ".asm", sizeof(".asm"));
877 pszTempAsm = psz;
878 }
879
880 RTEXITCODE rcExit = generateFile(pszTempAsm, "assembly", generateAssembly);
881 if (rcExit == RTEXITCODE_SUCCESS)
882 rcExit = generateInvokeAssembler(pszOutput, pszTempAsm);
883 RTFileDelete(pszTempAsm);
884 return rcExit;
885}
886
887
888static RTEXITCODE generateProbeDefineName(char *pszBuf, size_t cbBuf, const char *pszProvider, const char *pszProbe)
889{
890 size_t cbMax = strlen(pszProvider) + 1 + strlen(pszProbe) + 1;
891 if (cbMax > cbBuf || cbMax > 80)
892 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Probe '%s' in provider '%s' ends up with a too long defined\n", pszProbe, pszProvider);
893
894 while (*pszProvider)
895 *pszBuf++ = RT_C_TO_UPPER(*pszProvider++);
896
897 *pszBuf++ = '_';
898
899 while (*pszProbe)
900 {
901 if (pszProbe[0] == '_' && pszProbe[1] == '_')
902 pszProbe++;
903 *pszBuf++ = RT_C_TO_UPPER(*pszProbe++);
904 }
905
906 *pszBuf = '\0';
907 return RTEXITCODE_SUCCESS;
908}
909
910
911/**
912 * Called via generateFile to generate the header file.
913 *
914 * @returns Exit code status.
915 * @param pStrm The output stream.
916 */
917static RTEXITCODE generateHeader(PSCMSTREAM pStrm)
918{
919 /*
920 * Calc the double inclusion blocker define and then write the file header.
921 */
922 char szTmp[4096];
923 const char *pszName = RTPathFilename(g_pszScript);
924 size_t cchName = strlen(pszName);
925 if (cchName >= sizeof(szTmp) - 64)
926 return RTMsgErrorExit(RTEXITCODE_FAILURE, "File name is too long '%s'", pszName);
927 szTmp[0] = '_';
928 szTmp[1] = '_';
929 szTmp[2] = '_';
930 memcpy(&szTmp[3], pszName, cchName);
931 szTmp[3 + cchName + 0] = '_';
932 szTmp[3 + cchName + 1] = '_';
933 szTmp[3 + cchName + 2] = '_';
934 szTmp[3 + cchName + 3] = '\0';
935 char *psz = &szTmp[3];
936 while (*psz)
937 {
938 if (!RT_C_IS_ALNUM(*psz) && *psz != '_')
939 *psz = '_';
940 psz++;
941 }
942
943 ScmStreamPrintf(pStrm,
944 "/* $Id: VBoxTpG.cpp 54067 2015-02-03 11:12:16Z vboxsync $ */\n"
945 "/** @file\n"
946 " * Automatically generated from %s. Do NOT edit!\n"
947 " */\n"
948 "\n"
949 "#ifndef %s\n"
950 "#define %s\n"
951 "\n"
952 "#include <VBox/VBoxTpG.h>\n"
953 "\n"
954 "#ifndef %s\n"
955 "# error \"Expected '%s' to be defined\"\n"
956 "#endif\n"
957 "\n"
958 "RT_C_DECLS_BEGIN\n"
959 "\n"
960 "#ifdef VBOX_WITH_DTRACE\n"
961 "\n"
962 "# ifdef _MSC_VER\n"
963 "# pragma data_seg(VTG_LOC_SECT)\n"
964 "# pragma data_seg()\n"
965 "# endif\n"
966 "\n"
967 ,
968 g_pszScript,
969 szTmp,
970 szTmp,
971 g_pszContextDefine,
972 g_pszContextDefine);
973
974 /*
975 * Declare data, code and macros for each probe.
976 */
977 PVTGPROVIDER pProv;
978 PVTGPROBE pProbe;
979 PVTGARG pArg;
980 RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
981 {
982 RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
983 {
984 PVTGARG const pFirstArg = RTListGetFirst(&pProbe->ArgHead, VTGARG, ListEntry);
985
986 ScmStreamPrintf(pStrm,
987 "extern uint32_t g_cVTGProbeEnabled_%s_%s;\n"
988 "extern VTGDESCPROBE g_VTGProbeData_%s_%s;\n"
989 "DECLASM(void) VTGProbeStub_%s_%s(PVTGPROBELOC",
990 pProv->pszName, pProbe->pszMangledName,
991 pProv->pszName, pProbe->pszMangledName,
992 pProv->pszName, pProbe->pszMangledName);
993 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
994 {
995 ScmStreamPrintf(pStrm, ", %s", pArg->pszCtxType);
996 }
997 generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
998 ScmStreamPrintf(pStrm,
999 ");\n"
1000 "# define %s_ENABLED() \\\n"
1001 " (RT_UNLIKELY(g_cVTGProbeEnabled_%s_%s)) \n"
1002 "# define %s("
1003 , szTmp,
1004 pProv->pszName, pProbe->pszMangledName,
1005 szTmp);
1006 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1007 {
1008 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1009 ScmStreamPrintf(pStrm, "%s", pArg->pszName);
1010 else
1011 ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
1012 }
1013 ScmStreamPrintf(pStrm,
1014 ") \\\n"
1015 " do { \\\n"
1016 " if (RT_UNLIKELY(g_cVTGProbeEnabled_%s_%s)) \\\n"
1017 " { \\\n"
1018 " VTG_DECL_VTGPROBELOC(s_VTGProbeLoc) = \\\n"
1019 " { __LINE__, 0, 0, __FUNCTION__, &g_VTGProbeData_%s_%s }; \\\n"
1020 " VTGProbeStub_%s_%s(&s_VTGProbeLoc",
1021 pProv->pszName, pProbe->pszMangledName,
1022 pProv->pszName, pProbe->pszMangledName,
1023 pProv->pszName, pProbe->pszMangledName);
1024 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1025 {
1026 ScmStreamPrintf(pStrm, pArg->pszArgPassingFmt, pArg->pszName);
1027 }
1028 ScmStreamPrintf(pStrm,
1029 "); \\\n"
1030 " } \\\n"
1031 " { \\\n" );
1032 uint32_t iArg = 0;
1033 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1034 {
1035 if ((pArg->fType & (VTG_TYPE_FIXED_SIZED | VTG_TYPE_AUTO_CONV_PTR)) == VTG_TYPE_FIXED_SIZED)
1036 ScmStreamPrintf(pStrm,
1037 " AssertCompile(sizeof(%s) == %u); \\\n"
1038 " AssertCompile(sizeof(%s) <= %u); \\\n",
1039 pArg->pszTracerType, pArg->fType & VTG_TYPE_SIZE_MASK,
1040 pArg->pszName, pArg->fType & VTG_TYPE_SIZE_MASK);
1041 else if (pArg->fType & (VTG_TYPE_POINTER | VTG_TYPE_HC_ARCH_SIZED))
1042 ScmStreamPrintf(pStrm,
1043 " AssertCompile(sizeof(%s) <= sizeof(uintptr_t)); \\\n"
1044 " AssertCompile(sizeof(%s) <= sizeof(uintptr_t)); \\\n",
1045 pArg->pszName,
1046 pArg->pszTracerType);
1047 iArg++;
1048 }
1049 ScmStreamPrintf(pStrm,
1050 " } \\\n"
1051 " } while (0)\n"
1052 "\n");
1053 }
1054 }
1055
1056 ScmStreamPrintf(pStrm,
1057 "\n"
1058 "#else\n"
1059 "\n");
1060 RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
1061 {
1062 RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
1063 {
1064 generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
1065 ScmStreamPrintf(pStrm,
1066 "# define %s_ENABLED() (false)\n"
1067 "# define %s("
1068 , szTmp, szTmp);
1069 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1070 {
1071 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1072 ScmStreamPrintf(pStrm, "%s", pArg->pszName);
1073 else
1074 ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
1075 }
1076 ScmStreamPrintf(pStrm,
1077 ") do { } while (0)\n");
1078 }
1079 }
1080
1081 ScmStreamWrite(pStrm, RT_STR_TUPLE("\n"
1082 "#endif\n"
1083 "\n"
1084 "RT_C_DECLS_END\n"
1085 "#endif\n"));
1086 return RTEXITCODE_SUCCESS;
1087}
1088
1089
1090/**
1091 * Called via generateFile to generate the wrapper header file.
1092 *
1093 * @returns Exit code status.
1094 * @param pStrm The output stream.
1095 */
1096static RTEXITCODE generateWrapperHeader(PSCMSTREAM pStrm)
1097{
1098 /*
1099 * Calc the double inclusion blocker define and then write the file header.
1100 */
1101 char szTmp[4096];
1102 const char *pszName = RTPathFilename(g_pszScript);
1103 size_t cchName = strlen(pszName);
1104 if (cchName >= sizeof(szTmp) - 64)
1105 return RTMsgErrorExit(RTEXITCODE_FAILURE, "File name is too long '%s'", pszName);
1106 szTmp[0] = '_';
1107 szTmp[1] = '_';
1108 szTmp[2] = '_';
1109 memcpy(&szTmp[3], pszName, cchName);
1110 strcpy(&szTmp[3 + cchName ], "___WRAPPER___");
1111 char *psz = &szTmp[3];
1112 while (*psz)
1113 {
1114 if (!RT_C_IS_ALNUM(*psz) && *psz != '_')
1115 *psz = '_';
1116 psz++;
1117 }
1118
1119 ScmStreamPrintf(pStrm,
1120 "/* $Id: VBoxTpG.cpp 54067 2015-02-03 11:12:16Z vboxsync $ */\n"
1121 "/** @file\n"
1122 " * Automatically generated from %s. Do NOT edit!\n"
1123 " */\n"
1124 "\n"
1125 "#ifndef %s\n"
1126 "#define %s\n"
1127 "\n"
1128 "#include <VBox/VBoxTpG.h>\n"
1129 "\n"
1130 "#ifndef %s\n"
1131 "# error \"Expected '%s' to be defined\"\n"
1132 "#endif\n"
1133 "\n"
1134 "#ifdef VBOX_WITH_DTRACE\n"
1135 "\n"
1136 ,
1137 g_pszScript,
1138 szTmp,
1139 szTmp,
1140 g_pszContextDefine,
1141 g_pszContextDefine);
1142
1143 /*
1144 * Declare macros for each probe.
1145 */
1146 PVTGPROVIDER pProv;
1147 PVTGPROBE pProbe;
1148 PVTGARG pArg;
1149 RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
1150 {
1151 RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
1152 {
1153 PVTGARG const pFirstArg = RTListGetFirst(&pProbe->ArgHead, VTGARG, ListEntry);
1154
1155 generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
1156 ScmStreamPrintf(pStrm,
1157 "# define %s("
1158 , szTmp);
1159 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1160 {
1161 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1162 ScmStreamPrintf(pStrm, "%s", pArg->pszName);
1163 else
1164 ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
1165 }
1166 ScmStreamPrintf(pStrm,
1167 ") \\\n"
1168 " do { \\\n"
1169 " if (RT_UNLIKELY(%s_ENABLED())) \\\n"
1170 " { \\\n"
1171 " %s_ORIGINAL("
1172 , szTmp, szTmp);
1173 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1174 {
1175 const char *pszFmt = pArg->pszArgPassingFmt;
1176 if (pArg->fType & VTG_TYPE_AUTO_CONV_PTR)
1177 {
1178 /* Casting is required. ASSUMES sizeof(RTR0PTR) == sizeof(RTR3PTR) - safe! */
1179 pszFmt += sizeof(", ") - 1;
1180 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1181 ScmStreamPrintf(pStrm, "(%s)%M", pArg->pszTracerType, pszFmt, pArg->pszName);
1182 else
1183 ScmStreamPrintf(pStrm, ", (%s)%M", pArg->pszTracerType, pszFmt, pArg->pszName);
1184 }
1185 else if (pArg->fType & VTG_TYPE_CONST_CHAR_PTR)
1186 {
1187 /* Casting from 'const char *' (probe) to 'char *' (dtrace) is required to shut up warnings. */
1188 pszFmt += sizeof(", ") - 1;
1189 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1190 ScmStreamPrintf(pStrm, "(char *)%M", pszFmt, pArg->pszName);
1191 else
1192 ScmStreamPrintf(pStrm, ", (char *)%M", pszFmt, pArg->pszName);
1193 }
1194 else
1195 {
1196 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1197 ScmStreamPrintf(pStrm, pArg->pszArgPassingFmt + sizeof(", ") - 1, pArg->pszName);
1198 else
1199 ScmStreamPrintf(pStrm, pArg->pszArgPassingFmt, pArg->pszName);
1200 }
1201 }
1202 ScmStreamPrintf(pStrm,
1203 "); \\\n"
1204 " } \\\n"
1205 " } while (0)\n"
1206 "\n");
1207 }
1208 }
1209
1210 ScmStreamPrintf(pStrm,
1211 "\n"
1212 "#else\n"
1213 "\n");
1214 RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
1215 {
1216 RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
1217 {
1218 generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
1219 ScmStreamPrintf(pStrm,
1220 "# define %s("
1221 , szTmp);
1222 RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
1223 {
1224 if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
1225 ScmStreamPrintf(pStrm, "%s", pArg->pszName);
1226 else
1227 ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
1228 }
1229 ScmStreamPrintf(pStrm,
1230 ") do { } while (0)\n");
1231 }
1232 }
1233
1234 ScmStreamWrite(pStrm, RT_STR_TUPLE("\n"
1235 "#endif\n"
1236 "\n"
1237 "#endif\n"));
1238 return RTEXITCODE_SUCCESS;
1239}
1240
1241
1242/**
1243 * Parser error with line and position.
1244 *
1245 * @returns RTEXITCODE_FAILURE.
1246 * @param pStrm The stream.
1247 * @param cb The offset from the current position to the
1248 * point of failure.
1249 * @param pszMsg The message to display.
1250 */
1251static RTEXITCODE parseError(PSCMSTREAM pStrm, size_t cb, const char *pszMsg)
1252{
1253 if (cb)
1254 ScmStreamSeekRelative(pStrm, -(ssize_t)cb);
1255 size_t const off = ScmStreamTell(pStrm);
1256 size_t const iLine = ScmStreamTellLine(pStrm);
1257 ScmStreamSeekByLine(pStrm, iLine);
1258 size_t const offLine = ScmStreamTell(pStrm);
1259
1260 RTPrintf("%s:%d:%zd: error: %s.\n", g_pszScript, iLine + 1, off - offLine + 1, pszMsg);
1261
1262 size_t cchLine;
1263 SCMEOL enmEof;
1264 const char *pszLine = ScmStreamGetLineByNo(pStrm, iLine, &cchLine, &enmEof);
1265 if (pszLine)
1266 RTPrintf(" %.*s\n"
1267 " %*s^\n",
1268 cchLine, pszLine, off - offLine, "");
1269 return RTEXITCODE_FAILURE;
1270}
1271
1272
1273/**
1274 * Parser error with line and position.
1275 *
1276 * @returns RTEXITCODE_FAILURE.
1277 * @param pStrm The stream.
1278 * @param cb The offset from the current position to the
1279 * point of failure.
1280 * @param pszMsg The message to display.
1281 */
1282static RTEXITCODE parseErrorAbs(PSCMSTREAM pStrm, size_t off, const char *pszMsg)
1283{
1284 ScmStreamSeekAbsolute(pStrm, off);
1285 return parseError(pStrm, 0, pszMsg);
1286}
1287
1288/**
1289 * Handles a C++ one line comment.
1290 *
1291 * @returns Exit code.
1292 * @param pStrm The stream.
1293 */
1294static RTEXITCODE parseOneLineComment(PSCMSTREAM pStrm)
1295{
1296 ScmStreamSeekByLine(pStrm, ScmStreamTellLine(pStrm) + 1);
1297 return RTEXITCODE_SUCCESS;
1298}
1299
1300/**
1301 * Handles a multi-line C/C++ comment.
1302 *
1303 * @returns Exit code.
1304 * @param pStrm The stream.
1305 */
1306static RTEXITCODE parseMultiLineComment(PSCMSTREAM pStrm)
1307{
1308 unsigned ch;
1309 while ((ch = ScmStreamGetCh(pStrm)) != ~(unsigned)0)
1310 {
1311 if (ch == '*')
1312 {
1313 do
1314 ch = ScmStreamGetCh(pStrm);
1315 while (ch == '*');
1316 if (ch == '/')
1317 return RTEXITCODE_SUCCESS;
1318 }
1319 }
1320
1321 parseError(pStrm, 1, "Expected end of comment, got end of file");
1322 return RTEXITCODE_FAILURE;
1323}
1324
1325
1326/**
1327 * Skips spaces and comments.
1328 *
1329 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
1330 * @param pStrm The stream..
1331 */
1332static RTEXITCODE parseSkipSpacesAndComments(PSCMSTREAM pStrm)
1333{
1334 unsigned ch;
1335 while ((ch = ScmStreamPeekCh(pStrm)) != ~(unsigned)0)
1336 {
1337 if (!RT_C_IS_SPACE(ch) && ch != '/')
1338 return RTEXITCODE_SUCCESS;
1339 unsigned ch2 = ScmStreamGetCh(pStrm); AssertBreak(ch == ch2); NOREF(ch2);
1340 if (ch == '/')
1341 {
1342 ch = ScmStreamGetCh(pStrm);
1343 RTEXITCODE rcExit;
1344 if (ch == '*')
1345 rcExit = parseMultiLineComment(pStrm);
1346 else if (ch == '/')
1347 rcExit = parseOneLineComment(pStrm);
1348 else
1349 rcExit = parseError(pStrm, 2, "Unexpected character");
1350 if (rcExit != RTEXITCODE_SUCCESS)
1351 return rcExit;
1352 }
1353 }
1354
1355 return parseError(pStrm, 0, "Unexpected end of file");
1356}
1357
1358
1359/**
1360 * Skips spaces and comments, returning the next character.
1361 *
1362 * @returns Next non-space-non-comment character. ~(unsigned)0 on EOF or
1363 * failure.
1364 * @param pStrm The stream.
1365 */
1366static unsigned parseGetNextNonSpaceNonCommentCh(PSCMSTREAM pStrm)
1367{
1368 unsigned ch;
1369 while ((ch = ScmStreamGetCh(pStrm)) != ~(unsigned)0)
1370 {
1371 if (!RT_C_IS_SPACE(ch) && ch != '/')
1372 return ch;
1373 if (ch == '/')
1374 {
1375 ch = ScmStreamGetCh(pStrm);
1376 RTEXITCODE rcExit;
1377 if (ch == '*')
1378 rcExit = parseMultiLineComment(pStrm);
1379 else if (ch == '/')
1380 rcExit = parseOneLineComment(pStrm);
1381 else
1382 rcExit = parseError(pStrm, 2, "Unexpected character");
1383 if (rcExit != RTEXITCODE_SUCCESS)
1384 return ~(unsigned)0;
1385 }
1386 }
1387
1388 parseError(pStrm, 0, "Unexpected end of file");
1389 return ~(unsigned)0;
1390}
1391
1392
1393/**
1394 * Get the next non-space-non-comment character on a preprocessor line.
1395 *
1396 * @returns The next character. On error message and ~(unsigned)0.
1397 * @param pStrm The stream.
1398 */
1399static unsigned parseGetNextNonSpaceNonCommentChOnPpLine(PSCMSTREAM pStrm)
1400{
1401 size_t off = ScmStreamTell(pStrm) - 1;
1402 unsigned ch;
1403 while ((ch = ScmStreamGetCh(pStrm)) != ~(unsigned)0)
1404 {
1405 if (RT_C_IS_SPACE(ch))
1406 {
1407 if (ch == '\n' || ch == '\r')
1408 {
1409 parseErrorAbs(pStrm, off, "Invalid preprocessor statement");
1410 break;
1411 }
1412 }
1413 else if (ch == '\\')
1414 {
1415 size_t off2 = ScmStreamTell(pStrm) - 1;
1416 ch = ScmStreamGetCh(pStrm);
1417 if (ch == '\r')
1418 ch = ScmStreamGetCh(pStrm);
1419 if (ch != '\n')
1420 {
1421 parseErrorAbs(pStrm, off2, "Expected new line");
1422 break;
1423 }
1424 }
1425 else
1426 return ch;
1427 }
1428 return ~(unsigned)0;
1429}
1430
1431
1432
1433/**
1434 * Skips spaces and comments.
1435 *
1436 * @returns Same as ScmStreamCGetWord
1437 * @param pStrm The stream..
1438 * @param pcchWord Where to return the length.
1439 */
1440static const char *parseGetNextCWord(PSCMSTREAM pStrm, size_t *pcchWord)
1441{
1442 if (parseSkipSpacesAndComments(pStrm) != RTEXITCODE_SUCCESS)
1443 return NULL;
1444 return ScmStreamCGetWord(pStrm, pcchWord);
1445}
1446
1447
1448
1449/**
1450 * Parses interface stability.
1451 *
1452 * @returns Interface stability if parsed correctly, otherwise error message and
1453 * kVTGStability_Invalid.
1454 * @param pStrm The stream.
1455 * @param ch The first character in the stability spec.
1456 */
1457static kVTGStability parseStability(PSCMSTREAM pStrm, unsigned ch)
1458{
1459 switch (ch)
1460 {
1461 case 'E':
1462 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("External")))
1463 return kVTGStability_External;
1464 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Evolving")))
1465 return kVTGStability_Evolving;
1466 break;
1467 case 'I':
1468 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Internal")))
1469 return kVTGStability_Internal;
1470 break;
1471 case 'O':
1472 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Obsolete")))
1473 return kVTGStability_Obsolete;
1474 break;
1475 case 'P':
1476 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Private")))
1477 return kVTGStability_Private;
1478 break;
1479 case 'S':
1480 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Stable")))
1481 return kVTGStability_Stable;
1482 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Standard")))
1483 return kVTGStability_Standard;
1484 break;
1485 case 'U':
1486 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Unstable")))
1487 return kVTGStability_Unstable;
1488 break;
1489 }
1490 parseError(pStrm, 1, "Unknown stability specifier");
1491 return kVTGStability_Invalid;
1492}
1493
1494
1495/**
1496 * Parses data depndency class.
1497 *
1498 * @returns Data dependency class if parsed correctly, otherwise error message
1499 * and kVTGClass_Invalid.
1500 * @param pStrm The stream.
1501 * @param ch The first character in the stability spec.
1502 */
1503static kVTGClass parseDataDepClass(PSCMSTREAM pStrm, unsigned ch)
1504{
1505 switch (ch)
1506 {
1507 case 'C':
1508 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Common")))
1509 return kVTGClass_Common;
1510 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Cpu")))
1511 return kVTGClass_Cpu;
1512 break;
1513 case 'G':
1514 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Group")))
1515 return kVTGClass_Group;
1516 break;
1517 case 'I':
1518 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Isa")))
1519 return kVTGClass_Isa;
1520 break;
1521 case 'P':
1522 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Platform")))
1523 return kVTGClass_Platform;
1524 break;
1525 case 'U':
1526 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Unknown")))
1527 return kVTGClass_Unknown;
1528 break;
1529 }
1530 parseError(pStrm, 1, "Unknown data dependency class specifier");
1531 return kVTGClass_Invalid;
1532}
1533
1534/**
1535 * Parses a pragma D attributes statement.
1536 *
1537 * @returns Suitable exit code, errors message already written on failure.
1538 * @param pStrm The stream.
1539 */
1540static RTEXITCODE parsePragmaDAttributes(PSCMSTREAM pStrm)
1541{
1542 /*
1543 * "CodeStability/DataStability/DataDepClass" - no spaces allowed.
1544 */
1545 unsigned ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
1546 if (ch == ~(unsigned)0)
1547 return RTEXITCODE_FAILURE;
1548
1549 kVTGStability enmCode = parseStability(pStrm, ch);
1550 if (enmCode == kVTGStability_Invalid)
1551 return RTEXITCODE_FAILURE;
1552 ch = ScmStreamGetCh(pStrm);
1553 if (ch != '/')
1554 return parseError(pStrm, 1, "Expected '/' following the code stability specifier");
1555
1556 kVTGStability enmData = parseStability(pStrm, ScmStreamGetCh(pStrm));
1557 if (enmData == kVTGStability_Invalid)
1558 return RTEXITCODE_FAILURE;
1559 ch = ScmStreamGetCh(pStrm);
1560 if (ch != '/')
1561 return parseError(pStrm, 1, "Expected '/' following the data stability specifier");
1562
1563 kVTGClass enmDataDep = parseDataDepClass(pStrm, ScmStreamGetCh(pStrm));
1564 if (enmDataDep == kVTGClass_Invalid)
1565 return RTEXITCODE_FAILURE;
1566
1567 /*
1568 * Expecting 'provider' followed by the name of an provider defined earlier.
1569 */
1570 ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
1571 if (ch == ~(unsigned)0)
1572 return RTEXITCODE_FAILURE;
1573 if (ch != 'p' || !ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("provider")))
1574 return parseError(pStrm, 1, "Expected 'provider'");
1575
1576 size_t cchName;
1577 const char *pszName = parseGetNextCWord(pStrm, &cchName);
1578 if (!pszName)
1579 return parseError(pStrm, 1, "Expected provider name");
1580
1581 PVTGPROVIDER pProv;
1582 RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
1583 {
1584 if ( !strncmp(pProv->pszName, pszName, cchName)
1585 && pProv->pszName[cchName] == '\0')
1586 break;
1587 }
1588 if (RTListNodeIsDummy(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry))
1589 return parseError(pStrm, cchName, "Provider not found");
1590
1591 /*
1592 * Which aspect of the provider?
1593 */
1594 size_t cchAspect;
1595 const char *pszAspect = parseGetNextCWord(pStrm, &cchAspect);
1596 if (!pszAspect)
1597 return parseError(pStrm, 1, "Expected provider aspect");
1598
1599 PVTGATTRS pAttrs;
1600 if (cchAspect == 8 && !memcmp(pszAspect, "provider", 8))
1601 pAttrs = &pProv->AttrSelf;
1602 else if (cchAspect == 8 && !memcmp(pszAspect, "function", 8))
1603 pAttrs = &pProv->AttrFunctions;
1604 else if (cchAspect == 6 && !memcmp(pszAspect, "module", 6))
1605 pAttrs = &pProv->AttrModules;
1606 else if (cchAspect == 4 && !memcmp(pszAspect, "name", 4))
1607 pAttrs = &pProv->AttrName;
1608 else if (cchAspect == 4 && !memcmp(pszAspect, "args", 4))
1609 pAttrs = &pProv->AttrArguments;
1610 else
1611 return parseError(pStrm, cchAspect, "Unknown aspect");
1612
1613 if (pAttrs->enmCode != kVTGStability_Invalid)
1614 return parseError(pStrm, cchAspect, "You have already specified these attributes");
1615
1616 pAttrs->enmCode = enmCode;
1617 pAttrs->enmData = enmData;
1618 pAttrs->enmDataDep = enmDataDep;
1619 return RTEXITCODE_SUCCESS;
1620}
1621
1622/**
1623 * Parses a D pragma statement.
1624 *
1625 * @returns Suitable exit code, errors message already written on failure.
1626 * @param pStrm The stream.
1627 */
1628static RTEXITCODE parsePragma(PSCMSTREAM pStrm)
1629{
1630 RTEXITCODE rcExit;
1631 unsigned ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
1632 if (ch == ~(unsigned)0)
1633 rcExit = RTEXITCODE_FAILURE;
1634 else if (ch == 'D' && ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("D")))
1635 {
1636 ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
1637 if (ch == ~(unsigned)0)
1638 rcExit = RTEXITCODE_FAILURE;
1639 else if (ch == 'a' && ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("attributes")))
1640 rcExit = parsePragmaDAttributes(pStrm);
1641 else
1642 rcExit = parseError(pStrm, 1, "Unknown pragma D");
1643 }
1644 else
1645 rcExit = parseError(pStrm, 1, "Unknown pragma");
1646 return rcExit;
1647}
1648
1649
1650/**
1651 * Classifies the given type expression.
1652 *
1653 * @return Type flags.
1654 * @param pszType The type expression.
1655 */
1656static uint32_t parseTypeExpression(const char *pszType)
1657{
1658 size_t cchType = strlen(pszType);
1659#define MY_STRMATCH(a_sz) (cchType == sizeof(a_sz) - 1 && !memcmp(a_sz, pszType, sizeof(a_sz) - 1))
1660
1661 /*
1662 * Try detect pointers.
1663 */
1664 if (pszType[cchType - 1] == '*')
1665 {
1666 if (MY_STRMATCH("const char *")) return VTG_TYPE_POINTER | VTG_TYPE_CONST_CHAR_PTR;
1667 return VTG_TYPE_POINTER;
1668 }
1669 if (pszType[cchType - 1] == '&')
1670 {
1671 RTMsgWarning("Please avoid using references like '%s' for probe arguments!", pszType);
1672 return VTG_TYPE_POINTER;
1673 }
1674
1675 /*
1676 * Standard integer types and IPRT variants.
1677 * It's important that we catch all types larger than 32-bit here or we'll
1678 * screw up the probe argument handling.
1679 */
1680 if (MY_STRMATCH("int")) return VTG_TYPE_FIXED_SIZED | sizeof(int) | VTG_TYPE_SIGNED;
1681 if (MY_STRMATCH("uintptr_t")) return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_UNSIGNED;
1682 if (MY_STRMATCH("intptr_t")) return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_SIGNED;
1683
1684 //if (MY_STRMATCH("uint128_t")) return VTG_TYPE_FIXED_SIZED | sizeof(uint128_t) | VTG_TYPE_UNSIGNED;
1685 if (MY_STRMATCH("uint64_t")) return VTG_TYPE_FIXED_SIZED | sizeof(uint64_t) | VTG_TYPE_UNSIGNED;
1686 if (MY_STRMATCH("uint32_t")) return VTG_TYPE_FIXED_SIZED | sizeof(uint32_t) | VTG_TYPE_UNSIGNED;
1687 if (MY_STRMATCH("uint16_t")) return VTG_TYPE_FIXED_SIZED | sizeof(uint16_t) | VTG_TYPE_UNSIGNED;
1688 if (MY_STRMATCH("uint8_t")) return VTG_TYPE_FIXED_SIZED | sizeof(uint8_t) | VTG_TYPE_UNSIGNED;
1689
1690 //if (MY_STRMATCH("int128_t")) return VTG_TYPE_FIXED_SIZED | sizeof(int128_t) | VTG_TYPE_SIGNED;
1691 if (MY_STRMATCH("int64_t")) return VTG_TYPE_FIXED_SIZED | sizeof(int64_t) | VTG_TYPE_SIGNED;
1692 if (MY_STRMATCH("int32_t")) return VTG_TYPE_FIXED_SIZED | sizeof(int32_t) | VTG_TYPE_SIGNED;
1693 if (MY_STRMATCH("int16_t")) return VTG_TYPE_FIXED_SIZED | sizeof(int16_t) | VTG_TYPE_SIGNED;
1694 if (MY_STRMATCH("int8_t")) return VTG_TYPE_FIXED_SIZED | sizeof(int8_t) | VTG_TYPE_SIGNED;
1695
1696 if (MY_STRMATCH("RTUINT64U")) return VTG_TYPE_FIXED_SIZED | sizeof(uint64_t) | VTG_TYPE_UNSIGNED;
1697 if (MY_STRMATCH("RTUINT32U")) return VTG_TYPE_FIXED_SIZED | sizeof(uint32_t) | VTG_TYPE_UNSIGNED;
1698 if (MY_STRMATCH("RTUINT16U")) return VTG_TYPE_FIXED_SIZED | sizeof(uint16_t) | VTG_TYPE_UNSIGNED;
1699
1700 if (MY_STRMATCH("RTMSINTERVAL")) return VTG_TYPE_FIXED_SIZED | sizeof(RTMSINTERVAL) | VTG_TYPE_UNSIGNED;
1701 if (MY_STRMATCH("RTTIMESPEC")) return VTG_TYPE_FIXED_SIZED | sizeof(RTTIMESPEC) | VTG_TYPE_SIGNED;
1702 if (MY_STRMATCH("RTPROCESS")) return VTG_TYPE_FIXED_SIZED | sizeof(RTPROCESS) | VTG_TYPE_UNSIGNED;
1703 if (MY_STRMATCH("RTHCPHYS")) return VTG_TYPE_FIXED_SIZED | sizeof(RTHCPHYS) | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS;
1704
1705 if (MY_STRMATCH("RTR3PTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3;
1706 if (MY_STRMATCH("RTR0PTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0;
1707 if (MY_STRMATCH("RTRCPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_RC;
1708 if (MY_STRMATCH("RTHCPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0;
1709
1710 if (MY_STRMATCH("RTR3UINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_UNSIGNED;
1711 if (MY_STRMATCH("RTR0UINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0 | VTG_TYPE_UNSIGNED;
1712 if (MY_STRMATCH("RTRCUINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_RC | VTG_TYPE_UNSIGNED;
1713 if (MY_STRMATCH("RTHCUINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_UNSIGNED;
1714
1715 if (MY_STRMATCH("RTR3INTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_SIGNED;
1716 if (MY_STRMATCH("RTR0INTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0 | VTG_TYPE_SIGNED;
1717 if (MY_STRMATCH("RTRCINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_RC | VTG_TYPE_SIGNED;
1718 if (MY_STRMATCH("RTHCINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_SIGNED;
1719
1720 if (MY_STRMATCH("RTUINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_CTX_RC | VTG_TYPE_UNSIGNED;
1721 if (MY_STRMATCH("RTINTPTR")) return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_CTX_RC | VTG_TYPE_SIGNED;
1722
1723 if (MY_STRMATCH("RTHCUINTREG")) return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_UNSIGNED;
1724 if (MY_STRMATCH("RTR3UINTREG")) return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_CTX_R3 | VTG_TYPE_UNSIGNED;
1725 if (MY_STRMATCH("RTR0UINTREG")) return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_CTX_R3 | VTG_TYPE_UNSIGNED;
1726
1727 if (MY_STRMATCH("RTGCUINTREG")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCUINTREG) | VTG_TYPE_UNSIGNED | VTG_TYPE_CTX_GST;
1728 if (MY_STRMATCH("RTGCPTR")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPTR) | VTG_TYPE_UNSIGNED | VTG_TYPE_CTX_GST;
1729 if (MY_STRMATCH("RTGCINTPTR")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCUINTPTR) | VTG_TYPE_SIGNED | VTG_TYPE_CTX_GST;
1730 if (MY_STRMATCH("RTGCPTR32")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPTR32) | VTG_TYPE_SIGNED | VTG_TYPE_CTX_GST;
1731 if (MY_STRMATCH("RTGCPTR64")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPTR64) | VTG_TYPE_SIGNED | VTG_TYPE_CTX_GST;
1732 if (MY_STRMATCH("RTGCPHYS")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPHYS) | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS | VTG_TYPE_CTX_GST;
1733 if (MY_STRMATCH("RTGCPHYS32")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPHYS32) | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS | VTG_TYPE_CTX_GST;
1734 if (MY_STRMATCH("RTGCPHYS64")) return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPHYS64) | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS | VTG_TYPE_CTX_GST;
1735
1736 /*
1737 * The special VBox types.
1738 */
1739 if (MY_STRMATCH("PVM")) return VTG_TYPE_POINTER;
1740 if (MY_STRMATCH("PVMCPU")) return VTG_TYPE_POINTER;
1741 if (MY_STRMATCH("PCPUMCTX")) return VTG_TYPE_POINTER;
1742
1743 /*
1744 * Preaching time.
1745 */
1746 if ( MY_STRMATCH("unsigned long")
1747 || MY_STRMATCH("unsigned long long")
1748 || MY_STRMATCH("signed long")
1749 || MY_STRMATCH("signed long long")
1750 || MY_STRMATCH("long")
1751 || MY_STRMATCH("long long")
1752 || MY_STRMATCH("char")
1753 || MY_STRMATCH("signed char")
1754 || MY_STRMATCH("unsigned char")
1755 || MY_STRMATCH("double")
1756 || MY_STRMATCH("long double")
1757 || MY_STRMATCH("float")
1758 )
1759 {
1760 RTMsgError("Please do NOT use the type '%s' for probe arguments!", pszType);
1761 g_cTypeErrors++;
1762 return 0;
1763 }
1764
1765 if ( MY_STRMATCH("unsigned")
1766 || MY_STRMATCH("signed")
1767 || MY_STRMATCH("signed int")
1768 || MY_STRMATCH("unsigned int")
1769 || MY_STRMATCH("short")
1770 || MY_STRMATCH("signed short")
1771 || MY_STRMATCH("unsigned short")
1772 )
1773 RTMsgWarning("Please avoid using the type '%s' for probe arguments!", pszType);
1774 if (MY_STRMATCH("unsigned")) return VTG_TYPE_FIXED_SIZED | sizeof(int) | VTG_TYPE_UNSIGNED;
1775 if (MY_STRMATCH("unsigned int")) return VTG_TYPE_FIXED_SIZED | sizeof(int) | VTG_TYPE_UNSIGNED;
1776 if (MY_STRMATCH("signed")) return VTG_TYPE_FIXED_SIZED | sizeof(int) | VTG_TYPE_SIGNED;
1777 if (MY_STRMATCH("signed int")) return VTG_TYPE_FIXED_SIZED | sizeof(int) | VTG_TYPE_SIGNED;
1778 if (MY_STRMATCH("short")) return VTG_TYPE_FIXED_SIZED | sizeof(short) | VTG_TYPE_SIGNED;
1779 if (MY_STRMATCH("signed short")) return VTG_TYPE_FIXED_SIZED | sizeof(short) | VTG_TYPE_SIGNED;
1780 if (MY_STRMATCH("unsigned short")) return VTG_TYPE_FIXED_SIZED | sizeof(short) | VTG_TYPE_UNSIGNED;
1781
1782 /*
1783 * What we haven't caught by now is either unknown to us or wrong.
1784 */
1785 if (pszType[0] == 'P')
1786 {
1787 RTMsgError("Type '%s' looks like a pointer typedef, please do NOT use those "
1788 "but rather the non-pointer typedef or struct with '*'",
1789 pszType);
1790 g_cTypeErrors++;
1791 return VTG_TYPE_POINTER;
1792 }
1793
1794 RTMsgError("Don't know '%s' - please change or fix VBoxTpG", pszType);
1795 g_cTypeErrors++;
1796
1797#undef MY_STRCMP
1798 return 0;
1799}
1800
1801
1802/**
1803 * Initializes the members of an argument.
1804 *
1805 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE+msg.
1806 * @param pProbe The probe.
1807 * @param pArg The argument.
1808 * @param pStrm The input stream (for errors).
1809 * @param pchType The type.
1810 * @param cchType The type length.
1811 * @param pchName The name.
1812 * @param cchName The name length.
1813 */
1814static RTEXITCODE parseInitArgument(PVTGPROBE pProbe, PVTGARG pArg, PSCMSTREAM pStrm,
1815 char *pchType, size_t cchType, char *pchName, size_t cchName)
1816{
1817 Assert(!pArg->pszName); Assert(!pArg->pszTracerType); Assert(!pArg->pszCtxType); Assert(!pArg->fType);
1818
1819 pArg->pszArgPassingFmt = ", %s";
1820 pArg->pszName = RTStrDupN(pchName, cchName);
1821 pArg->pszTracerType = strtabInsertN(pchType, cchType);
1822 if (!pArg->pszTracerType || !pArg->pszName)
1823 return parseError(pStrm, 1, "Out of memory");
1824 pArg->fType = parseTypeExpression(pArg->pszTracerType);
1825
1826 if ( (pArg->fType & VTG_TYPE_POINTER)
1827 && !(g_fTypeContext & VTG_TYPE_CTX_R0) )
1828 {
1829 pArg->fType &= ~VTG_TYPE_POINTER;
1830 if ( !strcmp(pArg->pszTracerType, "struct VM *") || !strcmp(pArg->pszTracerType, "PVM")
1831 || !strcmp(pArg->pszTracerType, "struct VMCPU *") || !strcmp(pArg->pszTracerType, "PVMCPU")
1832 || !strcmp(pArg->pszTracerType, "struct CPUMCTX *") || !strcmp(pArg->pszTracerType, "PCPUMCTX")
1833 )
1834 {
1835 pArg->fType |= VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0
1836 | VTG_TYPE_FIXED_SIZED | (g_cHostBits / 8)
1837 | VTG_TYPE_AUTO_CONV_PTR;
1838 pArg->pszCtxType = RTStrDup("RTR0PTR");
1839
1840 if (!strcmp(pArg->pszTracerType, "struct VM *") || !strcmp(pArg->pszTracerType, "PVM"))
1841 pArg->pszArgPassingFmt = ", VTG_VM_TO_R0(%s)";
1842 else if (!strcmp(pArg->pszTracerType, "struct VMCPU *") || !strcmp(pArg->pszTracerType, "PVMCPU"))
1843 pArg->pszArgPassingFmt = ", VTG_VMCPU_TO_R0(%s)";
1844 else
1845 {
1846 PVTGARG pFirstArg = RTListGetFirst(&pProbe->ArgHead, VTGARG, ListEntry);
1847 if ( !pFirstArg
1848 || pFirstArg == pArg
1849 || strcmp(pFirstArg->pszName, "a_pVCpu")
1850 || ( strcmp(pFirstArg->pszTracerType, "struct VMCPU *")
1851 && strcmp(pFirstArg->pszTracerType, "PVMCPU *")) )
1852 return parseError(pStrm, 1, "The automatic ring-0 pointer conversion requires 'a_pVCpu' with type 'struct VMCPU *' as the first argument");
1853
1854 if (!strcmp(pArg->pszTracerType, "struct CPUMCTX *")|| !strcmp(pArg->pszTracerType, "PCPUMCTX"))
1855 pArg->pszArgPassingFmt = ", VTG_CPUMCTX_TO_R0(a_pVCpu, %s)";
1856 else
1857 pArg->pszArgPassingFmt = ", VBoxTpG-Is-Buggy!!";
1858 }
1859 }
1860 else
1861 {
1862 pArg->fType |= VTG_TYPE_CTX_POINTER | g_fTypeContext | VTG_TYPE_FIXED_SIZED | (g_cBits / 8);
1863 pArg->pszCtxType = RTStrDupN(pchType, cchType);
1864 }
1865 }
1866 else
1867 pArg->pszCtxType = RTStrDupN(pchType, cchType);
1868 if (!pArg->pszCtxType)
1869 return parseError(pStrm, 1, "Out of memory");
1870
1871 return RTEXITCODE_SUCCESS;
1872}
1873
1874
1875/**
1876 * Unmangles the probe name.
1877 *
1878 * This involves translating double underscore to dash.
1879 *
1880 * @returns Pointer to the unmangled name in the string table.
1881 * @param pszMangled The mangled name.
1882 */
1883static const char *parseUnmangleProbeName(const char *pszMangled)
1884{
1885 size_t cchMangled = strlen(pszMangled);
1886 char *pszTmp = (char *)alloca(cchMangled + 2);
1887 const char *pszSrc = pszMangled;
1888 char *pszDst = pszTmp;
1889
1890 while (*pszSrc)
1891 {
1892 if (pszSrc[0] == '_' && pszSrc[1] == '_' && pszSrc[2] != '_')
1893 {
1894 *pszDst++ = '-';
1895 pszSrc += 2;
1896 }
1897 else
1898 *pszDst++ = *pszSrc++;
1899 }
1900 *pszDst = '\0';
1901
1902 return strtabInsertN(pszTmp, pszDst - pszTmp);
1903}
1904
1905
1906/**
1907 * Parses a D probe statement.
1908 *
1909 * @returns Suitable exit code, errors message already written on failure.
1910 * @param pStrm The stream.
1911 * @param pProv The provider being parsed.
1912 */
1913static RTEXITCODE parseProbe(PSCMSTREAM pStrm, PVTGPROVIDER pProv)
1914{
1915 /*
1916 * Next up is a name followed by an opening parenthesis.
1917 */
1918 size_t cchProbe;
1919 const char *pszProbe = parseGetNextCWord(pStrm, &cchProbe);
1920 if (!pszProbe)
1921 return parseError(pStrm, 1, "Expected a probe name starting with an alphabetical character");
1922 unsigned ch = parseGetNextNonSpaceNonCommentCh(pStrm);
1923 if (ch != '(')
1924 return parseError(pStrm, 1, "Expected '(' after the probe name");
1925
1926 /*
1927 * Create a probe instance.
1928 */
1929 PVTGPROBE pProbe = (PVTGPROBE)RTMemAllocZ(sizeof(*pProbe));
1930 if (!pProbe)
1931 return parseError(pStrm, 0, "Out of memory");
1932 RTListInit(&pProbe->ArgHead);
1933 RTListAppend(&pProv->ProbeHead, &pProbe->ListEntry);
1934 pProbe->offArgList = UINT32_MAX;
1935 pProbe->pszMangledName = RTStrDupN(pszProbe, cchProbe);
1936 if (!pProbe->pszMangledName)
1937 return parseError(pStrm, 0, "Out of memory");
1938 pProbe->pszUnmangledName = parseUnmangleProbeName(pProbe->pszMangledName);
1939 if (!pProbe->pszUnmangledName)
1940 return parseError(pStrm, 0, "Out of memory");
1941
1942 /*
1943 * Parse loop for the argument.
1944 */
1945 PVTGARG pArg = NULL;
1946 size_t cchName = 0;
1947 size_t cchArg = 0;
1948 char szArg[4096];
1949 for (;;)
1950 {
1951 ch = parseGetNextNonSpaceNonCommentCh(pStrm);
1952 switch (ch)
1953 {
1954 case ')':
1955 case ',':
1956 {
1957 /* commit the argument */
1958 if (pArg)
1959 {
1960 if (!cchName)
1961 return parseError(pStrm, 1, "Argument has no name");
1962 if (cchArg - cchName - 1 >= 128)
1963 return parseError(pStrm, 1, "Argument type too long");
1964 RTEXITCODE rcExit = parseInitArgument(pProbe, pArg, pStrm,
1965 szArg, cchArg - cchName - 1,
1966 &szArg[cchArg - cchName], cchName);
1967 if (rcExit != RTEXITCODE_SUCCESS)
1968 return rcExit;
1969 if (VTG_TYPE_IS_LARGE(pArg->fType))
1970 pProbe->fHaveLargeArgs = true;
1971 pArg = NULL;
1972 cchName = cchArg = 0;
1973 }
1974 if (ch == ')')
1975 {
1976 size_t off = ScmStreamTell(pStrm);
1977 ch = parseGetNextNonSpaceNonCommentCh(pStrm);
1978 if (ch != ';')
1979 return parseErrorAbs(pStrm, off, "Expected ';'");
1980 return RTEXITCODE_SUCCESS;
1981 }
1982 break;
1983 }
1984
1985 default:
1986 {
1987 size_t cchWord;
1988 const char *pszWord = ScmStreamCGetWordM1(pStrm, &cchWord);
1989 if (!pszWord)
1990 return parseError(pStrm, 0, "Expected argument");
1991 if (!pArg)
1992 {
1993 pArg = (PVTGARG)RTMemAllocZ(sizeof(*pArg));
1994 if (!pArg)
1995 return parseError(pStrm, 1, "Out of memory");
1996 RTListAppend(&pProbe->ArgHead, &pArg->ListEntry);
1997 pProbe->cArgs++;
1998
1999 if (cchWord + 1 > sizeof(szArg))
2000 return parseError(pStrm, 1, "Too long parameter declaration");
2001 memcpy(szArg, pszWord, cchWord);
2002 szArg[cchWord] = '\0';
2003 cchArg = cchWord;
2004 cchName = 0;
2005 }
2006 else
2007 {
2008 if (cchArg + 1 + cchWord + 1 > sizeof(szArg))
2009 return parseError(pStrm, 1, "Too long parameter declaration");
2010
2011 szArg[cchArg++] = ' ';
2012 memcpy(&szArg[cchArg], pszWord, cchWord);
2013 cchArg += cchWord;
2014 szArg[cchArg] = '\0';
2015 cchName = cchWord;
2016 }
2017 break;
2018 }
2019
2020 case '*':
2021 {
2022 if (!pArg)
2023 return parseError(pStrm, 1, "A parameter type does not start with an asterix");
2024 if (cchArg + sizeof(" *") >= sizeof(szArg))
2025 return parseError(pStrm, 1, "Too long parameter declaration");
2026 szArg[cchArg++] = ' ';
2027 szArg[cchArg++] = '*';
2028 szArg[cchArg ] = '\0';
2029 cchName = 0;
2030 break;
2031 }
2032
2033 case ~(unsigned)0:
2034 return parseError(pStrm, 0, "Missing closing ')' on probe");
2035 }
2036 }
2037}
2038
2039/**
2040 * Parses a D provider statement.
2041 *
2042 * @returns Suitable exit code, errors message already written on failure.
2043 * @param pStrm The stream.
2044 */
2045static RTEXITCODE parseProvider(PSCMSTREAM pStrm)
2046{
2047 /*
2048 * Next up is a name followed by a curly bracket. Ignore comments.
2049 */
2050 RTEXITCODE rcExit = parseSkipSpacesAndComments(pStrm);
2051 if (rcExit != RTEXITCODE_SUCCESS)
2052 return parseError(pStrm, 1, "Expected a provider name starting with an alphabetical character");
2053 size_t cchName;
2054 const char *pszName = ScmStreamCGetWord(pStrm, &cchName);
2055 if (!pszName)
2056 return parseError(pStrm, 0, "Bad provider name");
2057 if (RT_C_IS_DIGIT(pszName[cchName - 1]))
2058 return parseError(pStrm, 1, "A provider name cannot end with digit");
2059
2060 unsigned ch = parseGetNextNonSpaceNonCommentCh(pStrm);
2061 if (ch != '{')
2062 return parseError(pStrm, 1, "Expected '{' after the provider name");
2063
2064 /*
2065 * Create a provider instance.
2066 */
2067 PVTGPROVIDER pProv = (PVTGPROVIDER)RTMemAllocZ(sizeof(*pProv));
2068 if (!pProv)
2069 return parseError(pStrm, 0, "Out of memory");
2070 RTListInit(&pProv->ProbeHead);
2071 RTListAppend(&g_ProviderHead, &pProv->ListEntry);
2072 pProv->pszName = strtabInsertN(pszName, cchName);
2073 if (!pProv->pszName)
2074 return parseError(pStrm, 0, "Out of memory");
2075
2076 /*
2077 * Parse loop.
2078 */
2079 for (;;)
2080 {
2081 ch = parseGetNextNonSpaceNonCommentCh(pStrm);
2082 switch (ch)
2083 {
2084 case 'p':
2085 if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("probe")))
2086 rcExit = parseProbe(pStrm, pProv);
2087 else
2088 rcExit = parseError(pStrm, 1, "Unexpected character");
2089 break;
2090
2091 case '}':
2092 {
2093 size_t off = ScmStreamTell(pStrm);
2094 ch = parseGetNextNonSpaceNonCommentCh(pStrm);
2095 if (ch == ';')
2096 return RTEXITCODE_SUCCESS;
2097 rcExit = parseErrorAbs(pStrm, off, "Expected ';'");
2098 break;
2099 }
2100
2101 case ~(unsigned)0:
2102 rcExit = parseError(pStrm, 0, "Missing closing '}' on provider");
2103 break;
2104
2105 default:
2106 rcExit = parseError(pStrm, 1, "Unexpected character");
2107 break;
2108 }
2109 if (rcExit != RTEXITCODE_SUCCESS)
2110 return rcExit;
2111 }
2112}
2113
2114
2115static RTEXITCODE parseScript(const char *pszScript)
2116{
2117 SCMSTREAM Strm;
2118 int rc = ScmStreamInitForReading(&Strm, pszScript);
2119 if (RT_FAILURE(rc))
2120 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to open & read '%s' into memory: %Rrc", pszScript, rc);
2121 if (g_cVerbosity > 0)
2122 RTMsgInfo("Parsing '%s'...", pszScript);
2123
2124 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
2125 unsigned ch;
2126 while ((ch = ScmStreamGetCh(&Strm)) != ~(unsigned)0)
2127 {
2128 if (RT_C_IS_SPACE(ch))
2129 continue;
2130 switch (ch)
2131 {
2132 case '/':
2133 ch = ScmStreamGetCh(&Strm);
2134 if (ch == '*')
2135 rcExit = parseMultiLineComment(&Strm);
2136 else if (ch == '/')
2137 rcExit = parseOneLineComment(&Strm);
2138 else
2139 rcExit = parseError(&Strm, 2, "Unexpected character");
2140 break;
2141
2142 case 'p':
2143 if (ScmStreamCMatchingWordM1(&Strm, RT_STR_TUPLE("provider")))
2144 rcExit = parseProvider(&Strm);
2145 else
2146 rcExit = parseError(&Strm, 1, "Unexpected character");
2147 break;
2148
2149 case '#':
2150 {
2151 ch = parseGetNextNonSpaceNonCommentChOnPpLine(&Strm);
2152 if (ch == ~(unsigned)0)
2153 rcExit = RTEXITCODE_FAILURE;
2154 else if (ch == 'p' && ScmStreamCMatchingWordM1(&Strm, RT_STR_TUPLE("pragma")))
2155 rcExit = parsePragma(&Strm);
2156 else
2157 rcExit = parseError(&Strm, 1, "Unsupported preprocessor directive");
2158 break;
2159 }
2160
2161 default:
2162 rcExit = parseError(&Strm, 1, "Unexpected character");
2163 break;
2164 }
2165 if (rcExit != RTEXITCODE_SUCCESS)
2166 return rcExit;
2167 }
2168
2169 ScmStreamDelete(&Strm);
2170 if (g_cVerbosity > 0 && rcExit == RTEXITCODE_SUCCESS)
2171 RTMsgInfo("Successfully parsed '%s'.", pszScript);
2172 return rcExit;
2173}
2174
2175
2176/**
2177 * Parses the arguments.
2178 */
2179static RTEXITCODE parseArguments(int argc, char **argv)
2180{
2181 /*
2182 * Set / Adjust defaults.
2183 */
2184 int rc = RTPathAbs(g_pszAssemblerIncVal, g_szAssemblerIncVal, sizeof(g_szAssemblerIncVal) - 1);
2185 if (RT_FAILURE(rc))
2186 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPathAbs failed: %Rrc", rc);
2187 strcat(g_szAssemblerIncVal, "/");
2188 g_pszAssemblerIncVal = g_szAssemblerIncVal;
2189
2190 /*
2191 * Option config.
2192 */
2193 enum
2194 {
2195 kVBoxTpGOpt_32Bit = 1000,
2196 kVBoxTpGOpt_64Bit,
2197 kVBoxTpGOpt_GenerateWrapperHeader,
2198 kVBoxTpGOpt_Assembler,
2199 kVBoxTpGOpt_AssemblerFmtOpt,
2200 kVBoxTpGOpt_AssemblerFmtVal,
2201 kVBoxTpGOpt_AssemblerOutputOpt,
2202 kVBoxTpGOpt_AssemblerOption,
2203 kVBoxTpGOpt_Pic,
2204 kVBoxTpGOpt_ProbeFnName,
2205 kVBoxTpGOpt_ProbeFnImported,
2206 kVBoxTpGOpt_ProbeFnNotImported,
2207 kVBoxTpGOpt_Host32Bit,
2208 kVBoxTpGOpt_Host64Bit,
2209 kVBoxTpGOpt_RawModeContext,
2210 kVBoxTpGOpt_Ring0Context,
2211 kVBoxTpGOpt_Ring0ContextAgnostic,
2212 kVBoxTpGOpt_Ring3Context,
2213 kVBoxTpGOpt_End
2214 };
2215
2216 static RTGETOPTDEF const s_aOpts[] =
2217 {
2218 /* dtrace w/ long options */
2219 { "-32", kVBoxTpGOpt_32Bit, RTGETOPT_REQ_NOTHING },
2220 { "-64", kVBoxTpGOpt_64Bit, RTGETOPT_REQ_NOTHING },
2221 { "--apply-cpp", 'C', RTGETOPT_REQ_NOTHING },
2222 { "--generate-obj", 'G', RTGETOPT_REQ_NOTHING },
2223 { "--generate-header", 'h', RTGETOPT_REQ_NOTHING },
2224 { "--output", 'o', RTGETOPT_REQ_STRING },
2225 { "--script", 's', RTGETOPT_REQ_STRING },
2226 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
2227 /* our stuff */
2228 { "--generate-wrapper-header", kVBoxTpGOpt_GenerateWrapperHeader, RTGETOPT_REQ_NOTHING },
2229 { "--assembler", kVBoxTpGOpt_Assembler, RTGETOPT_REQ_STRING },
2230 { "--assembler-fmt-opt", kVBoxTpGOpt_AssemblerFmtOpt, RTGETOPT_REQ_STRING },
2231 { "--assembler-fmt-val", kVBoxTpGOpt_AssemblerFmtVal, RTGETOPT_REQ_STRING },
2232 { "--assembler-output-opt", kVBoxTpGOpt_AssemblerOutputOpt, RTGETOPT_REQ_STRING },
2233 { "--assembler-option", kVBoxTpGOpt_AssemblerOption, RTGETOPT_REQ_STRING },
2234 { "--pic", kVBoxTpGOpt_Pic, RTGETOPT_REQ_NOTHING },
2235 { "--probe-fn-name", kVBoxTpGOpt_ProbeFnName, RTGETOPT_REQ_STRING },
2236 { "--probe-fn-imported", kVBoxTpGOpt_ProbeFnImported, RTGETOPT_REQ_NOTHING },
2237 { "--probe-fn-not-imported", kVBoxTpGOpt_ProbeFnNotImported, RTGETOPT_REQ_NOTHING },
2238 { "--host-32-bit", kVBoxTpGOpt_Host32Bit, RTGETOPT_REQ_NOTHING },
2239 { "--host-64-bit", kVBoxTpGOpt_Host64Bit, RTGETOPT_REQ_NOTHING },
2240 { "--raw-mode-context", kVBoxTpGOpt_RawModeContext, RTGETOPT_REQ_NOTHING },
2241 { "--ring-0-context", kVBoxTpGOpt_Ring0Context, RTGETOPT_REQ_NOTHING },
2242 { "--ring-0-context-agnostic", kVBoxTpGOpt_Ring0ContextAgnostic, RTGETOPT_REQ_NOTHING },
2243 { "--ring-3-context", kVBoxTpGOpt_Ring3Context, RTGETOPT_REQ_NOTHING },
2244 /** @todo We're missing a bunch of assembler options! */
2245 };
2246
2247 RTGETOPTUNION ValueUnion;
2248 RTGETOPTSTATE GetOptState;
2249 rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2250 AssertReleaseRCReturn(rc, RTEXITCODE_FAILURE);
2251
2252 /*
2253 * Process the options.
2254 */
2255 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
2256 {
2257 switch (rc)
2258 {
2259 /*
2260 * DTrace compatible options.
2261 */
2262 case kVBoxTpGOpt_32Bit:
2263 g_cHostBits = g_cBits = 32;
2264 g_pszAssemblerFmtVal = g_szAssemblerFmtVal32;
2265 break;
2266
2267 case kVBoxTpGOpt_64Bit:
2268 g_cHostBits = g_cBits = 64;
2269 g_pszAssemblerFmtVal = g_szAssemblerFmtVal64;
2270 break;
2271
2272 case 'C':
2273 g_fApplyCpp = true;
2274 RTMsgWarning("Ignoring the -C option - no preprocessing of the D script will be performed");
2275 break;
2276
2277 case 'G':
2278 if ( g_enmAction != kVBoxTpGAction_Nothing
2279 && g_enmAction != kVBoxTpGAction_GenerateObject)
2280 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "-G does not mix with -h or --generate-wrapper-header");
2281 g_enmAction = kVBoxTpGAction_GenerateObject;
2282 break;
2283
2284 case 'h':
2285 if (!strcmp(GetOptState.pDef->pszLong, "--generate-header"))
2286 {
2287 if ( g_enmAction != kVBoxTpGAction_Nothing
2288 && g_enmAction != kVBoxTpGAction_GenerateHeader)
2289 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "-h does not mix with -G or --generate-wrapper-header");
2290 g_enmAction = kVBoxTpGAction_GenerateHeader;
2291 }
2292 else
2293 {
2294 /* --help or similar */
2295 RTPrintf("VirtualBox Tracepoint Generator\n"
2296 "\n"
2297 "Usage: %s [options]\n"
2298 "\n"
2299 "Options:\n", RTProcShortName());
2300 for (size_t i = 0; i < RT_ELEMENTS(s_aOpts); i++)
2301 if ((unsigned)s_aOpts[i].iShort < 128)
2302 RTPrintf(" -%c,%s\n", s_aOpts[i].iShort, s_aOpts[i].pszLong);
2303 else
2304 RTPrintf(" %s\n", s_aOpts[i].pszLong);
2305 return RTEXITCODE_SUCCESS;
2306 }
2307 break;
2308
2309 case 'o':
2310 if (g_pszOutput)
2311 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Output file is already set to '%s'", g_pszOutput);
2312 g_pszOutput = ValueUnion.psz;
2313 break;
2314
2315 case 's':
2316 if (g_pszScript)
2317 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Script file is already set to '%s'", g_pszScript);
2318 g_pszScript = ValueUnion.psz;
2319 break;
2320
2321 case 'v':
2322 g_cVerbosity++;
2323 break;
2324
2325 case 'V':
2326 {
2327 /* The following is assuming that svn does it's job here. */
2328 static const char s_szRev[] = "$Revision: 54067 $";
2329 const char *psz = RTStrStripL(strchr(s_szRev, ' '));
2330 RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
2331 return RTEXITCODE_SUCCESS;
2332 }
2333
2334 case VINF_GETOPT_NOT_OPTION:
2335 if (g_enmAction == kVBoxTpGAction_GenerateObject)
2336 break; /* object files, ignore them. */
2337 return RTGetOptPrintError(rc, &ValueUnion);
2338
2339
2340 /*
2341 * Our options.
2342 */
2343 case kVBoxTpGOpt_GenerateWrapperHeader:
2344 if ( g_enmAction != kVBoxTpGAction_Nothing
2345 && g_enmAction != kVBoxTpGAction_GenerateWrapperHeader)
2346 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--generate-wrapper-header does not mix with -h or -G");
2347 g_enmAction = kVBoxTpGAction_GenerateWrapperHeader;
2348 break;
2349
2350 case kVBoxTpGOpt_Assembler:
2351 g_pszAssembler = ValueUnion.psz;
2352 break;
2353
2354 case kVBoxTpGOpt_AssemblerFmtOpt:
2355 g_pszAssemblerFmtOpt = ValueUnion.psz;
2356 break;
2357
2358 case kVBoxTpGOpt_AssemblerFmtVal:
2359 g_pszAssemblerFmtVal = ValueUnion.psz;
2360 break;
2361
2362 case kVBoxTpGOpt_AssemblerOutputOpt:
2363 g_pszAssemblerOutputOpt = ValueUnion.psz;
2364 break;
2365
2366 case kVBoxTpGOpt_AssemblerOption:
2367 if (g_cAssemblerOptions >= RT_ELEMENTS(g_apszAssemblerOptions))
2368 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Too many assembly options (max %u)", RT_ELEMENTS(g_apszAssemblerOptions));
2369 g_apszAssemblerOptions[g_cAssemblerOptions] = ValueUnion.psz;
2370 g_cAssemblerOptions++;
2371 break;
2372
2373 case kVBoxTpGOpt_Pic:
2374 g_fPic = true;
2375 break;
2376
2377 case kVBoxTpGOpt_ProbeFnName:
2378 g_pszProbeFnName = ValueUnion.psz;
2379 break;
2380
2381 case kVBoxTpGOpt_ProbeFnImported:
2382 g_fProbeFnImported = true;
2383 break;
2384
2385 case kVBoxTpGOpt_ProbeFnNotImported:
2386 g_fProbeFnImported = false;
2387 break;
2388
2389 case kVBoxTpGOpt_Host32Bit:
2390 g_cHostBits = 32;
2391 break;
2392
2393 case kVBoxTpGOpt_Host64Bit:
2394 g_cHostBits = 64;
2395 break;
2396
2397 case kVBoxTpGOpt_RawModeContext:
2398 g_fTypeContext = VTG_TYPE_CTX_RC;
2399 g_pszContextDefine = "IN_RC";
2400 g_pszContextDefine2 = NULL;
2401 break;
2402
2403 case kVBoxTpGOpt_Ring0Context:
2404 g_fTypeContext = VTG_TYPE_CTX_R0;
2405 g_pszContextDefine = "IN_RING0";
2406 g_pszContextDefine2 = NULL;
2407 break;
2408
2409 case kVBoxTpGOpt_Ring0ContextAgnostic:
2410 g_fTypeContext = VTG_TYPE_CTX_R0;
2411 g_pszContextDefine = "IN_RING0_AGNOSTIC";
2412 g_pszContextDefine2 = "IN_RING0";
2413 break;
2414
2415 case kVBoxTpGOpt_Ring3Context:
2416 g_fTypeContext = VTG_TYPE_CTX_R3;
2417 g_pszContextDefine = "IN_RING3";
2418 g_pszContextDefine2 = NULL;
2419 break;
2420
2421
2422 /*
2423 * Errors and bugs.
2424 */
2425 default:
2426 return RTGetOptPrintError(rc, &ValueUnion);
2427 }
2428 }
2429
2430 /*
2431 * Check that we've got all we need.
2432 */
2433 if (g_enmAction == kVBoxTpGAction_Nothing)
2434 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No action specified (-h, -G or --generate-wrapper-header)");
2435 if (!g_pszScript)
2436 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No script file specified (-s)");
2437 if (!g_pszOutput)
2438 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No output file specified (-o)");
2439
2440 return RTEXITCODE_SUCCESS;
2441}
2442
2443
2444int main(int argc, char **argv)
2445{
2446 int rc = RTR3InitExe(argc, &argv, 0);
2447 if (RT_FAILURE(rc))
2448 return 1;
2449
2450 RTEXITCODE rcExit = parseArguments(argc, argv);
2451 if (rcExit == RTEXITCODE_SUCCESS)
2452 {
2453 /*
2454 * Parse the script.
2455 */
2456 RTListInit(&g_ProviderHead);
2457 rcExit = parseScript(g_pszScript);
2458 if (rcExit == RTEXITCODE_SUCCESS)
2459 {
2460 /*
2461 * Take action.
2462 */
2463 if (g_enmAction == kVBoxTpGAction_GenerateHeader)
2464 rcExit = generateFile(g_pszOutput, "header", generateHeader);
2465 else if (g_enmAction == kVBoxTpGAction_GenerateWrapperHeader)
2466 rcExit = generateFile(g_pszOutput, "wrapper header", generateWrapperHeader);
2467 else
2468 rcExit = generateObject(g_pszOutput, g_pszTempAsm);
2469 }
2470 }
2471
2472 if (rcExit == RTEXITCODE_SUCCESS && g_cTypeErrors > 0)
2473 rcExit = RTEXITCODE_FAILURE;
2474 return rcExit;
2475}
2476
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