Merge pull request #4496 from Janrupf/develop

NOISSUE Attempt to make exit codes more useful on Windows
This commit is contained in:
Petr Mrázek 2022-05-20 20:47:41 +02:00 committed by GitHub
commit 66e165f4b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 2704 additions and 7 deletions

View File

@ -2,6 +2,8 @@
#include "MessageLevel.h"
#include <QDebug>
#include <sys.h>
LoggedProcess::LoggedProcess(QObject *parent) : QProcess(parent)
{
// QProcess has a strange interface... let's map a lot of those into a few.
@ -65,7 +67,7 @@ void LoggedProcess::on_exit(int exit_code, QProcess::ExitStatus status)
if (status == QProcess::NormalExit)
{
//: Message displayed on instance exit
emit log({tr("Process exited with code %1.").arg(exit_code)}, MessageLevel::Launcher);
emit log({tr("Process exited with code %1 (0x%2).").arg(exit_code).arg(exit_code, 0, 16)}, MessageLevel::Launcher);
changeState(LoggedProcess::Finished);
}
else
@ -74,9 +76,38 @@ void LoggedProcess::on_exit(int exit_code, QProcess::ExitStatus status)
if(exit_code == -1)
emit log({tr("Process crashed.")}, MessageLevel::Launcher);
else
emit log({tr("Process crashed with exitcode %1.").arg(exit_code)}, MessageLevel::Launcher);
emit log({tr("Process crashed with exitcode %1 (0x%2).").arg(exit_code).arg(exit_code, 0, 16)}, MessageLevel::Launcher);
changeState(LoggedProcess::Crashed);
}
// Filter out some exit codes, which would only result in erroneous output
// -1, 0, 1 and 255 are usually program generated and don't aid much in debugging
if((exit_code < -1 || exit_code > 1) && (exit_code != 255))
{
// Gross hack for preserving the **exact bit pattern**, we need to "cast" while ignoring the sign bit
unsigned int u_exit_code = *((unsigned int *) &exit_code);
std::string statusName;
std::string statusDescription;
bool hasNameOrDescription = Sys::lookupSystemStatusCode(u_exit_code, statusName, statusDescription);
if(hasNameOrDescription)
{
emit log({tr("Below is an analysis of the exit code. THIS MAY BE INCORRECT AND SHOULD BE TAKEN WITH A GRAIN OF SALT!")}, MessageLevel::Launcher);
if(!statusName.empty())
{
emit log({tr("System exit code name: %1").arg(QString::fromStdString(statusName))}, MessageLevel::Launcher);
}
if(!statusDescription.empty())
{
emit log({tr("System exit code description: %1").arg(QString::fromStdString(statusDescription))}, MessageLevel::Launcher);
}
}
}
emit log({tr("Please not that usually neither exit code nor its description are enough to diagnose issues!")}, MessageLevel::Launcher);
emit log({tr("Always upload the entire log and not just the exit code.")}, MessageLevel::Launcher);
}
else
{

View File

@ -3,13 +3,16 @@ project(systeminfo)
find_package(Qt5Core)
set(systeminfo_SOURCES
include/sys.h
include/distroutils.h
src/distroutils.cpp
include/sys.h
include/distroutils.h
src/distroutils.cpp
)
set(systeminfo_INCLUDE_DIRS
${CMAKE_CURRENT_LIST_DIR}/include)
if (WIN32)
list(APPEND systeminfo_SOURCES src/sys_win32.cpp)
list(APPEND systeminfo_SOURCES src/sys_win32.cpp src/ntstatus/NtStatusNames.cpp)
elseif (UNIX)
if(APPLE)
list(APPEND systeminfo_SOURCES src/sys_apple.cpp)
@ -20,7 +23,7 @@ endif()
add_library(systeminfo STATIC ${systeminfo_SOURCES})
target_link_libraries(systeminfo Qt5::Core Qt5::Gui Qt5::Network)
target_include_directories(systeminfo PUBLIC include)
target_include_directories(systeminfo PUBLIC ${systeminfo_INCLUDE_DIRS})
include (UnitTest)
add_unit_test(sys

View File

@ -0,0 +1,14 @@
// AUTO GENERATED FILE, DO NOT EDIT!
// This file has been generated by nt-status-gen from https://github.com/Janrupf/nt-status-gen
//
// Used compiler: MSVC version 19.28.29337.0
// Targeted OS: Windows version 10.0.19044
#pragma once
#include <cstdint>
#include <string>
namespace NtStatus {
bool lookupNtStatusCodeName(uint64_t code, std::string &nameOut);
}

View File

@ -0,0 +1,2 @@
Source files in this directory have been generated using
https://github.com/Janrupf/nt-status-gen

View File

@ -60,4 +60,6 @@ uint64_t getSystemRam();
bool isSystem64bit();
bool isCPU64bit();
bool lookupSystemStatusCode(uint64_t code, std::string &name, std::string &description);
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
Source files in this directory have been generated using
https://github.com/Janrupf/nt-status-gen

View File

@ -72,3 +72,8 @@ Sys::DistributionInfo Sys::getDistributionInfo()
DistributionInfo result;
return result;
}
bool Sys::lookupSystemStatusCode(uint64_t code, std::string &name, std::string &description)
{
return false;
}

View File

@ -116,3 +116,8 @@ Sys::DistributionInfo Sys::getDistributionInfo()
}
return result;
}
bool Sys::lookupSystemStatusCode(uint64_t code, std::string &name, std::string &description)
{
return false;
}

View File

@ -1,6 +1,9 @@
#include "sys.h"
#include <windows.h>
#include <QDebug>
#include "ntstatus/NtStatusNames.hpp"
Sys::KernelInfo Sys::getKernelInfo()
{
@ -54,3 +57,40 @@ Sys::DistributionInfo Sys::getDistributionInfo()
DistributionInfo result;
return result;
}
bool Sys::lookupSystemStatusCode(uint64_t code, std::string &name, std::string &description)
{
bool hasCodeName = NtStatus::lookupNtStatusCodeName(code, name);
PSTR messageBuffer = nullptr;
HMODULE ntdll = GetModuleHandleA("ntdll.dll");
if(!ntdll)
{
// ???
qWarning() << "GetModuleHandleA returned nullptr for ntdll.dll";
return false;
}
auto messageSize = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS,
ntdll,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<PSTR>(&messageBuffer),
0,
nullptr
);
bool hasDescription = messageSize > 0;
if(hasDescription)
{
description = std::string(messageBuffer, messageSize);
}
if(messageBuffer)
{
LocalFree(messageBuffer);
}
return hasCodeName || hasDescription;
}