Wireshark 5.1.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
lua_debugger_utils.cpp File Reference
#include <config.h>
#include "lua_debugger_utils.h"
#include <ui_lua_debugger_dialog.h>
#include <QAbstractItemModel>
#include <QAction>
#include <QApplication>
#include <QBrush>
#include <QCoreApplication>
#include <QFont>
#include <QGuiApplication>
#include <QKeyEvent>
#include <QKeySequence>
#include <QList>
#include <QMetaObject>
#include <QObject>
#include <QPainter>
#include <QPalette>
#include <QPen>
#include <QPersistentModelIndex>
#include <QPixmap>
#include <QPointer>
#include <QSizePolicy>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QStyle>
#include <QTimer>
#include <QToolButton>
#include <QTreeView>
#include <QWidget>
#include <algorithm>
#include <glib.h>
#include "app/application_flavor.h"
#include "lua_debugger_breakpoints.h"
#include "lua_debugger_code_editor.h"
#include "lua_debugger_dialog.h"
#include "lua_debugger_pause.h"
#include "wsutil/filesystem.h"
#include <epan/wslua/wslua_debugger.h>
#include <ui/qt/utils/qt_ui_utils.h>
#include <ui/qt/utils/theme_manager.h>
#include <ui/qt/utils/themes/color_math.h>
#include <ui/qt/utils/themes/contrast_adapt_icon.h>

Macros

#define LUA_DEBUGGER_SETTINGS_FILE   "lua_debugger.json"

Functions

QString luaDebuggerSettingsFilePath ()
 Returns the file path for Lua debugger settings.
void luaDbgRecordTreeSectionRootExpansion (QHash< QString, LuaDbgTreeSectionExpansionState > &map, const QString &rootKey, bool expanded)
 Record / clear root-level expansion for rootKey in map. Mutates map in place; collapsing a root with no remembered subpaths drops the entry entirely so the map stays minimal.
void luaDbgRecordTreeSectionSubpathExpansion (QHash< QString, LuaDbgTreeSectionExpansionState > &map, const QString &rootKey, const QString &key, bool expanded)
 Add / remove one descendant subpath key under rootKey in map. Mirrors luaDbgRecordTreeSectionRootExpansion: removing the last subpath of a collapsed root erases the root entry too.
QStringList luaDbgTreeSectionExpandedSubpaths (const QHash< QString, LuaDbgTreeSectionExpansionState > &map, const QString &rootKey)
 Expanded-descendant subpaths recorded for rootKey, or empty.
const QKeySequence kLuaDbgCtxGoToLine (QKeySequence(Qt::CTRL|Qt::Key_G))
const QKeySequence kLuaDbgCtxRunToLine (QKeySequence(Qt::CTRL|Qt::Key_F10))
const QKeySequence kLuaDbgCtxWatchEdit (Qt::Key_F2)
const QKeySequence kLuaDbgCtxWatchCopyValue (QKeySequence(Qt::CTRL|Qt::SHIFT|Qt::Key_C))
const QKeySequence kLuaDbgCtxWatchDuplicate (QKeySequence(Qt::CTRL|Qt::SHIFT|Qt::Key_D))
const QKeySequence kLuaDbgCtxWatchRemoveAll (QKeySequence(Qt::CTRL|Qt::SHIFT|Qt::Key_K))
const QKeySequence kLuaDbgCtxAddWatch (QKeySequence(Qt::CTRL|Qt::SHIFT|Qt::Key_W))
const QKeySequence kLuaDbgCtxToggleBreakpoint (QKeySequence(Qt::CTRL|Qt::SHIFT|Qt::Key_B))
const QKeySequence kLuaDbgCtxReloadLuaPlugins (QKeySequence(Qt::CTRL|Qt::SHIFT|Qt::Key_L))
const QKeySequence kLuaDbgCtxRemoveAllBreakpoints (QKeySequence(Qt::CTRL|Qt::SHIFT|Qt::Key_F9))
QKeySequence luaDbgSeqFromKeyEvent (const QKeyEvent *ke)
 Build a key sequence from a key event for matching against QAction shortcuts. Wraps the Qt5/Qt6 modifier-encoding split so callers do not have to repeat the version check.
