[Lldb-commits] [lldb] [lldb] Implement ${target.file} format variable (PR #123431)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Mon Jan 20 11:13:28 PST 2025


https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/123431

>From c5474b3a1f0f2ed78f799e8b907ef2f2aa5d97e1 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Fri, 17 Jan 2025 17:18:13 -0800
Subject: [PATCH 1/3] [lldb] Implement ${taret.file} format variable

Implements a format variable to print the basename and full path to the
current target.
---
 lldb/docs/use/formatting.rst             |  8 ++++++--
 lldb/include/lldb/Core/FormatEntity.h    |  1 +
 lldb/source/Core/FormatEntity.cpp        | 18 +++++++++++++++++-
 lldb/unittests/Core/FormatEntityTest.cpp |  3 +++
 4 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/lldb/docs/use/formatting.rst b/lldb/docs/use/formatting.rst
index 970bacfd8807a7..3b7819d29d0a27 100644
--- a/lldb/docs/use/formatting.rst
+++ b/lldb/docs/use/formatting.rst
@@ -113,11 +113,11 @@ A complete list of currently supported format string variables is listed below:
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 | ``module.file.basename``                          | The basename of the current module (shared library or executable)                                                                                                                                                                                                                           |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-| ``module.file.fullpath``                          | The basename of the current module (shared library or executable)                                                                                                                                                                                                                           |
+| ``module.file.fullpath``                          | The path of the current module (shared library or executable)                                                                                                                                                                                                                               |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 | ``process.file.basename``                         | The basename of the file for the process                                                                                                                                                                                                                                                    |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-| ``process.file.fullpath``                         | The fullname of the file for the process                                                                                                                                                                                                                                                    |
+| ``process.file.fullpath``                         | The path of the file for the process                                                                                                                                                                                                                                                        |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 | ``process.id``                                    | The process ID native to the system on which the inferior runs.                                                                                                                                                                                                                             |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
@@ -141,6 +141,10 @@ A complete list of currently supported format string variables is listed below:
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 | ``target.arch``                                   | The architecture of the current target                                                                                                                                                                                                                                                      |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+| ``target.file.basename``                          | The basename of the current current target                                                                                                                                                                                                                                                  |
++---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+| ``target.file.fullpath``                          | The path of the current current target                                                                                                                                                                                                                                                      |
++---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 | ``script.target:python_func``                     | Use a Python function to generate a piece of textual output                                                                                                                                                                                                                                 |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 | ``script.process:python_func``                    | Use a Python function to generate a piece of textual output                                                                                                                                                                                                                                 |
diff --git a/lldb/include/lldb/Core/FormatEntity.h b/lldb/include/lldb/Core/FormatEntity.h
index 36f6df4118c21f..c9d5af1f31673b 100644
--- a/lldb/include/lldb/Core/FormatEntity.h
+++ b/lldb/include/lldb/Core/FormatEntity.h
@@ -67,6 +67,7 @@ struct Entry {
     ScriptThread,
     ThreadInfo,
     TargetArch,
+    TargetFile,
     ScriptTarget,
     ModuleFile,
     File,
diff --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp
index e13284832cf571..8355b08c887020 100644
--- a/lldb/source/Core/FormatEntity.cpp
+++ b/lldb/source/Core/FormatEntity.cpp
@@ -162,7 +162,9 @@ constexpr Definition g_thread_child_entries[] = {
     Definition("completed-expression", EntryType::ThreadCompletedExpression)};
 
 constexpr Definition g_target_child_entries[] = {
-    Definition("arch", EntryType::TargetArch)};
+    Definition("arch", EntryType::TargetArch),
+    Entry::DefinitionWithChildren("file", EntryType::TargetFile,
+                                  g_file_child_entries)};
 
 #define _TO_STR2(_val) #_val
 #define _TO_STR(_val) _TO_STR2(_val)
@@ -322,6 +324,7 @@ const char *FormatEntity::Entry::TypeToCString(Type t) {
     ENUM_TO_CSTR(ScriptThread);
     ENUM_TO_CSTR(ThreadInfo);
     ENUM_TO_CSTR(TargetArch);
+    ENUM_TO_CSTR(TargetFile);
     ENUM_TO_CSTR(ScriptTarget);
     ENUM_TO_CSTR(ModuleFile);
     ENUM_TO_CSTR(File);
@@ -1469,6 +1472,19 @@ bool FormatEntity::Format(const Entry &entry, Stream &s,
     }
     return false;
 
+  case Entry::Type::TargetFile:
+    if (exe_ctx) {
+      Target *target = exe_ctx->GetTargetPtr();
+      if (target) {
+        Module *exe_module = target->GetExecutableModulePointer();
+        if (exe_module) {
+          if (DumpFile(s, exe_module->GetFileSpec(), (FileKind)entry.number))
+            return true;
+        }
+      }
+    }
+    return false;
+
   case Entry::Type::ScriptTarget:
     if (exe_ctx) {
       Target *target = exe_ctx->GetTargetPtr();
diff --git a/lldb/unittests/Core/FormatEntityTest.cpp b/lldb/unittests/Core/FormatEntityTest.cpp
index 0a68c9340b77ae..5983c9de99ef78 100644
--- a/lldb/unittests/Core/FormatEntityTest.cpp
+++ b/lldb/unittests/Core/FormatEntityTest.cpp
@@ -148,6 +148,9 @@ constexpr llvm::StringRef lookupStrings[] = {
     "${thread.return-value}",
     "${thread.completed-expression}",
     "${target.arch}",
+    "${target.file.basename}",
+    "${target.file.dirname}",
+    "${target.file.fullpath}",
     "${var.dummy-var-to-test-wildcard}"};
 
 TEST(FormatEntity, LookupAllEntriesInTree) {

>From 05601979441d5021433052c3732f866b44036239 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Mon, 20 Jan 2025 11:11:03 -0800
Subject: [PATCH 2/3] Fix spurious 'current'

---
 lldb/docs/use/formatting.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lldb/docs/use/formatting.rst b/lldb/docs/use/formatting.rst
index 3b7819d29d0a27..7b3f01eebc8917 100644
--- a/lldb/docs/use/formatting.rst
+++ b/lldb/docs/use/formatting.rst
@@ -141,9 +141,9 @@ A complete list of currently supported format string variables is listed below:
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 | ``target.arch``                                   | The architecture of the current target                                                                                                                                                                                                                                                      |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-| ``target.file.basename``                          | The basename of the current current target                                                                                                                                                                                                                                                  |
+| ``target.file.basename``                          | The basename of the current target                                                                                                                                                                                                                                                          |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-| ``target.file.fullpath``                          | The path of the current current target                                                                                                                                                                                                                                                      |
+| ``target.file.fullpath``                          | The path of the current target                                                                                                                                                                                                                                                              |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 | ``script.target:python_func``                     | Use a Python function to generate a piece of textual output                                                                                                                                                                                                                                 |
 +---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

>From 38596061cc5e660c45091a9a4fe5f3f80f3207cd Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Mon, 20 Jan 2025 11:13:02 -0800
Subject: [PATCH 3/3] Move ptr checks into if

---
 lldb/source/Core/FormatEntity.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/lldb/source/Core/FormatEntity.cpp b/lldb/source/Core/FormatEntity.cpp
index 8355b08c887020..fb7043ac74b8dd 100644
--- a/lldb/source/Core/FormatEntity.cpp
+++ b/lldb/source/Core/FormatEntity.cpp
@@ -1474,10 +1474,8 @@ bool FormatEntity::Format(const Entry &entry, Stream &s,
 
   case Entry::Type::TargetFile:
     if (exe_ctx) {
-      Target *target = exe_ctx->GetTargetPtr();
-      if (target) {
-        Module *exe_module = target->GetExecutableModulePointer();
-        if (exe_module) {
+      if (Target *target = exe_ctx->GetTargetPtr()) {
+        if (Module *exe_module = target->GetExecutableModulePointer()) {
           if (DumpFile(s, exe_module->GetFileSpec(), (FileKind)entry.number))
             return true;
         }



More information about the lldb-commits mailing list