Subject: [PATCH v2 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

---
V1->V2:
- Remove unnecessary variable. (Thanks Oleg)
- Refine commit message.

Kuan-Ying Lee (3):
scripts/gdb/tasks: Fix lx-ps command error
scripts/gdb/stackdepot: Rename pool_index 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 | 18 ++++------
4 files changed, 36 insertions(+), 49 deletions(-)

--
2.18.0


Subject: [PATCH v2 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 | 18 +++++++-----------
1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/scripts/gdb/linux/tasks.py b/scripts/gdb/linux/tasks.py
index 17ec19e9b5bf..aa5ab6251f76 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")
@@ -22,19 +22,15 @@ task_type = utils.CachedType("struct task_struct")
def task_lists():
task_ptr_type = task_type.get_type().pointer()
init_task = gdb.parse_and_eval("init_task").address
- t = g = init_task
+ t = init_task

while True:
- while True:
- yield t
+ 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['thread_group']['next'],
- task_ptr_type, "thread_group")
- if t == g:
- break
-
- t = g = utils.container_of(g['tasks']['next'],
- task_ptr_type, "tasks")
+ t = utils.container_of(t['tasks']['next'],
+ task_ptr_type, "tasks")
if t == init_task:
return

--
2.18.0

Subject: [PATCH v2 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-catch block. Thus, removing the try-catch 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

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

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

To avoid from the below issue, we rename consistently in
gdb scripts.

Python Exception <class 'gdb.error'>: No symbol "pool_index" in current
context.
Error occurred in Python: No symbol "pool_index" in current context.

[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

2023-11-29 08:11:42

by Oleg Nesterov

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

On 11/29, Kuan-Ying Lee wrote:
>
> 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.

Thanks again,

Acked-by: Oleg Nesterov <[email protected]>


> Fixes: 8e1f385104ac ("kill task_struct->thread_group")
> Cc: [email protected]

Is it possible to merge this simple change before v6.7 ?
Then "cc: stable" can be removed.

Oleg.

2023-11-29 22:15:59

by Andrew Morton

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

On Wed, 29 Nov 2023 09:10:09 +0100 Oleg Nesterov <[email protected]> wrote:

> > Fixes: 8e1f385104ac ("kill task_struct->thread_group")
> > Cc: [email protected]
>
> Is it possible to merge this simple change before v6.7 ?
> Then "cc: stable" can be removed.

Yes, I shall do all that.

2023-11-29 22:33:31

by Florian Fainelli

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

On 11/28/23 22:51, Kuan-Ying Lee wrote:
> 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]>

Tested-by: Florian Fainelli <[email protected]>
--
Florian

2023-11-29 22:33:35

by Florian Fainelli

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] scripts/gdb/stackdepot: Rename pool_index to pools_num

On 11/28/23 22:51, Kuan-Ying Lee wrote:
> After stackdepot evicting support patchset[1], we rename
> pool_index to pools_num.
>
> To avoid from the below issue, we rename consistently in
> gdb scripts.
>
> Python Exception <class 'gdb.error'>: No symbol "pool_index" in current
> context.
> Error occurred in Python: No symbol "pool_index" in current context.
>
> [1] https://lore.kernel.org/linux-mm/[email protected]/
> Cc: Andrey Konovalov <[email protected]>
> Signed-off-by: Kuan-Ying Lee <[email protected]>

Reviewed-by: Florian Fainelli <[email protected]>
--
Florian