Module: Byebug

Extended by:
Byebug
Includes:
Helpers::ReflectionHelper
Included in:
Byebug
Defined in:
lib/byebug/core.rb,
lib/byebug/frame.rb,
lib/byebug/errors.rb,
lib/byebug/remote.rb,
lib/byebug/runner.rb,
lib/byebug/command.rb,
lib/byebug/context.rb,
lib/byebug/history.rb,
lib/byebug/setting.rb,
lib/byebug/version.rb,
lib/byebug/attacher.rb,
lib/byebug/interface.rb,
lib/byebug/breakpoint.rb,
lib/byebug/commands/up.rb,
lib/byebug/helpers/bin.rb,
lib/byebug/helpers/var.rb,
lib/byebug/subcommands.rb,
lib/byebug/command_list.rb,
lib/byebug/commands/irb.rb,
lib/byebug/commands/pry.rb,
lib/byebug/commands/set.rb,
lib/byebug/commands/var.rb,
lib/byebug/helpers/eval.rb,
lib/byebug/helpers/file.rb,
lib/byebug/helpers/path.rb,
lib/byebug/commands/down.rb,
lib/byebug/commands/edit.rb,
lib/byebug/commands/help.rb,
lib/byebug/commands/info.rb,
lib/byebug/commands/kill.rb,
lib/byebug/commands/list.rb,
lib/byebug/commands/next.rb,
lib/byebug/commands/quit.rb,
lib/byebug/commands/save.rb,
lib/byebug/commands/show.rb,
lib/byebug/commands/skip.rb,
lib/byebug/commands/step.rb,
lib/byebug/helpers/frame.rb,
lib/byebug/helpers/parse.rb,
lib/byebug/option_setter.rb,
lib/byebug/printers/base.rb,
lib/byebug/remote/client.rb,
lib/byebug/remote/server.rb,
lib/byebug/commands/break.rb,
lib/byebug/commands/catch.rb,
lib/byebug/commands/debug.rb,
lib/byebug/commands/frame.rb,
lib/byebug/commands/where.rb,
lib/byebug/helpers/string.rb,
lib/byebug/helpers/thread.rb,
lib/byebug/helpers/toggle.rb,
lib/byebug/printers/plain.rb,
lib/byebug/settings/width.rb,
lib/byebug/commands/delete.rb,
lib/byebug/commands/enable.rb,
lib/byebug/commands/finish.rb,
lib/byebug/commands/method.rb,
lib/byebug/commands/source.rb,
lib/byebug/commands/thread.rb,
lib/byebug/commands/disable.rb,
lib/byebug/commands/display.rb,
lib/byebug/commands/history.rb,
lib/byebug/commands/restart.rb,
lib/byebug/commands/var/all.rb,
lib/byebug/settings/autoirb.rb,
lib/byebug/settings/autopry.rb,
lib/byebug/commands/continue.rb,
lib/byebug/commands/tracevar.rb,
lib/byebug/commands/var/args.rb,
lib/byebug/settings/autolist.rb,
lib/byebug/settings/autosave.rb,
lib/byebug/settings/basename.rb,
lib/byebug/settings/fullpath.rb,
lib/byebug/settings/histfile.rb,
lib/byebug/settings/histsize.rb,
lib/byebug/settings/listsize.rb,
lib/byebug/settings/savefile.rb,
lib/byebug/commands/condition.rb,
lib/byebug/commands/info/file.rb,
lib/byebug/commands/info/line.rb,
lib/byebug/commands/interrupt.rb,
lib/byebug/commands/undisplay.rb,
lib/byebug/commands/var/const.rb,
lib/byebug/commands/var/local.rb,
lib/byebug/helpers/reflection.rb,
lib/byebug/settings/callstyle.rb,
lib/byebug/settings/linetrace.rb,
lib/byebug/commands/untracevar.rb,
lib/byebug/commands/var/global.rb,
lib/byebug/commands/thread/list.rb,
lib/byebug/commands/thread/stop.rb,
lib/byebug/settings/post_mortem.rb,
lib/byebug/commands/info/display.rb,
lib/byebug/commands/info/program.rb,
lib/byebug/commands/var/instance.rb,
lib/byebug/source_file_formatter.rb,
lib/byebug/commands/thread/resume.rb,
lib/byebug/commands/thread/switch.rb,
lib/byebug/commands/enable/display.rb,
lib/byebug/commands/thread/current.rb,
lib/byebug/settings/stack_on_error.rb,
lib/byebug/commands/disable/display.rb,
lib/byebug/commands/info/breakpoints.rb,
lib/byebug/interfaces/test_interface.rb,
lib/byebug/interfaces/local_interface.rb,
lib/byebug/commands/enable/breakpoints.rb,
lib/byebug/interfaces/remote_interface.rb,
lib/byebug/interfaces/script_interface.rb,
lib/byebug/processors/script_processor.rb,
lib/byebug/commands/disable/breakpoints.rb,
lib/byebug/processors/command_processor.rb,
lib/byebug/processors/control_processor.rb,
lib/byebug/processors/post_mortem_processor.rb,
ext/byebug/byebug.c,
ext/byebug/byebug.c

