aboutsummaryrefslogtreecommitdiff
path: root/src/bug_report.cpp
blob: 5249cc3a4ceb4accaec89d90aa33feefc93eb434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
	Gather and print platform and version info to help with reporting Odin bugs.
*/

#if !defined(GB_COMPILER_MSVC)
	#if defined(GB_CPU_X86)
		#include <cpuid.h>
	#endif
#endif

#if defined(GB_SYSTEM_LINUX)
	#include <sys/utsname.h>
	#include <sys/sysinfo.h>
#endif

#if defined(GB_SYSTEM_OSX)
	#include <sys/sysctl.h>
#endif

/*
	NOTE(Jeroen): This prints the Windows product edition only, to be called from `print_platform_details`.
*/
#if defined(GB_SYSTEM_WINDOWS)
void report_windows_product_type(DWORD ProductType) {
	switch (ProductType) {
	case PRODUCT_ULTIMATE:
		gb_printf("Ultimate");
		break;

	case PRODUCT_HOME_BASIC:
		gb_printf("Home Basic");
		break;

	case PRODUCT_HOME_PREMIUM:
		gb_printf("Home Premium");
		break;

	case PRODUCT_ENTERPRISE:
		gb_printf("Enterprise");
		break;

	case PRODUCT_CORE:
		gb_printf("Home Basic");
		break;

	case PRODUCT_HOME_BASIC_N:
		gb_printf("Home Basic N");
		break;

	case PRODUCT_EDUCATION:
		gb_printf("Education");
		break;

	case PRODUCT_EDUCATION_N:
		gb_printf("Education N");
		break;

	case PRODUCT_BUSINESS:
		gb_printf("Business");
		break;

	case PRODUCT_STANDARD_SERVER:
		gb_printf("Standard Server");
		break;

	case PRODUCT_DATACENTER_SERVER:
		gb_printf("Datacenter");
		break;

	case PRODUCT_SMALLBUSINESS_SERVER:
		gb_printf("Windows Small Business Server");
		break;

	case PRODUCT_ENTERPRISE_SERVER:
		gb_printf("Enterprise Server");
		break;

	case PRODUCT_STARTER:
		gb_printf("Starter");
		break;

	case PRODUCT_DATACENTER_SERVER_CORE:
		gb_printf("Datacenter Server Core");
		break;

	case PRODUCT_STANDARD_SERVER_CORE:
		gb_printf("Server Standard Core");
		break;

	case PRODUCT_ENTERPRISE_SERVER_CORE:
		gb_printf("Enterprise Server Core");
		break;

	case PRODUCT_BUSINESS_N:
		gb_printf("Business N");
		break;

	case PRODUCT_HOME_SERVER:
		gb_printf("Home Server");
		break;

	case PRODUCT_SERVER_FOR_SMALLBUSINESS:
		gb_printf("Windows Server 2008 for Windows Essential Server Solutions");
		break;

	case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
		gb_printf("Small Business Server Premium");
		break;

	case PRODUCT_HOME_PREMIUM_N:
		gb_printf("Home Premium N");
		break;

	case PRODUCT_ENTERPRISE_N:
		gb_printf("Enterprise N");
		break;

	case PRODUCT_ULTIMATE_N:
		gb_printf("Ultimate N");
		break;

	case PRODUCT_HYPERV:
		gb_printf("HyperV");
		break;

	case PRODUCT_STARTER_N:
		gb_printf("Starter N");
		break;

	case PRODUCT_PROFESSIONAL:
		gb_printf("Professional");
		break;

	case PRODUCT_PROFESSIONAL_N:
		gb_printf("Professional N");
		break;

	case PRODUCT_UNLICENSED:
		gb_printf("Unlicensed");
		break;

	default:
		gb_printf("Unknown Edition (%08x)", ProductType);
	}
}
#endif

void odin_cpuid(int leaf, int result[]) {
	#if defined(GB_COMPILER_MSVC)
		__cpuid(result, leaf);
	#else
		__get_cpuid(leaf, (unsigned int*)&result[0], (unsigned int*)&result[1], (unsigned int*)&result[2], (unsigned int*)&result[3]);
	#endif
}

