| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
/** |
| 5 |
* @file |
| 6 |
* |
| 7 |
* Wrapper around drgn.h with internal-only definitions. |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef DRGN_INTERNAL_H |
| 11 |
#define DRGN_INTERNAL_H |
| 12 |
|
| 13 |
#include <assert.h> |
| 14 |
|
| 15 |
// Used for functions that are inline inside of libdrgn and also have an |
| 16 |
// external definition. |
| 17 |
#ifndef DRGN_ACCESSOR_LINKAGE |
| 18 |
#define DRGN_ACCESSOR_LINKAGE static inline |
| 19 |
#endif |
| 20 |
|
| 21 |
#include "drgn.h" // IWYU pragma: export |
| 22 |
|
| 23 |
enum drgn_type_flags { |
| 24 |
DRGN_TYPE_FLAG_IS_COMPLETE = 1 << 0, |
| 25 |
DRGN_TYPE_FLAG_IS_SIGNED = 1 << 1, |
| 26 |
DRGN_TYPE_FLAG_LITTLE_ENDIAN = 1 << 2, |
| 27 |
DRGN_TYPE_FLAG_IS_VARIADIC = 1 << 3, |
| 28 |
}; |
| 29 |
|
| 30 |
// Common part of all types. In order to keep it as compact as possible, it uses |
| 31 |
// a few unions and can be embedded in one of the structs below for types that |
| 32 |
// need more. |
| 33 |
// |
| 34 |
// Don't access this directly. Use the getter functions. |
| 35 |
struct drgn_type { |
| 36 |
/** @privatesection */ |
| 37 |
enum drgn_type_kind _kind; |
| 38 |
enum drgn_primitive_type _primitive; |
| 39 |
// These are the qualifiers for the wrapped type, not this type. |
| 40 |
enum drgn_qualifiers _qualifiers; |
| 41 |
enum drgn_type_flags _flags; |
| 42 |
struct drgn_program *_program; |
| 43 |
const struct drgn_language *_language; |
| 44 |
union { |
| 45 |
const char *_name; |
| 46 |
const char *_tag; |
| 47 |
struct drgn_type_parameter *_parameters; |
| 48 |
}; |
| 49 |
union { |
| 50 |
uint64_t _size; |
| 51 |
uint64_t _length; |
| 52 |
struct drgn_type_enumerator *_enumerators; |
| 53 |
size_t _num_parameters; |
| 54 |
}; |
| 55 |
union { |
| 56 |
struct drgn_type *_type; |
| 57 |
struct drgn_type_member *_members; |
| 58 |
}; |
| 59 |
}; |
| 60 |
|
| 61 |
struct drgn_extended_type { |
| 62 |
struct drgn_type type; |
| 63 |
// This is a layer violation; it'd be cleaner to have an opaque context |
| 64 |
// and callback table that could be passed when constructing a type. But |
| 65 |
// for now, we're only supporting this for DWARF. |
| 66 |
uintptr_t _die_addr; |
| 67 |
}; |
| 68 |
|
| 69 |
struct drgn_templated_type { |
| 70 |
struct drgn_extended_type extended; |
| 71 |
struct drgn_type_template_parameter *_template_parameters; |
| 72 |
size_t _num_template_parameters; |
| 73 |
}; |
| 74 |
|
| 75 |
struct drgn_compound_type { |
| 76 |
struct drgn_templated_type templated; |
| 77 |
size_t _num_members; |
| 78 |
}; |
| 79 |
|
| 80 |
struct drgn_enum_type { |
| 81 |
struct drgn_extended_type extended; |
| 82 |
size_t _num_enumerators; |
| 83 |
}; |
| 84 |
|
| 85 |
DRGN_ACCESSOR_LINKAGE |
| 86 |
enum drgn_type_kind drgn_type_kind(struct drgn_type *type) |
| 87 |
{ |
| 88 |
return type->_kind; |
| 89 |
} |
| 90 |
|
| 91 |
DRGN_ACCESSOR_LINKAGE |
| 92 |
enum drgn_primitive_type drgn_type_primitive(struct drgn_type *type) |
| 93 |
{ |
| 94 |
return type->_primitive; |
| 95 |
} |
| 96 |
|
| 97 |
DRGN_ACCESSOR_LINKAGE |
| 98 |
bool drgn_type_is_complete(struct drgn_type *type) |
| 99 |
{ |
| 100 |
return type->_flags & DRGN_TYPE_FLAG_IS_COMPLETE; |
| 101 |
} |
| 102 |
|
| 103 |
DRGN_ACCESSOR_LINKAGE |
| 104 |
struct drgn_program *drgn_type_program(struct drgn_type *type) |
| 105 |
{ |
| 106 |
return type->_program; |
| 107 |
} |
| 108 |
|
| 109 |
DRGN_ACCESSOR_LINKAGE |
| 110 |
const struct drgn_language *drgn_type_language(struct drgn_type *type) |
| 111 |
{ |
| 112 |
return type->_language; |
| 113 |
} |
| 114 |
|
| 115 |
DRGN_ACCESSOR_LINKAGE |
| 116 |
const char *drgn_type_name(struct drgn_type *type) |
| 117 |
{ |
| 118 |
assert(drgn_type_has_name(type)); |
| 119 |
return type->_name; |
| 120 |
} |
| 121 |
|
| 122 |
DRGN_ACCESSOR_LINKAGE |
| 123 |
uint64_t drgn_type_size(struct drgn_type *type) |
| 124 |
{ |
| 125 |
assert(drgn_type_has_size(type)); |
| 126 |
return type->_size; |
| 127 |
} |
| 128 |
|
| 129 |
DRGN_ACCESSOR_LINKAGE |
| 130 |
bool drgn_type_is_signed(struct drgn_type *type) |
| 131 |
{ |
| 132 |
assert(drgn_type_has_is_signed(type)); |
| 133 |
return type->_flags & DRGN_TYPE_FLAG_IS_SIGNED; |
| 134 |
} |
| 135 |
|
| 136 |
DRGN_ACCESSOR_LINKAGE |
| 137 |
bool drgn_type_little_endian(struct drgn_type *type) |
| 138 |
{ |
| 139 |
assert(drgn_type_has_little_endian(type)); |
| 140 |
return type->_flags & DRGN_TYPE_FLAG_LITTLE_ENDIAN; |
| 141 |
} |
| 142 |
|
| 143 |
DRGN_ACCESSOR_LINKAGE |
| 144 |
const char *drgn_type_tag(struct drgn_type *type) |
| 145 |
{ |
| 146 |
assert(drgn_type_has_tag(type)); |
| 147 |
return type->_tag; |
| 148 |
} |
| 149 |
|
| 150 |
DRGN_ACCESSOR_LINKAGE |
| 151 |
struct drgn_type_member *drgn_type_members(struct drgn_type *type) |
| 152 |
{ |
| 153 |
assert(drgn_type_has_members(type)); |
| 154 |
return type->_members; |
| 155 |
} |
| 156 |
|
| 157 |
DRGN_ACCESSOR_LINKAGE |
| 158 |
size_t drgn_type_num_members(struct drgn_type *type) |
| 159 |
{ |
| 160 |
assert(drgn_type_has_members(type)); |
| 161 |
return ((struct drgn_compound_type *)type)->_num_members; |
| 162 |
} |
| 163 |
|
| 164 |
DRGN_ACCESSOR_LINKAGE |
| 165 |
struct drgn_qualified_type drgn_type_type(struct drgn_type *type) |
| 166 |
{ |
| 167 |
assert(drgn_type_has_type(type)); |
| 168 |
return (struct drgn_qualified_type){ |
| 169 |
.type = type->_type, |
| 170 |
.qualifiers = type->_qualifiers, |
| 171 |
}; |
| 172 |
} |
| 173 |
|
| 174 |
DRGN_ACCESSOR_LINKAGE |
| 175 |
struct drgn_type_enumerator *drgn_type_enumerators(struct drgn_type *type) |
| 176 |
{ |
| 177 |
assert(drgn_type_has_enumerators(type)); |
| 178 |
return type->_enumerators; |
| 179 |
} |
| 180 |
|
| 181 |
DRGN_ACCESSOR_LINKAGE |
| 182 |
size_t drgn_type_num_enumerators(struct drgn_type *type) |
| 183 |
{ |
| 184 |
assert(drgn_type_has_enumerators(type)); |
| 185 |
return ((struct drgn_enum_type *)type)->_num_enumerators; |
| 186 |
} |
| 187 |
|
| 188 |
DRGN_ACCESSOR_LINKAGE |
| 189 |
uint64_t drgn_type_length(struct drgn_type *type) |
| 190 |
{ |
| 191 |
assert(drgn_type_has_length(type)); |
| 192 |
return type->_length; |
| 193 |
} |
| 194 |
|
| 195 |
DRGN_ACCESSOR_LINKAGE |
| 196 |
struct drgn_type_parameter *drgn_type_parameters(struct drgn_type *type) |
| 197 |
{ |
| 198 |
assert(drgn_type_has_parameters(type)); |
| 199 |
return type->_parameters; |
| 200 |
} |
| 201 |
|
| 202 |
DRGN_ACCESSOR_LINKAGE |
| 203 |
size_t drgn_type_num_parameters(struct drgn_type *type) |
| 204 |
{ |
| 205 |
assert(drgn_type_has_parameters(type)); |
| 206 |
return type->_num_parameters; |
| 207 |
} |
| 208 |
|
| 209 |
DRGN_ACCESSOR_LINKAGE |
| 210 |
bool drgn_type_is_variadic(struct drgn_type *type) |
| 211 |
{ |
| 212 |
assert(drgn_type_has_is_variadic(type)); |
| 213 |
return type->_flags & DRGN_TYPE_FLAG_IS_VARIADIC; |
| 214 |
} |
| 215 |
|
| 216 |
DRGN_ACCESSOR_LINKAGE |
| 217 |
struct drgn_type_template_parameter * |
| 218 |
drgn_type_template_parameters(struct drgn_type *type) |
| 219 |
{ |
| 220 |
assert(drgn_type_has_template_parameters(type)); |
| 221 |
return ((struct drgn_templated_type *)type)->_template_parameters; |
| 222 |
} |
| 223 |
|
| 224 |
DRGN_ACCESSOR_LINKAGE |
| 225 |
size_t drgn_type_num_template_parameters(struct drgn_type *type) |
| 226 |
{ |
| 227 |
assert(drgn_type_has_template_parameters(type)); |
| 228 |
return ((struct drgn_templated_type *)type)->_num_template_parameters; |
| 229 |
} |
| 230 |
|
| 231 |
static inline bool drgn_type_kind_has_die_addr(enum drgn_type_kind kind) |
| 232 |
{ |
| 233 |
return (kind == DRGN_TYPE_STRUCT || |
| 234 |
kind == DRGN_TYPE_UNION || |
| 235 |
kind == DRGN_TYPE_CLASS || |
| 236 |
kind == DRGN_TYPE_ENUM || |
| 237 |
kind == DRGN_TYPE_FUNCTION); |
| 238 |
} |
| 239 |
|
| 240 |
static inline bool drgn_type_has_die_addr(struct drgn_type *type) |
| 241 |
{ |
| 242 |
return drgn_type_kind_has_die_addr(drgn_type_kind(type)); |
| 243 |
} |
| 244 |
|
| 245 |
static inline uintptr_t drgn_type_die_addr(struct drgn_type *type) |
| 246 |
{ |
| 247 |
assert(drgn_type_has_die_addr(type)); |
| 248 |
return ((struct drgn_extended_type *)type)->_die_addr; |
| 249 |
} |
| 250 |
|
| 251 |
static inline void drgn_type_init_die_addr(struct drgn_type *type, |
| 252 |
uintptr_t die_addr) |
| 253 |
{ |
| 254 |
assert(drgn_type_has_die_addr(type)); |
| 255 |
((struct drgn_extended_type *)type)->_die_addr = die_addr; |
| 256 |
} |
| 257 |
|
| 258 |
#endif /* DRGN_INTERNAL_H */ |