Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drgn.helpers.linux.mm: have cmdline() and environ() return None for k… #372

Merged
merged 1 commit into from
Nov 29, 2023

Conversation

pcc
Copy link
Contributor

@pcc pcc commented Nov 29, 2023

…ernel tasks

Sometimes I need to enumerate all tasks in the system and print their command lines, among other information. This fails for kernel tasks (i.e. tasks without mm):

Traceback (most recent call last):
File "", line 2, in
File "[...]/drgn/helpers/linux/mm.py", line 1276, in cmdline
arg_start = mm.arg_start.value_()
^^^^^^^^^^^^^^^^^^^^^
_drgn.FaultError: address is not mapped: 0x140

It seems reasonable for this not to cause an exception but rather to return a value that the caller can conveniently recover from (consider also the behavior of cat /proc/2/cmdline). Therefore, change cmdline() to return None for kernel tasks. Do the same for environ() for consistency.

drgn/helpers/linux/mm.py Outdated Show resolved Hide resolved
Copy link
Owner

@osandov osandov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable. I'm debating whether I'd prefer returning an empty list rather than None. That would be more backwards compatible (e.g., type-checked scripts wouldn't start getting errors now that cmdline() can return None), and still in the spirit of /proc/2/cmdline and /proc/2/environ being empty files. I could be swayed either way.

@pcc
Copy link
Contributor Author

pcc commented Nov 29, 2023

I also considered returning an empty list. None seemed more accurate though, since it is possible for a userspace task to have an empty environment or (before commit dcd46d897adb70d63e025f175a00a89797d31a43) an empty argv. My gut feeling is that Python type checking is unlikely to be used in the sort of ad-hoc scripts that use drgn, so I wouldn't expect this to be likely to break anything.

Copy link
Owner

@osandov osandov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, distinguishing between kernel threads and truly empty seems like a good enough reason for this, so please update with the nitpicks and I'll merge this, thanks!

drgn/helpers/linux/mm.py Outdated Show resolved Hide resolved
…ernel tasks

Sometimes I need to enumerate all tasks in the system and print their
command lines, among other information. This fails for kernel tasks
(i.e. tasks without mm):

Traceback (most recent call last):
  File "<console>", line 2, in <module>
  File "[...]/drgn/helpers/linux/mm.py", line 1276, in cmdline
    arg_start = mm.arg_start.value_()
                ^^^^^^^^^^^^^^^^^^^^^
_drgn.FaultError: address is not mapped: 0x140

It seems reasonable for this not to cause an exception but rather
to return a value that the caller can conveniently recover from
(consider also the behavior of `cat /proc/2/cmdline`). Therefore,
change cmdline() to return None for kernel tasks. Do the same for
environ() for consistency.

Signed-off-by: Peter Collingbourne <[email protected]>
@osandov osandov merged commit 94ecc33 into osandov:main Nov 29, 2023
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants