<commit_before>#include "stackl_debugger.h"

#include <iostream>
using std::cout;
using std::cin;
#include <cmath>
#include <cstdio>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cstdlib>
using std::runtime_error;
#include <set>
using std::set;
#include <algorithm>
using std::transform;

extern "C"
{
    #include "../vmem.h"
    #include "../slasm.h"
}

stackl_debugger::stackl_debugger( const char* filename )
{
    string fname = filename;
    _binary_name = fname;

    size_t idx =  fname.find_last_of( '.' );
    if( idx != string::npos )
        fname.erase( idx );

    try
    {

        if( !file_exists( fname + ".dbg" ) || !file_exists( fname + ".ast" ) )
            opcode_debug_mode();
        else
        {
            _lst = asm_list( fname + ".dbg" );
            _ast = abstract_syntax_tree();

            for( const string& file : _lst.file_list() )
                _ast.add_ast( file );

            _ast.fixup_globals( _lst.global_offsets() );

            check_compile_time( filename );
        }

        load_commands();

        //if we get to this point it's because there were no exceptions thrown above
        set_flag( LOADED, true );
        cout << "Debugger for " << _binary_name << " loaded. Type 'help' for help.\n";
    }
    catch( exception& ex )
    {
        set_flag( LOADED, false );
        _failure_reason = ex.what();
    }
}

void stackl_debugger::check_compile_time( const char* filename )
{
    struct stat attrib;
    stat( filename, &attrib );
    if( attrib.st_mtime > _ast.compile_time() )
        throw runtime_error( "Debug files out of date." );
}

bool stackl_debugger::file_exists( const string& filename )
{
    struct stat buffer;
    return stat( filename.c_str(), &buffer ) == 0;
}

void stackl_debugger::opcode_debug_mode()
{
    char c;
    cout << "Could not find debug files. Would you like to debug the binary? [y/n] ";
    cin.get( c );
    cin.ignore( INT32_MAX, '\n' ); //ignore 2^31 characters up until the next newline
    if( c == 'n' )
        throw runtime_error( "Debug files not found" );

    set_flag( OPCODE_DEBUG, true );
}

void stackl_debugger::load_commands()
{
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_breakpoint, { "breakpoint", "break", "br", "b" }, "[line_num|file:line_num|func_name] - Adds breakpoint", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_breakpointi, { "breakpointi", "breaki", "bi" }, "[IP] - Adds breakpoint at the passed instruction pointer", true ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_removebreak, { "removebreak", "rbreak", "rb" }, "[line_num|file:line_num|func_name] - Removes breakpoint", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_removebreaki, { "removebreaki", "rbreaki", "rbi" }, "[IP] - Removes breakpoint at the passed instruction pointer", true ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_print, { "print", "p" }, "[var_name|0xaddress] - Prints the value of the variable", true ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_printi, { "printi", "pi" }, "[IP|0xaddress] - Prints the value of the instruction at the passed location", true ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_continue, { "continue", "cont", "c" }, "- Runs program until the next breakpoint", true ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_list, { "list" }, " optional[line_count] - Prints all source code within N lines of the current line", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_next, { "next", "n" }, "[line_num|file:line_num|func_name] - Removes breakpoint", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_step, { "step", "s" }, "- Steps over the current line", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_locals, { "locals", "listlocals" }, "- Print all local vars in current or specified function", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_globals, { "globals", "listglobals" }, "- Print all global variables", false ) );<commit_msg>Changed help string to next dbg cmd. Removed timestamp check<commit_after>#include "stackl_debugger.h"

#include <iostream>
using std::cout;
using std::cin;
#include <cmath>
#include <cstdio>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cstdlib>
using std::runtime_error;
#include <set>
using std::set;
#include <algorithm>
using std::transform;

extern "C"
{
    #include "../vmem.h"
    #include "../slasm.h"
}

stackl_debugger::stackl_debugger( const char* filename )
{
    string fname = filename;
    _binary_name = fname;

    size_t idx =  fname.find_last_of( '.' );
    if( idx != string::npos )
        fname.erase( idx );

    try
    {

        if( !file_exists( fname + ".dbg" ) || !file_exists( fname + ".ast" ) )
            opcode_debug_mode();
        else
        {
            _lst = asm_list( fname + ".dbg" );
            _ast = abstract_syntax_tree();

            for( const string& file : _lst.file_list() )
                _ast.add_ast( file );

            _ast.fixup_globals( _lst.global_offsets() );

            check_compile_time( filename );
        }

        load_commands();

        //if we get to this point it's because there were no exceptions thrown above
        set_flag( LOADED, true );
        cout << "Debugger for " << _binary_name << " loaded. Type 'help' for help.\n";
    }
    catch( exception& ex )
    {
        set_flag( LOADED, false );
        _failure_reason = ex.what();
    }
}

void stackl_debugger::check_compile_time( const char* filename )
{
    struct stat attrib;
    stat( filename, &attrib );
    //if( attrib.st_mtime > _ast.compile_time() )
    //    throw runtime_error( "Debug files out of date." );
}

bool stackl_debugger::file_exists( const string& filename )
{
    struct stat buffer;
    return stat( filename.c_str(), &buffer ) == 0;
}

void stackl_debugger::opcode_debug_mode()
{
    char c;
    cout << "Could not find debug files. Would you like to debug the binary? [y/n] ";
    cin.get( c );
    cin.ignore( INT32_MAX, '\n' ); //ignore 2^31 characters up until the next newline
    if( c == 'n' )
        throw runtime_error( "Debug files not found" );

    set_flag( OPCODE_DEBUG, true );
}

void stackl_debugger::load_commands()
{
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_breakpoint, { "breakpoint", "break", "br", "b" }, "[line_num|file:line_num|func_name] - Adds breakpoint", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_breakpointi, { "breakpointi", "breaki", "bi" }, "[IP] - Adds breakpoint at the passed instruction pointer", true ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_removebreak, { "removebreak", "rbreak", "rb" }, "[line_num|file:line_num|func_name] - Removes breakpoint", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_removebreaki, { "removebreaki", "rbreaki", "rbi" }, "[IP] - Removes breakpoint at the passed instruction pointer", true ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_print, { "print", "p" }, "[var_name|0xaddress] - Prints the value of the variable", true ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_printi, { "printi", "pi" }, "[IP|0xaddress] - Prints the value of the instruction at the passed location", true ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_continue, { "continue", "cont", "c" }, "- Runs program until the next breakpoint", true ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_list, { "list" }, " optional[line_count] - Prints all source code within N lines of the current line", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_next, { "next", "n" }, "- Steps into a function or over a line", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_step, { "step", "s" }, "- Steps over the current line", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_locals, { "locals", "listlocals" }, "- Print all local vars in current or specified function", false ) );
    _commands.push_back( debugger_command( *this, &stackl_debugger::cmd_globals, { "globals", "listglobals" }, "- Print all global variables", false ) );
{'fraction_non_alphanumeric': 0.14491073498724785, 'fraction_numerical': 0.0018548574078367725, 'mean_word_length': 3.7297149122807016, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 23, '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': '2737559', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2862, 'n_words': 738}