<commit_before>/*
 * Copyright 2002-2013 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <assert.h>

#include <windows.h>
#include <tlhelp32.h>

#include <process.h>
#include <stdlib.h>

#include "getopt.h"
#include "debugger.h"
#include "symbols.h"
#include "dialog.h"
#include "errmsg.h"
#include "log.h"
#include "outdbg.h"


static int process_id_given = 0;    /* Whether process-id was given.  */
static int install_given = 0;    /* Whether install was given.  */
static int uninstall_given = 0;    /* Whether uninstall was given.  */

static DebugOptions debug_options;

static void
help(void)
{
    MessageBoxA(
        NULL,
        "Usage: drmingw [OPTIONS]\r\n"
        "\r\n"
        "Options:\r\n"
        "  -h, --help\tPrint help and exit\r\n"
        "  -V, --version\tPrint version and exit\r\n"
        "  -i, --install\tInstall as the default JIT debugger\r\n"
        "  -u, --uninstall\tUninstall\r\n"
        "  -pLONG, --process-id=LONG\r\n"
        "\t\tAttach to the process with the given identifier\r\n"
        "  -eLONG, --event=LONG\r\n"
        "\t\tSignal an event after process is attached\r\n"
        "  -b, --breakpoint\tTreat debug breakpoints as exceptions\r\n"
        "  -v, --verbose\tVerbose output\r\n"
        "  -d, --debug\tDebug output\r\n"
        ,
        PACKAGE,
        MB_OK | MB_ICONINFORMATION
    );
}


static LSTATUS
regSetStr(HKEY hKey, LPCSTR lpValueName, LPCSTR szStr)
{
    return RegSetValueExA(
        hKey, lpValueName, 0, REG_SZ,
        reinterpret_cast<LPCBYTE>(szStr), strlen(szStr) + 1
    );
}


static DWORD
install(REGSAM samDesired)
{
    char szFile[MAX_PATH];

    if (!GetModuleFileNameA(NULL, szFile, MAX_PATH)) {
        return GetLastError();
    }
        
    char szFullCommand[MAX_PATH + 256];
    HKEY hKey;
    long lRet;
    DWORD dwDisposition;

    strcpy(szFullCommand, szFile);
    strcat(szFullCommand, " -p %ld -e %ld");
    if (debug_options.verbose_flag)
        strcat(szFullCommand, " -v");
    if (debug_options.breakpoint_flag)
        strcat(szFullCommand, " -b");
    if (debug_options.debug_flag)
        strcat(szFullCommand, " -d");

    lRet = RegCreateKeyExA(
        HKEY_LOCAL_MACHINE,
        "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",    // The AeDebug registry key.
        0,
        NULL,
        REG_OPTION_NON_VOLATILE,
        KEY_WRITE | samDesired,
        NULL,
        &hKey,
        &dwDisposition
    );
    if (lRet != ERROR_SUCCESS) {
        return lRet;
    }

    // Write the Debugger value.
    lRet = regSetStr(hKey, "Debugger", szFullCommand);
    if (lRet == ERROR_SUCCESS) {
        // Write the Auto value.
        lRet = regSetStr(hKey, "Auto", "1");
    }

    // Close the key no matter what happened.
    RegCloseKey(hKey);
<commit_msg>drmingw: Ensure drmingw path is quoted.<commit_after>/*
 * Copyright 2002-2013 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <assert.h>
#include <stdlib.h>

#include <string>

#include <windows.h>
#include <tlhelp32.h>

#include <process.h>

#include "getopt.h"
#include "debugger.h"
#include "symbols.h"
#include "dialog.h"
#include "errmsg.h"
#include "log.h"
#include "outdbg.h"


static int process_id_given = 0;    /* Whether process-id was given.  */
static int install_given = 0;    /* Whether install was given.  */
static int uninstall_given = 0;    /* Whether uninstall was given.  */

static DebugOptions debug_options;

static void
help(void)
{
    MessageBoxA(
        NULL,
        "Usage: drmingw [OPTIONS]\r\n"
        "\r\n"
        "Options:\r\n"
        "  -h, --help\tPrint help and exit\r\n"
        "  -V, --version\tPrint version and exit\r\n"
        "  -i, --install\tInstall as the default JIT debugger\r\n"
        "  -u, --uninstall\tUninstall\r\n"
        "  -pLONG, --process-id=LONG\r\n"
        "\t\tAttach to the process with the given identifier\r\n"
        "  -eLONG, --event=LONG\r\n"
        "\t\tSignal an event after process is attached\r\n"
        "  -b, --breakpoint\tTreat debug breakpoints as exceptions\r\n"
        "  -v, --verbose\tVerbose output\r\n"
        "  -d, --debug\tDebug output\r\n"
        ,
        PACKAGE,
        MB_OK | MB_ICONINFORMATION
    );
}


static LSTATUS
regSetStr(HKEY hKey, LPCSTR lpValueName, LPCSTR szStr)
{
    return RegSetValueExA(
        hKey, lpValueName, 0, REG_SZ,
        reinterpret_cast<LPCBYTE>(szStr), strlen(szStr) + 1
    );
}


static DWORD
install(REGSAM samDesired)
{
    char szFile[MAX_PATH];
    if (!GetModuleFileNameA(NULL, szFile, MAX_PATH)) {
        return GetLastError();
    }
        
    std::string debuggerCommand;
    debuggerCommand += '"';
    debuggerCommand += szFile;
    debuggerCommand += "\" -p %ld -e %ld";
    if (debug_options.verbose_flag)
        debuggerCommand += " -v";
    if (debug_options.breakpoint_flag)
        debuggerCommand += " -b";
    if (debug_options.debug_flag)
        debuggerCommand += " -d";

    HKEY hKey;
    long lRet;
    DWORD dwDisposition;

    lRet = RegCreateKeyExA(
        HKEY_LOCAL_MACHINE,
        "Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug",    // The AeDebug registry key.
        0,
        NULL,
        REG_OPTION_NON_VOLATILE,
        KEY_WRITE | samDesired,
        NULL,
        &hKey,
        &dwDisposition
    );
    if (lRet != ERROR_SUCCESS) {
        return lRet;
    }

    // Write the Debugger value.
    lRet = regSetStr(hKey, "Debugger", debuggerCommand.c_str());
    if (lRet == ERROR_SUCCESS) {
        // Write the Auto value.
        lRet = regSetStr(hKey, "Auto", "1");
    }

    // Close the key no matter what happened.
    RegCloseKey(hKey);

{'fraction_non_alphanumeric': 0.11462055166499928, 'fraction_numerical': 0.009003858796627125, 'mean_word_length': 2.5077694235588974, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '769629', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2296, 'n_words': 673}