LuaDbgInvalidFilterColors LuaDebuggerPath::invalidFilterColors ()
 Return the foreground and background colors used to indicate an invalid filter expression in the Lua debugger.
bool LuaDebuggerPath::watchSpecIsGlobalScoped (const QString &spec)
 Check whether a watch specification is globally scoped.
bool LuaDebuggerPath::variablesPathIsGlobalScoped (const QString &path)
 Check whether a variables path is globally scoped.
QString LuaDebuggerPath::changeKey (int stackLevel, const QString &path)
 Return the change key for a variable at a given stack level and path.
QString LuaDebuggerPath::watchSpecFromChangeKey (const QString &key)
 Return the watch specification corresponding to a change key.
QString LuaDebuggerPath::stripWatchExpressionErrorPrefix (const QString &errStr)
 Strip the error prefix from a watch expression error string.
QString LuaDebuggerPath::variableSectionRootKeyFromItem (const QStandardItem *item)
 Return the root key of the variable section containing an item.
bool LuaDebuggerPath::watchSpecUsesPathResolution (const QString &spec)
 Check whether a watch specification uses path resolution.
QString LuaDebuggerPath::variableTreeChildPath (const QString &parentPath, const QString &nameText)
 Return the path of a child node in the variable tree.
QString LuaDebuggerPath::expressionWatchChildSubpath (const QString &parentSubpath, const QString &nameText)
 Return the subpath of a child node in an expression watch tree.
bool LuaDebuggerPath::variableChildrenShouldSortByName (const QString &parentPath)
 Check whether children of a variable node should be sorted by name.
VariableRowFields LuaDebuggerPath::readVariableRowFields (const wslua_variable_t &v, const QString &parentPath)
 Read the display fields of a variable row from a Lua variable.
void LuaDebuggerPath::applyVariableExpansionIndicator (QStandardItem *anchor, bool canExpand, bool enabledOnlyPlaceholder, int columnCount=3)
 Apply an expansion indicator to a variable row anchor item.
QString LuaDebuggerPath::watchVariablePathForSpec (const QString &spec)
 Return the variable path for a watch specification.
QString LuaDebuggerPath::watchResolvedVariablePathForTooltip (const QString &spec)
 Return the resolved variable path for a watch tooltip.
void LuaDebuggerPath::watchRootSetVariablePathRoleFromSpec (QStandardItem *row, const QString &spec)
 Set the variable path role on a watch root row item from a watch specification.
QString LuaDebuggerPath::watchPathOriginSuffix (const QStandardItem *item, const QString &spec)
 Return the origin suffix for a watch path tooltip.
QString LuaDebuggerPath::capWatchTooltipText (const QString &s)
 Truncate a watch tooltip text string to a reasonable length.
QString LuaDebuggerPath::watchPathParentKey (const QString &path)
 Return the parent key of a watch path.
void LuaDebuggerPath::applyWatchChildRowTextAndTooltip (QStandardItem *specItem, const QString &rawVal, const QString &typeText)
 Apply the raw value and type text to a watch child row item.
int LuaDebuggerPath::watchSubpathBoundaryCount (const QString &subpath)
 Return the number of subpath boundaries in a watch subpath.
QStandardItem * LuaDebuggerPath::findWatchItemBySubpathOrPathKey (QStandardItem *subtree, const QString &key)
 Find a watch item in a subtree by its subpath or path key.
QStandardItem * LuaDebuggerPath::findVariableTreeItemByPathKey (QStandardItem *subtree, const QString &key)
 Find a variable tree item in a subtree by its path key.
void LuaDebuggerPath::reexpandTreeDescendantsByPathKeys (QTreeView *tree, QStandardItemModel *model, QStandardItem *subtree, QStringList pathKeys, TreePathKeyFinder findByKey)
 Re-expand previously expanded descendants of a subtree by their path keys.
void LuaDebuggerPath::reexpandWatchDescendantsByPathKeys (QTreeView *tree, QStandardItemModel *model, QStandardItem *subtree, QStringList pathKeys)
 Re-expand previously expanded watch descendants of a subtree by their path keys.
