Skip to content

Commit 347ff46

Browse files
author
Prakash Surya
authored
Merge pull request #19 from delphix/master
Merge branch 'master' into '6.0/stage'
2 parents 266b7f4 + 974e0ce commit 347ff46

27 files changed

+801
-444
lines changed

docs/advanced_usage.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,9 @@ Some of drgn's behavior can be modified through environment variables:
9595
``DRGN_USE_LIBKDUMPFILE_FOR_ELF``
9696
Whether drgn should use libkdumpfile for ELF vmcores (0 or 1). The default
9797
is 0. This functionality will be removed in the future.
98+
99+
``DRGN_USE_PROC_AND_SYS_MODULES``
100+
Whether drgn should use ``/proc/modules`` and ``/sys/module`` to find
101+
loaded kernel modules for the running kernel instead of getting them from
102+
the core dump (0 or 1). The default is 1. This environment variable is
103+
mainly intended for testing and may be ignored in the future.

libdrgn/Doxyfile

Lines changed: 176 additions & 118 deletions
Large diffs are not rendered by default.

libdrgn/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ARCH_INS = arch_x86_64.c.in
1919

2020
libdrgnimpl_la_SOURCES = $(ARCH_INS:.c.in=.c) \
2121
binary_search_tree.h \
22+
bitops.h \
2223
cityhash.h \
2324
debug_info.c \
2425
debug_info.h \
@@ -38,6 +39,7 @@ libdrgnimpl_la_SOURCES = $(ARCH_INS:.c.in=.c) \
3839
linux_kernel_helpers.c \
3940
memory_reader.c \
4041
memory_reader.h \
42+
minmax.h \
4143
mread.h \
4244
object.c \
4345
object.h \
@@ -47,6 +49,7 @@ libdrgnimpl_la_SOURCES = $(ARCH_INS:.c.in=.c) \
4749
path.h \
4850
platform.c \
4951
platform.h \
52+
pp.h \
5053
program.c \
5154
program.h \
5255
serialize.c \

libdrgn/bitops.h

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright (c) Facebook, Inc. and its affiliates.
2+
// SPDX-License-Identifier: GPL-3.0+
3+
4+
/**
5+
* @file
6+
*
7+
* Bitwise operations.
8+
*
9+
* See @ref BitwiseOperations.
10+
*/
11+
12+
#ifndef DRGN_BITOPS_H
13+
#define DRGN_BITOPS_H
14+
15+
#include "pp.h"
16+
17+
/**
18+
* @ingroup Internals
19+
*
20+
* @defgroup BitwiseOperations Bitwise operations
21+
*
22+
* Generic bitwise operations.
23+
*
24+
* @{
25+
*/
26+
27+
/**
28+
* Count Trailing Zero bits.
29+
*
30+
* Return the number of trailing least significant 0-bits in @p x. This is
31+
* undefined if @p x is zero.
32+
*
33+
* ```
34+
* ctz(1) == ctz(0b1) == 0
35+
* ctz(2) == ctz(0b10) == 1
36+
* ctz(12) == ctz(0b1100) == 2
37+
* ```
38+
*
39+
* @param[in] x Integer.
40+
*/
41+
#define ctz(x) generic_bitop(x, PP_UNIQUE(_x), __builtin_ctz)
42+
43+
/**
44+
* Find Last Set bit.
45+
*
46+
* Return the one-based index of the most significant 1-bit of @p x or 0 if @p x
47+
* is 0.
48+
*
49+
* ```
50+
* fls(0) == fls(0b0) == 0
51+
* fls(1) == fls(0b1) == 1
52+
* fls(13) == fls(0b1101) == 4
53+
* ```
54+
*
55+
* For unsigned integers,
56+
* ```
57+
* fls(x) = floor(log2(x)) + 1, if x > 0
58+
* 0, if x == 0
59+
* ```
60+
*
61+
* @param[in] x Integer.
62+
*/
63+
#define fls(x) generic_bitop(x, PP_UNIQUE(_x), fls_)
64+
/** @cond */
65+
/*
66+
* The straightfoward implementation is bits - clz. However, as noted by the
67+
* folly implementation: "If X is a power of two, X - Y = 1 + ((X - 1) ^ Y).
68+
* Doing this transformation allows GCC to remove its own xor that it adds to
69+
* implement clz using bsr."
70+
*
71+
* This doesn't do the normal macro argument safety stuff because it should only
72+
* be used via generic_bitop() which already does it.
73+
*/
74+
#define fls_impl(x, type, suffix) \
75+
(x ? 1 + ((8 * sizeof(type) - 1) ^ __builtin_clz##suffix(x)) : 0)
76+
#define fls_(x) fls_impl(x, unsigned int,)
77+
#define fls_l(x) fls_impl(x, unsigned long, l)
78+
#define fls_ll(x) fls_impl(x, unsigned long long, ll)
79+
80+
#define generic_bitop(x, unique_x, op) ({ \
81+
__auto_type unique_x = (x); \
82+
_Static_assert(sizeof(unique_x) <= sizeof(unsigned long long), \
83+
"type is too large"); \
84+
(unsigned int)(sizeof(unique_x) <= sizeof(unsigned int) ? \
85+
op(unique_x) : \
86+
sizeof(unique_x) <= sizeof(unsigned long) ? \
87+
op##l(unique_x) : \
88+
op##ll(unique_x)); \
89+
})
90+
/** @endcond */
91+
92+
/**
93+
* Return the smallest power of two greater than or equal to @p x.
94+
*
95+
* ```
96+
* next_power_of_two(0) == 1 // Zero is not a power of two
97+
* next_power_of_two(1) == 1
98+
* next_power_of_two(13) == 16
99+
* ```
100+
*
101+
* @param[in] x Integer.
102+
*/
103+
#define next_power_of_two(x) next_power_of_two_impl(x, PP_UNIQUE(_x))
104+
/** @cond */
105+
#define next_power_of_two_impl(x, unique_x) ({ \
106+
__auto_type unique_x = (x); \
107+
unique_x ? (typeof(unique_x))1 << fls(unique_x - 1) : \
108+
(typeof(unique_x))1; \
109+
})
110+
/** @endcond */
111+
112+
/**
113+
* Iterate over each 1-bit in @p mask.
114+
*
115+
* On each iteration, this sets @p i to the zero-based index of the least
116+
* significant 1-bit in @p mask and clears that bit in @p mask. It stops
117+
* iterating when @p mask is zero.
118+
*
119+
* ```
120+
* // Outputs 0 2 3
121+
* unsigned int mask = 13, i;
122+
* for_each_bit(i, mask)
123+
* printf("%u ", i);
124+
* ```
125+
*
126+
* @param[out] i Iteration variable name.
127+
* @param[in,out] mask Integer to iterate over. This is modified.
128+
*/
129+
#define for_each_bit(i, mask) \
130+
while (mask && (i = ctz(mask), mask &= mask - 1, 1))
131+
132+
/** @} */
133+
134+
#endif /* DRGN_BITOPS_H */

