3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-25 12:12:34 +01:00
pbot/applets/compiler_vm/guest/bin/guest-gdb

658 lines
17 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl
2022-01-29 05:21:10 +01:00
# File: guest-gdb
#
# Purpose: Wraps a GNU Debugger instance around a program. Watches for
# invocations of a gdb() function to handle gdb commands from the program.
# Reports information about local variables when signals are detected.
#
# Usage: guest-gdb [executable]
#
# If [executable] is omitted, it will default to `prog`. Don't forget to
# compile with -g for debugging symbols.
# The DEBUG environment variable can be set to an integer value to enable
# internal debugging output. Set it to 1 for minimal debugging; to 2 to
# also dump the parsed GDB/MI data structures.
# SPDX-FileCopyrightText: 2022 Pragmatic Software <pragma78@gmail.com>
2021-07-11 00:00:22 +02:00
# SPDX-License-Identifier: MIT
License project under MPL2 This patch adds the file LICENSE which is the verbatim copy of the Mozilla Public License Version 2.0 as retreived from https://www.mozilla.org/media/MPL/2.0/index.815ca599c9df.txt on 2017-03-05. This patch also places license headers for the MPL2 type A variant of the license header in the following files: PBot/AntiFlood.pm PBot/BanTracker.pm PBot/BlackList.pm PBot/BotAdminCommands.pm PBot/BotAdmins.pm PBot/ChanOpCommands.pm PBot/ChanOps.pm PBot/Channels.pm PBot/Commands.pm PBot/DualIndexHashObject.pm PBot/EventDispatcher.pm PBot/FactoidCommands.pm PBot/FactoidModuleLauncher.pm PBot/Factoids.pm PBot/HashObject.pm PBot/IRCHandlers.pm PBot/IgnoreList.pm PBot/IgnoreListCommands.pm PBot/Interpreter.pm PBot/LagChecker.pm PBot/Logger.pm PBot/MessageHistory.pm PBot/MessageHistory_SQLite.pm PBot/NickList.pm PBot/PBot.pm PBot/Plugins.pm PBot/Plugins/AntiAway.pm PBot/Plugins/AntiKickAutoRejoin.pm PBot/Plugins/AntiRepeat.pm PBot/Plugins/AntiTwitter.pm PBot/Plugins/AutoRejoin.pm PBot/Plugins/Counter.pm PBot/Plugins/Quotegrabs.pm PBot/Plugins/Quotegrabs/Quotegrabs_Hashtable.pm PBot/Plugins/Quotegrabs/Quotegrabs_SQLite.pm PBot/Plugins/UrlTitles.pm PBot/Plugins/_Example.pm PBot/Refresher.pm PBot/Registerable.pm PBot/Registry.pm PBot/RegistryCommands.pm PBot/SQLiteLogger.pm PBot/SQLiteLoggerLayer.pm PBot/SelectHandler.pm PBot/StdinReader.pm PBot/Timer.pm PBot/Utils/ParseDate.pm PBot/VERSION.pm build/update-version.pl modules/acronym.pl modules/ago.pl modules/c11std.pl modules/c2english.pl modules/c2english/CGrammar.pm modules/c2english/c2eng.pl modules/c99std.pl modules/cdecl.pl modules/cfaq.pl modules/cjeopardy/IRCColors.pm modules/cjeopardy/QStatskeeper.pm modules/cjeopardy/Scorekeeper.pm modules/cjeopardy/cjeopardy.pl modules/cjeopardy/cjeopardy_answer.pl modules/cjeopardy/cjeopardy_filter.pl modules/cjeopardy/cjeopardy_hint.pl modules/cjeopardy/cjeopardy_qstats.pl modules/cjeopardy/cjeopardy_scores.pl modules/cjeopardy/cjeopardy_show.pl modules/codepad.pl modules/compiler_block.pl modules/compiler_client.pl modules/compiler_vm/Diff.pm modules/compiler_vm/cc modules/compiler_vm/compiler_client.pl modules/compiler_vm/compiler_server.pl modules/compiler_vm/compiler_server_vbox_win32.pl modules/compiler_vm/compiler_server_watchdog.pl modules/compiler_vm/compiler_vm_client.pl modules/compiler_vm/compiler_vm_server.pl modules/compiler_vm/compiler_watchdog.pl modules/compiler_vm/languages/_c_base.pm modules/compiler_vm/languages/_default.pm modules/compiler_vm/languages/bash.pm modules/compiler_vm/languages/bc.pm modules/compiler_vm/languages/bf.pm modules/compiler_vm/languages/c11.pm modules/compiler_vm/languages/c89.pm modules/compiler_vm/languages/c99.pm modules/compiler_vm/languages/clang.pm modules/compiler_vm/languages/clang11.pm modules/compiler_vm/languages/clang89.pm modules/compiler_vm/languages/clang99.pm modules/compiler_vm/languages/clangpp.pm modules/compiler_vm/languages/clisp.pm modules/compiler_vm/languages/cpp.pm modules/compiler_vm/languages/freebasic.pm modules/compiler_vm/languages/go.pm modules/compiler_vm/languages/haskell.pm modules/compiler_vm/languages/java.pm modules/compiler_vm/languages/javascript.pm modules/compiler_vm/languages/ksh.pm modules/compiler_vm/languages/lua.pm modules/compiler_vm/languages/perl.pm modules/compiler_vm/languages/python.pm modules/compiler_vm/languages/python3.pm modules/compiler_vm/languages/qbasic.pm modules/compiler_vm/languages/scheme.pm modules/compiler_vm/languages/server/_c_base.pm modules/compiler_vm/languages/server/_default.pm modules/compiler_vm/languages/server/c11.pm modules/compiler_vm/languages/server/c89.pm modules/compiler_vm/languages/server/c99.pm modules/compiler_vm/languages/server/clang.pm modules/compiler_vm/languages/server/clang11.pm modules/compiler_vm/languages/server/clang89.pm modules/compiler_vm/languages/server/clang99.pm modules/compiler_vm/languages/server/cpp.pm modules/compiler_vm/languages/server/freebasic.pm modules/compiler_vm/languages/server/haskell.pm modules/compiler_vm/languages/server/java.pm modules/compiler_vm/languages/server/qbasic.pm modules/compiler_vm/languages/server/tendra.pm modules/compiler_vm/languages/sh.pm modules/compiler_vm/languages/tendra.pm modules/compliment modules/cstd.pl modules/define.pl modules/dice_roll.pl modules/excuse.sh modules/expand_macros.pl modules/fnord.pl modules/funnyish_quote.pl modules/g.pl modules/gdefine.pl modules/gen_cfacts.pl modules/gencstd.pl modules/get_title.pl modules/getcfact.pl modules/google.pl modules/gspy.pl modules/gtop10.pl modules/gtop15.pl modules/headlines.pl modules/horoscope modules/horrorscope modules/ideone.pl modules/insult.pl modules/love_quote.pl modules/man.pl modules/map.pl modules/math.pl modules/prototype.pl modules/qalc.pl modules/random_quote.pl modules/seen.pl modules/urban modules/weather.pl modules/wikipedia.pl pbot.pl pbot.sh It is highly recommended that this list of files is reviewed to ensure that all files are the copyright of the sole maintainer of the repository. If any files with license headers contain the intellectual property of anyone else, it is recommended that a request is made to revise this patch or that the explicit permission of the co-author is gained to allow for the license of the work to be changed. I (Tomasz Kramkowski), the contributor, take no responsibility for any legal action taken against the maintainer of this repository for incorrectly claiming copyright to any work not owned by the maintainer of this repository.
2017-03-05 22:33:31 +01:00
2022-01-29 05:21:10 +01:00
use 5.020;
2022-01-29 05:21:10 +01:00
use warnings;
use strict;
2022-01-29 05:21:10 +01:00
use feature qw(signatures);
no warnings qw(experimental::signatures);
use IPC::Open2;
2022-01-29 05:21:10 +01:00
use JSON::XS;
use Data::Dumper;
2022-01-29 05:21:10 +01:00
# set debug flag from DEBUG env or here
my $debug = $ENV{DEBUG} // 0;
2022-01-29 05:21:10 +01:00
# output from gdb will be prefixed with GDB_PREFIX and suffixed with GDB_SUFFIX.
# e.g., to wrap gdb output with <>'s set GDB_PREFIX to "<" and GDB_SUFFIX to ">".
use constant {
GDB_PREFIX => "\n",
GDB_SUFFIX => "\n\n",
};
# files to read stdin and write stdout
use constant {
INPUT_FILENAME => '.input',
OUTPUT_FILENAME => '.output',
};
# GDB/MI message types
use constant {
STATUS => '+',
EXEC => '*',
NOTIFY => '=',
CONSOLE => '~',
TARGET => '@',
LOG => '&',
RESULT => '^',
};
# send a command to gdb
sub gdb_send($in, $command) {
print STDERR "-> $command\n" if $debug;
print $in "$command\n";
}
# read a line from gdb
sub gdb_read($out) {
my $line;
while ($line = <$out>) {
chomp $line;
2022-01-29 05:21:10 +01:00
print STDERR "<- [$line]\n" if $debug;
next if $line eq '(gdb) '; # ignore gdb prompt
last;
}
2022-01-29 05:21:10 +01:00
return undef if not defined $line;
return parse_gdbmi_output($line);
}
# send a command to gdb and return all ~console and ^result output
sub gdb_send_and_read_console($context, $command, $report_error = 1) {
gdb_send($context->{gdb_input}, $command);
2022-01-29 05:21:10 +01:00
my $gdb = $context->{gdb_output};
my @console;
my $output;
while ($output = gdb_read_console($gdb)) {
push @console, $output;
last if $output->{_type} eq RESULT;
}
2022-01-29 05:21:10 +01:00
if ($report_error && $output->{_type} eq RESULT && $output->{_class} eq 'error') {
print_gdb_output($context, $output->{msg});
}
2022-01-29 05:21:10 +01:00
return \@console;
}
# send a command to gdb and return only the ^result output
sub gdb_send_and_read_result($context, $command, $report_error = 1) {
gdb_send($context->{gdb_input}, $command);
my $gdb = $context->{gdb_output};
my $output;
while ($output = gdb_read($gdb)) {
last if $output->{_type} eq RESULT;
}
2022-01-29 05:21:10 +01:00
if ($report_error && $output->{_class} eq 'error') {
print_gdb_output($context, $output->{msg});
}
2022-01-29 05:21:10 +01:00
return $output;
}
2022-01-29 05:21:10 +01:00
# alias to send_and_read_result
*gdb_send_and_discard = \&gdb_send_and_read_result;
2022-01-29 05:21:10 +01:00
# read line from gdb, discarding types other than ~console or ^result
sub gdb_read_console($out) {
my $output;
2022-01-29 05:21:10 +01:00
while ($output = gdb_read($out)) {
last if $output->{_type} eq RESULT;
last if $output->{_type} eq CONSOLE;
}
2022-01-29 05:21:10 +01:00
return $output;
}
2022-01-29 05:21:10 +01:00
# read and discard all output until ^result is reached
sub gdb_read_result($out) {
my $output;
2022-01-29 05:21:10 +01:00
while ($output = gdb_read($out)) {
last if $output->{_type} eq RESULT;
}
return $output;
}
sub gdbmi_to_json($text, $makejson = 1) {
$text =~ s/([\w-]+)=/"$1":/g if $makejson;
return decode_json("{$text}");
}
sub parse_gdbmi_output($line) {
my ($type, $text) = $line =~ /^\d*(.)(.*)/;
my $class = 'none';
my $makejson = 0;
if ($type =~ /[+*=]/) {
($class, $text) = split /,/, $text, 2;
$makejson = 1 if length $text;
} elsif ($type eq RESULT) {
($class, $text) = split /,/, $text, 2;
$text //= '';
$makejson = 1 if length $text;
} else {
$text = "\"_text\":$text";
}
my $output = gdbmi_to_json($text, $makejson);
if (exists $output->{_text}) {
chomp $output->{_text};
}
$output->{_type} = $type;
$output->{_class} = $class;
print STDERR Dumper($output), "\n" if $debug >= 2;
return $output;
}
# get local variables at current frame
sub get_locals($context) {
# use `info locals` gdb command
my $console = gdb_send_and_read_console($context, "info locals");
my @locals;
foreach my $output (@$console) {
last if $output->{_type} eq RESULT;
if ($output->{_text} =~ /([^=]+)\s+=\s+(.*)/) {
push @locals, [$1, $2];
}
2022-01-29 05:21:10 +01:00
}
return \@locals;
}
# compare two lists of locals and returns list containing just the
# newly modified locals
sub compare_locals($old, $new) {
# build hashtable of left-side locals for easier access
my %ht;
foreach my $local (@$old) {
my ($ident, $value) = @$local;
$ht{$ident} = $value;
}
# check for modified locals and add to modified list
my @modified;
foreach my $local (@$new) {
my ($ident, $value) = @$local;
$ht{$ident} //= ''; # set non-existing key to empty string
if ($ht{$ident} ne $value) {
push @modified, [$ident, $value];
}
2022-01-29 05:21:10 +01:00
}
2022-01-29 05:21:10 +01:00
return \@modified;
}
sub locals_to_string($locals) {
my @strings;
2022-01-29 05:21:10 +01:00
foreach my $local (@$locals) {
my ($ident, $value) = @$local;
push @strings, "$ident = $value";
}
2022-01-29 05:21:10 +01:00
return join '; ', @strings;
}
sub args_to_string($args) {
my @strings;
2022-01-29 05:21:10 +01:00
foreach my $arg (@$args) {
push @strings, "$arg->{name}=$arg->{value}";
}
2022-01-29 05:21:10 +01:00
return join ', ', @strings;
}
# determine on which line numbers the main() function begins and ends
sub get_main_start_end($context) {
gdb_send_and_discard($context, 'set listsize unlimited');
2022-01-29 05:21:10 +01:00
my ($start, $end);
# use `info functions` to find where main starts
my $console = gdb_send_and_read_console($context, 'info functions -q ^main$');
foreach my $output (@$console) {
last if $output->{_type} eq RESULT;
($start) = $output->{_text} =~ /^(\d+)/;
}
2022-01-29 05:21:10 +01:00
$console = gdb_send_and_read_console($context, 'list main');
# gdb shows extra context surrounding main, so we have to parse the output
# and count the braces to find the true end of main()
my $braces = 0;
foreach my $output (@$console) {
last if $output->{_type} eq RESULT;
# we have already found end; don't parse output
last if $end;
my ($line, $code) = split /\t/, $output->{_text};
# this line isn't part of main() yet
next if $line < $start;
# blank out contents of string and char literals
$code =~ s/(?:\"((?:\\\"|(?!\").)*)\")/'"' . ('-' x length $1) . '"'/ge;
$code =~ s/(?:\'((?:\\\'|(?!\').)*)\')/"'" . ('-' x length $1) . "'"/ge;
while ($code =~ /(.)/g) {
my $char = $1;
if ($char eq '{') {
$braces++;
} elsif ($char eq '}') {
$braces--;
if ($braces == 0) {
$end = $line;
last;
}
}
}
}
2022-01-29 05:21:10 +01:00
return ($start, $end);
}
sub get_backtrace($context) {
my $console = gdb_send_and_read_console($context, "bt");
my @backtrace;
foreach my $output (@$console) {
last if $output->{_type} eq RESULT;
$output->{_text} =~ /in (\w+) (.*) at/;
push @backtrace, { func => $1, args => $2 };
}
2022-01-29 05:21:10 +01:00
return \@backtrace;
}
# returns the source line at a given line number
sub get_lineno($context, $lineno) {
my $console = gdb_send_and_read_console($context, "list $lineno,$lineno");
my $line;
foreach my $output (@$console) {
last if $output->{_type} eq RESULT;
($line) = $output->{_text} =~ /\t\s+(.*)/;
}
2022-01-29 05:21:10 +01:00
return $line;
}
sub handle_breakpoint_hit($context, $data) {
if ($data->{frame}->{func} eq 'main') {
if ($data->{frame}->{line} == $context->{main_end}) {
$context->{locals_end} = get_locals($context);
}
}
2022-01-29 05:21:10 +01:00
elsif ($data->{frame}->{func} eq 'gdb') {
my $command = $data->{frame}->{args}->[0]->{value};;
# strip gdb junk from command
$command =~ s/^.*?"//;
$command =~ s/"$//;
2022-01-29 05:21:10 +01:00
dispatch_user_command($context, $command);
}
2022-01-29 05:21:10 +01:00
gdb_send_and_discard($context, "cont");
}
2022-01-29 05:21:10 +01:00
# gdb commands invoked by end-users
sub dispatch_user_command($context, $command) {
state %commands = (
'ptype' => \&cmd_ptype,
'whatis' => \&cmd_whatis,
'print' => \&cmd_print,
'dump' => \&cmd_print,
2022-01-29 05:21:10 +01:00
# special PBot command
'print_last_statement' => \&cmd_print,
);
2022-01-29 05:21:10 +01:00
$command = unescape($command);
2022-01-29 05:21:10 +01:00
# move out of gdb() function to caller
gdb_send_and_discard($context, "up");
my ($cmd, $args) = split /\s+/, $command, 2;
if (not exists $commands{$cmd}) {
cmd_gdb($context, $command);
} else {
$commands{$cmd}->($context, $args);
}
}
sub cmd_gdb($context, $command) {
my $console = gdb_send_and_read_console($context, "$command");
my $text = '';
2022-01-29 05:21:10 +01:00
foreach my $output (@$console) {
last if $output->{_type} eq RESULT;
$text .= "$output->{_text}\n";
}
print_gdb_output($context, $text);
}
sub cmd_ptype($context, $args) {
my $console = gdb_send_and_read_console($context, "ptype $args");
my $text = '';
foreach my $output (@$console) {
last if $output->{_type} eq RESULT;
if ($output->{_text} =~ /type\s*=\s*(.*)/) {
$text .= "$args = $1"
} else {
$output->{_text} =~ s/^\s+//;
$text .= " $output->{_text}";
}
2022-01-29 05:21:10 +01:00
}
2022-01-29 05:21:10 +01:00
print_gdb_output($context, $text);
}
2022-01-29 05:21:10 +01:00
sub cmd_whatis($context, $args) {
my $console = gdb_send_and_read_console($context, "whatis $args");
2022-01-29 05:21:10 +01:00
foreach my $output (@$console) {
last if $output->{_type} eq RESULT;
2022-01-29 05:21:10 +01:00
if ($output->{_text} =~ /type\s*=\s*(.*)/) {
print_gdb_output($context, "$args = $1");
}
2022-01-29 05:21:10 +01:00
}
}
sub cmd_print($context, $args) {
my $console = gdb_send_and_read_console($context, "print $args");
foreach my $output (@$console) {
last if $output->{_type} eq RESULT;
2022-01-29 05:21:10 +01:00
if ($output->{_text} =~ /^\$\d+\s*=\s*(.*)/) {
print_gdb_output($context, "$args = $1");
}
2022-01-29 05:21:10 +01:00
}
}
sub handle_program_exit($context, $data) {
my $reason = $data->{reason};
if (not -s OUTPUT_FILENAME) { # -s gets size of file
my $locals = locals_to_string($context->{locals_end});
if (length $locals) {
print_gdb_output($context, "no output: $locals");
}
2022-01-29 05:21:10 +01:00
}
_exit($context);
}
sub handle_program_signal($context, $data) {
my $locals = locals_to_string(get_locals($context));
my $text = "Program received signal $data->{'signal-name'}, $data->{'signal-meaning'} ";
if ($data->{frame}->{func} eq '??') {
$text .= "in ?? ";
} else {
$text .= "in $data->{frame}->{func} ";
}
my $args = args_to_string($data->{frame}->{args});
$text .= "($args) ";
my $line;
if (exists $data->{frame}->{line}) {
$line = get_lineno($context, $data->{frame}->{line});
$text .= "at statement: $line ";
}
my $backtrace = get_backtrace($context);
shift @$backtrace; # remove current frame
foreach my $trace (@$backtrace) {
my $console = gdb_send_and_read_console($context, "up");
foreach my $output (@$console) {
last if $output->{_type} eq RESULT;
if ($output->{_text} =~ /^\d+\t\s+(.*)/) {
$line = $1;
}
}
2022-01-29 05:21:10 +01:00
$text .= "called by $trace->{func} $trace->{args} at statement: $line ";
}
if (length $locals) {
$text .= "<local variables: $locals>"
}
print_gdb_output($context, $text);
_exit($context);
}
sub unescape($text) {
state %unescape = (
"\\n" => "\n",
"\\t" => "\t",
"\\\\" => "\\",
"\\\"" => '"',
);
$text =~ s/(\\n|\\t|\\\\|\\")/$unescape{$1}/g;
return $text;
}
# dispatch output from gdb commands to handlers
sub dispatch_gdbmi_output($context, $output) {
state %dispatch = (
STATUS , \&handle_status_async_output,
EXEC , \&handle_exec_async_output,
NOTIFY , \&handle_notify_async_output,
CONSOLE , \&handle_console_stream_output,
TARGET , \&handle_target_stream_output,
LOG , \&handle_log_stream_output,
RESULT , \&handle_result_output,
);
print STDERR "dispatch: ", Dumper($output), "\n" if $debug >= 3;
$dispatch{$output->{_type}}->($context, $output);
}
2022-01-29 05:21:10 +01:00
# (+) status-async-output contains on-going status information about the progress
# of a slow operation. It can be discarded.
sub handle_status_async_output($context, $output) {
}
2022-01-29 05:21:10 +01:00
# (*) exec-async-output contains asynchronous state change on the target (stopped,
# started, disappeared).
sub handle_exec_async_output($context, $output) {
if ($output->{_class} eq 'stopped') {
my $reason = $output->{reason};
if ($reason eq 'breakpoint-hit') {
handle_breakpoint_hit($context, $output);
}
elsif ($reason eq 'exited-normally') {
handle_program_exit($context, $output);
}
elsif ($reason eq 'signal-received') {
handle_program_signal($context, $output);
}
}
2022-01-29 05:21:10 +01:00
}
2022-01-29 05:21:10 +01:00
# (=) notify-async-output contains supplementary information that the client should
# handle (e.g., a new breakpoint information).
sub handle_notify_async_output($context, $output) {
}
# (~) console-stream-output is output that should be displayed as is in the console.
# It is the textual response to a CLI command.
sub handle_console_stream_output($context, $output) {
if ($output->{_text} =~ /^Reading symbols/) {
perform_preamble($context);
run_program($context);
}
2022-01-29 05:21:10 +01:00
}
# (@) target-stream-output is the output produced by the target program.
sub handle_target_stream_output($context, $output) {
}
# (&) log-stream-output is output text coming from GDBs internals, for instance
# messages that should be displayed as part of an error log.
sub handle_log_stream_output($context, $output) {
}
# (^) result-output "done" | "running" | "connected" | "error" | "exit"
sub handle_result_output($context, $output) {
}
sub start_timeout($context, $timeout) {
$SIG{ALRM} = sub {
print_prog_output($context, "[gdb time-out]\n");
_exit($context);
};
alarm $timeout;
}
sub _exit($context) {
close $context->{prog_output};
2022-01-29 05:21:10 +01:00
open my $fh, '<', OUTPUT_FILENAME or die "could not open ".OUTPUT_FILENAME." for read: $!\n";
my $output = do { local $/; <$fh> };
close $fh;
2022-01-29 05:21:10 +01:00
print STDOUT "$output\n";
2022-01-29 05:21:10 +01:00
exit;
}
2022-01-29 05:21:10 +01:00
# send text to OUTPUT_FILENAME file
sub print_prog_output($context, $text) {
print { $context->{prog_output} } $text;
$context->{prog_output}->flush();
}
2022-01-29 05:21:10 +01:00
# send gdb output to OUTPUT_FILENAME file, wrapped with prefix and suffix
sub print_gdb_output($context, $text) {
print { $context->{prog_output} } GDB_PREFIX . $text . GDB_SUFFIX;
$context->{prog_output}->flush();
}
2022-01-29 05:21:10 +01:00
sub perform_preamble($context) {
# get start and end line numbers for main() function
my ($start, $end) = get_main_start_end($context);
$context->{main_start} = $start;
$context->{main_end} = $end;
# break on main start and end so we can get locals
gdb_send_and_discard($context, "break $start");
gdb_send_and_discard($context, "break $end");
2022-01-29 05:21:10 +01:00
# break on gdb() function so we can pass along user-submitted gdb commands
gdb_send_and_discard($context, "break gdb");
gdb_send_and_discard($context, "set print null-stop on");
}
2022-01-29 05:21:10 +01:00
sub shellquote_args_to_string(@args) {
my $string = '';
foreach my $arg (@args) {
$arg =~ s/'/'"'"'/g;
$string .= "'$arg' ";
}
return $string;
}
sub run_program($context) {
my $cmdline_args = shellquote_args_to_string(@ARGV);
gdb_send($context->{gdb_input}, "run $cmdline_args < ".INPUT_FILENAME." >> ".OUTPUT_FILENAME);
}
sub main {
# first command-line argument can override file to debug
my $prog = $ARGV[0] // './prog';
2022-01-29 05:21:10 +01:00
# start gdb and grab references to its input and output streams
open2(my $out, my $in, "LIBC_FATAL_STDERR=1 MALLOC_CHECK_=1 gdb -i mi3 -q -nx $prog");
2022-01-29 05:21:10 +01:00
my $context = {
gdb_output => $out, # gdb output stream
gdb_input => $in, # gdb input stream
};
# open OUTPUT_FILENAME file to send gdb output
unlink OUTPUT_FILENAME;
open($context->{prog_output}, '>>', OUTPUT_FILENAME) or die "Could not open ".OUTPUT_FILENAME." for append: $!";
# time-out after a few seconds
start_timeout($context, 8);
# handle gdb output
while (my $output = gdb_read($out)) {
dispatch_gdbmi_output($context, $output);
}
}
2022-01-29 05:21:10 +01:00
main();