Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / wsutil / version_info.c
blob2f80be17eee7f5687b0107db10e39096aa8335c7
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 <string.h>
16 #include <locale.h>
18 #ifdef _WIN32
19 #include <windows.h>
20 #elif __APPLE__
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
23 #elif __linux__
24 #include <sys/sysinfo.h>
25 #endif
27 #include <glib.h>
28 #include <gmodule.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/application_flavor.h>
42 #include <wsutil/cpu_info.h>
43 #include <wsutil/os_version_info.h>
44 #include <wsutil/crash_info.h>
45 #include <wsutil/plugins.h>
47 static char *appname_with_version;
48 static char *copyright_info;
49 static char *license_info;
50 static char *comp_info;
51 static char *runtime_info;
53 static void end_string(GString *str);
54 static void get_compiler_info(GString *str);
55 static void get_mem_info(GString *str);
57 void
58 ws_init_version_info(const char *appname,
59 gather_feature_func gather_compile,
60 gather_feature_func gather_runtime)
62 GString *comp_info_str, *runtime_info_str;
63 GString *copyright_info_str;
64 GString *license_info_str;
66 copyright_info_str = g_string_new(get_copyright_info());
67 end_string(copyright_info_str);
68 copyright_info = g_string_free(copyright_info_str, FALSE);
70 license_info_str = g_string_new(get_license_info_short());
71 end_string(license_info_str);
72 license_info = g_string_free(license_info_str, FALSE);
75 * Combine the supplied application name string with the
76 * version - including the VCS version, for a build from
77 * a checkout.
79 if (strstr(appname, application_flavor_name_proper()) != NULL) {
80 appname_with_version = ws_strdup_printf("%s %s",
81 appname, get_ws_vcs_version_info());
83 /* Include our application flavor. The default is "Wireshark" */
84 else {
85 appname_with_version = ws_strdup_printf("%s (%s) %s",
86 appname, application_flavor_name_proper(), get_ws_vcs_version_info());
89 /* Get the compile-time version information string */
90 comp_info_str = get_compiled_version_info(gather_compile);
92 /* Get the run-time version information string */
93 runtime_info_str = get_runtime_version_info(gather_runtime);
95 comp_info = g_string_free(comp_info_str, FALSE);
96 runtime_info = g_string_free(runtime_info_str, FALSE);
98 /* Add this information to the information to be reported on a crash. */
99 ws_add_crash_info("%s\n"
100 "\n"
101 "%s\n"
102 "%s",
103 appname_with_version, comp_info, runtime_info);
107 * Used below in features_to_columns()
109 static void
110 rtrim_gstring(GString *str)
112 gsize end = str->len - 1; // get to 0-based offset
113 while(str->str[end] == ' ') {
114 end--;
116 end++; // return to 1-based length
117 if (end < str->len) {
118 g_string_truncate(str, end);
123 * Take the list of features, and format it into a number of vertical
124 * columns, presented in column-major order. Calculates the number of
125 * columns based on the length of the longest list item.
127 static void
128 features_to_columns(feature_list l, GString *str)
130 const uint8_t linelen = 85; // Same value used in end_string() +10
131 const uint8_t linepad = 2; // left-side padding
132 uint8_t ncols = 0; // number of columns to show
133 uint8_t maxlen = 0; // length of longest item
134 unsigned num = 0; // number of items in list
135 gchar *c;
136 GPtrArray *a;
137 GList *iter;
139 num = g_list_length(*l);
140 if (num == 0) {
141 return;
143 a = g_ptr_array_sized_new(num);
144 for (iter = *l; iter != NULL; iter = iter->next) {
145 c = (gchar *)iter->data;
146 maxlen = MAX(maxlen, (uint8_t)strlen(c));
147 g_ptr_array_add(a, iter->data);
149 maxlen += 2; // allow space between columns
150 ncols = (linelen - linepad) / maxlen;
151 if (ncols <= 1 || num <= 1) {
152 for (iter = *l; iter != NULL; iter = iter->next) {
153 c = (gchar *)iter->data;
154 g_string_append_printf(str, "%*s%s\n", linepad, "", c);
157 else {
158 uint8_t nrows = (num + ncols - 1) / ncols;
159 unsigned i, j;
160 for (i = 0; i < nrows; i++) {
161 g_string_append_printf(str, "%*s", linepad, "");
162 for (j = 0; j < ncols; j++) {
163 unsigned idx = i + (j * nrows);
164 if (idx < num) {
165 g_string_append_printf(str, "%-*s", maxlen, (gchar *)g_ptr_array_index(a, idx));
168 rtrim_gstring(str);
169 g_string_append(str, "\n");
172 g_ptr_array_free(a, TRUE);
176 * If the string doesn't end with a newline, append one.
177 * Then word-wrap it to 80 columns.
179 static void
180 end_string(GString *str)
182 size_t point;
183 char *p, *q;
185 point = str->len;
186 if (point == 0 || str->str[point - 1] != '\n')
187 g_string_append(str, "\n");
188 p = str->str;
189 while (*p != '\0') {
190 q = strchr(p, '\n');
191 if (q - p > 80) {
193 * Break at or before this point.
195 q = p + 80;
196 while (q > p && *q != ' ')
197 q--;
198 if (q != p)
199 *q = '\n';
201 p = q + 1;
205 const char *
206 get_appname_and_version(void)
208 return appname_with_version;
211 void
212 gather_pcre2_compile_info(feature_list l)
214 #define PCRE2_DATE_QUOTE(str) #str
215 #define PCRE2_DATE_EXPAND_AND_QUOTE(str) PCRE2_DATE_QUOTE(str)
217 * PCRE2_PRERELEASE appears to be empty for a regular release;
218 * I don't know what it is for a pre-release.
220 with_feature(l, "PCRE2 %u.%u %s", PCRE2_MAJOR, PCRE2_MINOR, PCRE2_DATE_EXPAND_AND_QUOTE(PCRE2_DATE));
223 void
224 gather_zlib_compile_info(feature_list l)
226 #ifdef HAVE_ZLIB
227 #ifdef ZLIB_VERSION
228 with_feature(l, "zlib " ZLIB_VERSION);
229 #else
230 with_feature(l, "zlib (version unknown)");
231 #endif /* ZLIB_VERSION */
232 #else
233 without_feature(l, "zlib");
234 #endif /* HAVE_ZLIB */
237 void
238 gather_zlib_ng_compile_info(feature_list l)
240 #ifdef HAVE_ZLIBNG
241 with_feature(l, "zlib-ng " ZLIBNG_VERSION_STRING);
242 #else
243 without_feature(l, "zlib-ng");
244 #endif /* HAVE_ZLIB */
249 * Get various library compile-time versions, put them in a GString,
250 * and return the GString.
252 GString *
253 get_compiled_version_info(gather_feature_func gather_compile)
255 GString *str;
256 GList *l = NULL, *with_list = NULL, *without_list = NULL;
258 str = g_string_new("Compile-time info:\n");
259 g_string_append_printf(str, " Bit width: %d-bit\n", (int)sizeof(str) * 8);
261 /* Compiler info */
262 g_string_append_printf(str, " Compiler: ");
263 get_compiler_info(str);
265 #ifdef GLIB_MAJOR_VERSION
266 g_string_append_printf(str,
267 " GLib: %d.%d.%d", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION,
268 GLIB_MICRO_VERSION);
269 #else
270 g_string_append(str, " GLib: version unknown");
271 #endif
272 #ifdef WS_DISABLE_DEBUG
273 g_string_append(str, ", release build");
274 #endif
275 #ifdef WS_DISABLE_ASSERT
276 g_string_append(str, ", without assertions");
277 #endif
278 g_string_append(str, "\n");
280 if (gather_compile != NULL) {
281 gather_compile(&l);
284 sort_features(&l);
285 separate_features(&l, &with_list, &without_list);
286 free_features(&l);
288 g_string_append(str, " With:\n");
289 features_to_columns(&with_list, str);
290 free_features(&with_list);
291 if (without_list != NULL) {
292 g_string_append(str, " Without:\n");
293 features_to_columns(&without_list, str);
294 free_features(&without_list);
297 end_string(str);
298 return str;
301 static void
302 get_mem_info(GString *str)
304 int64_t memsize = 0;
306 #ifdef _WIN32
307 MEMORYSTATUSEX statex;
309 statex.dwLength = sizeof (statex);
311 if (GlobalMemoryStatusEx(&statex))
312 memsize = statex.ullTotalPhys;
313 #elif __APPLE__
314 size_t len = sizeof(memsize);
315 sysctlbyname("hw.memsize", &memsize, &len, NULL, 0);
316 #elif __linux__
317 struct sysinfo info;
318 if (sysinfo(&info) == 0)
319 memsize = info.totalram * info.mem_unit;
320 #endif
322 if (memsize > 0)
323 g_string_append_printf(str, "%" G_GINT64_FORMAT " MB of physical memory", memsize/(1024*1024));
327 * Get compiler information, and append it to the GString.
329 static void
330 get_compiler_info(GString *str)
333 * See https://sourceforge.net/apps/mediawiki/predef/index.php?title=Compilers
334 * information on various defined strings.
336 * GCC's __VERSION__ is a nice text string for humans to
337 * read. The page at sourceforge.net largely describes
338 * numeric #defines that encode the version; if the compiler
339 * doesn't also offer a nice printable string, we try prettifying
340 * the number somehow.
342 #if defined(_MSC_FULL_VER)
344 * We check for this first, as Microsoft have a version of their
345 * compiler that has Clang as the front end and their code generator
346 * as the back end.
348 * My head asplode.
351 /* As of Wireshark 3.0, we support only Visual Studio 2015 (14.x)
352 * or later.
354 * https://dev.to/yumetodo/list-of-mscver-and-mscfullver-8nd
355 * has a *large* table of Microsoft product names, VC++ versions,
356 * _MSC_VER values, and _MSC_FULL_VER values. All the versions
357 * we support define _MSC_FULL_VER. We don't bother trying to
358 * get the SP/update/version number from the build number, as
359 * we'd have to keep updating that with every update; there's no
360 * way to get that information directly from a predefine, and in
361 * some cases multiple updates/versions have the *same* build
362 * number (because they didn't update the toolchain).
364 * https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2017
365 * defines the format of _MSC_VER and _MSC_FULL_VER. _MSC_FULL_VER
366 * is a decimal number of the form MMmmBBBBB, where MM is the compiler/
367 * toolchain major version, mm is the minor version, and BBBBB is the
368 * build. We break it down based on that.
370 #define COMPILER_MAJOR_VERSION (_MSC_FULL_VER / 10000000)
371 #define COMPILER_MINOR_VERSION ((_MSC_FULL_VER % 10000000) / 100000)
372 #define COMPILER_BUILD_NUMBER (_MSC_FULL_VER % 100000)
375 * From https://web.archive.org/web/20190125151548/https://blogs.msdn.microsoft.com/vcblog/2014/11/17/c111417-features-in-vs-2015-preview/
376 * Bakersfield: DevDiv's upper management determines the scheduling
377 * of new major versions. They also decided to increment the product
378 * version from 12 (for VS 2013) to 14 (for VS 2015). However, the
379 * C++ compiler's version incremented normally, from 18 to 19.
380 * (It's larger because the C++ compiler predates the "Visual" in
381 * Visual C++.)
383 * So the product version number is 5 less than the compiler version
384 * number.
386 #define VCPP_MAJOR_VERSION (COMPILER_MAJOR_VERSION - 5)
388 #if VCPP_MAJOR_VERSION == 14
390 * From https://devblogs.microsoft.com/cppblog/side-by-side-minor-version-msvc-toolsets-in-visual-studio-2017/
392 * We've been delivering improvements to Visual Studio 2017 more
393 * frequently than ever before. Since its first release in March
394 * we've released four major updates to VS2017 and are currently
395 * previewing the fifth update, VS2017 version 15.5.
397 * The MSVC toolset in VS2017 is built as a minor version update to
398 * the VS2015 compiler toolset. This minor version bump indicates
399 * that the VS2017 MSVC toolset is binary compatible with the VS2015
400 * MSVC toolset, enabling an easier upgrade for VS2015 users. Even
401 * though the MSVC compiler toolset in VS2017 delivers many new
402 * features and conformance improvements it is a minor version,
403 * compatible update from 14.00 in VS2015 to 14.10 in VS2017.
405 #if COMPILER_MINOR_VERSION < 10
406 #define VS_VERSION "2015"
407 #elif COMPILER_MINOR_VERSION < 20
408 #define VS_VERSION "2017"
409 #elif COMPILER_MINOR_VERSION < 30
410 #define VS_VERSION "2019"
411 #else
412 #define VS_VERSION "2022"
413 #endif
414 #else
416 * Add additional checks here, before the #else.
418 #define VS_VERSION "(unknown)"
419 #endif /* VCPP_MAJOR_VERSION */
422 * XXX - should we show the raw compiler version number, as is
423 * shown by "cl /?", which would be %d.%d.%d.%d with
424 * COMPILER_MAJOR_VERSION, COMPILER_MINOR_VERSION,
425 * COMPILER_BUILD_NUMBER, and _MSC_BUILD, the last of which is
426 * "the revision number element of the compiler's version number",
427 * which I guess is not to be confused with the build number,
428 * the _BUILD in the name nonwithstanding.
430 g_string_append_printf(str, "Microsoft Visual Studio " VS_VERSION " (VC++ %d.%d, build %d)",
431 VCPP_MAJOR_VERSION, COMPILER_MINOR_VERSION, COMPILER_BUILD_NUMBER);
432 #if defined(__clang__)
434 * See above.
436 g_string_append_printf(str, " clang/C2 %s and -fno-ms-compatibility",
437 __VERSION__);
438 #endif
439 #elif defined(__GNUC__) && defined(__VERSION__)
441 * Clang and llvm-gcc also define __GNUC__ and __VERSION__;
442 * distinguish between them.
444 #if defined(__clang__)
445 /* clang */
446 char *version; /* clang's version string has a trailing space. */
447 #if defined(__clang_version__)
448 version = g_strdup(__clang_version__);
449 g_string_append_printf(str, "Clang %s", g_strstrip(version));
450 #else
451 version = g_strdup(__VERSION__);
452 g_string_append_printf(str, "%s", g_strstrip(version));
453 #endif /* __clang_version__ */
454 g_free(version);
455 #elif defined(__llvm__)
456 /* llvm-gcc */
457 g_string_append_printf(str, "llvm-gcc %s", __VERSION__);
458 #else
459 /* boring old GCC */
460 g_string_append_printf(str, "GCC %s", __VERSION__);
461 #endif
462 #elif defined(__HP_aCC)
463 g_string_append_printf(str, "HP aCC %d", __HP_aCC);
464 #elif defined(__xlC__)
465 g_string_append_printf(str, "IBM XL C %d.%d",
466 (__xlC__ >> 8) & 0xFF, __xlC__ & 0xFF);
467 #ifdef __IBMC__
468 if ((__IBMC__ % 10) != 0)
469 g_string_append_printf(str, " patch %d", __IBMC__ % 10);
470 #endif /* __IBMC__ */
471 #elif defined(__INTEL_COMPILER)
472 g_string_append_printf(str, "Intel C %d.%d",
473 __INTEL_COMPILER / 100, (__INTEL_COMPILER / 10) % 10);
474 if ((__INTEL_COMPILER % 10) != 0)
475 g_string_append_printf(str, " patch %d", __INTEL_COMPILER % 10);
476 #ifdef __INTEL_COMPILER_BUILD_DATE
477 g_string_sprinta(str, ", compiler built %04d-%02d-%02d",
478 __INTEL_COMPILER_BUILD_DATE / 10000,
479 (__INTEL_COMPILER_BUILD_DATE / 100) % 100,
480 __INTEL_COMPILER_BUILD_DATE % 100);
481 #endif /* __INTEL_COMPILER_BUILD_DATE */
482 #elif defined(__SUNPRO_C)
483 g_string_append_printf(str, "Sun C %d.%d",
484 (__SUNPRO_C >> 8) & 0xF, (__SUNPRO_C >> 4) & 0xF);
485 if ((__SUNPRO_C & 0xF) != 0)
486 g_string_append_printf(str, " patch %d", __SUNPRO_C & 0xF);
487 #else
488 g_string_append(str, "unknown compiler");
489 #endif
490 g_string_append(str, "\n");
493 void
494 gather_pcre2_runtime_info(feature_list l)
496 /* From pcre2_api(3):
497 * The where argument should point to a buffer that is at least 24 code
498 * units long. (The exact length required can be found by calling
499 * pcre2_config() with where set to NULL.)
501 * The API should accept a buffer size as additional input. We could opt for a
502 * stack buffer size greater than 24 but let's just go with the weirdness...
504 int size;
505 char *buf_pcre2;
507 size = pcre2_config(PCRE2_CONFIG_VERSION, NULL);
508 if (size < 0 || size > 255) {
509 without_feature(l, "PCRE2 (error querying)");
510 return;
512 buf_pcre2 = g_malloc(size + 1);
513 pcre2_config(PCRE2_CONFIG_VERSION, buf_pcre2);
514 buf_pcre2[size] = '\0';
515 with_feature(l, "PCRE2 %s", buf_pcre2);
516 g_free(buf_pcre2);
519 void
520 gather_zlib_runtime_info(feature_list l)
522 (void)l;
523 #if defined(HAVE_ZLIB) && !defined(_WIN32)
524 with_feature(l, "zlib %s", zlibVersion());
525 #endif
529 * Get various library run-time versions, and the OS version, and append
530 * them to the specified GString.
532 * "additional_info" is called at the end to append any additional
533 * information; this is required in order to, for example, put the
534 * libcap information at the end of the string, as we currently
535 * don't use libcap in TShark.
537 GString *
538 get_runtime_version_info(gather_feature_func gather_runtime)
540 GString *str;
541 gchar *lc;
542 GList *l = NULL, *with_list = NULL, *without_list = NULL;
544 str = g_string_new("Runtime info:\n OS: ");
545 get_os_version_info(str);
547 /* CPU Info */
548 g_string_append(str, "\n CPU: ");
549 get_cpu_info(str);
551 /* Get info about installed memory */
552 g_string_append(str, "\n Memory: ");
553 get_mem_info(str);
555 g_string_append_printf(str, "\n GLib: %u.%u.%u\n",
556 glib_major_version, glib_minor_version, glib_micro_version);
558 * Display LC_CTYPE as a relevant, portable and sort of representative
559 * locale configuration without being exceedingly verbose and including
560 * the whole shebang of categories using LC_ALL.
562 if ((lc = setlocale(LC_CTYPE, NULL)) != NULL) {
563 g_string_append_printf(str, " Locale: LC_TYPE=%s\n", lc);
565 #ifdef HAVE_PLUGINS
566 if (g_module_supported()) {
567 g_string_append_printf(str, " Plugins: supported, %d loaded\n", plugins_get_count());
569 else {
570 g_string_append(str, " Plugins: not supported by platform\n");
572 #else
573 g_string_append(str, " Plugins: disabled at compile time\n");
574 #endif
576 if (gather_runtime != NULL) {
577 gather_runtime(&l);
580 sort_features(&l);
581 separate_features(&l, &with_list, &without_list);
582 free_features(&l);
584 g_string_append(str, " With:\n");
585 features_to_columns(&with_list, str);
586 free_features(&with_list);
587 if (without_list != NULL) {
588 g_string_append(str, " Without:\n");
589 features_to_columns(&without_list, str);
590 free_features(&without_list);
593 end_string(str);
594 return str;
598 * Return a version number string for Wireshark, including, for builds
599 * from a tree checked out from Wireshark's version control system,
600 * something identifying what version was checked out.
602 const char *
603 get_ws_vcs_version_info(void)
605 #ifdef VCS_VERSION
606 return VERSION " (" VCS_VERSION ")";
607 #else
608 return VERSION;
609 #endif
612 const char *
613 get_ss_vcs_version_info(void)
615 #ifdef VCS_COMMIT_ID
616 return LOG_VERSION " (" VCS_NUM_COMMITS "-" VCS_COMMIT_ID ")";
617 #else
618 return LOG_VERSION;
619 #endif
622 const char *
623 get_ws_vcs_version_info_short(void)
625 #ifdef VCS_VERSION
626 return VCS_VERSION;
627 #else
628 return VERSION;
629 #endif
632 void
633 get_ws_version_number(int *major, int *minor, int *micro)
635 if (major)
636 *major = VERSION_MAJOR;
637 if (minor)
638 *minor = VERSION_MINOR;
639 if (micro)
640 *micro = VERSION_MICRO;
643 void
644 show_version(void)
646 printf("%s.\n\n"
647 "%s"
648 "%s\n"
649 "%s\n"
650 "%s",
651 appname_with_version,
652 copyright_info,
653 license_info,
654 comp_info,
655 runtime_info);
658 void
659 show_help_header(const char *description)
661 printf("%s\n", appname_with_version);
662 if (description) {
663 printf("%s\n", description);
664 printf("See https://www.wireshark.org for more information.\n");
669 * Get copyright information.
671 const char *
672 get_copyright_info(void)
674 return
675 "Copyright 1998-2024 Gerald Combs <gerald@wireshark.org> and contributors.";
678 const char *
679 get_license_info_short(void)
681 return
682 "Licensed under the terms of the GNU General Public License (version 2 or later). "
683 "This is free software; see the file named COPYING in the distribution. "
684 "There is NO WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.";
687 const char *
688 get_license_info(void)
690 return
691 "This program is free software: you can redistribute it and/or modify "
692 "it under the terms of the GNU General Public License as published by "
693 "the Free Software Foundation, either version 2 of the License, or "
694 "(at your option) any later version. This program is distributed in the "
695 "hope that it will be useful, but WITHOUT ANY WARRANTY; without even "
696 "the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. "
697 "See the GNU General Public License for more details.";
701 * Editor modelines - https://www.wireshark.org/tools/modelines.html
703 * Local variables:
704 * c-basic-offset: 8
705 * tab-width: 8
706 * indent-tabs-mode: t
707 * End:
709 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
710 * :indentSize=8:tabSize=8:noTabs=false: