From: Stephen Brennan <stephen.s.brennan@oracle.com>
To: Ye Liu <ye.liu@linux.dev>, akpm@linux-foundation.org
Cc: linux-debuggers@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, linux-toolchains@vger.kernel.org,
osandov@osandov.com, paulmck@kernel.org,
sweettea-kernel@dorminy.me, liuye@kylinos.cn, fweimer@redhat.com,
sj@kernel.org
Subject: Re: [PATCH v4] tools/mm: Add script to display page state for a given PID and VADDR
Date: Thu, 29 May 2025 20:29:16 -0700 [thread overview]
Message-ID: <87frgmai9f.fsf@oracle.com> (raw)
In-Reply-To: <1f8af317-2fff-4a1f-ad0e-9d9c6c15f3a1@linux.dev>
Ye Liu <ye.liu@linux.dev> writes:
>>> +import argparse
>>> +from drgn import Object, FaultError
>>> +from drgn.helpers.linux import find_task, follow_page, page_size
>>> +from drgn.helpers.linux.mm import (
>>> + decode_page_flags, page_to_pfn, page_to_phys, page_to_virt, vma_find,
>>> + PageSlab, PageCompound, PageHead, PageTail, compound_head, compound_order, compound_nr
>>> +)
>>> +from drgn.helpers.linux.cgroup import cgroup_name, cgroup_path
>> Anything in "drgn.helpers.linux.*" can be imported from
>> "drgn.helpers.linux" instead, which would help if any helper moved
>> around from one module to another. I've recently started preferring
>> that, but I don't know if it's a huge improvement. EG:
>>
>> from drgn.helpers.linux import (
>> PageCompound, PageHead, PageSlab, PageTail, cgroup_name,
>> cgroup_path, compound_head, compound_nr, compound_order,
>> decode_page_flags, find_task, follow_page, page_size, page_to_pfn,
>> page_to_phys, page_to_virt, vma_find,
>> )
>>
>> Again, not sure it improves anything :)
> Thanks for the suggestion! After considering the trade-offs, I prefer
> keeping the current imports for clarity:
> Readability: Explicit module paths (e.g., mm/, cgroup/) make helper
> origins clearer.
> Debugging: Functional grouping helps when analyzing code.
> Both styles work, but the current approach aligns better with drgn’s
> documentation and our workflow. Happy to revisit if needs change.
Sounds good!
>>> +def show_page_state(page, addr, mm, pid, task):
>>> + """Display detailed information about a page."""
>>> + try:
>>> + print(f'PID: {pid} Comm: {task.comm.string_().decode()} mm: {hex(mm)}')
>>> + try:
>>> + print(format_page_data(prog.read(page.value_(), 64)))
>> Rather than hard-code the size of struct page, you can use sizeof(page).
>> And in fact, all drgn Objects have a .bytes_() that will just give you
>> the bytes of the object directly, which would even avoid the sizeof().
> I didn't find the .bytes_() method. Can you give an example?
> I used prog.type("struct page").size instead.
You're right, it's "to_bytes_()", sorry:
>>> prog["slab_caches"]
(struct list_head){
.next = (struct list_head *)0xffff9f604cbecd68,
.prev = (struct list_head *)0xffff9f6040042068,
}
>>> prog["slab_caches"].to_bytes_()
b'h\xcd\xbeL`\x9f\xff\xffh \x04@`\x9f\xff\xff'
https://drgn.readthedocs.io/en/latest/api_reference.html#drgn.Object.to_bytes_
But stick with sizeof() (or prog.type("struct page").size), that way you
can use Program.read_word() as mentioned above.
>>> +def main():
>>> + """Main function to parse arguments and display page state."""
>>> + parser = argparse.ArgumentParser(description=DESC, formatter_class=argparse.RawTextHelpFormatter)
>>> + parser.add_argument('pid', metavar='PID', type=int, help='Target process ID (PID)')
>>> + parser.add_argument('vaddr', metavar='VADDR', type=str, help='Target virtual address in hexadecimal format (e.g., 0x7fff1234abcd)')
>>> + args = parser.parse_args()
>>> +
>>> + try:
>>> + vaddr = int(args.vaddr, 16)
>>> + except ValueError:
>>> + print(f"Error: Invalid virtual address format: {args.vaddr}")
>>> + return
>> I find it quite useful to replace things like this with:
>>
>> sys.exit(f"Error: Invalid virtual address format: {args.vaddr}")
>>
>> Which will result in the script exiting with a non-zero exit code, and
>> it will print the message to stderr, rather than stdout. All while being
>> one line shorter, for the code golfers :)
> Agree, I can replace it in the main() function, but in other places,
> I prefer the script to continue running instead of exiting."
Yes definitely!
Regards,
Stephen
next prev parent reply other threads:[~2025-05-30 3:29 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-28 9:15 [PATCH v4] tools/mm: Add script to display page state for a given PID and VADDR Ye Liu
2025-05-28 16:36 ` Stephen Brennan
2025-05-30 3:09 ` Ye Liu
2025-05-30 3:29 ` Stephen Brennan [this message]
2025-05-28 23:42 ` SeongJae Park
2025-05-30 3:18 ` Ye Liu
2025-05-30 20:18 ` SeongJae Park
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87frgmai9f.fsf@oracle.com \
--to=stephen.s.brennan@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=fweimer@redhat.com \
--cc=linux-debuggers@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-toolchains@vger.kernel.org \
--cc=liuye@kylinos.cn \
--cc=osandov@osandov.com \
--cc=paulmck@kernel.org \
--cc=sj@kernel.org \
--cc=sweettea-kernel@dorminy.me \
--cc=ye.liu@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.