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-03-22 23:27:46 +00:00
|
|
|
// Copyright 2018-2019 - Omar Sandoval
|
|
|
|
// SPDX-License-Identifier: GPL-3.0+
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* Language support.
|
|
|
|
*
|
|
|
|
* See @ref Languages.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DRGN_LANGUAGE_H
|
|
|
|
#define DRGN_LANGUAGE_H
|
|
|
|
|
|
|
|
#include "drgn.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup Internals
|
|
|
|
*
|
|
|
|
* @defgroup Languages Languages
|
|
|
|
*
|
|
|
|
* Language support.
|
|
|
|
*
|
|
|
|
* This defines the interface which support for a language must implement,
|
|
|
|
* including operators and parsing.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct drgn_type_index;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Language implementation.
|
|
|
|
*
|
|
|
|
* This mainly provides callbacks used to implement the higher-level libdrgn
|
|
|
|
* helpers. These callbacks handle the language-specific parts of the helpers.
|
|
|
|
*
|
|
|
|
* In particular, the operator callbacks should do appropriate type checking for
|
|
|
|
* the language and call the implementation in @ref ObjectInternals.
|
|
|
|
*/
|
|
|
|
struct drgn_language {
|
|
|
|
/** Name of this programming language. */
|
|
|
|
const char *name;
|
|
|
|
/** Implement @ref drgn_pretty_print_type_name(). */
|
|
|
|
struct drgn_error *(*pretty_print_type_name)(struct drgn_qualified_type,
|
|
|
|
char **);
|
|
|
|
/** Implement @ref drgn_pretty_print_type(). */
|
|
|
|
struct drgn_error *(*pretty_print_type)(struct drgn_qualified_type,
|
|
|
|
char **);
|
|
|
|
/** Implement @ref drgn_pretty_print_object(). */
|
|
|
|
struct drgn_error *(*pretty_print_object)(const struct drgn_object *,
|
|
|
|
size_t, char **);
|
|
|
|
/**
|
|
|
|
* Implement @ref drgn_type_index_find().
|
|
|
|
*
|
|
|
|
* This should parse @p name and call @ref
|
|
|
|
* drgn_type_index_find_internal().
|
|
|
|
*/
|
|
|
|
struct drgn_error *(*find_type)(struct drgn_type_index *tindex,
|
|
|
|
const char *name, const char *filename,
|
|
|
|
struct drgn_qualified_type *ret);
|
|
|
|
/**
|
|
|
|
* Get the offset of a member in a type.
|
|
|
|
*
|
|
|
|
* This should parse @p member_designator (which may include one or more
|
|
|
|
* member references and zero or more array subscripts) and calculate
|
|
|
|
* the offset, in bits, of that member from the beginning of @p type.
|
|
|
|
*/
|
|
|
|
struct drgn_error *(*bit_offset)(struct drgn_program *prog,
|
|
|
|
struct drgn_type *type,
|
|
|
|
const char *member_designator,
|
|
|
|
uint64_t *ret);
|
|
|
|
/**
|
|
|
|
* Set an object to an integer literal.
|
|
|
|
*
|
|
|
|
* This should set @p res to the given value and appropriate type for an
|
|
|
|
* integer literal in the language.
|
|
|
|
*/
|
|
|
|
struct drgn_error *(*integer_literal)(struct drgn_object *res,
|
|
|
|
uint64_t uvalue);
|
|
|
|
/**
|
|
|
|
* Set an object to a boolean literal.
|
|
|
|
*
|
|
|
|
* This should set @p res to the given value and the boolean type in the
|
|
|
|
* language.
|
|
|
|
*/
|
|
|
|
struct drgn_error *(*bool_literal)(struct drgn_object *res,
|
|
|
|
bool bvalue);
|
|
|
|
/**
|
|
|
|
* Set an object to a floating-point literal.
|
|
|
|
*
|
|
|
|
* This should set @p res to the given value and appropriate type for a
|
|
|
|
* floating-point literal in the language.
|
|
|
|
*/
|
|
|
|
struct drgn_error *(*float_literal)(struct drgn_object *res,
|
|
|
|
double fvalue);
|
2019-04-13 00:37:28 +01:00
|
|
|
/** Implement @ref drgn_object_cast(). */
|
|
|
|
struct drgn_error *(*op_cast)(struct drgn_object *res,
|
|
|
|
struct drgn_qualified_type qualified_type,
|
|
|
|
const struct drgn_object *obj);
|
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-03-22 23:27:46 +00:00
|
|
|
struct drgn_error *(*op_bool)(const struct drgn_object *obj, bool *ret);
|
|
|
|
struct drgn_error *(*op_cmp)(const struct drgn_object *lhs,
|
|
|
|
const struct drgn_object *rhs, int *ret);
|
|
|
|
drgn_binary_op *op_add;
|
|
|
|
drgn_binary_op *op_sub;
|
|
|
|
drgn_binary_op *op_mul;
|
|
|
|
drgn_binary_op *op_div;
|
|
|
|
drgn_binary_op *op_mod;
|
|
|
|
drgn_binary_op *op_lshift;
|
|
|
|
drgn_binary_op *op_rshift;
|
|
|
|
drgn_binary_op *op_and;
|
|
|
|
drgn_binary_op *op_or;
|
|
|
|
drgn_binary_op *op_xor;
|
|
|
|
drgn_unary_op *op_pos;
|
|
|
|
drgn_unary_op *op_neg;
|
|
|
|
drgn_unary_op *op_not;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** The C programming language. */
|
|
|
|
extern const struct drgn_language drgn_language_c;
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
#endif /* DRGN_LANGUAGE_H */
|