Subject: [PATCH 0/3] Fix GDB commands error

This patchset fix some issues of gdb command.

Patch 1:
- Fix lx-ps command error
Patch 2:
- Fix stackdepot usage error
Patch 3:
- Remove exception handling
- Refine the printing format

---
This patchset is based on linux-next-20231127.

Kuan-Ying Lee (3):
scripts/gdb/tasks: Fix lx-ps command error
scripts/gdb/stackdepot: Rename pool_index_cached to pools_num
scripts/gdb: Remove exception handling and refine print format

scripts/gdb/linux/page_owner.py | 58 ++++++++++++++-------------------
scripts/gdb/linux/slab.py | 3 +-
scripts/gdb/linux/stackdepot.py | 6 ++--
scripts/gdb/linux/tasks.py | 12 +++----
4 files changed, 33 insertions(+), 46 deletions(-)

--
2.18.0


Subject: [PATCH 1/3] scripts/gdb/tasks: Fix lx-ps command error

Since commit 8e1f385104ac ("kill task_struct->thread_group") remove
the thread_group, we will encounter below issue.

(gdb) lx-ps
TASK PID COMM
0xffff800086503340 0 swapper/0
Python Exception <class 'gdb.error'>: There is no member named thread_group.
Error occurred in Python: There is no member named thread_group.

We use signal->thread_head to iterate all threads instead.

Fixes: 8e1f385104ac ("kill task_struct->thread_group")
Cc: [email protected]
Signed-off-by: Kuan-Ying Lee <[email protected]>
---
scripts/gdb/linux/tasks.py | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/scripts/gdb/linux/tasks.py b/scripts/gdb/linux/tasks.py
index 17ec19e9b5bf..7c32f4c8284b 100644
--- a/scripts/gdb/linux/tasks.py
+++ b/scripts/gdb/linux/tasks.py
@@ -13,7 +13,7 @@

import gdb

-from linux import utils
+from linux import utils, lists


task_type = utils.CachedType("struct task_struct")
@@ -25,13 +25,9 @@ def task_lists():
t = g = init_task

while True:
- while True:
- yield t
-
- t = utils.container_of(t['thread_group']['next'],
- task_ptr_type, "thread_group")
- if t == g:
- break
+ thread_head = t['signal']['thread_head']
+ for thread in lists.list_for_each_entry(thread_head, task_ptr_type, 'thread_node'):
+ yield thread

t = g = utils.container_of(g['tasks']['next'],
task_ptr_type, "tasks")
--
2.18.0

Subject: [PATCH 2/3] scripts/gdb/stackdepot: Rename pool_index_cached to pools_num

After stackdepot evicting support patchset[1], we rename
pool_index_cached to pools_num.

[1] https://lore.kernel.org/linux-mm/[email protected]/
Cc: Andrey Konovalov <[email protected]>
Signed-off-by: Kuan-Ying Lee <[email protected]>
---
scripts/gdb/linux/stackdepot.py | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/scripts/gdb/linux/stackdepot.py b/scripts/gdb/linux/stackdepot.py
index 047d329a6a12..0281d9de4b7c 100644
--- a/scripts/gdb/linux/stackdepot.py
+++ b/scripts/gdb/linux/stackdepot.py
@@ -25,10 +25,10 @@ def stack_depot_fetch(handle):
handle_parts_t = gdb.lookup_type("union handle_parts")
parts = handle.cast(handle_parts_t)
offset = parts['offset'] << DEPOT_STACK_ALIGN
- pool_index_cached = gdb.parse_and_eval('pool_index')
+ pools_num = gdb.parse_and_eval('pools_num')

- if parts['pool_index'] > pool_index_cached:
- gdb.write("pool index %d out of bounds (%d) for stack id 0x%08x\n" % (parts['pool_index'], pool_index_cached, handle))
+ if parts['pool_index'] > pools_num:
+ gdb.write("pool index %d out of bounds (%d) for stack id 0x%08x\n" % (parts['pool_index'], pools_num, handle))
return gdb.Value(0), 0

stack_pools = gdb.parse_and_eval('stack_pools')
--
2.18.0

Subject: [PATCH 3/3] scripts/gdb: Remove exception handling and refine print format

1. When we crash on a page, we want to check what
happened on this page instead of skipping this page by
try-except block. Thus, removing the try-except block.