Overview

Summary

This is a singleton class allows controlling byebug. Use it to start/stop byebug, set/remove breakpoints, etc.

Defined Under Namespace

Modules: Helpers, Printers, Remote, Subcommands Classes: AutoirbSetting, AutolistSetting, AutoprySetting, AutosaveSetting, BasenameSetting, BreakCommand, Breakpoint, CallstyleSetting, CatchCommand, Command, CommandList, CommandNotFound, CommandProcessor, ConditionCommand, Context, ContinueCommand, ControlProcessor, DebugCommand, DebugThread, DeleteCommand, DisableCommand, DisplayCommand, DownCommand, EditCommand, EnableCommand, FinishCommand, Frame, FrameCommand, FullpathSetting, HelpCommand, HistfileSetting, History, HistoryCommand, HistsizeSetting, InfoCommand, Interface, InterruptCommand, IrbCommand, KillCommand, LinetraceSetting, ListCommand, ListsizeSetting, LocalInterface, MethodCommand, NextCommand, OptionSetter, PostMortemProcessor, PostMortemSetting, PryCommand, QuitCommand, RemoteInterface, RestartCommand, Runner, SaveCommand, SavefileSetting, ScriptInterface, ScriptProcessor, SetCommand, Setting, ShowCommand, SkipCommand, SourceCommand, SourceFileFormatter, StackOnErrorSetting, StepCommand, TestInterface, ThreadCommand, ThreadsTable, TracevarCommand, UndisplayCommand, UntracevarCommand, UpCommand, VarCommand, WhereCommand, WidthSetting

Constant Summary collapse

PORT =

Port number used for remote debugging

8989
VERSION =
"12.0.0"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ReflectionHelper

#commands

Class Attribute Details

.wait_connectionObject

If in remote mode, wait for the remote connection



17
18
19
# File 'lib/byebug/remote.rb', line 17

def wait_connection
  @wait_connection
end

Instance Attribute Details

#displaysObject

Debugger’s display expressions



31
32
33
# File 'lib/byebug/core.rb', line 31

def displays
  @displays
end

#init_fileObject

Configuration file used for startup commands. Default value is .byebugrc



25
26
27
# File 'lib/byebug/core.rb', line 25

def init_file
  @init_file
end

#modeObject

Running mode of the debugger. Can be either:

  • :attached => Attached to a running program through the ‘byebug` method.

  • :standalone => Started through ‘byebug` script.

  • :off => Ignoring any ‘byebug` method calls.



41
42
43
# File 'lib/byebug/core.rb', line 41

def mode
  @mode
end

Class Method Details

.actual_control_portObject

The actual port that the control server is started at



25
26
27
# File 'lib/byebug/remote.rb', line 25

def actual_control_port
  control.actual_port
end

.actual_portObject

The actual port that the server is started at



20
21
22
# File 'lib/byebug/remote.rb', line 20

def actual_port
  server.actual_port
end

.add_catchpoint(exception) ⇒ Exception

Adds a new exception to the catchpoints hash.

Returns:



850
851
852
853
854
855
856
857
858
859
860
# File 'ext/byebug/byebug.c', line 850

static VALUE
Add_catchpoint(VALUE self, VALUE value)
{
  UNUSED(self);

  if (TYPE(value) != T_STRING)
    rb_raise(rb_eTypeError, "value of a catchpoint must be String");

  rb_hash_aset(catchpoints, rb_str_dup(value), INT2FIX(0));
  return value;
}

.attachObject

Starts byebug, and stops at the first line of user’s code.



10
11
12
13
14
15
16
17
18
19
# File 'lib/byebug/attacher.rb', line 10

def self.attach
  unless started?
    self.mode = :attached

    start
    run_init_script
  end

  current_context.step_out(3, true)
end

.breakpointsArray

Returns an array of breakpoints.

Returns:

  • (Array)


27
28
29
30
31
32
33
34
35
36
# File 'ext/byebug/byebug.c', line 27

static VALUE
Breakpoints(VALUE self)
{
  UNUSED(self);

  if (NIL_P(breakpoints))
    breakpoints = rb_ary_new();

  return breakpoints;
}

.catchpointsHash

Returns the catchpoints hash.

Returns:

  • (Hash)


44
45
46
47
48
49
50
# File 'ext/byebug/byebug.c', line 44

static VALUE
Catchpoints(VALUE self)
{
  UNUSED(self);

  return catchpoints;
}

.contextsArray

Returns an array of all contexts.

Returns:

  • (Array)


546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'ext/byebug/byebug.c', line 546

static VALUE
Contexts(VALUE self)
{
  volatile VALUE list;
  volatile VALUE new_list;
  VALUE context;
  threads_table_t *t_tbl;
  debug_context_t *dc;
  int i;

  UNUSED(self);

  check_started();

  new_list = rb_ary_new();
  list = rb_funcall(rb_cThread, rb_intern("list"), 0);

  for (i = 0; i < RARRAY_LENINT(list); i++)
  {
    VALUE thread = rb_ary_entry(list, i);

    thread_context_lookup(thread, &context);
    rb_ary_push(new_list, context);
  }

  Data_Get_Struct(threads, threads_table_t, t_tbl);
  st_clear(t_tbl->tbl);

  for (i = 0; i < RARRAY_LENINT(new_list); i++)
  {
    context = rb_ary_entry(new_list, i);
    Data_Get_Struct(context, debug_context_t, dc);
    st_insert(t_tbl->tbl, dc->thread, context);
  }

  return new_list;
}

.current_contextObject

Returns the current context.

<i>Note:</i> Byebug.current_context.thread == Thread.current


611
612
613
614
615
616
617
618
619
620
621
# File 'ext/byebug/byebug.c', line 611

static VALUE
Current_context(VALUE self)
{
  VALUE context;

  UNUSED(self);

  thread_context_lookup(rb_thread_current(), &context);

  return context;
}

.debug_load(file, stop = false) ⇒ nil

Same as Kernel#load but resets current context’s frames. stop parameter forces byebug to stop at the first line of code in file

Returns:

  • (nil)


724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'ext/byebug/byebug.c', line 724

static VALUE
Debug_load(int argc, VALUE *argv, VALUE self)
{
  VALUE file, stop, context;
  debug_context_t *dc;
  VALUE status = Qnil;
  int state = 0;

  UNUSED(self);

  if (rb_scan_args(argc, argv, "11", &file, &stop) == 1)
    stop = Qfalse;

  Start(self);

  context = Current_context(self);
  Data_Get_Struct(context, debug_context_t, dc);

  dc->calced_stack_size = 1;

  if (RTEST(stop))
    dc->steps = 1;

  rb_load_protect(file, 0, &state);
  if (0 != state)
  {
    status = rb_errinfo();
    byebug_reset_stepping_stop_points(dc);
  }

  return status;
}

.handle_post_mortemObject

Saves information about the unhandled exception and gives a byebug prompt back to the user before program termination.



76
77
78
79
80
81
82
# File 'lib/byebug/core.rb', line 76

def self.handle_post_mortem
  return unless raised_exception

  context = raised_exception.__bb_context

  PostMortemProcessor.new(context).at_line
end

.interruptObject

Interrupts the current thread



32
33
34
# File 'lib/byebug/remote.rb', line 32

def interrupt
  current_context.interrupt
end

.load_settingsObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/byebug/core.rb', line 61

def self.load_settings
  Dir.glob(File.join(__dir__, "settings", "*.rb")).each do |file|
    require file
  end

  constants.grep(/[a-z]Setting/).map do |name|
    setting = const_get(name).new
    Byebug::Setting.settings[setting.to_sym] = setting
  end
end

.lockThread.current

Locks global switch to reserve execution to current thread exclusively.

Returns:

  • (Thread.current)


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'ext/byebug/threads.c', line 204

static VALUE
Lock(VALUE self)
{
  debug_context_t *dc;
  VALUE context;

  UNUSED(self);

  if (!is_living_thread(rb_thread_current()))
    rb_raise(rb_eRuntimeError, "Current thread is dead!");

  thread_context_lookup(rb_thread_current(), &context);
  Data_Get_Struct(context, debug_context_t, dc);

  acquire_lock(dc);

  CTX_FL_UNSET(dc, CTX_FL_IGNORE);

  return locker;
}

.parse_host_and_port(host_port_spec) ⇒ Object



59
60
61
62
# File 'lib/byebug/remote.rb', line 59

def parse_host_and_port(host_port_spec)
  location = host_port_spec.split(":")
  location[1] ? [location[0], location[1].to_i] : ["localhost", location[0]]
end

.post_mortem=(bool) ⇒ Object

Sets post-moterm flag.



835
836
837
838
839
840
841
842
# File 'ext/byebug/byebug.c', line 835

static VALUE
Set_post_mortem(VALUE self, VALUE value)
{
  UNUSED(self);

  post_mortem = RTEST(value) ? Qtrue : Qfalse;
  return value;
}

.post_mortem?Boolean

Returns true if post-mortem debugging is enabled.

Returns:

  • (Boolean)


821
822
823
824
825
826
827
# File 'ext/byebug/byebug.c', line 821

static VALUE
Post_mortem(VALUE self)
{
  UNUSED(self);

  return post_mortem;
}

.raised_exceptionException

Returns raised exception when in post_mortem mode.

Returns:



58
59
60
61
62
63
64
# File 'ext/byebug/byebug.c', line 58

static VALUE
Raised_exception(VALUE self)
{
  UNUSED(self);

  return raised_exception;
}

.spawn(host = "localhost", port = nil) ⇒ Object



21
22
23
24
25
26
# File 'lib/byebug/attacher.rb', line 21

def self.spawn(host = "localhost", port = nil)
  require_relative "core"

  self.wait_connection = true
  start_server(host, port || PORT)
end

.startBoolean

The return value is the value of !Byebug.started? before issuing the start; That is, true is returned, unless byebug was previously started.

Returns:

  • (Boolean)


702
703
704
705
706
707
708
709
710
711
712
713
714
715
# File 'ext/byebug/byebug.c', line 702

static VALUE
Start(VALUE self)
{
  if (IS_STARTED)
    return Qfalse;

  catchpoints = rb_hash_new();

  threads = create_threads_table();

  register_tracepoints(self);

  return Qtrue;
}

.start_client(host = "localhost", port = PORT) ⇒ Object

Connects to the remote byebug



55
56
57
# File 'lib/byebug/remote.rb', line 55

def start_client(host = "localhost", port = PORT)
  client.start(host, port)
end

.start_control(host = nil, port = PORT + 1) ⇒ Object

Starts the remote server control thread



48
49
50
# File 'lib/byebug/remote.rb', line 48

def start_control(host = nil, port = PORT + 1)
  control.start(host, port)
end

.start_server(host = nil, port = PORT) ⇒ Object

Starts the remote server main thread



39
40
41
42
43
# File 'lib/byebug/remote.rb', line 39

def start_server(host = nil, port = PORT)
  start_control(host, port.zero? ? 0 : port + 1)

  server.start(host, port)
end

.started?Boolean

Returns true byebug is started.

Returns:

  • (Boolean)


629
630
631
632
633
634
635
# File 'ext/byebug/byebug.c', line 629

static VALUE
Started(VALUE self)
{
  UNUSED(self);

  return IS_STARTED ? Qtrue : Qfalse;
}

.stopBoolean

This method disables byebug. It returns true if byebug was already disabled, otherwise it returns false.

Returns:

  • (Boolean)


644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'ext/byebug/byebug.c', line 644

static VALUE
Stop(VALUE self)
{
  UNUSED(self);

  if (IS_STARTED)
  {
    clear_tracepoints(self);

    breakpoints = Qnil;
    catchpoints = Qnil;

    return Qfalse;
  }

  return Qtrue;
}

.stoppable?Boolean

Returns:

  • (Boolean)


662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'ext/byebug/byebug.c', line 662

static VALUE
Stoppable(VALUE self)
{
  VALUE context;
  debug_context_t *dc;

  if (!IS_STARTED)
    return Qfalse;

  if (!NIL_P(breakpoints) && rb_funcall(breakpoints, idEmpty, 0) == Qfalse)
    return Qfalse;

  if (!NIL_P(catchpoints) && rb_funcall(catchpoints, idEmpty, 0) == Qfalse)
    return Qfalse;

  if (post_mortem == Qtrue)
    return Qfalse;

  if (RTEST(tracing))
    return Qfalse;

  context = Current_context(self);
  if (!NIL_P(context))
  {
    Data_Get_Struct(context, debug_context_t, dc);

    if (dc->steps > 0)
      return Qfalse;
  }

  return Qtrue;
}

.thread_context(thread) ⇒ Object

Returns context of the thread passed as an argument.



590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'ext/byebug/byebug.c', line 590

static VALUE
Thread_context(VALUE self, VALUE thread)
{
  VALUE context;

  UNUSED(self);

  check_started();

  thread_context_lookup(thread, &context);

  return context;
}

.tracing=(bool) ⇒ Object

Sets the global tracing flag.



777
778
779
780
781
782
783
784
# File 'ext/byebug/byebug.c', line 777

static VALUE
Set_tracing(VALUE self, VALUE value)
{
  UNUSED(self);

  tracing = RTEST(value) ? Qtrue : Qfalse;
  return value;
}

.tracing?Boolean

Returns true if global tracing is enabled.

Returns:

  • (Boolean)


763
764
765
766
767
768
769
# File 'ext/byebug/byebug.c', line 763

static VALUE
Tracing(VALUE self)
{
  UNUSED(self);

  return tracing;
}

.unlocknil

Unlocks global switch so other threads can run.

Returns:

  • (nil)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'ext/byebug/threads.c', line 180

static VALUE
Unlock(VALUE self)
{
  debug_context_t *dc;
  VALUE context;

  UNUSED(self);

  thread_context_lookup(rb_thread_current(), &context);
  Data_Get_Struct(context, debug_context_t, dc);

  CTX_FL_SET(dc, CTX_FL_IGNORE);

  release_lock();

  return locker;
}

.verbose=(bool) ⇒ Object

Sets the global verbose flag for TracePoint API events is enabled.



806
807
808
809
810
811
812
813
# File 'ext/byebug/byebug.c', line 806

static VALUE
Set_verbose(VALUE self, VALUE value)
{
  UNUSED(self);

  verbose = RTEST(value) ? Qtrue : Qfalse;
  return value;
}

.verbose?Boolean

Returns true if global verbose flag for TracePoint API events is enabled.

Returns:

  • (Boolean)


792
793
794
795
796
797
798
# File 'ext/byebug/byebug.c', line 792

static VALUE
Verbose(VALUE self)
{
  UNUSED(self);

  return verbose;
}

Instance Method Details

#run_init_scriptObject

Runs normal byebug initialization scripts.

Reads and executes the commands from init file (if any) in the current working directory. This is only done if the current directory is different from your home directory. Thus, you can have more than one init file, one generic in your home directory, and another, specific to the program you are debugging, in the directory where you invoke byebug.



52
53
54
55
56
57
58
59
# File 'lib/byebug/core.rb', line 52

def run_init_script
  rc_dirs.each do |dir|
    rc_file = File.expand_path(File.join(dir, init_file))
    next unless File.exist?(rc_file)

    run_rc_file(rc_file)
  end
end
OSZAR »