Skip to content

Commit c596f2e

Browse files
committed
wip crash commands
1 parent 093b65b commit c596f2e

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

drgn/commands/_builtin/crash/mm.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
4+
import argparse
5+
from typing import Any
6+
7+
from drgn import Program
8+
from drgn.commands import argument, drgn_argument
9+
from drgn.commands.crash import crash_command
10+
from drgn.helpers.common.format import print_table
11+
from drgn.helpers.linux.mm import follow_phys, page_to_phys, phys_to_page
12+
13+
14+
@crash_command(
15+
description="bytes to page",
16+
long_description="Convert byte numbers (usually physical addresses) to page numbers.",
17+
arguments=(
18+
argument(
19+
"address",
20+
type="hexadecimal",
21+
nargs="+",
22+
help="hexadecimal byte number/physical address",
23+
),
24+
drgn_argument,
25+
),
26+
)
27+
def _crash_cmd_btop(
28+
prog: Program, name: str, args: argparse.Namespace, **kwargs: Any
29+
) -> None:
30+
if args.drgn:
31+
print(
32+
"""\
33+
PHYS_PFN(address)
34+
# or
35+
address >> prog["PAGE_SHIFT"]"""
36+
)
37+
return
38+
39+
PAGE_SHIFT = prog["PAGE_SHIFT"].value_()
40+
for address in args.address:
41+
print(f"{address:x}: {address >> PAGE_SHIFT:x}")
42+
43+
44+
@crash_command(
45+
description="page to bytes",
46+
long_description="Convert page frame numbers to byte numbers (physical addresses).",
47+
arguments=(
48+
argument(
49+
"pfn", type="decimal_or_hexadecimal", nargs="+", help="page frame number"
50+
),
51+
drgn_argument,
52+
),
53+
)
54+
def _crash_cmd_ptob(
55+
prog: Program, name: str, args: argparse.Namespace, **kwargs: Any
56+
) -> None:
57+
if args.drgn:
58+
print(
59+
"""\
60+
PFN_PHYS(address)
61+
# or
62+
address << prog["PAGE_SHIFT"]"""
63+
)
64+
return
65+
66+
PAGE_SHIFT = prog["PAGE_SHIFT"].value_()
67+
for pfn in args.pfn:
68+
print(f"{pfn:x}: {pfn << PAGE_SHIFT:x}")
69+
70+
71+
@crash_command(
72+
description="physical or per-CPU to virtual",
73+
long_description="TODO",
74+
arguments=(
75+
argument(
76+
"address",
77+
metavar="address|offset:cpuspec",
78+
type="hexadecimal",
79+
nargs="+",
80+
help="hexadecimal physical address or hexadecimal per-CPU offset and CPU specifier",
81+
),
82+
drgn_argument,
83+
),
84+
)
85+
def _crash_cmd_ptov(
86+
prog: Program, name: str, args: argparse.Namespace, **kwargs: Any
87+
) -> None:
88+
if args.drgn:
89+
print("phys_to_virt(address)")
90+
return
91+
92+
# TODO
93+
94+
95+
@crash_command(
96+
description="virtual to physical",
97+
long_description="TODO",
98+
arguments=(
99+
# TODO: -c flag
100+
# argument(
101+
# "-c", dest="task", metavar="pid | taskp", help="TODO"
102+
# ),
103+
# TODO: -k, -u flags
104+
argument(
105+
"address", type="hexadecimal", nargs="+", help="hexadecimal virtual address"
106+
),
107+
drgn_argument,
108+
),
109+
)
110+
def _crash_cmd_vtop(
111+
prog: Program, name: str, args: argparse.Namespace, **kwargs: Any
112+
) -> None:
113+
# TODO: --drgn
114+
115+
mm = prog["init_mm"].address_of_()
116+
117+
for i, address in enumerate(args.address):
118+
phys = follow_phys(mm, address)
119+
page = phys_to_page(phys)
120+
121+
if i != 0:
122+
print()
123+
print_table(
124+
(
125+
("VIRTUAL", "PHYSICAL"),
126+
(f"{address:x}", f"{phys.value_():x}"),
127+
)
128+
)
129+
130+
# TODO: page table info
131+
132+
# TODO: crash tries printing VMAs, possibly other things?
133+
134+
print()
135+
# TODO: we justify things differently than crash
136+
print_table(
137+
(
138+
("PAGE", "PHYSICAL", "MAPPING", "INDEX", "CNT", "FLAGS"),
139+
(
140+
f"{page.value_():x}",
141+
f"{page_to_phys(page).value_():x}",
142+
f"{page.mapping.value_():x}",
143+
"TODO", # TODO: not sure what base these are in
144+
"TODO",
145+
f"{page.flags.value_():x}",
146+
),
147+
)
148+
)

drgn/commands/_builtin/crash/sys.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,78 @@ def _crash_cmd_sys(
355355
return
356356

357357
_SysPrinter(prog, args.drgn).print()
358+
359+
360+
@crash_command(
361+
description="devices",
362+
# TODO: arguments
363+
arguments=(drgn_argument,),
364+
)
365+
def _crash_cmd_dev(
366+
prog: Program, command_name: str, args: argparse.Namespace, **kwargs: Any
367+
) -> None:
368+
# TODO: --drgn
369+
rows: List[Tuple[Any, ...]] = [
370+
(
371+
"CHRDEV",
372+
"NAME",
373+
CellFormat("CDEV", "^"),
374+
"OPERATIONS",
375+
)
376+
]
377+
for dev, _, name, cdev in for_each_registered_chrdev(prog):
378+
operations_cell: Any = ""
379+
if cdev:
380+
cdev_cell = CellFormat(cdev.value_(), "^x")
381+
ops = cdev.ops.value_()
382+
try:
383+
operations_cell = prog.symbol(ops).name
384+
except LookupError:
385+
operations_cell = CellFormat(ops, "x")
386+
else:
387+
cdev_cell = CellFormat("(none)", "^")
388+
rows.append(
389+
(
390+
MAJOR(dev),
391+
escape_ascii_string(name, escape_backslash=True),
392+
cdev_cell,
393+
operations_cell,
394+
)
395+
)
396+
397+
rows.append(())
398+
399+
major_to_gendisk = collections.defaultdict(list)
400+
for disk in for_each_disk(prog):
401+
major_to_gendisk[disk.major.value_()].append(disk)
402+
major_to_gendisk.default_factory = None
403+
404+
rows.append(
405+
(
406+
"BLKDEV",
407+
"NAME",
408+
CellFormat("GENDISK", "^"),
409+
"OPERATIONS",
410+
)
411+
)
412+
413+
for major, name in for_each_registered_blkdev(prog):
414+
name_string = escape_ascii_string(name, escape_backslash=True)
415+
try:
416+
gendisks = major_to_gendisk[major]
417+
except KeyError:
418+
rows.append((major, name_string, CellFormat("(none)", "^")))
419+
else:
420+
cell0 = major
421+
cell1 = name_string
422+
for disk in gendisks:
423+
ops = disk.fops.value_()
424+
try:
425+
operations_cell = prog.symbol(ops).name
426+
except LookupError:
427+
operations_cell = CellFormat(ops, "x")
428+
rows.append(
429+
(cell0, cell1, CellFormat(disk.value_(), "^x"), operations_cell)
430+
)
431+
432+
print_table(rows)

0 commit comments

Comments
 (0)