Commit Graph

582 Commits

Author SHA1 Message Date
Andrey Ignatov
26389b61fc helpers: Add kernfs helpers
Add helpers to get kernfs node name and path.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
2020-01-14 16:03:20 -08:00
Omar Sandoval
7356816f61 helpers: get rid of get_tcp_states()
After thinking about this some more, I decided that although it makes
sense for scripts to convert a type to an IntEnum class, I'd prefer that
the helpers take and return drgn Objects rather than these classes.
2020-01-14 14:25:32 -08:00
Omar Sandoval
b30fb57e2f helpers: add enum_type_to_class()
We have a few use cases for converting a drgn.Type to a Python
enum.IntEnum (if I recall correctly, the original Python version of drgn
even did this automatically for enumerated types). Add an
enum_type_to_class() helper which does this conversion.
2020-01-14 12:13:02 -08:00
Omar Sandoval
842c98b055 Add .git-blame-ignore-revs
To make the previous reformatting commit less annoying, add a file
containing the commits to ignore in git blame. To use it, run `git blame
--ignore-revs-file .git-blame-ignore-revs` or `git config
blame.ignoreRevsFile .git-blame-ignore-revs`.
2020-01-14 12:03:26 -08:00
Omar Sandoval
660276a0b8 Format Python code with Black
I'm not a fan of 100% of the Black coding style, but I've spent too much
time manually formatting Python code, so let's just pull the trigger.
2020-01-14 11:51:58 -08:00
Omar Sandoval
029002fa53 Document generic helpers
drgn.helpers has a couple of undocumented generic helpers. We might as
well document them. They're using type hints, so we need to use a sphinx
extension that strips the type hints out of the signature and adds them
to the documentation. We can also do the same for execscript().
Hopefully it can be used for the C extension documentation in the
future, too.
2020-01-14 11:50:55 -08:00
Omar Sandoval
d71b57e3e8 vmtest/manage: scrub compilation directory in debug info
In order to make builds slightly more reproducible (and avoid leaking
details about the build machine's directory layout), let's remap the
compilation directory in DW_AT_comp_dir to be empty.
2020-01-13 11:15:26 -08:00
Omar Sandoval
e8d1ef82fa Make drgn.h depend on configure.ac
The previous commit forgot to add this dependency so that when the
version number is updated drgn.h actually gets regenerated.
2020-01-11 22:34:03 -08:00
Omar Sandoval
09a64f5cba Define version in libdrgn/configure.ac
Currently the drgn version number is defined in drgn.h.in, and configure
and setup.py both parse it out of there. However, now that we're
generating drgn.h anyways, it's easier to make configure.ac the source
of truth.
2020-01-11 10:05:57 -08:00
Omar Sandoval
5cf37b96ef Add more setup.py metadata 2020-01-10 17:15:35 -08:00
Omar Sandoval
71312a4039 Tweak README introduction 2020-01-10 16:40:49 -08:00
Omar Sandoval
370bf6f16a tests: add tests for new net and tcp helpers
Because the vmtest kernels aren't currently built with networking
support, we need to skip them if TCP isn't supported.
2020-01-02 19:43:57 -05:00
Omar Sandoval
bb7a8eedcf helpers: add sk_tcpstate
Although it doesn't correspond to a Linux kernel function, it is more
convenient than open-coding it everywhere, especially since the leading
__ causes Python to mangle the name if it's inside of a class.
2020-01-02 19:43:57 -05:00
Andrey Ignatov
7a36136417 examples: Add tcp_sock.py
Add example script that goes over all TCP sockets and for every socket
prints:
- TCP state;
- IP:port pair;
- cgroup v2 path.

The script shows basic operations with sockets including getting `struct
sock`, `struct inet_sock`, socket fields and cgroup v2 of socket.

It provides output similar to `ss -nt` or `ss -lnt`:

  % sudo python36 -m drgn examples/linux/tcp_sock.py | shuf -n 5
  TIME_WAIT                                       [::1]:2560                                      [::1]:44041
  ESTABLISHED                                     [::1]:54178                                     [::1]:11101  system.slice/async-distillery-server.service
  LISTEN                                           [::]:3201                                       [::]:0      system.slice/fbflow.service
  ESTABLISHED           [2401:db00:21:718a:face:0:31:0]:2406            [2401:db00:12:a106:face:0:1e:0]:46306  system.slice/configerator_proxy2.service
  LISTEN                                        0.0.0.0:11150                                   0.0.0.0:0      system.slice/system-mcrouter.slice/mcrouter@tao.service

Signed-off-by: Andrey Ignatov <rdna@fb.com>
2020-01-02 19:43:57 -05:00
Andrey Ignatov
a253aed14b helpers: add network sockets helpers
Add helpers for working with network sockets defined in
include/net/sock.h:
- sk_fullsock()
- sk_nulls_for_each()

sk_fullsock() is useful to filter out timewait and request sockets since
not all operations can be applied to such sockets.

sk_nulls_for_each() is useful to e.g. iterate over established sockets.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
2020-01-02 19:43:57 -05:00
Andrey Ignatov
01c588b07e helpers: add TCP states helper
Add get_tcp_states() helper to get all TCP states enumerators and
construct enum.IntEnum from them.

The helper is useful in debugging TCP sockets.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
2020-01-02 19:43:57 -05:00
Andrey Ignatov
7013c37bdc helpers: add list_nulls helpers
These list helpers are useful to iterate over nulls hash lists of
sockets, specifically `ehash` member of `struct tcp_hashinfo`.

Corresponding functions and macros are defined in
`include/linux/list_nulls.h` kernel header.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
2020-01-02 19:43:57 -05:00
Omar Sandoval
73dd7def12 docs: add examples of Object.format_() 2019-12-19 11:45:52 -08:00
Omar Sandoval
1443d17fb4 libdrgn: add DRGN_FORMAT_OBJECT_IMPLICIT_ELEMENTS 2019-12-19 11:43:54 -08:00
Omar Sandoval
db66952b2e libdrgn: add DRGN_FORMAT_OBJECT_IMPLICIT_MEMBERS 2019-12-19 11:43:54 -08:00
Omar Sandoval
c8434e9a9e libdrgn: add DRGN_FORMAT_OBJECT_ELEMENT_INDICES 2019-12-19 11:43:54 -08:00
Omar Sandoval
cfceb491db libdrgn: add DRGN_FORMAT_OBJECT_MEMBER_NAMES 2019-12-19 11:43:54 -08:00
Omar Sandoval
4fad941ec1 libdrgn: add DRGN_FORMAT_OBJECT_{MEMBERS,ELEMENTS}_SAME_LINE 2019-12-19 11:43:54 -08:00
Omar Sandoval
c3f69ba559 libdrgn: use c_format_initializer for struct/union/class 2019-12-19 11:43:54 -08:00
Omar Sandoval
e2d2df4024 libdrgn: factor c_format_initializer out of c_format_array
This will also be used for compound types, and we're going to add a few
more options that we should handle in one place.
2019-12-19 11:43:54 -08:00
Omar Sandoval
6bb8da04a0 libdrgn: omit trailing comma when formatting one-line array
This is somewhat arbitrary, but I think it looks more natural to only
use the trailing comma for multi-line initializers.
2019-12-19 11:43:54 -08:00
Omar Sandoval
1411ba36a8 libdrgn: remove dead code in c_format_array_object
When we're checking whether the element that we formatted on one line
would fit on the previous line, we check whether the previous line is
empty with remaining_columns == start_columns. This is never true, as
remaining_columns is always set to start_columns - 1 at most, and it
only decreases from there until we start  a new line.
2019-12-19 11:43:54 -08:00
Omar Sandoval
7a3bf73df0 libdrgn: replace drgn_object_truthiness() with drgn_object_is_zero()
drgn_object_truthiness() is a misnomer, as truthiness is a
language-specific concept. Instead, invert the return value and rename
it to drgn_object_is_zero(), which more accurately conveys the meaning.
2019-12-19 11:43:54 -08:00
Omar Sandoval
d77b7bd7e3 libdrgn: add DRGN_FORMAT_OBJECT_{TYPE_NAME,MEMBER_TYPE_NAMES,ELEMENT_TYPE_NAMES} 2019-12-19 11:43:54 -08:00
Omar Sandoval
89307c532a libdrgn: add DRGN_FORMAT_OBJECT_CHAR 2019-12-19 11:43:54 -08:00
Omar Sandoval
7cee597fff libdrgn: add DRGN_FORMAT_OBJECT_STRING 2019-12-19 11:43:54 -08:00
Omar Sandoval
5865fa4d16 libdrgn: add DRGN_FORMAT_OBJECT_SYMBOLIZE 2019-12-19 11:43:54 -08:00
Omar Sandoval
0a707b0c9d libdrgn: rework drgn_find_symbol_internal()
Instead of having two internal variants (drgn_find_symbol_internal() and
drgn_program_find_symbol_in_module()), combine them into the former and
add a separate drgn_error_symbol_not_found() for translating the static
error to the user-facing one. This makes things more flexible for the
next change.
2019-12-19 11:43:54 -08:00
Omar Sandoval
f58bc4bf3a libdrgn: add DRGN_FORMAT_OBJECT_DEREFERENCE 2019-12-19 11:43:54 -08:00
Omar Sandoval
5fb02f03fd libdrgn: add flags to drgn_format_object() 2019-12-19 11:43:54 -08:00
Omar Sandoval
cf3a07bdfb libdrgn: python: replace Object.__format__ with Object.format_
We'd like to have more control over how objects are formatted. I
considered defining a custom string format specification syntax, but
that's not easily discoverable. Instead, let's get rid of the current
format specification support and replace it with a normal method.
2019-12-19 11:43:52 -08:00
Omar Sandoval
3b22bd3022 libdrgn: rename pretty_print -> format
In preparation for making drgn_pretty_print_object() more flexible
(i.e., not always "pretty"), rename it to drgn_format_object(). For
consistency, let's rename drgn_pretty_print_type_name(),
drgn_pretty_print_type(), and drgn_pretty_print_stack_trace(), too.
2019-12-16 11:21:12 -08:00
Omar Sandoval
c3dbb3006d tests: remove stray TODO comment
I added this as a reminder to handle errno but forgot to remove the
comment when I handled errno.
2019-12-16 11:20:22 -08:00
Serapheim Dimitropoulos
501d36c18e libdrgn: fix regression in kernel module loading
Commit f327552229 ("libdrgn: add strstartswith()") flipped the test
for a name entry in modinfo. This introduced a regression resulting in
kernel modules not loading at the right offset. This patch fixes the
regression.
2019-12-13 19:19:31 -05:00
Omar Sandoval
54e3e4a6d6 Rebase elfutils and remove dwfl_addrmodule patches
The previous commit was the real fix for the failed symbol lookups. On
the bright side, the build fixes were merged, so we can rebase on master
and drop those.

Based on:

277c2c54f libcpu: Compile i386_lex.c with -Wno-implicit-fallthrough

With the following patches:

configure: Add --disable-programs
configure: Add --disable-shared
libdwfl: add interface for attaching to/detaching from threads
libdwfl: cache Dwfl_Module and Dwarf_Frame for Dwfl_Frame
libdwfl: add interface for evaluating DWARF expressions in a frame
2019-12-12 21:14:51 -08:00
Omar Sandoval
b0c4f894d4 libdrgn: really fix failed kernel module symbol lookups
It turns out this wasn't a problem with dwfl_addrmodule() at all; the
real problem is that .init sections are freed once the module is loaded
but we're still considering them for the address range we pass to
dwfl_report_module(). Ignore those sections entirely (by omitting them
from the section name to section index map). While we're here, let's not
bother inserting non-SHF_ALLOC sections in the map.
2019-12-12 21:14:02 -08:00
Omar Sandoval
f327552229 libdrgn: add strstartswith()
Instead of open coding this check all over the place, add a helper
function.
2019-12-12 13:26:50 -08:00
Omar Sandoval
e0c6a2281d travis.yml: add 5.5 to vmtest kernels
v5.5-rc1 was released this week. Run vmtest with it.
2019-12-11 22:34:20 -08:00
Omar Sandoval
ad5c925aff Update elfutils with dwfl_addrmodule fix
This fixes the issue that Program.symbol() sometimes fails for kernel
module symbols.

Based on:

2c7c4037 elfutils.spec.in: Sync with fedora spec, remove rhel/fedora specifics.

With the following patches:

configure: Add --disable-programs
configure: Add --disable-shared
configure: Fix -D_FORTIFY_SOURCE=2 check when CFLAGS contains -Wno-error
libcpu: compile i386_lex.c with -Wno-implicit-fallthrough
libdwfl: add interface for attaching to/detaching from threads
libdwfl: cache Dwfl_Module and Dwarf_Frame for Dwfl_Frame
libdwfl: add interface for evaluating DWARF expressions in a frame
libdwfl: return error from __libdwfl_relocate_value for unloaded sections
libdwfl: remove broken coalescing logic in dwfl_report_segment
libdwfl: store module lookup table separately from segments
libdwfl: use sections of relocatable files for dwfl_addrmodule
2019-12-11 22:34:05 -08:00
Omar Sandoval
4a8152175b libdrgn: translate EIO from /proc/$pid/mem to DRGN_ERROR_FAULT
For live userspace processes, we add a single [0, UINT64_MAX) memory
file segment for /proc/$pid/mem. Of course, not every address in that
range is valid; reading from an invalid address returns EIO. We should
translate this to a DRGN_ERROR_FAULT instead of DRGN_ERROR_OS, but only
for /proc/$pid/mem.
2019-12-10 13:30:34 -08:00
Omar Sandoval
3239e72e4e python: update globals even if exescript() raises exception
execscript() is supposed to behave more or less like the script was
typed in at the console. However, if the script raises an exception,
then the variables or functions it defines are not added to the calling
context, which can be pretty confusing.

The problem is that runpy.run_code() returns the new globals, so if it
raises an exception, then we can't get them. Fix it by doing the exec()
ourselves; we just need to do the same setup that runpy does.
2019-12-05 17:08:28 -08:00
Omar Sandoval
248cec7f7c libdrgn: python: fix uninitialized index_args
In commit 55a9700435 ("libdrgn: python: accept integer-like arguments
in more places"), I converted Program_symbol to use index_converter but
forgot to initialize the struct index_arg. Then, in commit c243daed59
("Translate find_task() helper (and dependencies) to C"), I added a
bunch more cases of uninitialized struct index_arg. If
index_arg.allow_none gets a non-zero garbage value, then this can end up
allowing None through when it shouldn't. Furthermore, since commit
2561226918 ("libdrgn: python: add signed integer support to
index_converter"), if index_arg.is_signed gets a non-zero garbage value,
then this will try to get a signed integer when we're expecting an
unsigned integer, which can blow up for values >= 2**63 (like kernel
symbols). Fix it by initializing struct index_arg everywhere.

Fixes #30.
2019-12-05 14:35:54 -08:00
Omar Sandoval
00f5d5b034 vmtest: fix README link 2019-12-04 09:57:43 -08:00
Omar Sandoval
b727e39aa2 vmtest: split files up by architecture
For now, x86_64 is the only architecture, but in the future we will want
to test on others. Let's rearrange things now while it's not too
disruptive.
2019-12-04 03:32:46 -08:00
Omar Sandoval
1721a42dcd vmtest: rename bzImage uploads to vmlinuz
Not all architectures name the bootable image bzImage, so to make
supporting other architectures easier in the future, let's use the more
generic name, vmlinuz.
2019-12-03 22:59:57 -08:00