libdrgn/debug_info.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <string.h>
1717
#include <unistd.h>
1818

19-
#include "cityhash.h"
2019
#include "debug_info.h"
2120
#include "error.h"
2221
#include "hash_table.h"
@@ -32,26 +31,27 @@
3231
DEFINE_VECTOR_FUNCTIONS(drgn_debug_info_module_vector)
3332

3433
static inline struct hash_pair
35-
drgn_debug_info_module_hash(const struct drgn_debug_info_module_key *key)
34+
drgn_debug_info_module_key_hash_pair(const struct drgn_debug_info_module_key *key)
3635
{
37-
size_t hash = cityhash_size_t(key->build_id, key->build_id_len);
36+
size_t hash = hash_bytes(key->build_id, key->build_id_len);
3837
hash = hash_combine(hash, key->start);
3938
hash = hash_combine(hash, key->end);
4039
return hash_pair_from_avalanching_hash(hash);
4140
}
4241
static inline bool
43-
drgn_debug_info_module_eq(const struct drgn_debug_info_module_key *a,
44-
const struct drgn_debug_info_module_key *b)
42+
drgn_debug_info_module_key_eq(const struct drgn_debug_info_module_key *a,
43+
const struct drgn_debug_info_module_key *b)
4544
{
4645
return (a->build_id_len == b->build_id_len &&
4746
memcmp(a->build_id, b->build_id, a->build_id_len) == 0 &&
4847
a->start == b->start && a->end == b->end);
4948
}
5049
DEFINE_HASH_TABLE_FUNCTIONS(drgn_debug_info_module_table,
51-
drgn_debug_info_module_hash,
52-
drgn_debug_info_module_eq)
50+
drgn_debug_info_module_key_hash_pair,
51+
drgn_debug_info_module_key_eq)
5352

54-
DEFINE_HASH_TABLE_FUNCTIONS(c_string_set, c_string_hash, c_string_eq)
53+
DEFINE_HASH_TABLE_FUNCTIONS(c_string_set, c_string_key_hash_pair,
54+
c_string_key_eq)
5555

