[common/c-host] move agnostic code into common library

This commit is contained in:
Geoffrey McRae
2020-01-02 22:21:42 +11:00
parent 491ffc3576
commit 0c6ff6822d
26 changed files with 502 additions and 378 deletions

View File

@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.0)
project(lg_common_platform LANGUAGES C)
if (UNIX)
add_subdirectory("linux")
elseif(WIN32)
add_subdirectory("windows")
endif()
add_library(lg_common_platform INTERFACE)
target_link_libraries(lg_common_platform INTERFACE lg_common_platform_code)

View File

@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.0)
project(lg_common_platform_code LANGUAGES C)
include_directories(
${PROJECT_SOURCE_DIR}/include
)
add_library(lg_common_platform_code STATIC
crash.c
sysinfo.c
)

View File

@@ -0,0 +1,19 @@
#ifndef PACKAGE
#define PACKAGE
#ifndef PACKAGE_VERSION
#define PACKAGE_VERSION
#include <bfd.h>
#undef PACKAGE_VERSION
#else
#include <bfd.h>
#endif
#undef PACKAGE
#else
#ifndef PACKAGE_VERSION
#define PACKAGE_VERSION
#include <bfd.h>
#undef PACKAGE_VERSION
#else
#include <bfd.h>
#endif
#endif

View File

