summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Maidanski <ivmai@mail.ru>2021-10-12 10:14:54 +0300
committerIvan Maidanski <ivmai@mail.ru>2021-10-12 15:15:45 +0300
commited7058837e4b92cd8078b3c9ad198e6546efe3fd (patch)
tree341267ccacdeab156f78e769ade8fe8003217426
parent4ecc67a999dec9f6c0a279c7f3241caa3b5aceaf (diff)
downloadbdwgc-ed7058837e4b92cd8078b3c9ad198e6546efe3fd.tar.gz
Make all functions static in msvc_dbg.c except for backtrace[_symbols]
(refactoring) * extra/msvc_dbg.c [!_M_ARM && !_M_ARM64 && !_M_X64 && _MSC_VER] (MAX_SYM_NAME): Move from msvc_dbg.h. * extra/msvc_dbg.c [!_M_ARM && !_M_ARM64 && !_M_X64 && _MSC_VER] (GetStackFramesFromContext): Declare before use. * extra/msvc_dbg.c [!_M_ARM && !_M_ARM64 && !_M_X64 && _MSC_VER] (GetStackFrames, GetStackFramesFromContext, GetModuleNameFromAddress, GetSymbolNameFromAddress, GetFileLineFromAddress, GetDescriptionFromAddress, GetDescriptionFromStack): Define as static. * extra/msvc_dbg.c [!_M_ARM && !_M_ARM64 && !_M_X64 && _MSC_VER] (GetModuleNameFromAddress, GetFileLineFromAddress): Declare local variables at top level. * extra/msvc_dbg.c [!_M_ARM && !_M_ARM64 && !_M_X64 && _MSC_VER] (GetModuleNameFromStack, GetSymbolNameFromStack, GetFileLineFromStack): Remove. * include/private/msvc_dbg.h (MSVC_DBG_EXPORT): Always define as empty. * include/private/msvc_dbg.h (HANDLE, CONTEXT, GetStackFrames, GetStackFramesFromContext, GetModuleNameFromAddress, GetModuleNameFromStack, GetSymbolNameFromAddress, GetSymbolNameFromStack, GetFileLineFromAddress, GetFileLineFromStack, GetDescriptionFromAddress, GetDescriptionFromStack): Do not declare. * tests/test.c [!PCR && !GC_WIN32_THREADS && !GC_PTHREADS && CPPCHECK && _MSC_VER && !_M_ARM && !_M_ARM64 && !_M_X64]: Remove include of msvc_dbg.h. * tests/test.c [!PCR && !GC_WIN32_THREADS && !GC_PTHREADS && CPPCHECK && !_M_ARM && !_M_ARM64 && !_M_X64 && _MSC_VER] (WinMain): Do not pass GetFileLineFromStack, GetModuleNameFromStack and GetSymbolNameFromStack to UNTESTED().
-rw-r--r--extra/msvc_dbg.c138
-rw-r--r--include/private/msvc_dbg.h32
-rw-r--r--tests/test.c11
3 files changed, 58 insertions, 123 deletions
diff --git a/extra/msvc_dbg.c b/extra/msvc_dbg.c
index 9679cca3..b181e713 100644
--- a/extra/msvc_dbg.c
+++ b/extra/msvc_dbg.c
@@ -52,6 +52,10 @@ typedef GC_word word;
typedef ULONG ULONG_ADDR;
#endif
+#ifndef MAX_SYM_NAME
+#define MAX_SYM_NAME 2000
+#endif
+
static HANDLE GetSymHandle(void)
{
static HANDLE symHandle = NULL;
@@ -115,7 +119,11 @@ static ULONG_ADDR CheckAddress(void* address)
return dwAddress;
}
-size_t GetStackFrames(size_t skip, void* frames[], size_t maxFrames)
+static size_t GetStackFramesFromContext(HANDLE hProcess, HANDLE hThread,
+ CONTEXT* context, size_t skip,
+ void* frames[], size_t maxFrames);
+
+static size_t GetStackFrames(size_t skip, void* frames[], size_t maxFrames)
{
HANDLE hProcess = GetSymHandle();
HANDLE hThread = GetCurrentThread();
@@ -132,9 +140,9 @@ size_t GetStackFrames(size_t skip, void* frames[], size_t maxFrames)
frames, maxFrames);
}
-size_t GetStackFramesFromContext(HANDLE hProcess, HANDLE hThread,
- CONTEXT* context, size_t skip,
- void* frames[], size_t maxFrames)
+static size_t GetStackFramesFromContext(HANDLE hProcess, HANDLE hThread,
+ CONTEXT* context, size_t skip,
+ void* frames[], size_t maxFrames)
{
size_t frameIndex;
DWORD machineType;
@@ -180,37 +188,26 @@ size_t GetStackFramesFromContext(HANDLE hProcess, HANDLE hThread,
return frameIndex;
}
-size_t GetModuleNameFromAddress(void* address, char* moduleName, size_t size)
+static size_t GetModuleNameFromAddress(void* address, char* moduleName,
+ size_t size)
{
+ const char* sourceName;
+ IMAGEHLP_MODULE moduleInfo = { sizeof(moduleInfo) };
+
if (size) *moduleName = 0;
- {
- const char* sourceName;
- IMAGEHLP_MODULE moduleInfo = { sizeof (moduleInfo) };
- if (!SymGetModuleInfo(GetSymHandle(), CheckAddress(address),
- &moduleInfo)) {
- return 0;
- }
- sourceName = strrchr(moduleInfo.ImageName, '\\');
- if (sourceName) {
- sourceName++;
- } else {
- sourceName = moduleInfo.ImageName;
- }
- if (size) {
- strncpy(moduleName, sourceName, size)[size - 1] = 0;
- }
- return strlen(sourceName);
+ if (!SymGetModuleInfo(GetSymHandle(), CheckAddress(address), &moduleInfo)) {
+ return 0;
}
-}
-
-size_t GetModuleNameFromStack(size_t skip, char* moduleName, size_t size)
-{
- void* address = NULL;
- GetStackFrames(skip + 1, &address, 1);
- if (address) {
- return GetModuleNameFromAddress(address, moduleName, size);
+ sourceName = strrchr(moduleInfo.ImageName, '\\');
+ if (sourceName) {
+ sourceName++;
+ } else {
+ sourceName = moduleInfo.ImageName;
}
- return 0;
+ if (size) {
+ strncpy(moduleName, sourceName, size)[size - 1] = 0;
+ }
+ return strlen(sourceName);
}
union sym_namebuf_u {
@@ -218,8 +215,8 @@ union sym_namebuf_u {
char symNameBuffer[sizeof(IMAGEHLP_SYMBOL) + MAX_SYM_NAME];
};
-size_t GetSymbolNameFromAddress(void* address, char* symbolName, size_t size,
- size_t* offsetBytes)
+static size_t GetSymbolNameFromAddress(void* address, char* symbolName,
+ size_t size, size_t* offsetBytes)
{
if (size) *symbolName = 0;
if (offsetBytes) *offsetBytes = 0;
@@ -256,61 +253,38 @@ size_t GetSymbolNameFromAddress(void* address, char* symbolName, size_t size,
return 0;
}
-size_t GetSymbolNameFromStack(size_t skip, char* symbolName, size_t size,
- size_t* offsetBytes)
+static size_t GetFileLineFromAddress(void* address, char* fileName,
+ size_t size, size_t* lineNumber,
+ size_t* offsetBytes)
{
- void* address = NULL;
- GetStackFrames(skip + 1, &address, 1);
- if (address) {
- return GetSymbolNameFromAddress(address, symbolName, size, offsetBytes);
- }
- return 0;
-}
+ char* sourceName;
+ IMAGEHLP_LINE line = { sizeof (line) };
+ GC_ULONG_PTR dwOffset = 0;
-size_t GetFileLineFromAddress(void* address, char* fileName, size_t size,
- size_t* lineNumber, size_t* offsetBytes)
-{
if (size) *fileName = 0;
if (lineNumber) *lineNumber = 0;
if (offsetBytes) *offsetBytes = 0;
- {
- char* sourceName;
- IMAGEHLP_LINE line = { sizeof (line) };
- GC_ULONG_PTR dwOffset = 0;
- if (!SymGetLineFromAddr(GetSymHandle(), CheckAddress(address), &dwOffset,
- &line)) {
- return 0;
- }
- if (lineNumber) {
- *lineNumber = line.LineNumber;
- }
- if (offsetBytes) {
- *offsetBytes = dwOffset;
- }
- sourceName = line.FileName;
- /* TODO: resolve relative filenames, found in 'source directories' */
- /* registered with MSVC IDE. */
- if (size) {
- strncpy(fileName, sourceName, size)[size - 1] = 0;
- }
- return strlen(sourceName);
+ if (!SymGetLineFromAddr(GetSymHandle(), CheckAddress(address), &dwOffset,
+ &line)) {
+ return 0;
}
-}
-
-size_t GetFileLineFromStack(size_t skip, char* fileName, size_t size,
- size_t* lineNumber, size_t* offsetBytes)
-{
- void* address = NULL;
- GetStackFrames(skip + 1, &address, 1);
- if (address) {
- return GetFileLineFromAddress(address, fileName, size, lineNumber,
- offsetBytes);
+ if (lineNumber) {
+ *lineNumber = line.LineNumber;
}
- return 0;
+ if (offsetBytes) {
+ *offsetBytes = dwOffset;
+ }
+ sourceName = line.FileName;
+ /* TODO: resolve relative filenames, found in "source directories" */
+ /* registered with MSVC IDE. */
+ if (size) {
+ strncpy(fileName, sourceName, size)[size - 1] = 0;
+ }
+ return strlen(sourceName);
}
-size_t GetDescriptionFromAddress(void* address, const char* format,
- char* buffer, size_t size)
+static size_t GetDescriptionFromAddress(void* address, const char* format,
+ char* buffer, size_t size)
{
char*const begin = buffer;
char*const end = buffer + size;
@@ -353,9 +327,9 @@ size_t GetDescriptionFromAddress(void* address, const char* format,
return buffer - begin;
}
-size_t GetDescriptionFromStack(void* const frames[], size_t count,
- const char* format, char* description[],
- size_t size)
+static size_t GetDescriptionFromStack(void* const frames[], size_t count,
+ const char* format, char* description[],
+ size_t size)
{
const GC_ULONG_PTR begin = (GC_ULONG_PTR)description;
const GC_ULONG_PTR end = begin + size;
diff --git a/include/private/msvc_dbg.h b/include/private/msvc_dbg.h
index 10dd1035..ff64a425 100644
--- a/include/private/msvc_dbg.h
+++ b/include/private/msvc_dbg.h
@@ -29,39 +29,11 @@
extern "C" {
#endif
-#if !MSVC_DBG_DLL
-#define MSVC_DBG_EXPORT
-#elif MSVC_DBG_BUILD
-#define MSVC_DBG_EXPORT __declspec(dllexport)
-#else
-#define MSVC_DBG_EXPORT __declspec(dllimport)
-#endif
-
-#ifndef MAX_SYM_NAME
-#define MAX_SYM_NAME 2000
-#endif
-
-typedef void* HANDLE;
-typedef struct _CONTEXT CONTEXT;
-
-MSVC_DBG_EXPORT size_t GetStackFrames(size_t skip, void* frames[], size_t maxFrames);
-MSVC_DBG_EXPORT size_t GetStackFramesFromContext(HANDLE hProcess, HANDLE hThread, CONTEXT* context, size_t skip, void* frames[], size_t maxFrames);
-
-MSVC_DBG_EXPORT size_t GetModuleNameFromAddress(void* address, char* moduleName, size_t size);
-MSVC_DBG_EXPORT size_t GetModuleNameFromStack(size_t skip, char* moduleName, size_t size);
-
-MSVC_DBG_EXPORT size_t GetSymbolNameFromAddress(void* address, char* symbolName, size_t size, size_t* offsetBytes);
-MSVC_DBG_EXPORT size_t GetSymbolNameFromStack(size_t skip, char* symbolName, size_t size, size_t* offsetBytes);
-
-MSVC_DBG_EXPORT size_t GetFileLineFromAddress(void* address, char* fileName, size_t size, size_t* lineNumber, size_t* offsetBytes);
-MSVC_DBG_EXPORT size_t GetFileLineFromStack(size_t skip, char* fileName, size_t size, size_t* lineNumber, size_t* offsetBytes);
-
-MSVC_DBG_EXPORT size_t GetDescriptionFromAddress(void* address, const char* format, char* description, size_t size);
-MSVC_DBG_EXPORT size_t GetDescriptionFromStack(void*const frames[], size_t count, const char* format, char* description[], size_t size);
+#define MSVC_DBG_EXPORT /* empty */
/* Compatibility with <execinfo.h> */
MSVC_DBG_EXPORT int backtrace(void* addresses[], int count);
-MSVC_DBG_EXPORT char** backtrace_symbols(void*const addresses[], int count);
+MSVC_DBG_EXPORT char** backtrace_symbols(void* const addresses[], int count);
#ifdef __cplusplus
} /* extern "C" */
diff --git a/tests/test.c b/tests/test.c
index 2eaadadd..c728daa4 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -1943,11 +1943,6 @@ void enable_incremental_mode(void)
#if !defined(PCR) && !defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
-#if defined(CPPCHECK) && defined(_MSC_VER) && !defined(_M_ARM) \
- && !defined(_M_ARM64) && !defined(_M_X64)
-# include "private/msvc_dbg.h"
-#endif
-
#if defined(_DEBUG) && (_MSC_VER >= 1900) /* VS 2015+ */
# ifndef _CRTDBG_MAP_ALLOC
# define _CRTDBG_MAP_ALLOC
@@ -2044,12 +2039,6 @@ void enable_incremental_mode(void)
# if defined(MACOS) && defined(USE_TEMPORARY_MEMORY)
UNTESTED(GC_MacTemporaryNewPtr);
# endif
-# if !defined(_M_ARM) && !defined(_M_ARM64) \
- && !defined(_M_X64) && defined(_MSC_VER)
- UNTESTED(GetFileLineFromStack);
- UNTESTED(GetModuleNameFromStack);
- UNTESTED(GetSymbolNameFromStack);
-# endif
UNTESTED(GC_abort_on_oom);
UNTESTED(GC_malloc_explicitly_typed_ignore_off_page);
UNTESTED(GC_debug_strndup);