[PATCH i-g-t 2/4] tools/displaytop: Add Display debugfs entry for display top

Adith Narein T adith.narein.t at intel.com
Thu May 29 14:23:41 UTC 2025


This patch adds support for reading, displaying & dumping of display-related
debugfs entries within the DisplayTop tool. Users can now browse and dump
relevant i915 debugfs entries related to the display pipeline, enabling
easier real-time inspection and analysis of driver-level state.

Signed-off-by: Adith Narein T <adith.narein.t at intel.com>
---
 tools/displaytop/README.md                    |   1 +
 tools/displaytop/include/data.h               |   2 +
 tools/displaytop/include/display.h            |   2 +
 tools/displaytop/include/populate.h           |   1 +
 tools/displaytop/include/utils.h              |   2 +
 tools/displaytop/meson.build                  |   2 +
 tools/displaytop/src/display_debugfs.c        | 133 ++++++++++++++++++
 tools/displaytop/src/populate.c               |   1 +
 .../displaytop/src/populate_display_debugfs.c |  99 +++++++++++++
 tools/displaytop/src/utils_driver.c           |  54 +++++++
 10 files changed, 297 insertions(+)
 create mode 100644 tools/displaytop/src/display_debugfs.c
 create mode 100644 tools/displaytop/src/populate_display_debugfs.c

diff --git a/tools/displaytop/README.md b/tools/displaytop/README.md
index 7349f0760..3e123ee04 100644
--- a/tools/displaytop/README.md
+++ b/tools/displaytop/README.md
@@ -7,6 +7,7 @@ A terminal-based tool for monitoring and debugging the display pipeline.
 - Terminal UI using `ncurses`
 - Dump feature for all the menus
 - Real-time display Configuration
+- view Live Display Debugfs in a terminal ui
 
 ---
 
diff --git a/tools/displaytop/include/data.h b/tools/displaytop/include/data.h
index 0bc1b51d4..a6a6275f7 100644
--- a/tools/displaytop/include/data.h
+++ b/tools/displaytop/include/data.h
@@ -34,4 +34,6 @@ extern Node *root; /* shared throughout the program */
 #define DRM_PRIMARY_PREFIX "card"
 #define DRM_RENDER_PREFIX "renderD"
 
+#define DEBUGFS_DRI_PATH "/sys/kernel/debug/dri"
+
 #endif
\ No newline at end of file
diff --git a/tools/displaytop/include/display.h b/tools/displaytop/include/display.h
index db77ef785..dc9e9b4d2 100644
--- a/tools/displaytop/include/display.h
+++ b/tools/displaytop/include/display.h
@@ -43,4 +43,6 @@ void display_framebuffer(WINDOW *pad, Node *node, int *content_line);
 
 void display_summary(WINDOW *pad, Node *node, int *content_line);
 
+void display_debugfs_file(WINDOW *pad, Node *node, int *content_line);
+
 #endif
\ No newline at end of file
diff --git a/tools/displaytop/include/populate.h b/tools/displaytop/include/populate.h
index 5a5241355..fee62bbb1 100644
--- a/tools/displaytop/include/populate.h
+++ b/tools/displaytop/include/populate.h
@@ -33,5 +33,6 @@
 void populate_data(void);
 
 void initialize_display_config(void);
+void initialize_display_debugfs(void);
 
 #endif
\ No newline at end of file
diff --git a/tools/displaytop/include/utils.h b/tools/displaytop/include/utils.h
index eef5df5f3..17af75b8c 100644
--- a/tools/displaytop/include/utils.h
+++ b/tools/displaytop/include/utils.h
@@ -112,4 +112,6 @@ const char *get_basic_modifier_str(uint64_t modifier);
 const char *get_connector_type_name(uint32_t connector_type);
 const char *get_encoder_type_name(uint32_t encoder_type);
 
+char *find_debugfs_dir(void);
+
 #endif
\ No newline at end of file
diff --git a/tools/displaytop/meson.build b/tools/displaytop/meson.build
index 2d8c9d418..1f51a3cf2 100644
--- a/tools/displaytop/meson.build
+++ b/tools/displaytop/meson.build
@@ -58,6 +58,8 @@ if have_displaytop
     'src/utils_driver.c',
     'src/utils_drm.c',
     'src/utils_search.c',
+    'src/populate_display_debugfs.c',
+    'src/display_debugfs.c',
   )
 
   if meson.is_subproject()
