# class Byebug::ThreadCommand

Manipulation of Ruby threads

### Constants

Subcommands

### Public Class Methods

description() click to toggle source

    # File lib/byebug/commands/thread.rb, line 37
    def description
      prettify <<-EOD
        Commands to manipulate threads.
      EOD
    end

names() click to toggle source

    # File lib/byebug/commands/thread.rb, line 33
    def names
      %w(thread)
    end

### Public Instance Methods

execute() click to toggle source

    # File lib/byebug/commands/thread.rb, line 22
    def execute
      return puts(self.class.help) unless @match[1]

      name, thnum = @match[1].split(/ +/)[0..1]
      subcmd = Command.find(Subcommands, name)
      return errmsg("Unknown thread command '#{name}'\n") unless subcmd

      send("thread_#{subcmd.name}", thnum)
    end

regexp() click to toggle source

    # File lib/byebug/commands/thread.rb, line 18
    def regexp
      /^\s* th(?:read)? (?:\s+ (.+))? \s*$/x
    end

### Private Instance Methods

display_context(context) click to toggle source

    # File lib/byebug/commands/thread.rb, line 91
    def display_context(context)
      puts pr('thread.context', thread_arguments(context))
    end

parse_thread_num(subcmd, arg) click to toggle source

    # File lib/byebug/commands/thread.rb, line 121
    def parse_thread_num(subcmd, arg)
      thnum, err = get_int(arg, subcmd, 1)
      return [nil, err] unless thnum

      Byebug.contexts.find { |c| c.thnum == thnum }
    end

parse_thread_num_for_cmd(subcmd, arg) click to toggle source

    # File lib/byebug/commands/thread.rb, line 128
    def parse_thread_num_for_cmd(subcmd, arg)
      c, err = parse_thread_num(subcmd, arg)

      case
      when err
        [c, err]
      when c.nil?
        [nil, pr('thread.errors.no_thread')]
      when @state.context == c
        [c, pr('thread.errors.current_thread')]
      when c.ignored?
        [c, pr('thread.errors.wrong_action', subcmd: subcmd, arg: arg)]
      else
        [c, nil]
      end
    end

thread_arguments(context) click to toggle source

    # File lib/byebug/commands/thread.rb, line 95
    def thread_arguments(context)
      status_flag = if context.suspended?
                      '$'
                    else
                      context.thread == Thread.current ? '+' : ' '
                    end
      debug_flag = context.ignored? ? '!' : ' '

      if context == Byebug.current_context
        file_line = "#{@state.file}:#{@state.line}"
      else
        backtrace = context.thread.backtrace_locations
        if backtrace && backtrace[0]
          file_line = "#{backtrace[0].path}:#{backtrace[0].lineno}"
        end
      end

      {
        status_flag: status_flag,
        debug_flag: debug_flag,
        id: context.thnum,
        thread: context.thread.inspect,
        file_line: file_line || ''
      }
    end

thread_current(thnum) click to toggle source

    # File lib/byebug/commands/thread.rb, line 58
    def thread_current(thnum)
      return errmsg("thread current doesn't need params") unless thnum.nil?

      display_context(@state.context)
    end

thread_list(thnum) click to toggle source

    # File lib/byebug/commands/thread.rb, line 46
    def thread_list(thnum)
      return errmsg("thread list doesn't need params") unless thnum.nil?

      contexts = Byebug.contexts.sort_by(&:thnum)

      thread_list = prc('thread.context', contexts) do |context, _|
        thread_arguments(context)
      end

      print(thread_list)
    end

thread_resume(thnum) click to toggle source

    # File lib/byebug/commands/thread.rb, line 72
    def thread_resume(thnum)
      ctx, err = parse_thread_num_for_cmd('thread resume', thnum)
      return errmsg(err) if err
      return errmsg(pr('thread.errors.already_running')) unless ctx.suspended?

      ctx.resume
      display_context(ctx)
    end

thread_stop(thnum) click to toggle source

    # File lib/byebug/commands/thread.rb, line 64
    def thread_stop(thnum)
      ctx, err = parse_thread_num_for_cmd('thread stop', thnum)
      return errmsg(err) if err

      ctx.suspend
      display_context(ctx)
    end

thread_switch(thnum) click to toggle source

    # File lib/byebug/commands/thread.rb, line 81
    def thread_switch(thnum)
      ctx, err = parse_thread_num_for_cmd('thread switch', thnum)
      return errmsg(err) if err

      display_context(ctx)

      ctx.switch
      @state.proceed
    end