void LuaDebuggerPath::clearWatchFilterErrorChrome (QStandardItem *specItem, QTreeView *tree)
 Clear the filter error chrome from a watch specification item.
void LuaDebuggerPath::applyWatchFilterErrorChrome (QStandardItem *specItem, QTreeView *tree)
 Apply filter error chrome to a watch specification item.
void LuaDebuggerPath::setupWatchRootItemFromSpec (QStandardItem *specItem, QStandardItem *valueItem, const QString &spec)
 Set up a watch root item from a watch specification.
QStandardItem * LuaDebuggerPath::findVariableItemByPathRecursive (QStandardItem *node, const QString &path)
 Find a variable item by path in a subtree, searching recursively.
QString LuaDebuggerPath::watchItemExpansionKey (const QStandardItem *item)
 Return the expansion key for a watch item.
QIcon luaDbgCircleIcon (bool actions, const QColor &fill, const QColor &outline)
 Return a Fluent circle icon for breakpoints and debugger state.
void luaDbgDrawBreakpointDot (QPainter &painter, qreal dotLeft, qreal dotTop, qreal radius, bool enabled, const QColor &outlineColor, bool hasExtras, int alpha)
 Draws a breakpoint dot on the given painter.
QIcon luaDbgBreakpointHeaderIconForMode (LuaDbgBpHeaderIconMode mode)
 Return the appropriate breakpoint header icon for the given mode.
void styleLuaDebuggerHeaderIconOnlyButton (QToolButton *btn, int side, int iconGrow)
 Style a Lua debugger header icon-only button.

Variables

const QString kLuaDbgHeaderToolButtonStyle {QStringLiteral("QToolButton { border: none; padding: 0px; margin: 0px; }")}

Detailed Description

Shared helpers used across the debugger panels.

Function Documentation

◆ applyVariableExpansionIndicator()

void LuaDebuggerPath::applyVariableExpansionIndicator ( QStandardItem * anchor,
bool canExpand,
bool enabledOnlyPlaceholder,
int columnCount = 3 )

Apply an expansion indicator to a variable row anchor item.

Parameters
anchorThe anchor item in the variable tree.
canExpandWhether the item can be expanded.
enabledOnlyPlaceholderWhether to use an enabled-only placeholder.
columnCountThe number of columns in the tree (default 3).

◆ applyWatchChildRowTextAndTooltip()

void LuaDebuggerPath::applyWatchChildRowTextAndTooltip ( QStandardItem * specItem,
const QString & rawVal,
const QString & typeText )

Apply the raw value and type text to a watch child row item.

Parameters
specItemThe watch specification item.
rawValThe raw value string.
typeTextThe type text string.

◆ applyWatchFilterErrorChrome()

void LuaDebuggerPath::applyWatchFilterErrorChrome ( QStandardItem * specItem,
QTreeView * tree )

Apply filter error chrome to a watch specification item.

Parameters
specItemThe watch specification item.
treeThe tree view.

◆ capWatchTooltipText()

QString LuaDebuggerPath::capWatchTooltipText ( const QString & s)

Truncate a watch tooltip text string to a reasonable length.

Parameters
sThe tooltip text string.
Returns
The capped tooltip text string.

◆ changeKey()

QString LuaDebuggerPath::changeKey ( int stackLevel,
const QString & path )

Return the change key for a variable at a given stack level and path.

Parameters
stackLevelThe stack level of the variable.
pathThe path of the variable.
Returns
The change key string.

◆ clearWatchFilterErrorChrome()

void LuaDebuggerPath::clearWatchFilterErrorChrome ( QStandardItem * specItem,
QTreeView * tree )

Clear the filter error chrome from a watch specification item.

Parameters
specItemThe watch specification item.
treeThe tree view.

◆ expressionWatchChildSubpath()

QString LuaDebuggerPath::expressionWatchChildSubpath ( const QString & parentSubpath,
const QString & nameText )

Return the subpath of a child node in an expression watch tree.

Parameters
parentSubpathThe subpath of the parent node.
nameTextThe name of the child node.
Returns
The subpath of the child node.