diff --git a/tools/displaytop/src/display_debugfs.c b/tools/displaytop/src/display_debugfs.c
new file mode 100644
index 000000000..9b7467e8a
--- /dev/null
+++ b/tools/displaytop/src/display_debugfs.c
@@ -0,0 +1,133 @@
+/*
+ * Copyright © 2025 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "display.h"
+#include "utils.h"
+
+/**
+ * /docs
+ * build_full_path - Constructs the full filesystem path for a given node in the debugfs tree.
+ * @node:       Pointer to the Node structure for which the path is to be built.
+ * @full_path:  Buffer to store the resulting full path as a null-terminated string.
+ * @size:       Size of the @full_path buffer.
+ *
+ * This function traverses the parent hierarchy of the given node, collecting
+ * each node's name (excluding a trailing '*' if present), and constructs the
+ * relative path from the root node named "Display Debugfs" down to the given node.
+ * The resulting path is prefixed with the debugfs directory path (as returned by
+ * find_debugfs_dir()) and stored in @full_path.
+ *
+ * The function ensures that the constructed path does not exceed the provided buffer size.
+ */
+static void build_full_path(Node *node, char *full_path, size_t size)
+{
+    int i;
+    int depth = 0;
+    char segments[20][256];
+    char relative_path[1024] = {0};
+
+    node = node;
+    while (node && node->parent && strcmp(node->name, "Display Debugfs") != 0)
+    {
+        char *name = node->name;
+        size_t name_len = strlen(name);
+
+        if (name_len > 0 && name[name_len - 1] == '*')
+        {
+            snprintf(segments[depth], sizeof(segments[depth]), "%.*s", (int)(name_len - 1), name);
+        }
+        else
+        {
+            snprintf(segments[depth], sizeof(segments[depth]), "%s", name);
+        }
+
+        depth++;
+        node = node->parent;
+    }
+
+    for (i = depth - 1; i >= 0; i--)
+    {
+        strncat(relative_path, "/", sizeof(relative_path) - strlen(relative_path) - 1);
+        strncat(relative_path, segments[i], sizeof(relative_path) - strlen(relative_path) - 1);
+    }
+
+    snprintf(full_path, size, "%s%s", find_debugfs_dir(), relative_path);
+}
+
+void display_debugfs_file(WINDOW *pad, Node *node, int *content_line)
+{
+    int line = 0;
+    int max_x = getmaxx(pad) - 4;
+    int start, break_point;
+
+    FILE *file;
+    char temp[1024];
+    char buffer[1024];
+    char full_file_path[1024] = {0};
+
+    build_full_path(node, full_file_path, sizeof(full_file_path));
+
+    file = fopen(full_file_path, "r");
+    if (!file)
+    {
+        print_red_text(pad, line++, 1, "Error opening file: %s", full_file_path);
+        *content_line = line;
+        return;
+    }
+
+    while (fgets(buffer, sizeof(buffer), file))
+    {
+        buffer[strcspn(buffer, "\n")] = '\0';
+
+        start = 0;
+        while (start < (int)strlen(buffer))
+        {
+            break_point = start + max_x;
+            if (break_point < (int)strlen(buffer))
+            {
+                while (break_point > start && !isspace(buffer[break_point]))
+                    break_point--;
+
+                if (break_point == start)
+                    break_point = start + max_x;
+            }
+            else
+            {
+                break_point = strlen(buffer);
+            }
+
+            strncpy(temp, buffer + start, break_point - start);
+            temp[break_point - start] = '\0';
+
+            mvwprintw(pad, line++, 2, "%s", temp);
+
+            start = (buffer[break_point] == ' ') ? break_point + 1 : break_point;
+        }
+    }
+
+    line++;
+    mvwprintw(pad, line++, 2, "-----------------EOF-------------------");
+
+    *content_line = line;
+    fclose(file);
+}
\ No newline at end of file
diff --git a/tools/displaytop/src/populate.c b/tools/displaytop/src/populate.c
index 0872b0b43..fe8cd215c 100644
--- a/tools/displaytop/src/populate.c
+++ b/tools/displaytop/src/populate.c
@@ -29,4 +29,5 @@ void populate_data(void)
     root = create_node("Display Top", display_summary, NULL);
 
     initialize_display_config();
+    initialize_display_debugfs();
 }
