gdb/python: add py-jit.exp

This commit is contained in:
Jan Vrany 2022-04-07 10:06:52 +02:00
parent d25a69ad17
commit fe9dd76b75
3 changed files with 195 additions and 0 deletions

View File

@ -0,0 +1,56 @@
/* Copyright (C) 2022-2022 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
/* "JIT-ed" function, with the prototype `long (long, long)`. */
static const unsigned char jit_function_add_code[] = {
0x48, 0x01, 0xfe, /* add %rdi,%rsi */
0x48, 0x89, 0xf0, /* mov %rsi,%rax */
0xc3, /* retq */
};
void jit_register_code (char * name, uintptr_t code, size_t size)
{
}
int
main (int argc, char **argv)
{
void *code = mmap (NULL, getpagesize (), PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
assert (code != MAP_FAILED);
/* "Compile" jit_function_add. */
memcpy (code, jit_function_add_code,
sizeof (jit_function_add_code));
jit_register_code("jit_function_add", (uintptr_t)code, sizeof (jit_function_add_code));
assert (((long (*)(long, long))code)(1,5) == 6); /* breakpoint 1 line */
return 0;
}

View File

@ -0,0 +1,93 @@
# Copyright (C) 2010-2022 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This file is part of the GDB testsuite. It test the Python API to
# create dynamic (JIT) symbols.
load_lib gdb-python.exp
standard_testfile
if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
return -1
}
if ![runto_main] {
return 0
}
if { [skip_python_tests] } { continue }
set remote_python_file [gdb_remote_download host \
${srcdir}/${subdir}/${testfile}.py]
gdb_test_no_output "source ${remote_python_file}" "load python file"
gdb_breakpoint [gdb_get_line_number "breakpoint 1 line" ${testfile}.c]
gdb_run_cmd
gdb_interact
#
# Test creating objfile dynamically from Python
#
gdb_py_test_silent_cmd "python objfile = gdb.Objfile(\"jit\")" \
"create objfile" 1
gdb_test "python print(objfile)" \
"<gdb.Objfile filename=jit>" \
"print dynamic objfile"
gdb_test "python print (objfile.is_dynamic)" "True" \
"Check is_dynamic() returns True for Python-created objfile"
gdb_py_test_silent_cmd "python symtab = gdb.Symtab(objfile, \"jit.st\")" \
"create symtab" 1
gdb_test "python print(symtab)" \
"jit.st" \
"test str(gdb.Symtab)"
gdb_test "python print(symtab.filename)" \
"jit.st" \
"test gdb.Symtab.filename"
gdb_test "python print(symtab.fullname())" \
"jit.st" \
"test gdb.Symtab.fullname()"
gdb_test "python print(symtab.objfile == objfile)" \
"True" \
"test gdb.Symtab.objfile"
gdb_test "python print(symtab.global_block().start)" \
"0" \
"test gdb.Symtab.global_block().start"
gdb_test "python print(symtab.global_block().end)" \
"0" \
"test gdb.Symtab.global_block().end"
gdb_py_test_silent_cmd "python block = symtab.add_block('jit_func_1', 0xcafe0000, 0xcafe00ff)" \
"test symtab.add_block()" 1
gdb_test "python print(hex(symtab.global_block().start))" \
"0xcafe0000" \
"test gdb.Symtab.global_block().start after adding new block"
gdb_test "python print(hex(symtab.global_block().end))" \
"0xcafe00ff" \
"test gdb.Symtab.global_block().end after adding new block"

View File

@ -0,0 +1,46 @@
# Copyright (C) 2022-2022 Free Software Foundation, Inc.
#
# This file is part of GDB.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. */
import gdb
objfile = None
symtab = None
class JITRegisterCode(gdb.Breakpoint):
def stop(self):
global objfile
global symtab
frame = gdb.newest_frame()
name = frame.read_var('name').string()
code = int(frame.read_var('code'))
size = int(frame.read_var('size'))
objfile = gdb.Objfile(name)
symtab = gdb.Symtab(objfile, "py-jit.c")
symtab.add_block(name, code, code + size)
symtab.set_linetable([
gdb.LineTableEntry(29, code, True),
gdb.LineTableEntry(30, code+3, True),
gdb.LineTableEntry(31, code+6, True)
])
return False # Continue execution
JITRegisterCode("jit_register_code", internal=True)