◆ findVariableItemByPathRecursive()

QStandardItem * LuaDebuggerPath::findVariableItemByPathRecursive ( QStandardItem * node,
const QString & path )

Find a variable item by path in a subtree, searching recursively.

Parameters
nodeThe root item of the subtree to search.
pathThe path to search for.
Returns
The matching item, or nullptr if not found.

◆ findVariableTreeItemByPathKey()

QStandardItem * LuaDebuggerPath::findVariableTreeItemByPathKey ( QStandardItem * subtree,
const QString & key )

Find a variable tree item in a subtree by its path key.

Parameters
subtreeThe root item of the subtree to search.
keyThe path key to search for.
Returns
The matching item, or nullptr if not found.

◆ findWatchItemBySubpathOrPathKey()

QStandardItem * LuaDebuggerPath::findWatchItemBySubpathOrPathKey ( QStandardItem * subtree,
const QString & key )

Find a watch item in a subtree by its subpath or path key.

Parameters
subtreeThe root item of the subtree to search.
keyThe subpath or path key to search for.
Returns
The matching item, or nullptr if not found.

◆ invalidFilterColors()

LuaDbgInvalidFilterColors LuaDebuggerPath::invalidFilterColors ( )

Return the foreground and background colors used to indicate an invalid filter expression in the Lua debugger.

Background is read from ThemeManager::FilterInvalid; foreground is derived locally via ColorMath::contrastingText against that background (WCAG luminance pick — black on light tints, white on dark).

Returns
A struct containing the foreground and background colors.

◆ luaDbgBreakpointHeaderIconForMode()

QIcon luaDbgBreakpointHeaderIconForMode ( LuaDbgBpHeaderIconMode mode)

Return the appropriate breakpoint header icon for the given mode.

Parameters
modeThe breakpoint header icon mode.
Returns
A QIcon for the breakpoint header in the given mode, filled when breakpoints are active and an outline otherwise.

◆ luaDbgCircleIcon()

QIcon luaDbgCircleIcon ( bool actions,
const QColor & fill,
const QColor & outline )

Return a Fluent circle icon for breakpoints and debugger state.

Parameters
actionsUse the concentric variant, for breakpoints with a condition, hit count or log message.
fillFill colour, or an invalid colour for no fill.
outlineOutline ring colour, or an invalid colour for no ring. With both valid the ring is drawn over the fill's edge.
Returns
A cached icon; the Fluent circle covers 20/24 of its box.

◆ luaDbgDrawBreakpointDot()

void luaDbgDrawBreakpointDot ( QPainter & painter,
qreal dotLeft,
qreal dotTop,
qreal radius,
bool enabled,
const QColor & outlineColor,
bool hasExtras = false,
int alpha = 255 )

Draws a breakpoint dot on the given painter.

An enabled breakpoint is filled with the theme's error accent colour, a disabled one is drawn as an outline in outlineColor.

Parameters
painterThe QPainter object to draw on.
dotLeftThe left coordinate of the dot.
dotTopThe top coordinate of the dot.
radiusThe radius of the dot.
enabledWhether the breakpoint is enabled.
outlineColorThe outline colour of a disabled breakpoint.
hasExtrasWhether the breakpoint has a condition, hit count or log message.
alphaThe transparency level of the dot.

◆ luaDbgRecordTreeSectionRootExpansion()

void luaDbgRecordTreeSectionRootExpansion ( QHash< QString, LuaDbgTreeSectionExpansionState > & map,
const QString & rootKey,
bool expanded )

Record / clear root-level expansion for rootKey in map. Mutates map in place; collapsing a root with no remembered subpaths drops the entry entirely so the map stays minimal.

Parameters
mapReference to the hash map that stores the expansion state of tree sections.
rootKeyThe key representing the root section whose expansion state is to be recorded or cleared.
expandedBoolean indicating whether the root section should be expanded (true) or collapsed (false).

◆ luaDbgRecordTreeSectionSubpathExpansion()