diff --git a/tools/displaytop/src/populate_display_debugfs.c b/tools/displaytop/src/populate_display_debugfs.c
new file mode 100644
index 000000000..494414764
--- /dev/null
+++ b/tools/displaytop/src/populate_display_debugfs.c
@@ -0,0 +1,99 @@
+/*
+ * Copyright © 2025 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "populate.h"
+#include "utils.h"
+
+static void populate_debugfs_recursive(Node *parent, const char *currentPath)
+{
+    DIR *dir;
+    struct dirent *entry;
+    char full_path[1024];
+    char dir_name_with_asterisk[1024];
+    struct stat pathStat;
+    Node *fileNode;
+    Node *dirNode;
+
+    dir = opendir(currentPath);
+    if (!dir)
+    {
+        log_message(LOG_ERROR, "Failed to open directory: %s", currentPath);
+        return;
+    }
+
+    while ((entry = readdir(dir)) != NULL)
+    {
+        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
+            continue;
+
+        snprintf(full_path, sizeof(full_path), "%s/%s", currentPath, entry->d_name);
+
+        if (stat(full_path, &pathStat) == -1)
+        {
+            log_message(LOG_WARNING, "Could not stat path: %s", full_path);
+            continue;
+        }
+
+        if (S_ISREG(pathStat.st_mode))
+        {
+            fileNode = create_node(entry->d_name, display_debugfs_file, parent);
+            add_child(parent, fileNode);
+        }
+        else if (S_ISDIR(pathStat.st_mode))
+        {
+            snprintf(dir_name_with_asterisk, sizeof(dir_name_with_asterisk), "%s*", entry->d_name);
+
+            dirNode = create_node(dir_name_with_asterisk, NULL, parent);
+            populate_debugfs_recursive(dirNode, full_path);
+            add_child(parent, dirNode);
+        }
+    }
+
+    closedir(dir);
+}
+
+static void populate_debugfs(Node *parent)
+{
+    char *selected_path = find_debugfs_dir();
+    if (!selected_path)
+        return;
+
+    populate_debugfs_recursive(parent, selected_path);
+    free(selected_path);
+}
+
+void initialize_display_debugfs(void)
+{
+    Node *display_debugfs;
+
+    display_debugfs = create_node("Display Debugfs", NULL, root);
+    populate_debugfs(display_debugfs);
+
+    if (display_debugfs->children_size == 0)
+    {
+        free(display_debugfs);
+        return;
+    }
+
+    add_child(root, display_debugfs);
+}
diff --git a/tools/displaytop/src/utils_driver.c b/tools/displaytop/src/utils_driver.c
index 7b24d2f41..b09e2abba 100644
--- a/tools/displaytop/src/utils_driver.c
+++ b/tools/displaytop/src/utils_driver.c
@@ -187,3 +187,57 @@ void check_and_load_driver(void)
         show_driver_menu();
     }
 }
+
+char *find_debugfs_dir(void)
+{
+    DIR *dir;
+    struct dirent *entry;
+    char *selected_path = NULL;
+
+    dir = opendir(DEBUGFS_DRI_PATH);
+    if (!dir)
+    {
+        log_message(LOG_ERROR, "Failed to open debugfs dri directory");
+        return NULL;
+    }
+
+    while ((entry = readdir(dir)) != NULL)
+    {
+        if (strncmp(entry->d_name, "0000:", 5) == 0)
+        {
+            selected_path = malloc(512);
+            if (!selected_path)
+            {
+                log_message(LOG_ERROR, "Memory allocation failed");
+                closedir(dir);
+                return NULL;
+            }
+
+            snprintf(selected_path, 512, "%s/%s", DEBUGFS_DRI_PATH, entry->d_name);
+            closedir(dir);
+            return selected_path;
+        }
+    }
+
+    rewinddir(dir);
+    while ((entry = readdir(dir)) != NULL)
+    {
+        if (!isdigit(entry->d_name[0]))
+            continue;
+
+        selected_path = malloc(512);
+        if (!selected_path)
+        {
+            log_message(LOG_ERROR, "Memory allocation failed");
+            closedir(dir);
+            return NULL;
+        }
+
+        snprintf(selected_path, 512, "%s/%s", DEBUGFS_DRI_PATH, entry->d_name);
+        log_message(LOG_INFO, "Using debugfs path: %s", selected_path);
+        break;
+    }
+
+    closedir(dir);
+    return selected_path;
+}
\ No newline at end of file
-- 
2.43.0



More information about the igt-dev mailing list