This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 8264c05c15fa280bf43692bfabd3b86fdd4549f1
Author: xuxingliang <xuxingli...@xiaomi.com>
AuthorDate: Tue Aug 6 22:52:56 2024 +0800

    tools/gdb: use f string where possible
    
    misc update.
    
    Signed-off-by: xuxingliang <xuxingli...@xiaomi.com>
---
 tools/gdb/__init__.py |  6 ++--
 tools/gdb/stack.py    |  4 +--
 tools/gdb/thread.py   | 76 ++++++++++++++++++++++++---------------------------
 3 files changed, 41 insertions(+), 45 deletions(-)

diff --git a/tools/gdb/__init__.py b/tools/gdb/__init__.py
index 02819a3e9b..599c1402ba 100644
--- a/tools/gdb/__init__.py
+++ b/tools/gdb/__init__.py
@@ -31,7 +31,7 @@ python_dir = os.path.dirname(python_dir)
 
 sys.path.insert(1, python_dir)
 # Search the python dir for all .py files, and source each
-py_files = glob.glob("%s/*.py" % python_dir)
+py_files = glob.glob(f"{python_dir}/*.py")
 py_files.remove(os.path.abspath(__file__))
 
 gdb.execute("set pagination off")
@@ -39,8 +39,8 @@ gdb.write("set pagination off\n")
 gdb.execute("set python print-stack full")
 gdb.write("set python print-stack full\n")
 for py_file in py_files:
-    gdb.execute("source %s" % py_file)
-    gdb.write("source %s\n" % py_file)
+    gdb.execute(f"source {py_file}")
+    gdb.write(f"source {py_file}\n")
 
 gdb.execute('handle SIGUSR1 "nostop" "pass" "noprint"')
 gdb.write('"handle SIGUSR1 "nostop" "pass" "noprint"\n')
diff --git a/tools/gdb/stack.py b/tools/gdb/stack.py
index caa3e635db..ac5c3b536b 100644
--- a/tools/gdb/stack.py
+++ b/tools/gdb/stack.py
@@ -119,9 +119,9 @@ def fetch_stacks():
     for tcb in utils.get_tcbs():
         if tcb["task_state"] == gdb.parse_and_eval("TSTATE_TASK_RUNNING") \
                 and not utils.in_interrupt_context():
-            sp = utils.get_register_byname(utils.get_arch_sp_name(), tcb=None)
+            sp = utils.get_sp()
         else:
-            sp = utils.get_register_byname(utils.get_arch_sp_name(), tcb=tcb)
+            sp = utils.get_sp(tcb=tcb)
 
         try:
             stacks[int(tcb["pid"])] = Stack(
diff --git a/tools/gdb/thread.py b/tools/gdb/thread.py
index bb303561f9..fda2aa7977 100644
--- a/tools/gdb/thread.py
+++ b/tools/gdb/thread.py
@@ -61,7 +61,7 @@ def restore_regs():
         if i >= tcbinfo["regs_num"]:
             break
 
-        gdb.execute("set $%s=%d" % (reg.name, int(saved_regs[i])))
+        gdb.execute(f"set ${reg.name}={saved_regs[i]}")
         i += 1
 
     saved_regs = None
@@ -91,7 +91,7 @@ class Nxsetregs(gdb.Command):
         arg = args.split(" ")
 
         if arg[0] != "":
-            regs = gdb.parse_and_eval("%s" % arg[0]).cast(
+            regs = gdb.parse_and_eval(f"{arg[0]}").cast(
                 gdb.lookup_type("char").pointer()
             )
         else:
@@ -115,7 +115,7 @@ class Nxsetregs(gdb.Command):
                 value = gdb.Value(regs + tcbinfo["reg_off"]["p"][i]).cast(
                     gdb.lookup_type("uintptr_t").pointer()
                 )[0]
-                gdb.execute("set $%s = 0x%x" % (reg.name, value))
+                gdb.execute(f"set ${reg.name} = {value}")
 
             i += 1
 
@@ -138,51 +138,49 @@ class Nxinfothreads(gdb.Command):
             gdb.write("%-5s %-4s %-4s %-21s %-80s %-30s\n" % ("Index", "Tid", 
"Pid", "Thread", "Info", "Frame"))
 
         for i in range(0, npidhash):
-            if pidhash[i] == 0:
+            tcb = pidhash[i]
+            if not tcb:
                 continue
 
-            pid = pidhash[i]["group"]["tg_pid"]
-            tid = pidhash[i]["pid"]
+            pid = tcb["group"]["tg_pid"]
+            tid = tcb["pid"]
 
-            if pidhash[i]["task_state"] == 
gdb.parse_and_eval("TSTATE_TASK_RUNNING"):
-                index = "*%s" % i
-                pc = utils.get_register_byname(utils.get_arch_pc_name(), 
tcb=None)
+            if tcb["task_state"] == gdb.parse_and_eval("TSTATE_TASK_RUNNING"):
+                index = f"*{i}"
+                pc = utils.get_pc()
             else:
-                index = " %s" % i
-                pc = utils.get_register_byname(utils.get_arch_pc_name(), 
tcb=pidhash[i])
+                index = f" {i}"
+                pc = utils.get_pc(tcb=tcb)
 
-            thread = "Thread 0x%x" % pidhash[i]
+            thread = f"Thread {hex(tcb)}"
 
-            statename = statenames[pidhash[i]["task_state"]].string()
-            if statename == "Running":
-                statename = "\x1b[32;1m%s\x1b[m" % statename
-            else:
-                statename = "\x1b[33;1m%s\x1b[m" % statename
+            statename = statenames[tcb["task_state"]].string()
+            statename = f'\x1b{"[32;1m" if statename == "Running" else 
"[33;1m"}{statename}\x1b[m'
 
-            if pidhash[i]["task_state"] == 
gdb.parse_and_eval("TSTATE_WAIT_SEM"):
-                mutex = 
pidhash[i]["waitobj"].cast(gdb.lookup_type("sem_t").pointer())
+            if tcb["task_state"] == gdb.parse_and_eval("TSTATE_WAIT_SEM"):
+                mutex = tcb["waitobj"].cast(gdb.lookup_type("sem_t").pointer())
                 if mutex["flags"] & SEM_TYPE_MUTEX:
-                    mutex = 
pidhash[i]["waitobj"].cast(gdb.lookup_type("mutex_t").pointer())
-                    statename = "Waiting,Mutex:%d" % (mutex["holder"])
+                    mutex = 
tcb["waitobj"].cast(gdb.lookup_type("mutex_t").pointer())
+                    statename = f"Waiting,Mutex:{mutex['holder']}"
 
             try:
                 """Maybe tcb not have name member, or name is not utf-8"""
                 info = "(Name: \x1b[31;1m%s\x1b[m, State: %s, Priority: %d, 
Stack: %d)" % (
-                    pidhash[i]["name"].string(),
+                    tcb["name"].string(),
                     statename,
-                    pidhash[i]["sched_priority"],
-                    pidhash[i]["adj_stack_size"],
+                    tcb["sched_priority"],
+                    tcb["adj_stack_size"],
                 )
             except gdb.error and UnicodeDecodeError:
                 info = "(Name: Not utf-8, State: %s, Priority: %d, Stack: %d)" 