2. Remove redundant comma and print the task name properly.

Signed-off-by: Kuan-Ying Lee <[email protected]>
---
scripts/gdb/linux/page_owner.py | 58 ++++++++++++++-------------------
scripts/gdb/linux/slab.py | 3 +-
2 files changed, 26 insertions(+), 35 deletions(-)

diff --git a/scripts/gdb/linux/page_owner.py b/scripts/gdb/linux/page_owner.py
index 844fd5d0c912..8e713a09cfe7 100644
--- a/scripts/gdb/linux/page_owner.py
+++ b/scripts/gdb/linux/page_owner.py
@@ -122,27 +122,24 @@ class DumpPageOwner(gdb.Command):
if not (page_ext['flags'] & (1 << PAGE_EXT_OWNER_ALLOCATED)):
gdb.write("page_owner is not allocated\n")

- try:
- page_owner = self.get_page_owner(page_ext)
- gdb.write("Page last allocated via order %d, gfp_mask: 0x%x, pid: %d, tgid: %d (%s), ts %u ns, free_ts %u ns\n" %\
- (page_owner["order"], page_owner["gfp_mask"],\
- page_owner["pid"], page_owner["tgid"], page_owner["comm"],\
- page_owner["ts_nsec"], page_owner["free_ts_nsec"]))
- gdb.write("PFN: %d, Flags: 0x%x\n" % (pfn, page['flags']))
- if page_owner["handle"] == 0:
- gdb.write('page_owner allocation stack trace missing\n')
- else:
- stackdepot.stack_depot_print(page_owner["handle"])
+ page_owner = self.get_page_owner(page_ext)
+ gdb.write("Page last allocated via order %d, gfp_mask: 0x%x, pid: %d, tgid: %d (%s), ts %u ns, free_ts %u ns\n" %\
+ (page_owner["order"], page_owner["gfp_mask"],\
+ page_owner["pid"], page_owner["tgid"], page_owner["comm"].string(),\
+ page_owner["ts_nsec"], page_owner["free_ts_nsec"]))
+ gdb.write("PFN: %d, Flags: 0x%x\n" % (pfn, page['flags']))
+ if page_owner["handle"] == 0:
+ gdb.write('page_owner allocation stack trace missing\n')
+ else:
+ stackdepot.stack_depot_print(page_owner["handle"])

- if page_owner["free_handle"] == 0:
- gdb.write('page_owner free stack trace missing\n')
- else:
- gdb.write('page last free stack trace:\n')
- stackdepot.stack_depot_print(page_owner["free_handle"])
- if page_owner['last_migrate_reason'] != -1:
- gdb.write('page has been migrated, last migrate reason: %s\n' % self.migrate_reason_names[page_owner['last_migrate_reason']])
- except:
- gdb.write("\n")
+ if page_owner["free_handle"] == 0:
+ gdb.write('page_owner free stack trace missing\n')
+ else:
+ gdb.write('page last free stack trace:\n')
+ stackdepot.stack_depot_print(page_owner["free_handle"])
+ if page_owner['last_migrate_reason'] != -1:
+ gdb.write('page has been migrated, last migrate reason: %s\n' % self.migrate_reason_names[page_owner['last_migrate_reason']])

def read_page_owner(self):
pfn = self.min_pfn
@@ -173,18 +170,13 @@ class DumpPageOwner(gdb.Command):
pfn += 1
continue

- try:
- page_owner = self.get_page_owner(page_ext)
- gdb.write("Page allocated via order %d, gfp_mask: 0x%x, pid: %d, tgid: %d (%s), ts %u ns, free_ts %u ns\n" %\
- (page_owner["order"], page_owner["gfp_mask"],\
- page_owner["pid"], page_owner["tgid"], page_owner["comm"],\
- page_owner["ts_nsec"], page_owner["free_ts_nsec"]))
- gdb.write("PFN: %d, Flags: 0x%x\n" % (pfn, page['flags']))
- stackdepot.stack_depot_print(page_owner["handle"])
- pfn += (1 << page_owner["order"])
- continue
- except:
- gdb.write("\n")
- pfn += 1
+ page_owner = self.get_page_owner(page_ext)
+ gdb.write("Page allocated via order %d, gfp_mask: 0x%x, pid: %d, tgid: %d (%s), ts %u ns, free_ts %u ns\n" %\
+ (page_owner["order"], page_owner["gfp_mask"],\
+ page_owner["pid"], page_owner["tgid"], page_owner["comm"].string(),\
+ page_owner["ts_nsec"], page_owner["free_ts_nsec"]))
+ gdb.write("PFN: %d, Flags: 0x%x\n" % (pfn, page['flags']))
+ stackdepot.stack_depot_print(page_owner["handle"])
+ pfn += (1 << page_owner["order"])