void luaDbgRecordTreeSectionSubpathExpansion ( QHash< QString, LuaDbgTreeSectionExpansionState > & map,
const QString & rootKey,
const QString & key,
bool expanded )

Add / remove one descendant subpath key under rootKey in map. Mirrors luaDbgRecordTreeSectionRootExpansion: removing the last subpath of a collapsed root erases the root entry too.

Parameters
mapReference to the hash map that stores the expansion state of tree sections.
rootKeyThe root key under which the subpath is being expanded or collapsed.
keyThe subpath key to be added or removed.
expandedBoolean indicating whether the subpath is being expanded (true) or collapsed (false).

◆ luaDbgSeqFromKeyEvent()

QKeySequence luaDbgSeqFromKeyEvent ( const QKeyEvent * ke)

Build a key sequence from a key event for matching against QAction shortcuts. Wraps the Qt5/Qt6 modifier-encoding split so callers do not have to repeat the version check.

Parameters
kePointer to the QKeyEvent object containing the key event information.
Returns
QKeySequence The constructed key sequence based on the key event.

◆ luaDbgTreeSectionExpandedSubpaths()

QStringList luaDbgTreeSectionExpandedSubpaths ( const QHash< QString, LuaDbgTreeSectionExpansionState > & map,
const QString & rootKey )

Expanded-descendant subpaths recorded for rootKey, or empty.

Parameters
mapHash containing expansion states for LuaDbgTreeSection objects.
rootKeyKey of the root section to retrieve expanded subpaths for.
Returns
QStringList List of expanded subpaths under the specified rootKey.

◆ luaDebuggerSettingsFilePath()

QString luaDebuggerSettingsFilePath ( )

Returns the file path for Lua debugger settings.

Global personal config path — debugger settings are not profile-specific.

Returns
QString The file path for Lua debugger settings.

◆ readVariableRowFields()

VariableRowFields LuaDebuggerPath::readVariableRowFields ( const wslua_variable_t & v,
const QString & parentPath )

Read the display fields of a variable row from a Lua variable.

Parameters
vThe Lua variable to read from.
parentPathThe path of the parent variable node.
Returns
A VariableRowFields struct containing the display fields.

◆ reexpandTreeDescendantsByPathKeys()

void LuaDebuggerPath::reexpandTreeDescendantsByPathKeys ( QTreeView * tree,
QStandardItemModel * model,
QStandardItem * subtree,
QStringList pathKeys,
TreePathKeyFinder findByKey )

Re-expand previously expanded descendants of a subtree by their path keys.

Parameters
treeThe tree view.
modelThe tree model.
subtreeThe root item of the subtree to re-expand.
pathKeysThe list of path keys to re-expand.
findByKeyThe function to find an item by path key.

◆ reexpandWatchDescendantsByPathKeys()

void LuaDebuggerPath::reexpandWatchDescendantsByPathKeys ( QTreeView * tree,
QStandardItemModel * model,
QStandardItem * subtree,
QStringList pathKeys )

Re-expand previously expanded watch descendants of a subtree by their path keys.

Parameters
treeThe tree view.
modelThe tree model.
subtreeThe root item of the subtree to re-expand.
pathKeysThe list of path keys to re-expand.

◆ setupWatchRootItemFromSpec()

void LuaDebuggerPath::setupWatchRootItemFromSpec ( QStandardItem * specItem,
QStandardItem * valueItem,
const QString & spec )

Set up a watch root item from a watch specification.

Parameters
specItemThe watch specification item.
valueItemThe watch value item.
specThe watch specification string.

◆ stripWatchExpressionErrorPrefix()

QString LuaDebuggerPath::stripWatchExpressionErrorPrefix ( const QString & errStr)

Strip the error prefix from a watch expression error string.

Parameters
errStrThe error string to strip.
Returns
The error string with the watch expression error prefix removed.

◆ styleLuaDebuggerHeaderIconOnlyButton()

void styleLuaDebuggerHeaderIconOnlyButton ( QToolButton * btn,
int side,
int iconGrow = 0 )

Style a Lua debugger header icon-only button.

Parameters
btnThe tool button to style.
sideThe side length of the button in pixels.
iconGrowPixels to add to the default icon size, for icons that draw smaller than their neighbours.

