Suppose I have
fn1<-function(arg) {
if(is.null(arg))
browser()
}
fn2<-function() {
#Complicated code with a bug that produces NULL
arg<-NULL
fn1(arg)
}
When I run my application, once in hundreds of times the complicated code in fn2 incorrectly sets arg<-NULL, which gets trapped by fn1.
How can I conveniently debug the fn2 in the context which led to this bug? I want something that will set a breakpoint in the next command of the calling function, so when I press Shift+F6 the focus will bring me there.
All that is left to me, is to use bare R commands, like e<-parent.frame() to get the context of the parent function and all the symbols therein, and eval(expr=<command>, envir=e) to execute in that environment.
Related
Function 1 is called from the main script and code execution is halted by the command
browser()
However the file seems locked and I cannot type and code whilst in debug mode. However I would like to debug with all the parsed arguments' values available.
Scope in R is global, but still, I want to debug while the code has all the values available for all the variables calculated so far...
It isn't clear what you mean by 'debug'.
You cannot edit function code while it is being executed. You can print the values of arguments and variables, and perform computations on them while stopped at a breakpoint (=="browser()") - from the R console.
You tagged the question RStudio - if indeed you're using it and all you need is to observe the values of variables calculated so far - you don't even need to type their name at the console, just switch to the 'Environment' pane (by default at the top right).
I am creating many games using Lua and LOVE2D, but whenever I implement a new function and want to test it out, or simply want to know a value of a variable in Lua, I either display it on the game screen or just hope that it works.
Now my question is...
IS THERE A WAY TO DISPLAY SOME INFO, such as A VARIABLE VALUE or something else into the terminal or somewhere else? Just like console.log in javascript which displays some content in the javascript console in the browser. So, is there a way to do this is Lua?? using LOVE2D?
I am using a Mac, so I have a terminal and not a command prompt. Is there a way to display some content there? Anywhere else would also be fine, I just need to see if those values are as expected or not.
Use a conf.lua file to enable the console, then you should be able to use a standard print(). You can read the wiki entry here.
Note: You have to run Lua and Love2D via the terminal for this to work. Running Lua and Love2D like this is required for the print statements to show:
/Applications/love.app/Contents/MacOS/love "/Users/myuser/Desktop/love2d-test-proj"
You just need to add a conf.lua file to the same location where your main.lua. Your file may be as simple as this:
function love.conf(t)
t.console = true
end
But feel free to copy the whole configuration file from the above link and edit what you need.
I can't be completely sure about this, because I have no access to Mac, but the console is disabled by default and even on Windows, no prints are shown until you turn it on.
Alternatively You can also display debug info in the game itself like some games do.
What I like to do is add something like debugVariable = {} for logging events that happen in each loop and debugPermanent = {} for events that happen rarely. Possibly add convenience functions for writing to the variables:
function debugAddVariable(str)
table.insert(debugVariable, str)
end
--..and similarly for debugPermanent
Now a function to draw our debug info:
function debugDraw()
love.graphics.push() --remember graphics state
love.graphics.origin() --clear any previous transforms
love.graphics.setColor(--[[select color for debug info]])
love.graphics.setFont(--[[select font for debug info]])
for i, v in ipairs(debugPermanent) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
for i, v in ipairs(debugVariable) do
love.graphics.print(v)
love.graphics.translate(0, --[[fontHeight]])
end
debugVariable = {} --clear debugVariable to prepare it for the next loop
love.graphics.pop() --recall graphics state
end
And we just call this draw function at the end of our love.draw() and the texts should appear.
Obviously, this method can be refined further and further almost infinitely, displaying specific variables, and adding graphs for some other variables to clarify the information you want to show, but that's kind of outside of the scope of the question.
Lastly Feel free to check here for debug libraries submitted by users.
The pdb Debugger has a nice feature:
r(eturn) Continue execution until the current function returns.
This is handy if a method has several return statements.
With this feature you can see where the method would get left, but you still see which return statement gets used.
The pdb Debugger stops at a code line like this:
return foo
I could not find this feature in PyCharm.
Is it available, or it this a feature request?
If knowing the return value is enough (without knowing which return statement was exactly triggered), you can try this:
In the Debugger window click the cogwheel and make sure Show Return Values is checked.
After you Step Out of the function, you'll see a new item in the Variables pane called Return Values, with the value returned.
I am writing a custom function to be used in a spreadsheet and I would like to be able to at least display some data. Few things seem to work, for example Browser.msgBox doesn't find the appropriate permissions.
Breakpoints don't interrupt execution.
I had some hope for this code
function test() {
var s = "test"
Logger.log(s)
return s + s
}
But when I set a cell in the spreadsheet to "=test()" the cell properly shows the value "testtest" but when I return to the script editor and use view>execution transcript or view>logs I don't see anything.
Perhaps this logging goes to a special file somewhere?
When you use a custom function as a formula, it can be evaluated and re-evaluated at many times. Therefore, it is not appropriate to fill up the Logging output or the Execution Transcript with this. If you want to debug, you must run (or debug) the script manually from the script editor.
Take an example, where you have two custom functions - f1() and f2()
And say, in cell A1, you enter the formula =f1() and in A2, you enter =f2(A1).
In such a case, both the cells will be re-evaluated. So what should the logger output show ?
In IDA Pro 6.1, I have a dll which has twenty calls to the "CreateFileA" and "CreateFileW" function APIs.
I would like to specify breakpoints for all the CreateFileA/CreateFileW automatically.
I could do it manually for all the xrefs, but that is tedious.
Is there a way to specify a breakpoint directly for the CreateFileA/CreateFileW call?
Thanks a lot :)
You could set a break point at the first instruction of both CreateFile, or you could whip something up with IDAPython to create the breakpoints.
Iterate over all the instructions/calls and look for calls to the appropriate function.
add_bpt() I believe is the call,
Here's a script I wrote up to accomplish what you want. It sets soft-breakpoints at the locations that call your specified functions.
// Script used to set a breakpoint at the callsite
// of the specified function using cross-references.
#include <idc.idc>
static SetBreakpoint(location)
{
// Sets a breakpoint to be activated when
// the debugger runs over the address.
AddBptEx(location, 0, BPT_SOFT);
}
static CrossReferenceSource(source)
{
// Find the linear address of the source
// location to start xref'ing from.
auto sourcefn = LocByName(source);
auto iterfn = DfirstB(sourcefn);
if (sourcefn != BADADDR && iterfn != BADADDR)
{
do
{
Message("Setting breakpoint # 0x%08x\n", iterfn);
SetBreakpoint(iterfn);
iterfn = DnextB(sourcefn, iterfn);
} while(iterfn != BADADDR);
}
}
static main()
{
auto source = "FunctionName";
Message("--- Setting breakpoints at cross-reference ---\n");
CrossReferenceSource(source);
Message("--- Finished settings breakpoints --\n");
}
Replace "FunctionName" with the name of your function and run it within IDA's 'Execute Script' window available through File > Script command
A known limitation is that it won't recognize indirect cross-references (e.g. calls using the registers).
If CreateFileA/W are all imports (ie, externs defined in an .idata section), can you not just select the symbol in question and hit F2 (add breakpoint)? The Breakpoint settings dialog that comes up allows you to specify the Hardware breakpoint mode, which in this case we would want to limit to Read (since the symbol's value would be written to at startup when imports are resolved), which should only happen in 'call ds:CreateFileA' instances.
Some breakpoint notes from the IDA Help:
It is impossible to create more than 4 hardware breakpoints
Please note that hardware breakpoints occur AFTER the instruction execution while software breakpoints occur BEFORE the instruction.
As far as I know and according with kornman00 "CreateFile" is imported from a dll. In fact, it is imported directly from Kernel32.dll, You can take a look here if you are not sure how it works.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx
Therefore, In order to do what you are looking for, the best approach is going directly to it, set the breakpoint in kernel32.CreateFileA or kernel32.CreateFileW. The differences are just if the app use Ansichar or Widechar.
Obviously in order to do that, you need to start the debug process because kernel32 must be load for your app before you will be able to set a break-point there.
If you are getting confuse my advice is "load the binary in a simpler debugger" and try to figure out what we explained you before