void report_cpu_info() {
	gb_printf("\tCPU:  ");

	#if defined(GB_CPU_X86)

	/*
		Get extended leaf info
	*/
	int cpu[4];

	odin_cpuid(0x80000000, &cpu[0]);
	int number_of_extended_ids = cpu[0];

	int brand[0x12] = {};

	/*
		Read CPU brand if supported.
	*/
	if (number_of_extended_ids >= 0x80000004) {
		odin_cpuid(0x80000002, &brand[0]);
		odin_cpuid(0x80000003, &brand[4]);
		odin_cpuid(0x80000004, &brand[8]);

		/*
			Some CPUs like `      Intel(R) Xeon(R) CPU E5-1650 v2 @ 3.50GHz` may include leading spaces. Trim them.
		*/
		char * brand_name = (char *)&brand[0];
		for (; brand_name[0] == ' '; brand_name++) {}

		gb_printf("%s\n", brand_name);
	} else {
		gb_printf("Unable to retrieve.\n");
	}

	#elif defined(GB_CPU_ARM)
		/*
			TODO(Jeroen): On *nix, perhaps query `/proc/cpuinfo`.
		*/
		#if defined(GB_ARCH_64_BIT)
			gb_printf("ARM64\n");
		#else
			gb_printf("ARM\n");
		#endif
	#else
		gb_printf("Unknown\n");
	#endif
}

/*
	Report the amount of installed RAM.
*/
void report_ram_info() {

	gb_printf("\tRAM:  ");

	#if defined(GB_SYSTEM_WINDOWS)
		MEMORYSTATUSEX statex;
		statex.dwLength = sizeof(statex);
		GlobalMemoryStatusEx (&statex);

		gb_printf("%lld MiB\n", statex.ullTotalPhys / gb_megabytes(1));

	#elif defined(GB_SYSTEM_LINUX)
		/*
			Retrieve RAM info using `sysinfo()`, 
		*/
		struct sysinfo info;
		int result = sysinfo(&info);

		if (result == 0x0) {
			gb_printf("%lu MiB\n", info.totalram * info.mem_unit / gb_megabytes(1));
		} else {
			gb_printf("Unknown.\n");
		}
	#elif defined(GB_SYSTEM_OSX)
		uint64_t ram_amount;
		size_t   val_size = sizeof(ram_amount);

		int sysctls[] = { CTL_HW, HW_MEMSIZE };
		if (sysctl(sysctls, 2, &ram_amount, &val_size, NULL, 0) != -1) {
			gb_printf("%lld MiB\n", ram_amount / gb_megabytes(1));
		}
	#else
		gb_printf("Unknown.\n");
	#endif
}

