Class: Udb::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/udb/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


354
355
356
# File 'lib/udb/cli.rb', line 354

def self.exit_on_failure?
  true
end

Instance Method Details

#disasm(encoding_str) ⇒ Object

Raises:

  • (ArgumentError)


378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/udb/cli.rb', line 378

def disasm(encoding_str)
  raise ArgumentError, "Arch directory does not exist: #{options[:arch]}" unless File.directory?(options[:arch])
  raise MalformattedArgumentError, "encoding must be a hex string" unless encoding_str =~ /\A(0[xX])?[a-fA-F0-9]+\z/

  cfg_file =
    if File.file?(options[:config])
      Pathname.new(options[:config])
    elsif File.file?("#{options[:config_dir]}/#{options[:config]}.yaml")
      Pathname.new("#{options[:config_dir]}/#{options[:config]}.yaml")
    else
      raise ArgumentError, "Cannot find config: #{options[:config]}"
    end

  resolver =
    Udb::Resolver.new(
      std_path_override: Pathname.new(options[:arch]),
      gen_path_override: Pathname.new(options[:gen]),
      custom_path_override: Pathname.new(options[:arch_overlay])
    )
  cfg_arch = resolver.cfg_arch_for(cfg_file.realpath)

  encoding = encoding_str.to_i(16)

  matches = { 32 => [], 64 => [] }

  cfg_arch.possible_xlens.each do |xlen|
    say "RV#{xlen}:"

    matches[xlen] = cfg_arch.instructions.select do |i|
      next unless i.defined_in_base?(xlen)

      opcode_mask = i.encoding(xlen).format.gsub(/[01]/, "1").gsub("-", "0").to_i(2)
      match = i.encoding(xlen).format.gsub("-", "0").to_i(2)
      (opcode_mask & encoding) == match
    end

    if matches[xlen].empty?
      say "  Illegal Instruction"
    else
      matches[xlen].each do |inst|
        say "  #{inst.name}"
      end
    end
  end
end

#versionObject



359
360
361
# File 'lib/udb/cli.rb', line 359

def version
  puts Udb.version
end