How to debug Halide's print_when() only when environment variable is set? - halide

I'd like to set an environment variable when running my code, e.g. DEBUG=TRUE ./run_my_halide_program, and only see the output of Halide's print_when() statements when said variable is set. However, print_when() seems to only take Halide::Expr's, not booleans. How can I do this in Halide?

I got it. You can construct Halide::Expr from an int value. The constructed Expr can then be used with print_when().
So something along the lines of this:
char* do_i_debug = getenv("DEBUG");
int debug_val = do_i_debug == nullptr ? 0 : 1; // we'll use 1 to mean debug
Halide::Expr DEBUG_HALIDE = Expr(debug_val) == 1;
Halide::print_when(DEBUG_HALIDE, expr_or_func_to_print);

Related

Hi, I`ve got a problem with my course work, "return value ignored 'fscanf'":

i`ve got a problem with my course work, i need to find
The only problem is to scan input file, so the result become wrong.
How can I fix it and why?
"return value ignored 'fscanf'"
double E;
for (int i = 0; i < 3; i++)
{
fscanf(inp_file, "%lf%lf%lf%lf%lf%lf%lf%lf", &inp[i].T, &inp[i].delt, &inp[i].Tk0, &inp[i].m0, &inp[i].n, &inp[i].R0, &inp[i].h, &inp[i].delF);
Checking return values is crucial in C, and that what this warning is about.
scanf and it's similar functions returns the amount of successful assignments. If this is not equal to the number of variables you passed for assignment, that would mean that something is wrong.
So you should do something like this:
int n = fscanf(inp_file, "%lf%lf%lf%lf%lf%lf%lf%lf", &inp[i].T, &inp[i].delt, &inp[i].Tk0, &inp[i].m0, &inp[i].n, &inp[i].R0, &inp[i].h, &inp[i].delF);
if(n != 8) {
// Handle error
}

Get Constant name from value in VB6

I have defined some Global Constants:
'Reason Codes'
Global Const MQRC_NONE = 0
Global Const MQRC_APPL_FIRST = 900
Global Const MQRC_APPL_LAST = 999
Now I want to get the constant name from its value in VB6.
Is it possible, I know it can be done in .Net and Java. Not sure about vb6.
Use a select statement. I do not like this solution but at least you can get things done with it.
Select Case constantValue
Case MQRC_NONE
result = "MQRC_NONE"
Case MQRC_APPL_FIRST
result = "MQRC_APPL_FIRST"
Case MQRC_APPL_LAST
result = "MQRC_APPL_LAST"
Case Else
result = "N/A"
End Select
If you have control over the constant values make them range 0,1,2. Then you can just index another array of equivalent constant strings based on this constant index.

Pex ignores default parameter assignment

I am using Pex to analyse function executions.
However, I noticed that default parameters are not looked at.
Here's an example of what I mean:
public int bla(int x = 2)
{
return x * 2;
}
When I run Pex, it generates the test case for int result = bla(0);. (x = 0)
Is there a way to tell Pex that it should also try to call bla( without parameter (i.e. int result = bla() )?
The 1st rule of IntelliTest/Pex is it tries to increase code coverage.
If all statements have been covered, Pex will stop.
There are many ways to add some code that only gets covered when x=2, such as in the test method. This might be the simplest that worked for me:
[PexMethod]
public int bla([PexAssumeUnderTest]Class1 target, int x)
{
if(x == 2)
{
PexAssert.ReachEventually();
}
int result = target.bla(x);
return result;
// TODO: add assertions to method Class1Test.bla(Class1, Int32)
}
The exploration results window should show:
x result
0 0
2 4
I don't know of any way to have Pex automatically generate test cases for all default parameters.
In real world production code it's highly likely the default value will be used in the code so you might not run into this problem often.
And if you have all the code paths covered by Pex does it really matter whether the default value is used or not?
It's probably more import to test the methods that call 'bla' with and without supplying a value.

Is it possible to inject values in the frama-c value analyzer?

I'm experimenting with the frama-c value analyzer to evaluate C-Code, which is actually threaded.
I want to ignore any threading problems that might occur und just inspect the possible values for a single thread. So far this works by setting the entry point to where the thread starts.
Now to my problem: Inside one thread I read values that are written by another thread, because frama-c does not (and should not?) consider threading (currently) it assumes my variable is in some broad range, but I know that the range is in fact much smaller.
Is it possible to tell the value analyzer the value range of this variable?
Example:
volatile int x = 0;
void f() {
while(x==0)
sleep(100);
...
}
Here frama-c detects that x is volatile and thus has range [--..--], but I know what the other thread will write into x, and I want to tell the analyzer that x can only be 0 or 1.
Is this possible with frama-c, especially in the gui?
Thanks in advance
Christian
This is currently not possible automatically. The value analysis considers that volatile variables always contain the full range of values included in their underlying type. There however exists a proprietary plug-in that transforms accesses to volatile variables into calls to user-supplied function. In your case, your code would be transformed into essentially this:
int x = 0;
void f() {
while(1) {
x = f_volatile_x();
if (x == 0)
sleep(100);
...
}
By specifying f_volatile_x correctly, you can ensure it returns values between 0 and 1 only.
If the variable 'x' is not modified in the thread you are studying, you could also initialize it at the beginning of the 'main' function with :
x = Frama_C_interval (0, 1);
This is a function defined by Frama-C in ...../share/frama-c/builtin.c so you have to add this file to your inputs when you use it.

How to create conditional breakpoint with std::string

Suppose I have this function:
std::string Func1(std::string myString)
{
//do some string processing
std::string newString = Func2(myString)
return newString;
}
How do I set a conditional break when newString has a specific value? (without changing the source)
Setting the condition newString == "my value" didn't work. The breakpoints were disabled with an error overloaded operator not found.
There is a much easier way in Visual Studio 2010/2012.
To accomplish what you are looking for in ANSI use this:
strcmp(newString._Bx._Ptr,"my value")==0
And in unicode (if newString were unicode) use this:
wcscmp(newString._Bx._Ptr, L"my value")==0
There are more things you can do than just a compare, you can read more about it here:
http://blogs.msdn.com/b/habibh/archive/2009/07/07/new-visual-studio-debugger-2010-feature-for-c-c-developers-using-string-functions-in-conditional-breakpoints.aspx
In VS2017, I was able to set the condition as:
strcmp(&newString[0], "my value") == 0
Some searching has failed to turn up any way to do this. Suggested alternatives are to put the test in your code and add a standard breakpoint:
if (myStr == "xyz")
{
// Set breakpoint here
}
Or to build up your test from individual character comparisons. Even looking at individual characters in the string is a bit dicey; in Visual Studio 2005 I had to dig down into the member variables like
myStr._Bx._Buf[0] == 'x' && myStr._Bx._Buf[1] == 'y' && myStr._Bx._Buf[2] == 'z'
Neither of these approaches is very satisfactory. We should have better access to a ubiquitous feature of the Standard Library.
In VS2017 you can do
strcmp(newString._Mypair._Myval2._Bx._Buf,"myvalue")==0
While I've had to work around this using something similar to Brad's answer (plus using DebugBreak() to break right from the code), sometimes editing/recompiling/re-running a bit of code is either too time consuming or just plain impossible.
Luckily, it's apparently possible to spelunk into the actual members of the std::string class. One way is mentioned here -- and though he calls out VS2010 specifically, you can still access individual chars manually in earlier versions. So if you're using 2010, you can just use the nice strcmp() functions and the like (more info), but if you're like me and still have 2008 or earlier, you can come up with a raggedy, terrible, but functional alternative by setting a breakpoint conditional something like:
strVar._Bx._Ptr[0] == 'a' && strVar._Bx._Ptr[1] == 'b' &&
strVar._Bx._Ptr[2] == 'c'
to break if the first three characters in strVar are "abc". You can keep going with additional chars, of course. Ugly.. but it's saved me a little time just now.
VS2012:
I just used the condition below because newString._Bx._Ptr ( as in OBWANDO's answer ) referenced illegal memory
strcmp( newString._Bx._Buf, "my value")==0
and it worked...
#OBWANDO (almost) has the solution, but as multiple comments rightly point out, the actual buffer depends on the string size; I see 16 to be the threshold. Prepending a size check to the strcmp on the appropriate buffer works.
newString._Mysize < 16 && strcmp(newString._Bx._Buf, "test value") == 0
or
newString._Mysize >= 16 && strcmp(newString._Bx._Ptr, "ultra super long test value") == 0
Tried to use strcmp in gdb8.1 under ubuntu18.04, but it doesn't work:
(ins)(gdb) p strcmp("a", "b")
$20 = (int (*)(const char *, const char *)) 0x7ffff5179d60 <__strcmp_ssse3>
According to this answer, strcmp, is a special IFUNC, one can setup condition like this:
condition 1 __strcmp_ssse3(camera->_name.c_str(), "ping")==0
It's pretty ugly, don't want to do it the second time.
This answer gives a much better solution, it use std::string::compare :
condition 1 camera->_name.compare("ping") == 0
In VS2015 you can do
newstring[0]=='x' && newString[1]=='y' && newString[2]=='z'
Comparing string works better than comparing characters
strcmp(name._Mypair._Myval2._Bx._Buf, "foo")==0
This works, but is very inconvenient to use and error prone.
name._Mypair._Myval2._Bx._Buf[0] == 'f' &&
name._Mypair._Myval2._Bx._Buf[1] == '0' &&
name._Mypair._Myval2._Bx._Buf[2] == '0'
You could convert it into a c string using c_str() like so:
$_streq(myStr.c_str(), "foo")
To set a conditional breakpoint in std::string you need to set it on real internal members of std::string. What you see on watch window is simplified.
You can display real structure of a variable in the watch window by using ,! suffix. In your example:
newString,!
For MSVC 2015 – 2019 you can use:
For string that were never longer than 15 characters:
(newString._Mypair._Myval2._Myres < 16) ?
strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 :
false
For (even historically) longer strings:
(newString._Mypair._Myval2._Myres < 16) ? false :
strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0
Beware:
The variable name is written twice in each condition!
You need whole expression on single line. Use the copy-paste versions bellow.
Universal condition needs to put the test value twice and variable name three times:
(newString._Mypair._Myval2._Myres < 16) ?
strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 :
strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0
Notes: use wcscmp instead of strcmp if you are working with std::wstring.
Find more info on small string optimization in C++ https://vorbrodt.blog/2019/03/30/sso-of-stdstring/ includes sample code to find size of string's internal buffer.
All std:string and std::wstring single line versions for your copy paste convenience:
(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 : false
(newString._Mypair._Myval2._Myres < 16) ? false : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0
(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0
(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"short") == 0 : false
(newString._Mypair._Myval2._Myres < 16) ? false : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_str_value_longer_than_16_chars") == 0
(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"My_test_string") == 0 : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_string") == 0
All above copy/paste samples tested on MSVC version 16.9.10 and program for Windows 10.

Resources