Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ set(SOURCES
misc/interlock.cpp
misc/miscpalapi.cpp
misc/msgbox.cpp
misc/stackstring.cpp
misc/strutil.cpp
misc/sysinfo.cpp
misc/time.cpp
Expand Down
20 changes: 16 additions & 4 deletions src/pal/src/debug/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Revision History:
#include "pal/misc.h"
#include "pal/malloc.hpp"
#include "pal/module.h"
#include "pal/stackstring.hpp"
#include "pal/virtual.h"

#include <signal.h>
Expand Down Expand Up @@ -343,15 +344,24 @@ DebugBreakCommand()
const char *command_string = getenv (PAL_RUN_ON_DEBUG_BREAK);
if (command_string) {
char pid_buf[sizeof (PID_TEXT) + 32];
char exe_buf[sizeof (EXE_TEXT) + MAX_LONGPATH + 1];
PathCharString exe_bufString;
int libNameLength = 10;
if (exe_module.lib_name != NULL)
{
libNameLength = PAL_wcslen(exe_module.lib_name);
}

SIZE_T dwexe_buf = strlen(EXE_TEXT) + libNameLength + 1;
CHAR * exe_buf = exe_bufString.OpenStringBuffer(dwexe_buf);

if (snprintf (pid_buf, sizeof (pid_buf), PID_TEXT "%d", getpid()) <= 0) {
goto FAILED;
}
if (snprintf (exe_buf, sizeof (exe_buf), EXE_TEXT "%ls", (wchar_t *)exe_module.lib_name) <= 0) {
if (snprintf (exe_buf, sizeof (CHAR) * (dwexe_buf + 1), EXE_TEXT "%ls", (wchar_t *)exe_module.lib_name) <= 0) {
goto FAILED;
}

exe_bufString.CloseBuffer(dwexe_buf);
/* strictly speaking, we might want to only set these environment
variables in the child process, but if we do that we can't check
for errors. putenv/setenv can fail when out of memory */
Expand Down Expand Up @@ -1598,7 +1608,7 @@ PAL_CreateExecWatchpoint(
CPalThread *pTargetThread = NULL;
IPalObject *pobjThread = NULL;
int fd = -1;
char ctlPath[MAX_LONGPATH];
char ctlPath[50];

struct
{
Expand Down Expand Up @@ -1642,6 +1652,7 @@ PAL_CreateExecWatchpoint(
}

snprintf(ctlPath, sizeof(ctlPath), "/proc/%u/lwp/%u/lwpctl", getpid(), pTargetThread->GetLwpId());

fd = InternalOpen(pThread, ctlPath, O_WRONLY);
if (-1 == fd)
{
Expand Down Expand Up @@ -1719,7 +1730,7 @@ PAL_DeleteExecWatchpoint(
CPalThread *pTargetThread = NULL;
IPalObject *pobjThread = NULL;
int fd = -1;
char ctlPath[MAX_LONGPATH];
char ctlPath[50];

struct
{
Expand All @@ -1744,6 +1755,7 @@ PAL_DeleteExecWatchpoint(
}

snprintf(ctlPath, sizeof(ctlPath), "/proc/%u/lwp/%u/lwpctl", getpid(), pTargetThread->GetLwpId());

fd = InternalOpen(pThread, ctlPath, O_WRONLY);
if (-1 == fd)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include "pal/malloc.hpp"
#include "pal/dbgmsg.h"
#ifndef __STACKSTRING_H_
#define __STACKSTRING_H_

SET_DEFAULT_DEBUG_CHANNEL(MISC);
#include "pal/malloc.hpp"

template <SIZE_T STACKCOUNT>
template <SIZE_T STACKCOUNT, class T>
class StackString
{
private:
WCHAR m_innerBuffer[STACKCOUNT + 1];
WCHAR * m_buffer;
T m_innerBuffer[STACKCOUNT + 1];
T * m_buffer;
SIZE_T m_count; // actual allocated count

void NullTerminate()
{
m_buffer[m_count] = W('\0');
m_buffer[m_count] = 0;
}

void DeleteBuffer()
Expand All @@ -31,10 +31,9 @@ class StackString
void ReallocateBuffer(SIZE_T count)
{
// count is always > STACKCOUNT here.
WCHAR * newBuffer = (WCHAR *)PAL_malloc((count + 1) * sizeof(WCHAR));
T * newBuffer = (T *)PAL_malloc((count + 1) * sizeof(T));
if (NULL == newBuffer)
{
ERROR("malloc failed\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);

DeleteBuffer();
Expand Down Expand Up @@ -84,24 +83,19 @@ class StackString
Set(s);
}

~StackString()
{
DeleteBuffer();
}

public:
StackString()
: m_count(0), m_buffer(m_innerBuffer)
: m_buffer(m_innerBuffer), m_count(0)
{
}

BOOL Set(const WCHAR * buffer, SIZE_T count)
BOOL Set(const T * buffer, SIZE_T count)
{
Resize(count);
if (NULL == m_buffer)
return FALSE;

CopyMemory(m_buffer, buffer, (count + 1) * sizeof(WCHAR));
CopyMemory(m_buffer, buffer, (count + 1) * sizeof(T));
NullTerminate();
return TRUE;
}
Expand All @@ -111,20 +105,25 @@ class StackString
return Set(s.m_buffer, s.m_count);
}

SIZE_T Getcount() const
SIZE_T GetCount() const
{
return m_count;
}

SIZE_T GetSizeOf() const
{
return (m_count+1) * sizeof(T);
}

CONST WCHAR * GetString() const
CONST T * GetString() const
{
return (const WCHAR *)m_buffer;
return (const T *)m_buffer;
}

WCHAR * OpenStringBuffer(SIZE_T count)
T * OpenStringBuffer(SIZE_T count)
{
Resize(count);
return (WCHAR *)m_buffer;
return (T *)m_buffer;
}

void CloseBuffer(SIZE_T count)
Expand All @@ -135,4 +134,18 @@ class StackString
NullTerminate();
return;
}

~StackString()
{
DeleteBuffer();
}
};

#if _DEBUG
typedef StackString<32, CHAR> PathCharString;
typedef StackString<32, WCHAR> PathWCharString;
#else
typedef StackString<260, CHAR> PathCharString;
typedef StackString<260, WCHAR> PathWCharString;
#endif
#endif