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)


289
290
291
# File 'lib/udb/cli.rb', line 289

def self.exit_on_failure?
  true
end

Instance Method Details

#disasm(encoding_str) ⇒ Object

Raises:

  • (ArgumentError)


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/udb/cli.rb', line 313

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



294
295
296
# File 'lib/udb/cli.rb', line 294

def version
  puts Udb.version
end