5656
/**
5757
* @c Dwfl_Callbacks::find_elf() implementation.
@@ -338,7 +338,7 @@ drgn_debug_info_report_module(struct drgn_debug_info_load_state *load,
338338
.start = start,
339339
.end = end,
340340
};
341-
hp = drgn_debug_info_module_hash(&key);
341+
hp = drgn_debug_info_module_table_hash(&key);
342342
it = drgn_debug_info_module_table_search_hashed(&dbinfo->modules,
343343
&key, hp);
344344
if (it.entry &&
@@ -1019,8 +1019,8 @@ bool drgn_debug_info_is_indexed(struct drgn_debug_info *dbinfo,
10191019
return c_string_set_search(&dbinfo->module_names, &name).entry != NULL;
10201020
}
10211021

1022-
DEFINE_HASH_TABLE_FUNCTIONS(drgn_dwarf_type_map, hash_pair_ptr_type,
1023-
hash_table_scalar_eq)
1022+
DEFINE_HASH_TABLE_FUNCTIONS(drgn_dwarf_type_map, ptr_key_hash_pair,
1023+
scalar_key_eq)
10241024

10251025
struct drgn_type_from_dwarf_thunk {
10261026
struct drgn_type_thunk thunk;

libdrgn/debug_info.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* @ingroup Internals
2626
*
27-
* @defgroup DebugInfo Debugging information cache
27+
* @defgroup DebugInfo Debugging information
2828
*
2929
* Caching of debugging information.
3030
*
@@ -141,8 +141,8 @@ struct drgn_debug_info {
141141
/**
142142
* Names of indexed modules.
143143
*
144-
* The entries in this set are @ref drgn_dwarf_module::name, so they
145-
* should not be freed.
144+
* The entries in this set are @ref drgn_debug_info_module::name, so
145+
* they should not be freed.
146146
*/
147147
struct c_string_set module_names;
148148
/** Index of DWARF debugging information. */

libdrgn/drgn.h.in

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ struct drgn_error {
106106
* the system call.
107107
*/
108108
int errnum;
109-
/*
109+
/**
110110
* If @c code is @c DRGN_ERROR_OS, then the path of the file which
111111
* encountered the error if applicable. Otherwise, @c NULL.
112112
*/
113113
char *path;
114-
/*
114+
/**
115115
* If @c code is @c DRGN_ERROR_FAULT, then the address of the read
116116
* which encountered the error.
117117
*/
@@ -1665,7 +1665,6 @@ struct drgn_object {
16651665
bool is_reference;
16661666
/** Whether this object is a bit field. */
16671667
bool is_bit_field;
1668-
/** Reference to this object in @ref drgn_object::prog, or its value. */
16691668
union {
16701669
/** Value. */
16711670
union drgn_value value;
@@ -1986,7 +1985,7 @@ drgn_object_dereference_offset(struct drgn_object *res,
19861985
*
19871986
* If @c obj is already a value, then this is equivalent to @ref
19881987
* drgn_object_copy(). If @c is a reference, then this reads the reference and
1989-
* sets @res to the value.
1988+
* sets @p res to the value.
19901989
*
19911990
* @param[out] res Object to set.
19921991
* @param[in] obj Object to read.
@@ -2366,9 +2365,9 @@ struct drgn_error *drgn_object_member_dereference(struct drgn_object *res,
23662365
*
23672366
* @param[out] res Returned object. May be the same as @p obj.
23682367
* @param[in] obj Pointer to a member.
2369-
* @param[in] type Type which contains the member.
2370-
* @param[in] member_designator Name of the member in @p type. This can include
2371-
* one or more member references and zero or more array subscripts.
2368+
* @param[in] qualified_type Type which contains the member.
2369+
* @param[in] member_designator Name of the member in @p qualified_type. This
2370+
* can include one or more member references and zero or more array subscripts.
23722371
* @return @c NULL on success, non-@c NULL on error. @p res is not modified on
23732372
* error.
23742373
*/

libdrgn/dwarf_index.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,11 @@ static inline const char *section_end(Elf_Data *data)
121121
return (const char *)data->d_buf + data->d_size;
122122
}
123123

124-
DEFINE_HASH_TABLE_FUNCTIONS(drgn_dwarf_index_die_map, string_hash, string_eq)
124+
DEFINE_HASH_TABLE_FUNCTIONS(drgn_dwarf_index_die_map, string_hash_pair,
125+
string_eq)
125126
DEFINE_VECTOR_FUNCTIONS(drgn_dwarf_index_die_vector)
126127
DEFINE_HASH_TABLE_FUNCTIONS(drgn_dwarf_index_specification_map,
127-
hash_pair_int_type, hash_table_scalar_eq)
128+
int_key_hash_pair, scalar_key_eq)
128129

129130
static inline size_t hash_pair_to_shard(struct hash_pair hp)
130131
{

libdrgn/dwarf_index.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,10 @@ struct drgn_dwarf_index_iterator {
274274
};
275275

276276
/**
277-
* Create an iterator over DIEs in a DWARF index.
277+
* Create an iterator over DIEs in a DWARF index namespace.
278278
*
279279
* @param[out] it DWARF index iterator to initialize.
280-
* @param[in] dindex DWARF index.
280+
* @param[in] ns DWARF index namespace.
281281
* @param[in] name Name of DIE to search for, or @c NULL for any name.
282282
* @param[in] name_len Length of @c name.
283283
* @param[in] tags List of DIE tags to search for.

0 commit comments

Comments
 (0)