DumpPageOwner()
diff --git a/scripts/gdb/linux/slab.py b/scripts/gdb/linux/slab.py
index f012ba38c7d9..0e2d93867fe2 100644
--- a/scripts/gdb/linux/slab.py
+++ b/scripts/gdb/linux/slab.py
@@ -228,8 +228,7 @@ def slabtrace(alloc, cache_name):
nr_cpu = gdb.parse_and_eval('__num_online_cpus')['counter']
if nr_cpu > 1:
gdb.write(" cpus=")
- for i in loc['cpus']:
- gdb.write("%d," % i)
+ gdb.write(','.join(str(cpu) for cpu in loc['cpus']))
gdb.write("\n")
if constants.LX_CONFIG_STACKDEPOT:
if loc['handle']:
--
2.18.0

2023-11-27 12:04:42

by Oleg Nesterov

[permalink] [raw]
Subject: Re: [PATCH 1/3] scripts/gdb/tasks: Fix lx-ps command error

On 11/27, Kuan-Ying Lee wrote:
>
> @@ -25,13 +25,9 @@ def task_lists():
> t = g = init_task
>
> while True:
> - while True:
> - yield t
> -
> - t = utils.container_of(t['thread_group']['next'],
> - task_ptr_type, "thread_group")
> - if t == g:
> - break
> + thread_head = t['signal']['thread_head']
> + for thread in lists.list_for_each_entry(thread_head, task_ptr_type, 'thread_node'):
> + yield thread
>
> t = g = utils.container_of(g['tasks']['next'],
> task_ptr_type, "tasks")

Thanks!

I do not know python, but it seems that with this patch we can kill g or t?
Can't

def task_lists():
task_ptr_type = task_type.get_type().pointer()
init_task = gdb.parse_and_eval("init_task").address
t = init_task

while True:
thread_head = t['signal']['thread_head']
for thread in lists.list_for_each_entry(thread_head, task_ptr_type, 'thread_node'):
yield thread

t = utils.container_of(t['tasks']['next'],
task_ptr_type, "tasks")
if t == init_task:
return

work?

Oleg.

Subject: Re: [PATCH 1/3] scripts/gdb/tasks: Fix lx-ps command error

On Mon, 2023-11-27 at 13:03 +0100, Oleg Nesterov wrote:
>
> External email : Please do not click links or open attachments until
> you have verified the sender or the content.
> On 11/27, Kuan-Ying Lee wrote:
> >
> > @@ -25,13 +25,9 @@ def task_lists():
> > t = g = init_task
> >
> > while True:
> > - while True:
> > - yield t
> > -
> > - t = utils.container_of(t['thread_group']['next'],
> > - task_ptr_type, "thread_group")
> > - if t == g:
> > - break
> > + thread_head = t['signal']['thread_head']
> > + for thread in lists.list_for_each_entry(thread_head,
> task_ptr_type, 'thread_node'):
> > + yield thread
> >
> > t = g = utils.container_of(g['tasks']['next'],
> > task_ptr_type, "tasks")
>
> Thanks!
>
> I do not know python, but it seems that with this patch we can kill g
> or t?
> Can't
>
> def task_lists():
> task_ptr_type = task_type.get_type().pointer()
> init_task = gdb.parse_and_eval("init_task").address
> t = init_task
>
> while True:
> thread_head = t['signal']['thread_head']
> for thread in lists.list_for_each_entry(thread_head, task_ptr_type,
> 'thread_node'):
> yield thread
>
> t = utils.container_of(t['tasks']['next'],
> task_ptr_type, "tasks")
> if t == init_task:
> return
>
> work?

Yes, you are right.
I will fix it in v2.

Thanks,
Kuan-Ying Lee
>
> Oleg.
>