@@ -0,0 +1,233 @@
/*
KVMGFX Client - A KVM Client for VGA Passthrough
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _GNU_SOURCE
#include "common/crash.h"
#include "common/debug.h"
#if defined(ENABLE_BACKTRACE)
#include <execinfo.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ucontext.h>
#include <unistd.h>
#include <inttypes.h>
#include <limits.h>
#include <link.h>
#include "bfd.inc.h"
struct range
{
intptr_t start, end;
};
struct crash
{
char * exe;
struct range * ranges;
int rangeCount;
bfd * fd;
asection * section;
asymbol ** syms;
long symCount;
};
static struct crash crash = {0};
static void load_symbols()
{
bfd_init();
crash.fd = bfd_openr(crash.exe, NULL);
if (!crash.fd)
{
DEBUG_ERROR("failed to open '%s'", crash.exe);
return;
}
crash.fd->flags |= BFD_DECOMPRESS;
char **matching;
if (!bfd_check_format_matches(crash.fd, bfd_object, &matching))
{
DEBUG_ERROR("executable is not a bfd_object");
return;
}
crash.section = bfd_get_section_by_name(crash.fd, ".text");
if (!crash.section)
{
DEBUG_ERROR("failed to find .text section");
return;
}
if ((bfd_get_file_flags(crash.fd) & HAS_SYMS) == 0)
{
DEBUG_ERROR("executable '%s' has no symbols", crash.exe);
return;
}
long storage = bfd_get_symtab_upper_bound(crash.fd);
crash.syms = (asymbol **)malloc(storage);
crash.symCount = bfd_canonicalize_symtab(crash.fd, crash.syms);
if (crash.symCount < 0)
{
DEBUG_ERROR("failed to get the symbol count");
return;
}
}
static bool lookup_address(bfd_vma pc, const char ** filename, const char ** function, unsigned int * line, unsigned int * discriminator)
{
if ((bfd_get_section_flags(crash.fd, crash.section) & SEC_ALLOC) == 0)
return false;
bfd_size_type size = bfd_get_section_size(crash.section);
if (pc >= size)
return false;
if (!bfd_find_nearest_line_discriminator(
crash.fd,
crash.section,
crash.syms,
pc,
filename,
function,
line,
discriminator
))
return false;
if (!*filename)
return false;
return true;
}
static void cleanup()
{
if (crash.syms)
free(crash.syms);
if (crash.fd)
bfd_close(crash.fd);
if (crash.ranges)
free(crash.ranges);
if (crash.exe)
free(crash.exe);
}
static int dl_iterate_phdr_callback(struct dl_phdr_info * info, size_t size, void * data)
{
// we are not a module, and as such we don't have a name
if (strlen(info->dlpi_name) != 0)
return 0;
size_t ttl = 0;
for(int i = 0; i < info->dlpi_phnum; ++i)
{
const ElfW(Phdr) hdr = info->dlpi_phdr[i];
if (hdr.p_type == PT_LOAD && (hdr.p_flags & PF_X) == PF_X)
ttl += hdr.p_memsz;
}
crash.ranges = realloc(crash.ranges, sizeof(struct range) * (crash.rangeCount + 1));
crash.ranges[crash.rangeCount].start = info->dlpi_addr;
crash.ranges[crash.rangeCount].end = info->dlpi_addr + ttl;
++crash.rangeCount;
return 0;
}
static void crit_err_hdlr(int sig_num, siginfo_t * info, void * ucontext)
{
void * array[50];
char ** messages;
int size, i;
dl_iterate_phdr(dl_iterate_phdr_callback, NULL);
load_symbols();
DEBUG_ERROR("==== FATAL CRASH (" BUILD_VERSION ") ====");
DEBUG_ERROR("signal %d (%s), address is %p", sig_num, strsignal(sig_num), info->si_addr);
size = backtrace(array, 50);
messages = backtrace_symbols(array, size);
for (i = 2; i < size && messages != NULL; ++i)
{
intptr_t base = -1;
for(int c = 0; c < crash.rangeCount; ++c)
{
if ((intptr_t)array[i] >= crash.ranges[c].start && (intptr_t)array[i] < crash.ranges[c].end)
{
base = crash.ranges[c].start + crash.section->vma;
break;
}
}
if (base != -1)
{
const char * filename, * function;
unsigned int line, discriminator;
if (lookup_address((intptr_t)array[i] - base, &filename, &function, &line, &discriminator))
{
DEBUG_ERROR("[trace]: (%d) %s:%u (%s)", i - 2, filename, line, function);
continue;
}
}
DEBUG_ERROR("[trace]: (%d) %s", i - 2, messages[i]);
}
free(messages);
cleanup();
exit(EXIT_FAILURE);
}
bool installCrashHandler(const char * exe)
{
struct sigaction sigact = { 0 };
crash.exe = realpath(exe, NULL);
sigact.sa_sigaction = crit_err_hdlr;
sigact.sa_flags = SA_RESTART | SA_SIGINFO;
if (sigaction(SIGSEGV, &sigact, (struct sigaction *)NULL) != 0)
{
DEBUG_ERROR("Error setting signal handler for %d (%s)", SIGSEGV, strsignal(SIGSEGV));
return false;
}
return true;
}
#else //ENABLE_BACKTRACE
bool installCrashHandler(const char * exe)
{
return true;
}
#endif

View File

@@ -0,0 +1,64 @@
/*
KVMGFX Client - A KVM Client for VGA Passthrough
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <GL/glx.h>
int sysinfo_gfx_max_multisample()
{
Display * dpy = XOpenDisplay(NULL);
if (!dpy)
return -1;
XVisualInfo queryTemplate;
queryTemplate.screen = 0;
int visualCount;
int maxSamples = -1;
XVisualInfo * visuals = XGetVisualInfo(dpy, VisualScreenMask, &queryTemplate, &visualCount);
for (int i = 0; i < visualCount; i++)
{
XVisualInfo * visual = &visuals[i];
int res, supportsGL;
// Some GLX visuals do not use GL, and these must be ignored in our search.
if ((res = glXGetConfig(dpy, visual, GLX_USE_GL, &supportsGL)) != 0 || !supportsGL)
continue;
int sampleBuffers, samples;
if ((res = glXGetConfig(dpy, visual, GLX_SAMPLE_BUFFERS, &sampleBuffers)) != 0)
continue;
// Will be 1 if this visual supports multisampling
if (sampleBuffers != 1)
continue;
if ((res = glXGetConfig(dpy, visual, GLX_SAMPLES, &samples)) != 0)
continue;
// Track the largest number of samples supported
if (samples > maxSamples)
maxSamples = samples;
}
XCloseDisplay(dpy);
return maxSamples;
}

View File

@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.0)
project(lg_common_platform_code LANGUAGES C)
include_directories(
${PROJECT_SOURCE_DIR}/include
)
add_library(lg_common_platform_code STATIC
crash.c
sysinfo.c
thread.c
event.c
windebug.c
)

View File

@@ -0,0 +1,26 @@
/*
KVMGFX Client - A KVM Client for VGA Passthrough
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common/crash.h"
bool installCrashHandler(const char * exe)
{
//TODO
return true;
}

View File

@@ -0,0 +1,221 @@
/*
Looking Glass - KVM FrameRelay (KVMFR) Client
Copyright (C) 2017-2020 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common/event.h"
#include "common/windebug.h"
#include "common/time.h"
#include <windows.h>
struct LGEvent
{
volatile int lock;
bool reset;
HANDLE handle;
bool wrapped;
unsigned int msSpinTime;
volatile bool signaled;
};
LGEvent * lgCreateEvent(bool autoReset, unsigned int msSpinTime)
{
LGEvent * event = (LGEvent *)malloc(sizeof(LGEvent));
if (!event)
{
DEBUG_ERROR("out of ram");
return NULL;
}
event->lock = 0;
event->reset = autoReset;
event->handle = CreateEvent(NULL, autoReset ? FALSE : TRUE, FALSE, NULL);
event->wrapped = false;
event->msSpinTime = msSpinTime;
event->signaled = false;
if (!event->handle)
{
DEBUG_WINERROR("Failed to create the event", GetLastError());
free(event);
return NULL;
}
return event;
}
LGEvent * lgWrapEvent(void * handle)
{
LGEvent * event = (LGEvent *)malloc(sizeof(LGEvent));
if (!event)
{
DEBUG_ERROR("out of ram");
return NULL;
}
event->lock = 0;
event->reset = false;
event->handle = (HANDLE)handle;
event->wrapped = true;
event->msSpinTime = 0;
event->signaled = false;
return event;
}
void lgFreeEvent(LGEvent * event)
{
CloseHandle(event->handle);
}
bool lgWaitEvent(LGEvent * event, unsigned int timeout)
{
// wrapped events can't be enahnced
if (!event->wrapped)
{
if (event->signaled)
{
if (event->reset)
event->signaled = false;
return true;
}
if (timeout == 0)
{
bool ret = event->signaled;
if (event->reset)
event->signaled = false;
return ret;
}
if (event->msSpinTime)
{
unsigned int spinTime = event->msSpinTime;
if (timeout != TIMEOUT_INFINITE)
{
if (timeout > event->msSpinTime)
timeout -= event->msSpinTime;
else
{
spinTime -= timeout;
timeout = 0;
}
}
uint64_t now = getMicrotime();
uint64_t end = now + spinTime * 1000;
while(!event->signaled)
{
now = getMicrotime();
if (now >= end)
break;
}
if (event->signaled)
{
if (event->reset)
event->signaled = false;
return true;
}
}
}
const DWORD to = (timeout == TIMEOUT_INFINITE) ? INFINITE : (DWORD)timeout;
while(true)
{
switch(WaitForSingleObject(event->handle, to))
{
case WAIT_OBJECT_0:
if (!event->reset)
event->signaled = true;
return true;
case WAIT_ABANDONED:
continue;
case WAIT_TIMEOUT:
if (timeout == TIMEOUT_INFINITE)
continue;
return false;
case WAIT_FAILED:
DEBUG_WINERROR("Wait for event failed", GetLastError());
return false;
}
DEBUG_ERROR("Unknown wait event return code");
return false;
}
}
bool lgWaitEvents(LGEvent * events[], int count, bool waitAll, unsigned int timeout)
{
const DWORD to = (timeout == TIMEOUT_INFINITE) ? INFINITE : (DWORD)timeout;
HANDLE * handles = (HANDLE *)malloc(sizeof(HANDLE) * count);
for(int i = 0; i < count; ++i)
handles[i] = events[i]->handle;
while(true)
{
DWORD result = WaitForMultipleObjects(count, handles, waitAll, to);
if (result >= WAIT_OBJECT_0 && result < count)
{
// null non signaled events from the handle list
for(int i = 0; i < count; ++i)
if (i != result && !lgWaitEvent(events[i], 0))
handles[i] = NULL;
free(handles);
return true;
}
if (result >= WAIT_ABANDONED_0 && result - WAIT_ABANDONED_0 < count)
continue;
switch(result)
{
case WAIT_TIMEOUT:
if (timeout == TIMEOUT_INFINITE)
continue;
free(handles);
return false;
case WAIT_FAILED:
DEBUG_WINERROR("Wait for event failed", GetLastError());
free(handles);
return false;
}
DEBUG_ERROR("Unknown wait event return code");
free(handles);
return false;
}
}
bool lgSignalEvent(LGEvent * event)
{
event->signaled = true;
return SetEvent(event->handle);
}
bool lgResetEvent(LGEvent * event)
{
event->signaled = false;
return ResetEvent(event->handle);
}

View File

@@ -0,0 +1,24 @@
/*
KVMGFX Client - A KVM Client for VGA Passthrough
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
int sysinfo_gfx_max_multisample()
{
//FIXME: Implement this
return 4;
}

View File

@@ -0,0 +1,93 @@
/*
Looking Glass - KVM FrameRelay (KVMFR) Client
Copyright (C) 2017-2020 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common/thread.h"
#include "common/debug.h"
#include "common/windebug.h"
#include <windows.h>
struct LGThread
{
const char * name;
LGThreadFunction function;
void * opaque;
HANDLE handle;
DWORD threadID;
int resultCode;
};
static DWORD WINAPI threadWrapper(LPVOID lpParameter)
{
LGThread * handle = (LGThread *)lpParameter;
handle->resultCode = handle->function(handle->opaque);
return 0;
}
bool lgCreateThread(const char * name, LGThreadFunction function, void * opaque, LGThread ** handle)
{
*handle = (LGThread *)malloc(sizeof(LGThread));
(*handle)->name = name;
(*handle)->function = function;
(*handle)->opaque = opaque;
(*handle)->handle = CreateThread(NULL, 0, threadWrapper, *handle, 0, &(*handle)->threadID);
if (!(*handle)->handle)
{
free(*handle);
*handle = NULL;
DEBUG_WINERROR("CreateThread failed", GetLastError());
return false;
}
return true;
}
bool lgJoinThread(LGThread * handle, int * resultCode)
{
while(true)
{
switch(WaitForSingleObject(handle->handle, INFINITE))
{
case WAIT_OBJECT_0:
if (resultCode)
*resultCode = handle->resultCode;
CloseHandle(handle->handle);
free(handle);
return true;
case WAIT_ABANDONED:
case WAIT_TIMEOUT:
continue;
case WAIT_FAILED:
DEBUG_WINERROR("Wait for thread failed", GetLastError());
CloseHandle(handle->handle);
free(handle);
return false;
}
break;
}
DEBUG_WINERROR("Unknown failure waiting for thread", GetLastError());
return false;
}

View File

@@ -0,0 +1,42 @@
/*
Looking Glass - KVM FrameRelay (KVMFR) Client
Copyright (C) 2017-2020 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common/windebug.h"
#include <stdio.h>
void DebugWinError(const char * file, const unsigned int line, const char * function, const char * desc, HRESULT status)
{
char *buffer;
FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL,
status,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(char*)&buffer,
1024,
NULL
);
for(size_t i = strlen(buffer) - 1; i > 0; --i)
if (buffer[i] == '\n' || buffer[i] == '\r')
buffer[i] = 0;
fprintf(stderr, "[E] %20s:%-4u | %-30s | %s: 0x%08x (%s)\n", file, line, function, desc, (int)status, buffer);
LocalFree(buffer);
}