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 1c884ee2304f5d57089673b319cbedce6a611e5d
Author: Zhe Weng <weng...@xiaomi.com>
AuthorDate: Thu Aug 22 20:26:35 2024 +0800

    tools/gdb: Add packet statistic in netstats
    
    Unlike NuttX's one, we use decimal instead of hexadecimal, because other
    stats may all use decimal.
    
    (gdb) netstats
    IOB:       size    ntotal     nfree     nwait nthrottle
               1518        72        72         0        40
    
    Packets:   IPv4   IPv6    TCP    UDP   ICMP ICMPv6
    Received    137     43      0     49     88      0
    Dropped       0     43      0      0     74      0
      VHL         0      0      -      -      -      -
      Frag        0      0      -      -      -      -
      Chksum      0      -      0      0      -      -
      Type        -      -      -      -     74      0
      Proto       0      0      -      -      -      -
    Sent      11481  11542      0  17223     27      4
      Rexmit      -      -      0      -      -      -
    
    Signed-off-by: Zhe Weng <weng...@xiaomi.com>
---
 tools/gdb/net.py   | 35 +++++++++++++++++++++++++++++++++++
 tools/gdb/utils.py |  8 ++++++++
 2 files changed, 43 insertions(+)

diff --git a/tools/gdb/net.py b/tools/gdb/net.py
index d361a211a1..c2f2e3ca31 100644
--- a/tools/gdb/net.py
+++ b/tools/gdb/net.py
@@ -52,9 +52,44 @@ class NetStats(gdb.Command):
         except gdb.error as e:
             gdb.write("Failed to get IOB stats: %s\n" % e)
 
+    def pkt_stats(self):
+        try:
+            netstats = gdb.parse_and_eval("g_netstats")
+            gdb.write(
+                "Packets:%7s%7s%7s%7s%7s%7s\n"
+                % ("IPv4", "IPv6", "TCP", "UDP", "ICMP", "ICMPv6")
+            )
+
+            def stats_line(title, member):
+                gdb.write("%-8s" % title)
+                for proto in ("ipv4", "ipv6", "tcp", "udp", "icmp", "icmpv6"):
+                    gdb.write(
+                        "%7s"
+                        % utils.get_field(utils.get_field(netstats, proto), 
member, "-")
+                    )
+                gdb.write("\n")
+
+            stats_line("Received", "recv")
+            stats_line("Dropped", "drop")
+            stats_line("  VHL", "vhlerr")
+            stats_line("  Frag", "fragerr")
+            stats_line("  Chksum", "chkerr")
+            stats_line("  Type", "typeerr")
+            stats_line("  Proto", "protoerr")
+            # TODO: Maybe print TCP's ackerr, rst, syndrop, synrst here
+            stats_line("Sent", "sent")
+            stats_line("  Rexmit", "rexmit")
+
+        except gdb.error as e:
+            gdb.write("Failed to get Net Stats: %s\n" % e)
+
     def invoke(self, args, from_tty):
         if utils.get_symbol_value("CONFIG_MM_IOB"):
             self.iob_stats()
+            gdb.write("\n")
+        if utils.get_symbol_value("CONFIG_NET_STATISTICS"):
+            self.pkt_stats()
+            gdb.write("\n")
 
 
 if utils.get_symbol_value("CONFIG_NET"):
diff --git a/tools/gdb/utils.py b/tools/gdb/utils.py
index 51dbd01958..1f130893e9 100644
--- a/tools/gdb/utils.py
+++ b/tools/gdb/utils.py
@@ -169,6 +169,14 @@ def get_symbol_value(name, locspec="nx_start", 
cacheable=True):
     return value
 
 
+def get_field(val, key, default=None):
+    """Get a field from a gdb.Value, return default if key not found"""
+    try:
+        return val[key] if val else default
+    except gdb.error:
+        return default
+
+
 def import_check(module, name="", errmsg=""):
     try:
         module = __import__(module, fromlist=[name])

Reply via email to