◆ variableChildrenShouldSortByName()

bool LuaDebuggerPath::variableChildrenShouldSortByName ( const QString & parentPath)

Check whether children of a variable node should be sorted by name.

Parameters
parentPathThe path of the parent variable node.
Returns
true if children should be sorted by name, false otherwise.

◆ variableSectionRootKeyFromItem()

QString LuaDebuggerPath::variableSectionRootKeyFromItem ( const QStandardItem * item)

Return the root key of the variable section containing an item.

Parameters
itemThe item in the variable tree.
Returns
The root key string of the variable section.

◆ variablesPathIsGlobalScoped()

bool LuaDebuggerPath::variablesPathIsGlobalScoped ( const QString & path)

Check whether a variables path is globally scoped.

Parameters
pathThe variables path string.
Returns
true if the path is globally scoped, false otherwise.

◆ variableTreeChildPath()

QString LuaDebuggerPath::variableTreeChildPath ( const QString & parentPath,
const QString & nameText )

Return the path of a child node in the variable tree.

Parameters
parentPathThe path of the parent node.
nameTextThe name of the child node.
Returns
The path of the child node.

◆ watchItemExpansionKey()

QString LuaDebuggerPath::watchItemExpansionKey ( const QStandardItem * item)

Return the expansion key for a watch item.

Parameters
itemThe watch item.
Returns
The expansion key string used to restore expansion state.

◆ watchPathOriginSuffix()

QString LuaDebuggerPath::watchPathOriginSuffix ( const QStandardItem * item,
const QString & spec )

Return the origin suffix for a watch path tooltip.

Parameters
itemThe item in the watch tree.
specThe watch specification string.
Returns
The origin suffix string.

◆ watchPathParentKey()

QString LuaDebuggerPath::watchPathParentKey ( const QString & path)

Return the parent key of a watch path.

Parameters
pathThe watch path string.
Returns
The parent key string.

◆ watchResolvedVariablePathForTooltip()

QString LuaDebuggerPath::watchResolvedVariablePathForTooltip ( const QString & spec)

Return the resolved variable path for a watch tooltip.

Parameters
specThe watch specification string.
Returns
The resolved variable path string for display in a tooltip.

◆ watchRootSetVariablePathRoleFromSpec()

void LuaDebuggerPath::watchRootSetVariablePathRoleFromSpec ( QStandardItem * row,
const QString & spec )

Set the variable path role on a watch root row item from a watch specification.

Parameters
rowThe root row item.
specThe watch specification string.

◆ watchSpecFromChangeKey()

QString LuaDebuggerPath::watchSpecFromChangeKey ( const QString & key)

Return the watch specification corresponding to a change key.

Parameters
keyThe change key string.
Returns
The watch specification string.

◆ watchSpecIsGlobalScoped()

bool LuaDebuggerPath::watchSpecIsGlobalScoped ( const QString & spec)

Check whether a watch specification is globally scoped.

Parameters
specThe watch specification string.
Returns
true if the specification is globally scoped, false otherwise.

◆ watchSpecUsesPathResolution()

bool LuaDebuggerPath::watchSpecUsesPathResolution ( const QString & spec)

Check whether a watch specification uses path resolution.

Parameters
specThe watch specification string.
Returns
true if the specification uses path resolution, false otherwise.

◆ watchSubpathBoundaryCount()

int LuaDebuggerPath::watchSubpathBoundaryCount ( const QString & subpath)

Return the number of subpath boundaries in a watch subpath.

Parameters
subpathThe watch subpath string.
Returns
The number of subpath boundaries.

◆ watchVariablePathForSpec()

QString LuaDebuggerPath::watchVariablePathForSpec ( const QString & spec)

Return the variable path for a watch specification.

Parameters
specThe watch specification string.
Returns
The variable path string.

Variable Documentation

◆ kLuaDbgHeaderToolButtonStyle

const QString kLuaDbgHeaderToolButtonStyle {QStringLiteral("QToolButton { border: none; padding: 0px; margin: 0px; }")}

Flat tool button style for section header buttons.