Wireshark Lua dissector加载模块时debug.getinfo为nil问题求助
First, let's break down why your original debug.getinfo approach fails: Wireshark restricts access to the debug module in the plugin loading context for security and sandboxing reasons. That's why you're seeing the "attempt to index a nil value" error—debug simply isn't available when the dissector loads automatically on startup.
Here are three reliable alternative methods to get your module directory, tailored to work across different Wireshark versions and operating systems:
Method 1: Use Wireshark's Built-in Path Functions (Recommended)
Wireshark provides standard Lua API functions to access its core directories, which works consistently across all supported systems. This is the most robust approach:
local function setup_module_path() -- Replace "modules" with your actual subdirectory name (if using one) local module_subdir = "modules" local sep = package.config:sub(1, 1) -- Get OS-specific path separator -- Get standard Wireshark plugin directories local personal_plugins = persconffile_path() .. sep .. "plugins" local global_plugins = programfile_path() .. sep .. "plugins" -- Build full paths to your module directory local personal_module_path = personal_plugins .. sep .. module_subdir local global_module_path = global_plugins .. sep .. module_subdir -- Add the existing path to package search order if Dir.exists(personal_module_path) then package.prepend_path(personal_module_path) elseif Dir.exists(global_module_path) then package.prepend_path(global_module_path) else -- Fallback to current directory if all else fails package.prepend_path("." .. sep .. module_subdir) end end -- Run the setup before requiring modules setup_module_path() -- Now you can safely require your modules local my_protocol_utils = require("protocol_utils") local my_message_parser = require("message_parser")
Why this works:
persconffile_path()returns the user's personal Wireshark config directory (e.g.,~/.config/wiresharkon Linux,%APPDATA%\Wiresharkon Windows)programfile_path()returns the global Wireshark installation directoryDir.exists()checks if the module directory exists before adding it to the path- Uses the OS-native path separator to avoid cross-platform issues
Method 2: Add Current Directory to Package Path (Simplest)
If your modules are in the same directory as your main dissector script, you can directly add the current directory to package.path:
-- Add current directory to package search path local sep = package.config:sub(1, 1) package.prepend_path("." .. sep) -- Require your modules local core_dissector = require("core_dissector")
Note:
This works best if you keep all your plugin files in the same folder. It's simple but less flexible if you use subdirectories for modules.
Method 3: Locate Your Main Script with package.searchpath
If you know the exact filename of your main dissector script (e.g., my_custom_protocol.lua), you can use package.searchpath to find its absolute path:
local main_script_name = "my_custom_protocol.lua" local script_path = package.searchpath(main_script_name, package.path) if script_path then local sep = package.config:sub(1, 1) local module_dir = script_path:match("(.*" .. sep .. ")") package.prepend_path(module_dir) end -- Require modules local packet_parser = require("packet_parser")
Caveat:
This method breaks if the user renames your main script, so it's best for plugins where you control the filename distribution.
内容的提问来源于stack exchange,提问作者immnek

