User Tools

Site Tools


gdb

This is an old revision of the document!


GDB

Официальный сайт https://www.sourceware.org/gdb/

git clone https://sourceware.org/git/binutils-gdb.git
cd binutils-gdb
git checkout gdb-15.2-release

Компиляция

wget https://ftp.gnu.org/gnu/gdb/gdb-15.2.tar.xz
tar xf gdb-15.2.tar.xz 
cd gdb-15.2
./configure --prefix=/usr/local/gdb
make
sudo make install

lookup_cmd() - поиск по списку cmdlist
execute_command()
command_handler()

при обработке команд call и print вызывается функция print_command_1() из нее вызывается value_print(), а из нее - apply_ext_lang_val_pretty_printer() и c_value_print_inner()

Для тестирования
b exec_simple_query
print *parsetree_list


gdbtypes.c

static bool type_is_Node(struct type *type)
{
return type->code () == TYPE_CODE_STRUCT && type->num_fields () > 0 
		   && strcmp(type->field (0).name (), "type") == 0
		   && strcmp(type->field (0).type ()->name (), "NodeTag") == 0;
}

static struct type * get_type_from_NodeTag(struct type *nodeType, CORE_ADDR addr)
{
struct type *typeNodeTag = check_typedef (nodeType->field (0).type ());
LONGEST enumval = read_memory_unsigned_integer(addr, typeNodeTag->length(), BFD_ENDIAN_LITTLE);

unsigned enumLen = typeNodeTag->num_fields ();
for (unsigned i = 0; i < enumLen; i++)
	if (enumval == typeNodeTag->field (i).loc_enumval ())
	{
		const char *type_name = typeNodeTag->field (i).name ();
		if (strlen(type_name) <= 2 || type_name[0] != 'T' || type_name[1] != '_')
			break;

		type_name += 2;
		return lookup_typename (current_language, type_name, NULL, 0);
	}

return nullptr;
}
/* Worker for resolved_dynamic_type.  */

static struct type *
resolve_dynamic_type_internal (struct type *type,
		       struct property_addr_info *addr_stack,
		       const frame_info_ptr &frame,
		       bool top_level)
{
struct type *real_type = check_typedef (type);
struct type *resolved_type = nullptr;
struct dynamic_prop *prop;
CORE_ADDR value;

if (real_type->code () == TYPE_CODE_PTR)
{
  real_type = check_typedef (real_type->target_type());
  if (type_is_Node(real_type))
	{
		CORE_ADDR ptr;
		read_memory (addr_stack->addr, (gdb_byte*)&ptr, sizeof(ptr));
		struct type *t = get_type_from_NodeTag(real_type, ptr);
		if (t == nullptr)
			return type;
		return lookup_pointer_type(t);
	}
}
else if (type_is_Node(real_type))
{
	struct type *t = get_type_from_NodeTag(real_type, addr_stack->addr);
	if (t == nullptr)
		return type;
	return check_typedef(t);
}

if (!is_dynamic_type_internal (real_type, top_level))
  return type;

еще вариант, но он не работает с IDE

void
cp_print_value_fields (struct value *val, struct ui_file *stream,
	       int recurse, const struct value_print_options *options,
	       struct type **dont_print_vb,
	       int dont_print_statmem)
{
int i, len, n_baseclasses;
int fields_seen = 0;
static int last_set_recurse = -1;

struct type *type = check_typedef (val->type ());

if (type->code () == TYPE_CODE_STRUCT && type->num_fields () > 0 
     && strcmp(type->field (0).name (), "type") == 0
     && strcmp(type->field (0).type ()->name (), "NodeTag") == 0)
  {
      struct type *type0 = check_typedef (type->field (0).type ());
	struct value *v0 = val->primitive_field (0, 0, type);
	const gdb_byte *valaddr = v0->contents_for_printing ().data ();

	LONGEST enumval = unpack_long (type0, valaddr);

	unsigned int len0 = type0->num_fields ();
	for (unsigned int i0 = 0; i0 < len0; i0++)
		if (enumval == type0->field (i0).loc_enumval ())
		{
			const char *tname = type0->field (i0).name ();
			if (strlen(tname) <= 2 || tname[0] != 'T' || tname[1] != '_')
				break;

			tname += 2;
			type = check_typedef (lookup_typename (current_language, tname, NULL, 0));
			val->deprecated_set_type(type);
			val->set_enclosing_type(type);
			break;
		}
  }

Сделать сборку статической в изолированном окружении

$ ldd /usr/local/gdb/bin/gdb
linux-vdso.so.1 (0x00007ffd631b9000)
libzstd.so.1 => /lib64/libzstd.so.1 (0x00007f4bb00fd000)
libncursesw.so.5 => /usr/lib64/libncursesw.so.5 (0x00007f4bb00ca000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f4baf1cf000)
libexpat.so.1 => /lib64/libexpat.so.1 (0x00007f4baf19e000)
liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f4baf174000)
libmpfr.so.6 => /usr/lib64/libmpfr.so.6 (0x00007f4baee00000)
libgmp.so.10 => /usr/lib64/libgmp.so.10 (0x00007f4baf0fa000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f4baf0f4000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f4baea00000)
libm.so.6 => /lib64/libm.so.6 (0x00007f4baecbb000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f4baf0da000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f4baf0b9000)
libc.so.6 => /lib64/libc.so.6 (0x00007f4bae827000)
libgpm.so.1 => /usr/lib64/libgpm.so.1 (0x00007f4baf0af000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4bb01d3000)
gdb.1733990571.txt.gz · Last modified: 2024/12/12 08:02 by keremet