/*
	NOTE(Jeroen): `odin report` prints some system information for easier bug reporting.
*/
void print_bug_report_help() {
	gb_printf("Where to find more information and get into contact when you encounter a bug:\n\n");
	gb_printf("\tWebsite: https://odin-lang.org\n");
	gb_printf("\tGitHub:  https://github.com/odin-lang/Odin/issues\n");
	/*
		Uncomment and update URL once we have a Discord vanity URL. For now people can get here from the site.
		gb_printf("\tDiscord: https://discord.com/invite/sVBPHEv\n");
	*/
	gb_printf("\n\n");

	gb_printf("Useful information to add to a bug report:\n\n");

	gb_printf("\tOdin: %.*s", LIT(ODIN_VERSION));

	#ifdef NIGHTLY
	gb_printf("-nightly");
	#endif

	#ifdef GIT_SHA
	gb_printf(":%s", GIT_SHA);
	#endif

	gb_printf("\n");

	/*
		Print OS information.
	*/
	gb_printf("\tOS:   ");

	#if defined(GB_SYSTEM_WINDOWS)
		/*
			NOTE(Jeroen): 
				`GetVersionEx`  will return 6.2 for Windows 10 unless the program is manifested for Windows 10.
				`RtlGetVersion` will return the true version.

				Rather than include the WinDDK, we ask the kernel directly.

				`HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion` is for the minor build version (Update Build Release)

		*/
		OSVERSIONINFOEXW osvi;
		ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXW));
		osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);

		typedef NTSTATUS (WINAPI* RtlGetVersionPtr)(OSVERSIONINFOW*);
		typedef BOOL (WINAPI* GetProductInfoPtr)(DWORD dwOSMajorVersion, DWORD dwOSMinorVersion, DWORD dwSpMajorVersion, DWORD dwSpMinorVersion, PDWORD pdwReturnedProductType);

		// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlgetversion
		RtlGetVersionPtr  RtlGetVersion  =  (RtlGetVersionPtr)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")), "RtlGetVersion");
		// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getproductinfo
		GetProductInfoPtr GetProductInfo = (GetProductInfoPtr)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");

		NTSTATUS status  = {};
		DWORD ProductType = {};
		if (RtlGetVersion != nullptr) {
			status = RtlGetVersion((OSVERSIONINFOW*)&osvi);
		}

		if (RtlGetVersion == nullptr || status != 0x0) {
			gb_printf("Windows (Unknown Version)");
		} else {
			if (GetProductInfo != nullptr) {
				GetProductInfo(osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.wServicePackMajor, osvi.wServicePackMinor, &ProductType);
			}

			if (false) {
				gb_printf("dwMajorVersion:    %d\n", osvi.dwMajorVersion);
				gb_printf("dwMinorVersion:    %d\n", osvi.dwMinorVersion);
				gb_printf("dwBuildNumber:     %d\n", osvi.dwBuildNumber);
				gb_printf("dwPlatformId:      %d\n", osvi.dwPlatformId);
				gb_printf("wServicePackMajor: %d\n", osvi.wServicePackMajor);
				gb_printf("wServicePackMinor: %d\n", osvi.wServicePackMinor);
				gb_printf("wSuiteMask:        %d\n", osvi.wSuiteMask);
				gb_printf("wProductType:      %d\n", osvi.wProductType);
			}

			gb_printf("Windows ");

			switch (osvi.dwMajorVersion) {
			case 10:
				/*
					Windows 10 (Pro), Windows 2016 Server, Windows 2019 Server, Windows 2022 Server
				*/
				switch (osvi.wProductType) {
				case VER_NT_WORKSTATION: // Workstation
					if (osvi.dwBuildNumber < 22000) {
						gb_printf("10 ");
					} else {
						gb_printf("11 ");
					}
					
					report_windows_product_type(ProductType);

					break;
				default: // Server or Domain Controller
					switch(osvi.dwBuildNumber) {
					case 14393:
						gb_printf("2016 Server");
						break;
					case 17763:
						gb_printf("2019 Server");
						break;
					case 20348:
						gb_printf("2022 Server");
						break;
					default:
						gb_printf("Unknown Server");
						break;
					}
				}
				break;
			case 6:
				switch (osvi.dwMinorVersion) {
					case 0:
						switch (osvi.wProductType) {
							case VER_NT_WORKSTATION:
								gb_printf("Windows Vista ");
								report_windows_product_type(ProductType);
								break;
							case 3:
								gb_printf("Windows Server 2008");
								break;
						}
						break;

					case 1:
						switch (osvi.wProductType) {
							case VER_NT_WORKSTATION:
								gb_printf("Windows 7 ");
								report_windows_product_type(ProductType);
								break;
							case 3:
								gb_printf("Windows Server 2008 R2");
								break;
						}
						break;
					case 2:
						switch (osvi.wProductType) {
							case VER_NT_WORKSTATION:
								gb_printf("Windows 8 ");
								report_windows_product_type(ProductType);
								break;
							case 3:
								gb_printf("Windows Server 2012");
								break;
						}
						break;
					case 3:
						switch (osvi.wProductType) {
							case VER_NT_WORKSTATION:
								gb_printf("Windows 8.1 ");
								report_windows_product_type(ProductType);
								break;
							case 3:
								gb_printf("Windows Server 2012 R2");
								break;
						}
						break;
				}
				break;
			case 5:
				switch (osvi.dwMinorVersion) {
					case 0:
						gb_printf("Windows 2000");
						break;
					case 1:
						gb_printf("Windows XP");
						break;
					case 2:
						gb_printf("Windows Server 2003");
						break;
				}
				break;
			default:
				break;
			}

			/*
				Grab Windows DisplayVersion (like 20H02)
			*/
			LPDWORD ValueType = {};
			DWORD   UBR;
			char    DisplayVersion[256];
			DWORD   ValueSize = 256;

			status = RegGetValue(
				HKEY_LOCAL_MACHINE,
				TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"),
				TEXT("DisplayVersion"),
				RRF_RT_REG_SZ,
				ValueType,
				&DisplayVersion,
				&ValueSize
			);

			if (status == 0x0) {
				gb_printf(" (version: %s)", &DisplayVersion);
			}

			/*
				Now print build number.
			*/
			gb_printf(", build %d", osvi.dwBuildNumber);

			ValueSize = sizeof(UBR);
			status = RegGetValue(
				HKEY_LOCAL_MACHINE,
				TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"),
				TEXT("UBR"),
				RRF_RT_REG_DWORD,
				ValueType,
				&UBR,
				&ValueSize
			);

			if (status == 0x0) {
				gb_printf(".%d", UBR);
			}
			gb_printf("\n");
		}

	#elif defined(GB_SYSTEM_LINUX)
		/*
			Try to parse `/usr/lib/os-release` for `PRETTY_NAME="Ubuntu 20.04.3 LTS`
		*/
		gbAllocator a = heap_allocator();

		gbFileContents release = gb_file_read_contents(a, 1, "/usr/lib/os-release");
		defer (gb_file_free_contents(&release));

		b32 found = 0;
		if (release.size) {
			char *start        = (char *)release.data;
			char *end          = (char *)release.data + release.size;
			const char *needle = "PRETTY_NAME=\"";
			isize needle_len   = gb_strlen((needle));
		
			char *c = start;
			for (; c < end; c++) {
				if (gb_strncmp(c, needle, needle_len) == 0) {
					found = 1;
					start = c + needle_len;
					break;
				}
			}

			if (found) {
				for (c = start; c < end; c++) {
					if (*c == '"') {
						// Found the closing quote. Replace it with \0
						*c = 0;
						gb_printf("%s", (char *)start);
						break;
					} else if (*c == '\n') {
						found = 0;
					}
				}
			}
		}

		if (!found) {
			gb_printf("Unknown Linux Distro");
		}

		/*
			Print kernel info using `uname()` syscall, https://linux.die.net/man/2/uname
		*/
		char buffer[1024];
		uname((struct utsname *)&buffer[0]);

		struct utsname *info;
		info = (struct utsname *)&buffer[0];

		gb_printf(", %s %s\n", info->sysname, info->release);

	#elif defined(GB_SYSTEM_OSX)
		b32 found = 1;
		uint32_t major, minor;

		#define osx_version_buffer 100
		char buffer[osx_version_buffer];
		size_t buffer_size = osx_version_buffer - 1;
		#undef osx_version_buffer

		int sysctls[] = { CTL_KERN, KERN_OSRELEASE };
		if (sysctl(sysctls, 2, buffer, &buffer_size, NULL, 0) == -1) {
			found = 0;
		} else {
			if (sscanf(buffer, "%u.%u", &major, &minor) != 2) {
				found = 0;
			}
		}

		if (found) {
			switch (major) {
			case 21:
				/*
					macOS Big Sur and newer
				*/
				major -= 9;

				gb_printf("macOS 12.0.%d Monterey\n", minor);

				break;
			case 20:
				/*
					macOS Big Sur and newer
				*/
				major -= 9;

				gb_printf("macOS 11.%d Big Sur\n", minor);

				break;
			case 14:
				/*
					macOS 10.1.1 and newer
				*/
				major -= 4;

				switch (minor) {
				case 1:
					gb_printf("macOS Puma 10.1\n");
					break;

				case 2:
					gb_printf("macOS Jaguar 10.2\n");
					break;

				case 3:
					gb_printf("macOS Panther 10.3\n");
					break;

				case 4:
					gb_printf("macOS Tiger 10.4\n");
					break;

				case 5:
					gb_printf("macOS Leopard 10.5\n");
					break;

				case 6:
					gb_printf("macOS Snow Leopard 10.6\n");
					break;

				case 7:
					gb_printf("macOS Lion 10.7\n");
					break;

				case 8:
					gb_printf("macOS Mountain Lion 10.8\n");
					break;

				case 9:
					gb_printf("macOS Mavericks 10.9\n");
					break;

				case 10:
					gb_printf("macOS Yosemite 10.10\n");
					break;

				case 11:
					gb_printf("macOS El Capitan 10.11\n");
					break;

				case 12:
					gb_printf("macOS Sierra 10.12\n");
					break;

				case 13:
					gb_printf("macOS High Sierra 10.13\n");
					break;

				case 14:
					gb_printf("macOS Mojave 10.14\n");
					break;

				case 15:
					gb_printf("macOS Catalina 10.15\n");
					break;

				default:
					gb_printf("macOS %d.%d\n", major, minor);
					break;
				}

				break;
			default:
				gb_printf("macOS Unknown (kernel version %d.%d)\n", major, minor);
				break;
			}
		} else {
			gb_printf("macOS: Unknown\n");
		}			
	#else
		gb_printf("Unknown\n");

	#endif

	/*
		Now print CPU info.
	*/
	report_cpu_info();

	/*
		And RAM info.
	*/
	report_ram_info();
}