% (
                     statename,
-                    pidhash[i]["sched_priority"],
-                    pidhash[i]["adj_stack_size"],
+                    tcb["sched_priority"],
+                    tcb["adj_stack_size"],
                 )
 
             line = gdb.find_pc_line(pc)
             if line.symtab:
-                func = gdb.execute("info symbol %d " % pc, to_string=True)
+                func = gdb.execute(f"info symbol {pc} ", to_string=True)
                 frame = "\x1b[34;1m0x%x\x1b[\t\x1b[33;1m%s\x1b[m at %s:%d" % (
                     pc,
                     func.split()[0] + "()",
@@ -193,7 +191,7 @@ class Nxinfothreads(gdb.Command):
                 frame = "No symbol with pc"
 
             if utils.is_target_smp():
-                cpu = "%d" % pidhash[i]["cpu"]
+                cpu = f"{tcb['cpu']}"
                 gdb.write(
                     "%-5s %-4s %-4s %-4s %-21s %-80s %-30s\n" % (index, tid, 
pid, cpu, thread, info, frame)
                 )
@@ -224,16 +222,16 @@ class Nxthread(gdb.Command):
                     if pidhash[i] == 0:
                         continue
                     try:
-                        gdb.write("Thread %d %s\n" % (i, 
pidhash[i]["name"].string()))
+                        gdb.write(f"Thread {i} 
{pidhash[i]['name'].string()}\n")
                     except gdb.error and UnicodeDecodeError:
-                        gdb.write("Thread %d\n" % (i))
+                        gdb.write(f"Thread {i}\n")
 
-                    gdb.execute("nxsetregs g_pidhash[%d]->xcp.regs" % i)
+                    gdb.execute(f"nxsetregs g_pidhash[{i}]->xcp.regs")
                     cmd_arg = ""
                     for cmd in arg[2:]:
                         cmd_arg += cmd + " "
 
-                    gdb.execute("%s\n" % cmd_arg)
+                    gdb.execute(f"{cmd_arg}\n")
                     restore_regs()
             else:
                 threadlist = []
@@ -256,14 +254,12 @@ class Nxthread(gdb.Command):
                             continue
 
                         try:
-                            gdb.write(
-                                "Thread %d %s\n" % (i, 
pidhash[i]["name"].string())
-                            )
+                            gdb.write(f"Thread {i} 
{pidhash[i]['name'].string()}\n")
                         except gdb.error and UnicodeDecodeError:
-                            gdb.write("Thread %d\n" % (i))
+                            gdb.write(f"Thread {i}\n")
 
-                        gdb.execute("nxsetregs g_pidhash[%d]->xcp.regs" % i)
-                        gdb.execute("%s\n" % cmd)
+                        gdb.execute(f"nxsetregs g_pidhash[{i}]->xcp.regs")
+                        gdb.execute(f"{cmd}\n")
                         restore_regs()
 
         else:
@@ -273,7 +269,7 @@ class Nxthread(gdb.Command):
                 else:
                     gdb.execute("nxsetregs g_pidhash[%s]->xcp.regs" % arg[0])
             else:
-                gdb.write("Invalid thread id %s\n" % arg[0])
+                gdb.write(f"Invalid thread id {arg[0]}\n")
 
 
 class Nxcontinue(gdb.Command):
@@ -366,7 +362,7 @@ class Ps(gdb.Command):
             int(tcb["stack_base_ptr"]),
             int(tcb["stack_alloc_ptr"]),
             int(tcb["adj_stack_size"]),
-            utils.get_register_byname("sp", tcb),
+            utils.get_sp(tcb),
             4,)
 
         stacksz = st._stack_size

Reply via email to