drgn/libdrgn/object_index.c
Omar Sandoval 75c3679147 Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:

- It's too slow for some common use cases, like iterating over large
  data structures.
- It can't be reused in utilities written in other languages.

This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:

- Types are now represented by a single Type class rather than the messy
  polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
  functions.

The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.

Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-04-02 14:12:07 -07:00

67 lines
1.6 KiB
C

// Copyright 2018-2019 - Omar Sandoval
// SPDX-License-Identifier: GPL-3.0+
#include <string.h>
#include "internal.h"
#include "object_index.h"
#include "type.h"
struct drgn_error *
drgn_partial_object_from_enumerator(struct drgn_partial_object *pobj,
const char *name)
{
struct drgn_type *type = pobj->qualified_type.type;
size_t num_enumerators, i;
const struct drgn_type_enumerator *enumerators;
pobj->is_enumerator = true;
pobj->little_endian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
enumerators = drgn_type_enumerators(type);
num_enumerators = drgn_type_num_enumerators(type);
for (i = 0; i < num_enumerators; i++) {
if (strcmp(enumerators[i].name, name) != 0)
continue;
if (drgn_enum_type_is_signed(type))
pobj->svalue = enumerators[i].svalue;
else
pobj->svalue = enumerators[i].uvalue;
return NULL;
}
return drgn_error_format(DRGN_ERROR_LOOKUP,
"could not find '%s' in 'enum %s'", name,
drgn_type_is_anonymous(type) ?
"<anonymous>" : drgn_type_tag(type));
}
struct drgn_error *
drgn_object_index_not_found_error(const char *name, const char *filename,
enum drgn_find_object_flags flags)
{
const char *kind;
switch (flags) {
case DRGN_FIND_OBJECT_CONSTANT:
kind = "constant ";
break;
case DRGN_FIND_OBJECT_FUNCTION:
kind = "function ";
break;
case DRGN_FIND_OBJECT_VARIABLE:
kind = "variable ";
break;
default:
kind = "";
break;
}
if (filename) {
return drgn_error_format(DRGN_ERROR_LOOKUP,
"could not find %s'%s' in '%s'", kind,
name, filename);
} else {
return drgn_error_format(DRGN_ERROR_LOOKUP,
"could not find %s'%s'", kind, name);
}
}