|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "ruby/gdb" |
| 4 | +require "fileutils" |
| 5 | + |
| 6 | +# Install GDB Python extensions to XDG data directory or custom prefix |
| 7 | +# @parameter prefix [String] Optional installation prefix (defaults to XDG_DATA_HOME) |
| 8 | +def install(prefix: nil) |
| 9 | + install_path = Ruby::GDB.install_path(prefix: prefix) |
| 10 | + |
| 11 | + puts "Installing Ruby GDB extensions to: #{install_path}" |
| 12 | + |
| 13 | + # Create installation directory |
| 14 | + FileUtils.mkdir_p(install_path) |
| 15 | + |
| 16 | + # Copy Python scripts |
| 17 | + scripts = [ |
| 18 | + Ruby::GDB.object_script_path, |
| 19 | + Ruby::GDB.fiber_script_path |
| 20 | + ] |
| 21 | + |
| 22 | + scripts.each do |script| |
| 23 | + if File.exist?(script) |
| 24 | + dest = File.join(install_path, File.basename(script)) |
| 25 | + puts " Installing #{File.basename(script)}..." |
| 26 | + FileUtils.cp(script, dest) |
| 27 | + else |
| 28 | + warn " Warning: #{script} not found" |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + # Create a loader script that sources both extensions |
| 33 | + loader_path = File.join(install_path, "init.gdb") |
| 34 | + puts " Creating loader script: #{File.basename(loader_path)}" |
| 35 | + |
| 36 | + File.write(loader_path, <<~GDB) |
| 37 | + # Ruby GDB Extensions Loader |
| 38 | + # This file loads Ruby debugging extensions for GDB |
| 39 | + |
| 40 | + python |
| 41 | + import sys |
| 42 | + import os |
| 43 | + |
| 44 | + # Add the Ruby GDB extensions directory to Python path |
| 45 | + ruby_gdb_dir = os.path.dirname(__file__) |
| 46 | + if ruby_gdb_dir not in sys.path: |
| 47 | + sys.path.insert(0, ruby_gdb_dir) |
| 48 | + |
| 49 | + # Load Ruby object printing extensions |
| 50 | + exec(open(os.path.join(ruby_gdb_dir, 'object.py')).read()) |
| 51 | + |
| 52 | + # Load Ruby fiber debugging extensions |
| 53 | + exec(open(os.path.join(ruby_gdb_dir, 'fiber.py')).read()) |
| 54 | + |
| 55 | + print("Ruby GDB extensions loaded successfully!") |
| 56 | + print("Use 'help rb-' to see available commands.") |
| 57 | + end |
| 58 | + GDB |
| 59 | + |
| 60 | + puts "\nInstallation complete!" |
| 61 | + puts "\nTo use these extensions, add the following to your ~/.gdbinit:" |
| 62 | + puts " source #{loader_path}" |
| 63 | + puts "\nOr load them manually in GDB with:" |
| 64 | + puts " (gdb) source #{loader_path}" |
| 65 | +end |
| 66 | + |
| 67 | +# Uninstall GDB Python extensions |
| 68 | +# @parameter prefix [String] Optional installation prefix (defaults to XDG_DATA_HOME) |
| 69 | +def uninstall(prefix: nil) |
| 70 | + install_path = Ruby::GDB.install_path(prefix: prefix) |
| 71 | + |
| 72 | + if Dir.exist?(install_path) |
| 73 | + puts "Removing Ruby GDB extensions from: #{install_path}" |
| 74 | + FileUtils.rm_rf(install_path) |
| 75 | + puts "Uninstallation complete!" |
| 76 | + else |
| 77 | + puts "No installation found at: #{install_path}" |
| 78 | + end |
| 79 | +end |
| 80 | + |
| 81 | +# Show installation information |
| 82 | +def info |
| 83 | + puts "Ruby GDB Extensions v#{Ruby::GDB::VERSION}" |
| 84 | + puts "\nData directory: #{Ruby::GDB.data_path}" |
| 85 | + puts "Default install path: #{Ruby::GDB.install_path}" |
| 86 | + puts "\nAvailable scripts:" |
| 87 | + puts " - object.py (rb-object-print command)" |
| 88 | + puts " - fiber.py (rb-scan-fibers, rb-fiber-bt, and more)" |
| 89 | +end |
| 90 | + |
0 commit comments