VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/err/errmsg-sorter.cpp@ 95841

Last change on this file since 95841 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 18.9 KB
Line 
1/* $Id: errmsg-sorter.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - Status code messages, sorter build program.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/err.h>
32#include <iprt/asm.h>
33#include <iprt/string.h>
34#include <VBox/err.h>
35
36#include <stdio.h>
37#include <stdlib.h>
38
39
40/*
41 * Include the string table code.
42 */
43#define BLDPROG_STRTAB_MAX_STRLEN 512
44#define BLDPROG_STRTAB_WITH_COMPRESSION
45#define BLDPROG_STRTAB_PURE_ASCII
46#define BLDPROG_STRTAB_WITH_CAMEL_WORDS
47#include <iprt/bldprog-strtab-template.cpp.h>
48
49
50/*********************************************************************************************************************************
51* Structures and Typedefs *
52*********************************************************************************************************************************/
53/** Used for raw-input and sorting. */
54typedef struct RTSTATUSMSGINT1
55{
56 /** Pointer to the short message string. */
57 const char *pszMsgShort;
58 /** Pointer to the full message string. */
59 const char *pszMsgFull;
60 /** Pointer to the define string. */
61 const char *pszDefine;
62 /** Status code number. */
63 int iCode;
64 /** Set if duplicate. */
65 bool fDuplicate;
66} RTSTATUSMSGINT1;
67typedef RTSTATUSMSGINT1 *PRTSTATUSMSGINT1;
68
69
70/** This is used when building the string table and printing it. */
71typedef struct RTSTATUSMSGINT2
72{
73 /** The short message string. */
74 BLDPROGSTRING MsgShort;
75 /** The full message string. */
76 BLDPROGSTRING MsgFull;
77 /** The define string. */
78 BLDPROGSTRING Define;
79 /** Pointer to the define string. */
80 const char *pszDefine;
81 /** Status code number. */
82 int iCode;
83 /** Index into the primary table (for multiple passes). */
84 unsigned idx1;
85} RTSTATUSMSGINT2;
86typedef RTSTATUSMSGINT2 *PRTSTATUSMSGINT2;
87
88
89/** This used to determin minimum field sizes. */
90typedef struct RTSTATUSMSGSTATS
91{
92 unsigned offMax;
93 unsigned cchMax;
94 unsigned cBitsOffset;
95 unsigned cBitsLength;
96} RTSTATUSMSGSTATS;
97typedef RTSTATUSMSGSTATS *PRTSTATUSMSGSTATS;
98
99
100/*********************************************************************************************************************************
101* Global Variables *
102*********************************************************************************************************************************/
103static const char *g_pszProgName = "errmsg-sorter";
104static RTSTATUSMSGINT1 g_aStatusMsgs[] =
105{
106#if !defined(IPRT_NO_ERROR_DATA) && !defined(DOXYGEN_RUNNING)
107# include "errmsgdata.h"
108#else
109 { "Success.", "Success.", "VINF_SUCCESS", 0, false },
110#endif
111};
112
113
114static RTEXITCODE error(const char *pszFormat, ...)
115{
116 va_list va;
117 va_start(va, pszFormat);
118 fprintf(stderr, "%s: error: ", g_pszProgName);
119 vfprintf(stderr, pszFormat, va);
120 va_end(va);
121 return RTEXITCODE_FAILURE;
122}
123
124
125/** qsort callback. */
126static int CompareErrMsg(const void *pv1, const void *pv2) RT_NOTHROW_DEF
127{
128 PRTSTATUSMSGINT1 p1 = (PRTSTATUSMSGINT1)pv1;
129 PRTSTATUSMSGINT1 p2 = (PRTSTATUSMSGINT1)pv2;
130 int iDiff;
131 if (p1->iCode < p2->iCode)
132 iDiff = -1;
133 else if (p1->iCode > p2->iCode)
134 iDiff = 1;
135 else
136 iDiff = 0;
137 return iDiff;
138}
139
140
141/**
142 * Checks whether @a pszDefine is a deliberate duplicate define that should be
143 * omitted.
144 */
145static bool IgnoreDuplicateDefine(const char *pszDefine)
146{
147 size_t const cchDefine = strlen(pszDefine);
148
149 static const RTSTRTUPLE s_aTails[] =
150 {
151 { RT_STR_TUPLE("_FIRST") },
152 { RT_STR_TUPLE("_LAST") },
153 { RT_STR_TUPLE("_HIGEST") },
154 { RT_STR_TUPLE("_LOWEST") },
155 };
156 for (size_t i = 0; i < RT_ELEMENTS(s_aTails); i++)
157 if ( cchDefine > s_aTails[i].cch
158 && memcmp(&pszDefine[cchDefine - s_aTails[i].cch], s_aTails[i].psz, s_aTails[i].cch) == 0)
159 return true;
160
161 static const RTSTRTUPLE s_aDeliberateOrSilly[] =
162 {
163 { RT_STR_TUPLE("VERR_VRDP_TIMEOUT") },
164 { RT_STR_TUPLE("VINF_VRDP_SUCCESS") },
165 { RT_STR_TUPLE("VWRN_CONTINUE_RECOMPILE") },
166 { RT_STR_TUPLE("VWRN_PATM_CONTINUE_SEARCH") },
167 };
168 for (size_t i = 0; i < RT_ELEMENTS(s_aDeliberateOrSilly); i++)
169 if ( cchDefine == s_aDeliberateOrSilly[i].cch
170 && memcmp(pszDefine, s_aDeliberateOrSilly[i].psz, cchDefine) == 0)
171 return true;
172
173 return false;
174}
175
176
177DECLINLINE(void) GatherStringStats(PRTSTATUSMSGSTATS pStats, PBLDPROGSTRING pString)
178{
179 if (pStats->offMax < pString->offStrTab)
180 pStats->offMax = pString->offStrTab;
181 if (pStats->cchMax < pString->cchString)
182 pStats->cchMax = (unsigned)pString->cchString;
183}
184
185
186DECLINLINE(unsigned) CalcBitsForValue(size_t uValue)
187{
188 unsigned cBits = 1;
189 while (RT_BIT_64(cBits) < uValue && cBits < 64)
190 cBits++;
191 return cBits;
192}
193
194
195static void CalcBitsForStringStats(PRTSTATUSMSGSTATS pStats)
196{
197 pStats->cBitsOffset = CalcBitsForValue(pStats->offMax);
198 pStats->cBitsLength = CalcBitsForValue(pStats->cchMax);
199}
200
201
202int main(int argc, char **argv)
203{
204 /*
205 * Parse arguments.
206 */
207 enum { kMode_All, kMode_NoFullMsg, kMode_OnlyDefines } enmMode;
208 if (argc == 3 && strcmp(argv[1], "--all") == 0)
209 enmMode = kMode_All;
210 else if (argc == 3 && strcmp(argv[1], "--no-full-msg") == 0)
211 enmMode = kMode_NoFullMsg;
212 else if (argc == 3 && strcmp(argv[1], "--only-defines") == 0)
213 enmMode = kMode_OnlyDefines;
214 else
215 {
216 fprintf(stderr,
217 "syntax error!\n"
218 "Usage: %s <--all|--no-full-msg|--only-defines> <outfile>\n", argv[0]);
219 return RTEXITCODE_SYNTAX;
220 }
221 const char * const pszOutFile = argv[2];
222
223 /*
224 * Sort the table and mark duplicates.
225 */
226 qsort(g_aStatusMsgs, RT_ELEMENTS(g_aStatusMsgs), sizeof(g_aStatusMsgs[0]), CompareErrMsg);
227
228 int rcExit = RTEXITCODE_SUCCESS;
229 int iPrev = INT32_MAX;
230 for (size_t i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
231 {
232 /* Deal with duplicates, trying to eliminate unnecessary *_FIRST, *_LAST,
233 *_LOWEST, and *_HIGHEST entries as well as some deliberate duplicate entries.
234 This means we need to look forward and backwards here. */
235 PRTSTATUSMSGINT1 pMsg = &g_aStatusMsgs[i];
236 if (pMsg->iCode == iPrev && i != 0)
237 {
238 if (IgnoreDuplicateDefine(pMsg->pszDefine))
239 {
240 pMsg->fDuplicate = true;
241 continue;
242 }
243 PRTSTATUSMSGINT1 pPrev = &g_aStatusMsgs[i - 1];
244 rcExit = error("Duplicate value %d - %s and %s\n", iPrev, pMsg->pszDefine, pPrev->pszDefine);
245 }
246 else if (i + 1 < RT_ELEMENTS(g_aStatusMsgs))
247 {
248 PRTSTATUSMSGINT1 pNext = &g_aStatusMsgs[i];
249 if ( pMsg->iCode == pNext->iCode
250 && IgnoreDuplicateDefine(pMsg->pszDefine))
251 {
252 pMsg->fDuplicate = true;
253 continue;
254 }
255 }
256 iPrev = pMsg->iCode;
257 pMsg->fDuplicate = false;
258 }
259
260 /*
261 * Create a string table for it all.
262 */
263 BLDPROGSTRTAB StrTab;
264 if (!BldProgStrTab_Init(&StrTab, RT_ELEMENTS(g_aStatusMsgs) * 3))
265 return error("Out of memory!\n");
266
267 static RTSTATUSMSGINT2 s_aStatusMsgs2[RT_ELEMENTS(g_aStatusMsgs)];
268 unsigned cStatusMsgs = 0;
269 for (unsigned i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
270 if (!g_aStatusMsgs[i].fDuplicate)
271 {
272 s_aStatusMsgs2[cStatusMsgs].idx1 = i;
273 s_aStatusMsgs2[cStatusMsgs].iCode = g_aStatusMsgs[i].iCode;
274 s_aStatusMsgs2[cStatusMsgs].pszDefine = g_aStatusMsgs[i].pszDefine;
275 BldProgStrTab_AddStringDup(&StrTab, &s_aStatusMsgs2[cStatusMsgs].Define, g_aStatusMsgs[i].pszDefine);
276 cStatusMsgs++;
277 }
278
279 if (enmMode != kMode_OnlyDefines)
280 for (size_t i = 0; i < cStatusMsgs; i++)
281 BldProgStrTab_AddStringDup(&StrTab, &s_aStatusMsgs2[i].MsgShort, g_aStatusMsgs[s_aStatusMsgs2[i].idx1].pszMsgShort);
282
283 if (enmMode == kMode_All)
284 for (size_t i = 0; i < cStatusMsgs; i++)
285 BldProgStrTab_AddStringDup(&StrTab, &s_aStatusMsgs2[i].MsgFull, g_aStatusMsgs[s_aStatusMsgs2[i].idx1].pszMsgFull);
286
287 if (!BldProgStrTab_CompileIt(&StrTab, true))
288 return error("BldProgStrTab_CompileIt failed!\n");
289
290 /*
291 * Prepare output file.
292 */
293 FILE *pOut = fopen(pszOutFile, "wt");
294 if (pOut)
295 {
296 /*
297 * .
298 */
299 RTSTATUSMSGSTATS Defines = {0, 0, 0, 0};
300 RTSTATUSMSGSTATS MsgShort = {0, 0, 0, 0};
301 RTSTATUSMSGSTATS MsgFull = {0, 0, 0, 0};
302 for (size_t i = 0; i < cStatusMsgs; i++)
303 {
304 GatherStringStats(&Defines, &s_aStatusMsgs2[i].Define);
305 GatherStringStats(&MsgShort, &s_aStatusMsgs2[i].MsgShort);
306 GatherStringStats(&MsgFull, &s_aStatusMsgs2[i].MsgFull);
307 }
308 CalcBitsForStringStats(&Defines);
309 CalcBitsForStringStats(&MsgShort);
310 CalcBitsForStringStats(&MsgFull);
311 printf(" Defines: max offset %#x -> %u bits, max length %#x -> bits %u\n",
312 Defines.offMax, Defines.cBitsOffset, (unsigned)Defines.cchMax, Defines.cBitsLength);
313 if (enmMode != kMode_OnlyDefines)
314 printf("MsgShort: max offset %#x -> %u bits, max length %#x -> bits %u\n",
315 MsgShort.offMax, MsgShort.cBitsOffset, (unsigned)MsgShort.cchMax, MsgShort.cBitsLength);
316 if (enmMode == kMode_All)
317 printf(" MsgFull: max offset %#x -> %u bits, max length %#x -> bits %u\n",
318 MsgFull.offMax, MsgFull.cBitsOffset, (unsigned)MsgFull.cchMax, MsgFull.cBitsLength);
319
320 unsigned cBitsCodePos = CalcBitsForValue((size_t)s_aStatusMsgs2[cStatusMsgs - 1].iCode);
321 unsigned cBitsCodeNeg = CalcBitsForValue((size_t)-s_aStatusMsgs2[0].iCode);
322 unsigned cBitsCode = RT_MAX(cBitsCodePos, cBitsCodeNeg) + 1;
323 printf("Statuses: min %d, max %d -> %u bits\n",
324 s_aStatusMsgs2[0].iCode, s_aStatusMsgs2[cStatusMsgs - 1].iCode, cBitsCode);
325
326 /*
327 * Print the table.
328 */
329 fprintf(pOut,
330 "\n"
331 "#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)\n"
332 "# pragma pack(1)\n"
333 "#endif\n"
334 "typedef struct RTMSGENTRYINT\n"
335 "{\n");
336 /* 16 + 16 + 8 */
337 bool fOptimalLayout = true;
338 if ( enmMode == kMode_OnlyDefines
339 && cBitsCode <= 16
340 && Defines.cBitsOffset <= 16
341 && Defines.cBitsLength <= 8)
342 fprintf(pOut,
343 " uint16_t offDefine; /* need %2u bits, max %#x */\n"
344 " uint8_t cchDefine; /* need %2u bits, max %#x */\n"
345 " int16_t iCode; /* need %2u bits */\n",
346 Defines.cBitsOffset, Defines.offMax, Defines.cBitsLength, Defines.cchMax, cBitsCode);
347 else if ( enmMode == kMode_NoFullMsg
348 && cBitsCode + Defines.cBitsOffset + Defines.cBitsLength + MsgShort.cBitsOffset + MsgShort.cBitsLength <= 64)
349 fprintf(pOut,
350 " uint64_t offDefine : %2u; /* max %#x */\n"
351 " uint64_t cchDefine : %2u; /* max %#x */\n"
352 " uint64_t offMsgShort : %2u; /* max %#x */\n"
353 " uint64_t cchMsgShort : %2u; /* max %#x */\n"
354 " int64_t iCode : %2u;\n",
355 Defines.cBitsOffset, Defines.offMax,
356 Defines.cBitsLength, Defines.cchMax,
357 MsgShort.cBitsOffset, MsgShort.offMax,
358 MsgShort.cBitsLength, MsgShort.cchMax,
359 cBitsCode);
360 else if ( enmMode == kMode_All
361 && Defines.cBitsOffset + Defines.cBitsLength
362 + MsgShort.cBitsOffset + MsgShort.cBitsLength
363 + MsgFull.cBitsOffset + MsgFull.cBitsLength
364 + cBitsCode <= 96
365 && cBitsCode + Defines.cBitsLength + MsgShort.cBitsLength <= 32)
366 fprintf(pOut,
367 " uint64_t offDefine : %2u; /* max %#x */\n"
368 " uint64_t offMsgShort : %2u; /* max %#x */\n"
369 " uint64_t offMsgFull : %2u; /* max %#x */\n"
370 " uint64_t cchMsgFull : %2u; /* max %#x */\n"
371 " int32_t iCode : %2u;\n"
372 " uint32_t cchDefine : %2u; /* max %#x */\n"
373 " uint32_t cchMsgShort : %2u; /* max %#x */\n",
374 Defines.cBitsOffset, Defines.offMax,
375 MsgShort.cBitsOffset, MsgShort.offMax,
376 MsgFull.cBitsOffset, MsgFull.offMax,
377 MsgFull.cBitsLength, MsgFull.cchMax,
378 cBitsCode,
379 Defines.cBitsLength, Defines.cchMax,
380 MsgShort.cBitsLength, MsgShort.cchMax);
381 else
382 {
383 fprintf(stderr, "%s: warning: Optimized structure layouts needs readjusting...\n", g_pszProgName);
384 fOptimalLayout = false;
385 fprintf(pOut,
386 " uint32_t offDefine : 23; /* need %u bits, max %#x */\n"
387 " uint32_t cchDefine : 9; /* need %u bits, max %#x */\n",
388 Defines.cBitsOffset, Defines.offMax, Defines.cBitsLength, Defines.cchMax);
389 if (enmMode != kMode_OnlyDefines)
390 fprintf(pOut,
391 " uint32_t offMsgShort : 23; /* need %u bits, max %#x */\n"
392 " uint32_t cchMsgShort : 9; /* need %u bits, max %#x */\n",
393 MsgShort.cBitsOffset, MsgShort.offMax, MsgShort.cBitsLength, MsgShort.offMax);
394 if (enmMode == kMode_All)
395 fprintf(pOut,
396 " uint32_t offMsgFull : 23; /* need %u bits, max %#x */\n"
397 " uint32_t cchMsgFull : 9; /* need %u bits, max %#x */\n",
398 MsgFull.cBitsOffset, MsgFull.offMax, MsgFull.cBitsLength, MsgFull.cchMax);
399 fprintf(pOut,
400 " int32_t iCode; /* need %u bits */\n", cBitsCode);
401 }
402 fprintf(pOut,
403 "} RTMSGENTRYINT;\n"
404 "typedef RTMSGENTRYINT *PCRTMSGENTRYINT;\n"
405 "#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)\n"
406 "# pragma pack()\n"
407 "#endif\n"
408 "\n"
409 "static const RTMSGENTRYINT g_aStatusMsgs[ /*%lu*/ ] =\n"
410 "{\n"
411 ,
412 (unsigned long)cStatusMsgs);
413
414 if (enmMode == kMode_All && fOptimalLayout)
415 for (size_t i = 0; i < cStatusMsgs; i++)
416 fprintf(pOut, " { %#08x, %#08x, %#08x, %3u, %6d, %3u, %3u }, /* %s */\n",
417 s_aStatusMsgs2[i].Define.offStrTab,
418 s_aStatusMsgs2[i].MsgShort.offStrTab,
419 s_aStatusMsgs2[i].MsgFull.offStrTab,
420 (unsigned)s_aStatusMsgs2[i].MsgFull.cchString,
421 s_aStatusMsgs2[i].iCode,
422 (unsigned)s_aStatusMsgs2[i].Define.cchString,
423 (unsigned)s_aStatusMsgs2[i].MsgShort.cchString,
424 s_aStatusMsgs2[i].pszDefine);
425 else if (enmMode == kMode_All)
426 for (size_t i = 0; i < cStatusMsgs; i++)
427 fprintf(pOut, " { %#08x, %3u, %#08x, %3u, %#08x, %3u, %8d }, /* %s */\n",
428 s_aStatusMsgs2[i].Define.offStrTab,
429 (unsigned)s_aStatusMsgs2[i].Define.cchString,
430 s_aStatusMsgs2[i].MsgShort.offStrTab,
431 (unsigned)s_aStatusMsgs2[i].MsgShort.cchString,
432 s_aStatusMsgs2[i].MsgFull.offStrTab,
433 (unsigned)s_aStatusMsgs2[i].MsgFull.cchString,
434 s_aStatusMsgs2[i].iCode,
435 s_aStatusMsgs2[i].pszDefine);
436 else if (enmMode == kMode_NoFullMsg)
437 for (size_t i = 0; i < cStatusMsgs; i++)
438 fprintf(pOut, " { %#08x, %3u, %#08x, %3u, %8d }, /* %s */\n",
439 s_aStatusMsgs2[i].Define.offStrTab,
440 (unsigned)s_aStatusMsgs2[i].Define.cchString,
441 s_aStatusMsgs2[i].MsgShort.offStrTab,
442 (unsigned)s_aStatusMsgs2[i].MsgShort.cchString,
443 s_aStatusMsgs2[i].iCode,
444 s_aStatusMsgs2[i].pszDefine);
445 else if (enmMode == kMode_OnlyDefines)
446 for (size_t i = 0; i < cStatusMsgs; i++)
447 fprintf(pOut, " { %#08x, %3u, %8d }, /* %s */\n",
448 s_aStatusMsgs2[i].Define.offStrTab,
449 (unsigned)s_aStatusMsgs2[i].Define.cchString,
450 s_aStatusMsgs2[i].iCode,
451 s_aStatusMsgs2[i].pszDefine);
452 else
453 return error("Unsupported message selection (%d)!\n", enmMode);
454 fprintf(pOut,
455 "};\n"
456 "\n");
457
458 BldProgStrTab_WriteStringTable(&StrTab, pOut, "static ", "g_", "StatusMsgStrTab");
459
460 /*
461 * Close the output file and we're done.
462 */
463 fflush(pOut);
464 if (ferror(pOut))
465 rcExit = error("Error writing '%s'!\n", pszOutFile);
466 if (fclose(pOut) != 0)
467 rcExit = error("Failed to close '%s' after writing it!\n", pszOutFile);
468 }
469 else
470 rcExit = error("Failed to open '%s' for writing!\n", pszOutFile);
471 return rcExit;
472}
473
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