drsuapi_dissect_element_DsReplicaObjectIdentifier_dn parents append
[wireshark-sm.git] / wsutil / version_info.c
blobdac8ec11f72291016da64296e9869fedc8a2e1e9
1 /* version_info.c
2 * Routines to report version information for Wireshark programs
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #include <config.h>
12 #include "version_info.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <locale.h>
19 #ifdef _WIN32
20 #include <windows.h>
21 #elif __APPLE__
22 #include <sys/types.h>
23 #include <sys/sysctl.h>
24 #elif __linux__
25 #include <sys/sysinfo.h>
26 #endif
28 #include <glib.h>
29 #include <pcre2.h>
31 #ifdef HAVE_ZLIB
32 #include <zlib.h>
33 #endif
35 //#ifdef HAVE_ZLIBNG
36 //#include <zlib-ng.h>
37 //#endif
39 #include "vcs_version.h"
41 #include <wsutil/cpu_info.h>
42 #include <wsutil/os_version_info.h>
43 #include <wsutil/crash_info.h>
44 #include <wsutil/plugins.h>
46 static char *appname_with_version;
47 static char *copyright_info;
48 static char *license_info;
49 static char *comp_info;
50 static char *runtime_info;
52 static void end_string(GString *str);
53 static void get_compiler_info(GString *str);
54 static void get_mem_info(GString *str);
56 void
57 ws_init_version_info(const char *appname,
58 gather_feature_func gather_compile,
59 gather_feature_func gather_runtime)
61 GString *comp_info_str, *runtime_info_str;
62 GString *copyright_info_str;
63 GString *license_info_str;
65 copyright_info_str = g_string_new(get_copyright_info());
66 end_string(copyright_info_str);
67 copyright_info = g_string_free(copyright_info_str, FALSE);
69 license_info_str = g_string_new(get_license_info_short());
70 end_string(license_info_str);
71 license_info = g_string_free(license_info_str, FALSE);
74 * Combine the supplied application name string with the
75 * version - including the VCS version, for a build from
76 * a checkout.
78 if (strstr(appname, "Wireshark") != NULL) {
79 appname_with_version = ws_strdup_printf("%s %s",
80 appname, get_ws_vcs_version_info());
82 else {
83 appname_with_version = ws_strdup_printf("%s (Wireshark) %s",
84 appname, get_ws_vcs_version_info());
87 /* Get the compile-time version information string */
88 comp_info_str = get_compiled_version_info(gather_compile);
90 /* Get the run-time version information string */
91 runtime_info_str = get_runtime_version_info(gather_runtime);
93 comp_info = g_string_free(comp_info_str, FALSE);
94 runtime_info = g_string_free(runtime_info_str, FALSE);
96 /* Add this information to the information to be reported on a crash. */
97 ws_add_crash_info("%s\n"
98 "\n"
99 "%s\n"
100 "%s",
101 appname_with_version, comp_info, runtime_info);
105 * Take the gathered list of present/absent features (dependencies)
106 * and add them to the given string.
107 * Callback function for g_list_foreach() used in
108 * get_compiled_version_info() and get_runtime_version_info().
110 static void
111 feature_to_gstring(void * data, void * user_data)
113 char *feature = (char *)data;
114 GString *str = (GString *)user_data;
115 if (str->len > 0) {
116 g_string_append(str, ", ");
118 g_string_append_printf(str, "%s %s",
119 (*feature == '+' ? "with" : "without"), feature + 1);
123 * If the string doesn't end with a newline, append one.
124 * Then word-wrap it to 80 columns.
126 static void
127 end_string(GString *str)
129 size_t point;
130 char *p, *q;
132 point = str->len;
133 if (point == 0 || str->str[point - 1] != '\n')
134 g_string_append(str, "\n");
135 p = str->str;
136 while (*p != '\0') {
137 q = strchr(p, '\n');
138 if (q - p > 80) {
140 * Break at or before this point.
142 q = p + 80;
143 while (q > p && *q != ' ')
144 q--;
145 if (q != p)
146 *q = '\n';
148 p = q + 1;
152 const char *
153 get_appname_and_version(void)
155 return appname_with_version;
158 void
159 gather_pcre2_compile_info(feature_list l)
161 with_feature(l, "PCRE2");
164 void
165 gather_zlib_compile_info(feature_list l)
167 #ifdef HAVE_ZLIB
168 #ifdef ZLIB_VERSION
169 with_feature(l, "zlib " ZLIB_VERSION);
170 #else
171 with_feature(l, "zlib (version unknown)");
172 #endif /* ZLIB_VERSION */
173 #else
174 without_feature(l, "zlib");
175 #endif /* HAVE_ZLIB */
178 void
179 gather_zlib_ng_compile_info(feature_list l)
181 #ifdef HAVE_ZLIBNG
182 with_feature(l, "zlib-ng " ZLIBNG_VERSION_STRING);
183 #else
184 without_feature(l, "zlib-ng");
185 #endif /* HAVE_ZLIB */
190 * Get various library compile-time versions, put them in a GString,
191 * and return the GString.
193 GString *
194 get_compiled_version_info(gather_feature_func gather_compile)
196 GString *str;
197 GList *l = NULL;
199 str = g_string_new("Compiled ");
200 g_string_append_printf(str, "(%d-bit) ", (int)sizeof(str) * 8);
202 /* Compiler info */
203 g_string_append(str, "using ");
204 get_compiler_info(str);
206 #ifdef GLIB_MAJOR_VERSION
207 with_feature(&l,
208 "GLib %d.%d.%d", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
209 GLIB_MICRO_VERSION);
210 #else
211 with_feature(&l,
212 "GLib (version unknown)");
213 #endif
215 if (gather_compile != NULL) {
216 gather_compile(&l);
219 l = g_list_reverse(l);
220 g_list_foreach(l, feature_to_gstring, str);
222 #ifdef HAVE_PLUGINS
223 g_string_append(str, ", with binary plugins");
224 #else
225 g_string_append(str, ", without binary plugins");
226 #endif
228 #ifdef WS_DEBUG
229 g_string_append(str, ", debug build");
230 #endif
232 g_string_append(str, ".");
233 end_string(str);
234 free_features(&l);
236 return str;
239 static void
240 get_mem_info(GString *str)
242 int64_t memsize = 0;
244 #ifdef _WIN32
245 MEMORYSTATUSEX statex;
247 statex.dwLength = sizeof (statex);
249 if (GlobalMemoryStatusEx(&statex))
250 memsize = statex.ullTotalPhys;
251 #elif __APPLE__
252 size_t len = sizeof(memsize);
253 sysctlbyname("hw.memsize", &memsize, &len, NULL, 0);
254 #elif __linux__
255 struct sysinfo info;
256 if (sysinfo(&info) == 0)
257 memsize = info.totalram * info.mem_unit;
258 #endif
260 if (memsize > 0)
261 g_string_append_printf(str, ", with %" PRId64 " MB of physical memory", memsize/(1024*1024));
265 * Get compiler information, and append it to the GString.
267 static void
268 get_compiler_info(GString *str)
271 * See https://sourceforge.net/apps/mediawiki/predef/index.php?title=Compilers
272 * information on various defined strings.
274 * GCC's __VERSION__ is a nice text string for humans to
275 * read. The page at sourceforge.net largely describes
276 * numeric #defines that encode the version; if the compiler
277 * doesn't also offer a nice printable string, we try prettifying
278 * the number somehow.
280 #if defined(_MSC_FULL_VER)
282 * We check for this first, as Microsoft have a version of their
283 * compiler that has Clang as the front end and their code generator
284 * as the back end.
286 * My head asplode.
289 /* As of Wireshark 3.0, we support only Visual Studio 2015 (14.x)
290 * or later.
292 * https://dev.to/yumetodo/list-of-mscver-and-mscfullver-8nd
293 * has a *large* table of Microsoft product names, VC++ versions,
294 * _MSC_VER values, and _MSC_FULL_VER values. All the versions
295 * we support define _MSC_FULL_VER. We don't bother trying to
296 * get the SP/update/version number from the build number, as
297 * we'd have to keep updating that with every update; there's no
298 * way to get that information directly from a predefine, and in
299 * some cases multiple updates/versions have the *same* build
300 * number (because they didn't update the toolchain).
302 * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2017
303 * defines the format of _MSC_VER and _MSC_FULL_VER. _MSC_FULL_VER
304 * is a decimal number of the form MMmmBBBBB, where MM is the compiler/
305 * toolchain major version, mm is the minor version, and BBBBB is the
306 * build. We break it down based on that.
308 #define COMPILER_MAJOR_VERSION (_MSC_FULL_VER / 10000000)
309 #define COMPILER_MINOR_VERSION ((_MSC_FULL_VER % 10000000) / 100000)
310 #define COMPILER_BUILD_NUMBER (_MSC_FULL_VER % 100000)
313 * From https://web.archive.org/web/20190125151548/https://blogs.msdn.microsoft.com/vcblog/2014/11/17/c111417-features-in-vs-2015-preview/
314 * Bakersfield: DevDiv's upper management determines the scheduling
315 * of new major versions. They also decided to increment the product
316 * version from 12 (for VS 2013) to 14 (for VS 2015). However, the
317 * C++ compiler's version incremented normally, from 18 to 19.
318 * (It's larger because the C++ compiler predates the "Visual" in
319 * Visual C++.)
321 * So the product version number is 5 less than the compiler version
322 * number.
324 #define VCPP_MAJOR_VERSION (COMPILER_MAJOR_VERSION - 5)
326 #if VCPP_MAJOR_VERSION == 14
328 * From https://devblogs.microsoft.com/cppblog/side-by-side-minor-version-msvc-toolsets-in-visual-studio-2017/
330 * We've been delivering improvements to Visual Studio 2017 more
331 * frequently than ever before. Since its first release in March
332 * we've released four major updates to VS2017 and are currently
333 * previewing the fifth update, VS2017 version 15.5.
335 * The MSVC toolset in VS2017 is built as a minor version update to
336 * the VS2015 compiler toolset. This minor version bump indicates
337 * that the VS2017 MSVC toolset is binary compatible with the VS2015
338 * MSVC toolset, enabling an easier upgrade for VS2015 users. Even
339 * though the MSVC compiler toolset in VS2017 delivers many new
340 * features and conformance improvements it is a minor version,
341 * compatible update from 14.00 in VS2015 to 14.10 in VS2017.
343 #if COMPILER_MINOR_VERSION < 10
344 #define VS_VERSION "2015"
345 #elif COMPILER_MINOR_VERSION < 20
346 #define VS_VERSION "2017"
347 #elif COMPILER_MINOR_VERSION < 30
348 #define VS_VERSION "2019"
349 #else
350 #define VS_VERSION "2022"
351 #endif
352 #else
354 * Add additional checks here, before the #else.
356 #define VS_VERSION "(unknown)"
357 #endif /* VCPP_MAJOR_VERSION */
360 * XXX - should we show the raw compiler version number, as is
361 * shown by "cl /?", which would be %d.%d.%d.%d with
362 * COMPILER_MAJOR_VERSION, COMPILER_MINOR_VERSION,
363 * COMPILER_BUILD_NUMBER, and _MSC_BUILD, the last of which is
364 * "the revision number element of the compiler's version number",
365 * which I guess is not to be confused with the build number,
366 * the _BUILD in the name nonwithstanding.
368 g_string_append_printf(str, "Microsoft Visual Studio " VS_VERSION " (VC++ %d.%d, build %d)",
369 VCPP_MAJOR_VERSION, COMPILER_MINOR_VERSION, COMPILER_BUILD_NUMBER);
370 #if defined(__clang__)
372 * See above.
374 g_string_append_printf(str, " clang/C2 %s and -fno-ms-compatibility",
375 __VERSION__);
376 #endif
377 #elif defined(__GNUC__) && defined(__VERSION__)
379 * Clang and llvm-gcc also define __GNUC__ and __VERSION__;
380 * distinguish between them.
382 #if defined(__clang__)
383 /* clang */
384 char *version; /* clang's version string has a trailing space. */
385 #if defined(__clang_version__)
386 version = g_strdup(__clang_version__);
387 g_string_append_printf(str, "Clang %s", g_strstrip(version));
388 #else
389 version = g_strdup(__VERSION__);
390 g_string_append_printf(str, "%s", g_strstrip(version));
391 #endif /* __clang_version__ */
392 g_free(version);
393 #elif defined(__llvm__)
394 /* llvm-gcc */
395 g_string_append_printf(str, "llvm-gcc %s", __VERSION__);
396 #else
397 /* boring old GCC */
398 g_string_append_printf(str, "GCC %s", __VERSION__);
399 #endif
400 #elif defined(__HP_aCC)
401 g_string_append_printf(str, "HP aCC %d", __HP_aCC);
402 #elif defined(__xlC__)
403 g_string_append_printf(str, "IBM XL C %d.%d",
404 (__xlC__ >> 8) & 0xFF, __xlC__ & 0xFF);
405 #ifdef __IBMC__
406 if ((__IBMC__ % 10) != 0)
407 g_string_append_printf(str, " patch %d", __IBMC__ % 10);
408 #endif /* __IBMC__ */
409 #elif defined(__INTEL_COMPILER)
410 g_string_append_printf(str, "Intel C %d.%d",
411 __INTEL_COMPILER / 100, (__INTEL_COMPILER / 10) % 10);
412 if ((__INTEL_COMPILER % 10) != 0)
413 g_string_append_printf(str, " patch %d", __INTEL_COMPILER % 10);
414 #ifdef __INTEL_COMPILER_BUILD_DATE
415 g_string_sprinta(str, ", compiler built %04d-%02d-%02d",
416 __INTEL_COMPILER_BUILD_DATE / 10000,
417 (__INTEL_COMPILER_BUILD_DATE / 100) % 100,
418 __INTEL_COMPILER_BUILD_DATE % 100);
419 #endif /* __INTEL_COMPILER_BUILD_DATE */
420 #elif defined(__SUNPRO_C)
421 g_string_append_printf(str, "Sun C %d.%d",
422 (__SUNPRO_C >> 8) & 0xF, (__SUNPRO_C >> 4) & 0xF);
423 if ((__SUNPRO_C & 0xF) != 0)
424 g_string_append_printf(str, " patch %d", __SUNPRO_C & 0xF);
425 #else
426 g_string_append(str, "unknown compiler");
427 #endif
430 void
431 gather_pcre2_runtime_info(feature_list l)
433 /* From pcre2_api(3):
434 * The where argument should point to a buffer that is at least 24 code
435 * units long. (The exact length required can be found by calling
436 * pcre2_config() with where set to NULL.)
438 * The API should accept a buffer size as additional input. We could opt for a
439 * stack buffer size greater than 24 but let's just go with the weirdness...
441 int size;
442 char *buf_pcre2;
444 size = pcre2_config(PCRE2_CONFIG_VERSION, NULL);
445 if (size < 0 || size > 255) {
446 without_feature(l, "PCRE2 (error querying)");
447 return;
449 buf_pcre2 = g_malloc(size + 1);
450 pcre2_config(PCRE2_CONFIG_VERSION, buf_pcre2);
451 buf_pcre2[size] = '\0';
452 with_feature(l, "PCRE2 %s", buf_pcre2);
453 g_free(buf_pcre2);
456 void
457 gather_zlib_runtime_info(feature_list l)
459 (void)l;
460 #if defined(HAVE_ZLIB) && !defined(_WIN32)
461 with_feature(l, "zlib %s", zlibVersion());
462 #endif
466 * Get various library run-time versions, and the OS version, and append
467 * them to the specified GString.
469 * "additional_info" is called at the end to append any additional
470 * information; this is required in order to, for example, put the
471 * libcap information at the end of the string, as we currently
472 * don't use libcap in TShark.
474 GString *
475 get_runtime_version_info(gather_feature_func gather_runtime)
477 GString *str;
478 char *lc;
479 GList *l = NULL;
481 str = g_string_new("Running on ");
483 get_os_version_info(str);
485 /* CPU Info */
486 get_cpu_info(str);
488 /* Get info about installed memory */
489 get_mem_info(str);
491 with_feature(&l, "GLib %u.%u.%u",
492 glib_major_version, glib_minor_version, glib_micro_version);
494 if (gather_runtime != NULL) {
495 gather_runtime(&l);
498 l = g_list_reverse(l);
499 g_list_foreach(l, feature_to_gstring, str);
502 * Display LC_CTYPE as a relevant, portable and sort of representative
503 * locale configuration without being exceedingly verbose and including
504 * the whole shebang of categories using LC_ALL.
506 if ((lc = setlocale(LC_CTYPE, NULL)) != NULL) {
507 g_string_append_printf(str, ", with LC_TYPE=%s", lc);
510 #ifdef HAVE_PLUGINS
511 if (plugins_supported()) {
512 g_string_append(str, ", binary plugins supported");
514 #endif
516 g_string_append_c(str, '.');
517 end_string(str);
518 free_features(&l);
520 return str;
524 * Return a version number string for Wireshark, including, for builds
525 * from a tree checked out from Wireshark's version control system,
526 * something identifying what version was checked out.
528 const char *
529 get_ws_vcs_version_info(void)
531 #ifdef VCS_VERSION
532 return VERSION " (" VCS_VERSION ")";
533 #else
534 return VERSION;
535 #endif
538 const char *
539 get_ss_vcs_version_info(void)
541 #ifdef VCS_COMMIT_ID
542 return LOG_VERSION " (" VCS_NUM_COMMITS "-" VCS_COMMIT_ID ")";
543 #else
544 return LOG_VERSION;
545 #endif
548 const char *
549 get_ws_vcs_version_info_short(void)
551 #ifdef VCS_VERSION
552 return VCS_VERSION;
553 #else
554 return VERSION;
555 #endif
558 void
559 get_ws_version_number(int *major, int *minor, int *micro)
561 if (major)
562 *major = VERSION_MAJOR;
563 if (minor)
564 *minor = VERSION_MINOR;
565 if (micro)
566 *micro = VERSION_MICRO;
569 void
570 show_version(void)
572 printf("%s.\n\n"
573 "%s"
574 "%s\n"
575 "%s\n"
576 "%s",
577 appname_with_version,
578 copyright_info,
579 license_info,
580 comp_info,
581 runtime_info);
584 void
585 show_help_header(const char *description)
587 printf("%s\n", appname_with_version);
588 if (description) {
589 printf("%s\n", description);
590 printf("See https://www.wireshark.org for more information.\n");
595 * Get copyright information.
597 const char *
598 get_copyright_info(void)
600 return
601 "Copyright 1998-2024 Gerald Combs <gerald@wireshark.org> and contributors.";
604 const char *
605 get_license_info_short(void)
607 return
608 "Licensed under the terms of the GNU General Public License (version 2 or later). "
609 "This is free software; see the file named COPYING in the distribution. "
610 "There is NO WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.";
613 const char *
614 get_license_info(void)
616 return
617 "This program is free software: you can redistribute it and/or modify "
618 "it under the terms of the GNU General Public License as published by "
619 "the Free Software Foundation, either version 2 of the License, or "
620 "(at your option) any later version. This program is distributed in the "
621 "hope that it will be useful, but WITHOUT ANY WARRANTY; without even "
622 "the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. "
623 "See the GNU General Public License for more details.";
627 * Editor modelines - https://www.wireshark.org/tools/modelines.html
629 * Local variables:
630 * c-basic-offset: 8
631 * tab-width: 8
632 * indent-tabs-mode: t
633 * End:
635 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
636 * :indentSize=8:tabSize=8:noTabs=false: