...
BREAKPOINT `int i = 10 + 12;`
...
When I add a break point to code in Android Studio as above, AS will run to the line I added the break point to, as expected. However, if there is no code after the break point line I can't see what i resolves to in the debugger. I can fix this problem by inserting a single line of dummy code, but there must be an easier/more elegant way to get around this problem that I'm missing.
How can I avoid adding a dummy line of code to view these values?
EDIT: I'm using the latest version of Android Studio (1.0.2)
It's possible to put a breakpoint on the line following the last line you want executed.
A breakpoint stops execution before the line under the breakpoint is executed.
In the example above, a new variable is introduced (i), which is as yet unknown to the interpreter. It's necessary to step-over (execute) the line of code in order for this variable to be initialized.
See the example below, in which there's a test function that contains the line of code from the question:
I've put breakpoints on the start brace of the function {, the line in question, and the end brace }. Note that there's an X on the first breakpoint, as it's invalid. The current execution point is on (or just before execution of) the dark-blue highlited line. Note that the variable i does not exist in the Variables window below.
When I step over (execute) the line of code, the active line is now over the final brace in the function }, and that the variable i is now initialized and set:
So, if you're interested in breaking after a given line of code, just put the breakpoint after that line of code, even if that line is the final brace of a function.
Note: this won't work if there is an exit statement (e.g. break, return etc.) as the final executed line of code in a function.
Related
(Very new to AHK, so sorry if this sounds very stupid/trivial)
I have a small AHK script (see below) with a variable called var. I would like to call and modify this variable from within multiple hotkeys. However, even when trying to "yield" its value (with the F8 hotkey) the value doesn't get printed. How can I go about with this?
#SingleInstance, force
+Escape::ExitApp
!r::Reload
!p::Suspend
var := 42
F8::
MsgBox % "Var value is " . var . "."
Return
;F12::
;blabla not relevant yet
Your variable declaration is unreachable code.
The code execution stops when the first hotkey label is encountered. This is called the Auto-execute Section.
Move your hotkey definition to be at the very bottom.
(All hotkeys defined by hotkey labels always get created regardless of being in the auto-execute section or not)
And as a bonus, the concatenation operator . is redundant, you don't need to use it, you can just leave it out.
(Unless you prefer to use it, of course)
When, during a pry debug session, I want to inspect step-by-step execution of FooClass.new.foo I'd do this in pry console
$ FooClass.new.foo #this gives me path and line of the method
break /path/to/foo_class.rb:LINE_WHERE_FOO_IS_DEFINED
FooClass.new.foo
This works but I need to look for the path, line and it leaves a breakpoint I sometimes have to remove.
There's a faster way:
break FooClass#foo
FooClass.new.foo
but it's still two steps, and the breakpoint stays.
Is there a way to do it in one command, like
step-into FooClass.new.foo
which would start a Pry subsession, enter the method execution and after it exits/finishes I'm back in the original session with no extra breakpoints left?
In other words: I'm in the middle of debugging, and see a method called few lines before (I can't step into it immediately). I don't want to put a binding.pry in the source (it may take a lot of time to start the debugging session again).
The problem has hidden complexity. For instance, one thing you can do is:
pry Foo.new.foo
or
Foo.new.foo.pry
This puts you into a binding on the return value of the function foo, but that is not what you want. It also shows there is complexity when your line of code Foo.new.foo runs, for a function to know what exactly to put a binding on.
So that's really the problem I feel; it would take understanding of the expression on the line to understand where to put a breakpoint. If you imagine it was working like the step function.
If your code Foo.new.foo was already a break point (e.g. you had a function with that code in and you put a breakpoint on a line like that) then you typed step, you would go into Foo.new Foo.initialize instance of foo foo.foo and so on. So, any code trying to do what you would like it to do would have a very hard time understanding where in the expression tree to stop and put your into pry.
This is why you have to tell pry where to stop using break in this context.
I am writing Ruby in Emacs, but my Emacs skills are actually pretty low. What I can do, is open the project, TDD using M-x rinari-test, or play inferior Ruby in the second window using M-x run-ruby. Now I woul like to start using debugger from StdLib. I am able to summon it from irb by saying:
require 'debug'
Upon which I get into a prompt
(rdb:1)
but there my aptitude ends. I don't even know how to step into a file. Typing 'help' brought a screenful, but it didn't help me to finally start debugging my buggy gem. Online, everybody writes about things such as "rdebug" or "ruby-debug" or whatever which I firstly don't want to use and secondly, being a muggle, I am unable to install on my Debian. Please help!!!
You really need to try reading the output of help in the debugger. It explains the commands nicely.
For instance, for practice, try this at the command-line, not inside an editor/IDE:
ruby -rdebug -e 'p 1'
h
Ruby's debugger will output the help summary:
Debugger help v.-0.002b
Commands
b[reak] [file:|class:]<line|method>
b[reak] [class.]<line|method>
set breakpoint to some position
wat[ch] <expression> set watchpoint to some expression
cat[ch] (<exception>|off) set catchpoint to an exception
b[reak] list breakpoints
cat[ch] show catchpoint
del[ete][ nnn] delete some or all breakpoints
disp[lay] <expression> add expression into display expression list
undisp[lay][ nnn] delete one particular or all display expressions
c[ont] run until program ends or hit breakpoint
s[tep][ nnn] step (into methods) one line or till line nnn
n[ext][ nnn] go over one line or till line nnn
w[here] display frames
f[rame] alias for where
l[ist][ (-|nn-mm)] list program, - lists backwards
nn-mm lists given lines
up[ nn] move to higher frame
down[ nn] move to lower frame
fin[ish] return to outer frame
tr[ace] (on|off) set trace mode of current thread
tr[ace] (on|off) all set trace mode of all threads
q[uit] exit from debugger
v[ar] g[lobal] show global variables
v[ar] l[ocal] show local variables
v[ar] i[nstance] <object> show instance variables of object
v[ar] c[onst] <object> show constants of object
m[ethod] i[nstance] <obj> show methods of object
m[ethod] <class|module> show instance methods of class or module
th[read] l[ist] list all threads
th[read] c[ur[rent]] show current thread
th[read] [sw[itch]] <nnn> switch thread context to nnn
th[read] stop <nnn> stop thread nnn
th[read] resume <nnn> resume thread nnn
p expression evaluate expression and print its value
h[elp] print this help
<everything else> evaluate
The important commands to start with are s, n, c and b, and q.
s steps into a method.
n steps over a method.
c number runs (continue) until you reach line number.
b number sets a breakpoint on line number. After setting your breakpoints use c to continue running until that line is executed.
q exits the debugger.
Personally, I use the debugger gem. Others use PRY, which is similar to IRB, but with debugger-like extensions.
Knowing how to use a debugger is a good skill. There are problems you can trace down quickly using a debugger, that will take longer trying to use puts statements, because you can see what a variable contains interactively, or loop conditionally until a variable contains a certain value.
Is it possible to detect when the value of a variable has changed using the lua debug library.
Something like A callback function which would give details like the function in which the value was changed, previous value, etc.
Is such a thing possible?
I read about hooks, but I'm not sure hooks can be set to variables.
If you don't mind using a debugger, then some debuggers allow you to set Watch expressions, which will be triggered when the condition in the expression is true. I'll show how this can be done in MobDebug (it is using lua debug library, but there is no direct way to detect a variable change as far as I know).
Let say we have a script start.lua like the one below and want to detect where foo gets value 2:
print("Start")
local foo = 0
for i = 1, 3 do
local function bar()
print("In bar")
end
foo = i
print("Loop")
bar()
end
print("End")
Download mobdebug.lua and make it available to your scripts (the simplest way is to put it into the folder with your scripts).
Start the server using lua -e "require('mobdebug').listen()" command.
Start the client using lua -e "require('mobdebug').loop()" command.
You will see the prompt in the server window: '>'. Type load start.lua to load the script.
Type step and then step again. You will see "Paused at file start.lua line 3".
Let's see what the value of foo is. Type eval foo and you should see 0.
Now we can set up our watch. Type setw foo == 2. You can specify any Lua expression after setw command; the execution of your script will be stopped when the condition is evaluated as true.
Continue execution of the script using "run" command.
The watch now fires, which will show you the message like: "Paused at file start.lua line 8 (watch expression 1: [foo == 2])". This means that the previous expression changed the value of foo to 2 and the execution is stopped at line 8. You can then inspect your script and the current values (you can use "eval" and "exec" commands to run any Lua code to be evaluated in your script environment) to find what triggered the change.
The benefit of this approach is that you are not limited to monitoring table values and can specify any expression. The main disadvantage is that your script runs under a debugger and the expression is evaluated after each step, which may get really slow.
You can do this to a certain extent in Lua by using metatables and keeping a "proxy" table, and using the __newindex function call to detect attempts to add a variable.
This is covered here in the Programming in Lua book under the section "Tracking Table Accesses":
http://www.lua.org/pil/13.4.4.html
See Also
http://www.gammon.com.au/forum/?id=10887
I have a method which returns a block. I want to add a breakpoint inside the block. In Xcode adding a breakpoint on a line that's inside the block causes execution to pause when the method is returned and not when the block is executed. How do I add a breakpoint inside a block?
I had the same difficulty, until I tried using Xcode 4's LLDB debugger (go to Product>Edit Scheme to turn it on). Perhaps you'll have better luck with it.
From the documentation
You can set breakpoints and single step into blocks. You can invoke a block from within a GDB session using invoke-block, as illustrated in this example:
$ invoke-block myBlock 10 20
If you want to pass in a C string, you must quote it. For example, to pass this string into the doSomethingWithString block, you would write the following:
$ invoke-block doSomethingWithString "\"this string\""