How to check return value of a function in Lauterbach(Trace32)? - trace32

I need to check the return value of a function from a TRACE32 script.
Reading the documentation I see a possible solution to read Program Counter(IP) register and then after executing the instruction at address where PC points to take the value from there.
Is there any other function which returns directly the value returned by a function ?

Every function has usually a pseudo-variable called "return". You can see that in window sYmbol.Browse.Var \\*\*\<myfunc>\* (where myfunc is the name of your function)
Of any variable you can get its value with PRACTICE function Var.VALUE(<variable>).
So you get the return value of function myfunc() with
GO sYmbol.EXIT(myfunc) // go to return statement of myfunc
PRINT Var.VALUE(return) // get the return value
If you'd like to do a module test, another approach might be interesting for you: So imaging you just want to call the function int func3(int a, int b) with random arguments (e.g. 5 and 3) and get the return value. In this case, do the following:
Var.NEWLOCAL \x // create artificial variable on the PRACTICE stack
Var.Set \x=func3(5,3) // execute func3() with arguments 5 and 3 on your CPU
PRINT Var.VALUE(\x) // get the return value

Related

Assure that parameters passed to a function are unique

I have a function that looks like this, that calls a function from a third party dll:
void f1(const double * const input, double * output){
// this function will modify output[]
// I have no knowledge of the implementation of this function, only that it wants
// 3 doubles as the input parameters, and it will modify the last 3 parameters to
// pass the result to me.
call_to_third_party_dll(input[0], input[1], input[2], output[1], output[2], output[3]);
}
I am calling this function like this:
double value[3] { 1, 2, 3 };
f1(value, value);
// value is now modified
This works, and gives the expected results. (It modifies 'value' as expected.)
I am calling the function with the same pointer for both parameters because it is convenient in the calling context, but I have questioned whether this is safe to do, since I don't know how 'call_to_third_party_dll' handles the parameters. (for example, it might modify one of the 'output' parameters, then use the modified value as an input for a subsequent calculation.)
I would like to construct f1 in a way that the compiler would complain if 'input' and 'output' are the same thing (like my example).
Is there a way to construct f1 so that it can fail at compile time if 'input' and 'output' are pointing to the same thing?
Note, f1 would not have to use double *. Whatever is passed will have to resolve to 3 doubles for the input parameters to the third party dll, and 3 doubles for the output of the third party dll.
No, you can't have such a check at compile time. There are other options you have though, which are done at runtime.
Make a copy of the input. That way, you won't ever get in a situation where you change the same memory. This is of course not very useful if the array is big, but if the array is only 3 doubles, it's no big deal.
Compare the pointers. If the pointers points to the same memory, raise an error. You should also make sure that the pointer with the lower address is at least n * sizeof(type) bytes away from the pointer with the higher address. I'm unsure if this is a good solution, but it might be a "good-enough" protection for debugging.
You change your function to:
std::array<double, 3> f1(std::array<double, 3> input){
std::array<double, 3> output;
call_to_third_party_dll(input[0], input[1], input[2], output[1], output[2], output[3]);
return output;
}
You can be sure that input and output are distinct objects.

Explaining Pass by Pointer in C++?

I have a C++ function in which I have two int's, who's purpose is to serve as counters, that are declared outside of the function in my main code. My goal is to update the counter variables with the result from the execution of the function.
I have them declared as such
int cor_letters = 0;
int cor_place = 0;
and then call my function like
res = compare(input, secret_word, &cor_letters, &cor_place);
My compare function header is:
bool compare(string user_input, string secret, int * correct_letters, int * correct_place)
and in my compare code, when I get the final values of the counters, I update them as such:
correct_letters = &cor_l;
correct_place = &cor_p;
I arrived at this solution after carefully reading through my compiler errors, and this seems to work. However, I don't quite understand why this works. In the beginning, I take the address of the two variables and pass them into the function. But the function takes two pointers. So the pointers point to the address of the passed in variables.
Up to this point I seem to grasp what's going on. But its the final assignments that I'm confused by - the pointers (note they're the var names from the function header) are then being updated to the address of the temporary inner function variables that I'm using. Why does this get me the values?
I'm more of a visual learner, and pointers are hard to grasp by just reading some text, so if you wouldn't mind making some quick text diagram to represent what's going on, that would be great. Thank you
I guess you ended up with
correct_letters = &cor_l;
correct_place = &cor_p;
in order to make the compiler stop complaining.
Your analyse about taking the address of local variable is correct.
You probably want to do this
*correct_letters = cor_l;
*correct_place = cor_p;
in order to assign the correct values to the variables
which are outside the function.
A brief memo about reference (&) and dereference (*) operations.
TYPE var_a=..., var_b=...; // some variables of a chosen type
TYPE *ptr=NULL; // a pointer on such a type, but not referencing anything yet
ptr=&var_a; // now ptr memorises the address of var_a (reference operation)
var_b=*ptr; // access the value which is stored at the address memorised
// by ptr (dereference operation) in order to read it
// (here, this has the same effect as var_b=var_a; )
*ptr=var_a+var_b; // access the value which is stored at the address memorised
// by ptr (dereference operation) in order to alter it
// (here, this has the same effect as var_a=var_a+var_b; )

Direct Write in Function Behaving As Pass By Value

I have been chasing this problem down for a week now but I don't know why it's happening so am unable to fix it. I am using a function to write directly to variables that have been declared outside of that function (i.e. not passed as arguments to the function in any way) , the value does not persist when the function ends.
Depending on where if and where I assign a value to num, I have seen three things:
The lifetime of the last assigned value of num doesn't survive the exit of MyFunc().
The lifetime of the last assigned value of num survives while within the function that is calling MyFunc(), which is TestFunc() in this example. This is how the MyFunc() is intended to work in normal operation: num is initialized once by the constructor and never have it's value set by MyFunc(). MyFunc() only ever increment num when it is run, always picking up num from where it left off each time it is called.
In cases where MyFunc() never sets the value of num, and only ever increments it, the value of num persists globally as long as MyFunc() is not called again. As soon as MyFunc() is called again, it uses the initialized value of num rather than working from where it left off.
This occurs in both C and C++ versions of the code.
a. In the C++ version (shown here), I am using class member functions to write to class member variables.
b. In the C version (not shown), instead of a class with member variables and functions, I have standalone functions that accept a pointer to the structure type as an argument, and dereferences the structure to access it's member variables.
This is running on an STM32F303 K8 (ARM Cortex-M4 microcontroller) using Rowley Crossworks. I need to use <> to tag some pseudo code to tell you what's going on since this isn't in the actual system and it's a bit more involved to read stuff out.
I boiled down the code as much as I could but left in some things that might seem extraneous. These things are there because they seem to matter and are representative of what is there so they might seem like useless placeholders. (For example, num = 1; being before or after the if(true) statement changes the results)
In this test code, the member function writes to the member variable a few times and reads it out at various times. Please ignore little syntax errors if there are any since this is a stripped, generalized version of the code and the actual code I am working with compiles and runs.
Header file
class MyClass
{
public:
unsigned num;
void MyFunc();
MyClass(unsigned FirstNum);
};
extern MyClass MyObject;
C++ file
MyClass MyObject(0);
MyClass::MyClass(unsigned FirstNum)
{
num = FirstNum;
}
void MyClass::MyFunc()
{
//num = 1; //uncomment this and only this for SCENARIO A
if(true)
{
//num = 1; //uncomment this and only this for SCENARIO B
//comment both for SCENARIO C
for(unsigned n = 0; n < 3;)
{
n++;
num++;
<PRINT NUM TO TERMINAL>
}
}
}
void TestMyFunc
{
<some setup code to test conditions to test MyFunc() is here>
MyObject::MyFunc();
//NOTE: that if I was to run <prinout MyObject::num> directly right here, I can get different results than if if I encapsulate the command in dummy function before I run it. The two values I can get are either the last assigned value of num or the value that num was initialized to before MyFunc was called.
}
TOP LEVEL C++ file
int main()
{
CallingFunc();
<PRINT MYNUM TO TERMINAL>
CallingFunc(); //this second call is only relevant for Scenario C and is to demonstrate how Num always starts off at the value initialized by the constructor whenever it is called if no explicit assignment is made at the start of in MyFunc()
<PRINT MYNUM TO TERMINAL>
}
Output
SCENARIO A: 2 3 4 4 2 3 4 4 --last assignment is maintained when MyFunc() exits.
SCENARIO B: 2 3 4 0 2 3 4 0 --MyNum reverts to the value initialized by the its constructor when MyFunc() exits. This is also the output if I remove the if(true) conditional check.
SCENARIO C: 1 2 3 3 1 2 3 3 --MyNum is initialized by the constructor and is only incremented in MyFunc(). Value maintained after MyFunc() exits, but not, but is always reset to initialized value every time another call is made, despite having no "num =" in MyFunc().
Expected Results for Scenario A: AS EXPECTED
Expected Results for Scenario B: 2 3 4 4 2 3 4 4
Expected Results for Scenario C: 1 2 3 3 4 5 6 6
Also, note that something similar was happening even in the C version of the code where class members variables and functions were replaced by struct member variables and functions external to the struct. The struct as a whole was passed via pointer to the function.
Does anyone have any idea why it seems like assigning member variables directly, or struct members via pointers almost makes them seem like there is a copy-by value going on?

How do I call a matlab variable from an S function?

I am working on an S function in simulink. There are some variables in the MATLAB workspace available. I want to call them.
So in MATLAB:
a=3;
and in the S function (written in C/C++):
double a = CallFromMATLABWorkSpace(a); //Something like this.
How do I do this? There is something like mexCallMATLAB but it is not clear how I should use this in this situation.
To get data from a workspace use the function mexGetVariable.
However, this is a somewhat unusual thing to do.
Why isn't the data being passed as a parameter to the S-Function?
From what I can see in the documentation for mexCallMATLAB, as well as interoping with C++ source code, it would look something like the following:
Let's say you have a MatLab function MyDoubleFunction that takes a single scalar double value and returns a scalar double value. You would do the following if you wanted to pass the function a value of 4.0 and see what the answer is:
//setup the input args
mxArray* input_args[1] = {mxCreateDoubleScalar(4.0)};
mxArray** output_args; //will be allocated during call to mexCallMATLAB
//make the call to the Matlab function
if (mexCallMATLAB( 1 /* number of output arguments */,
output_args,
1 /* number of input arguments */,
&input_args,
"MyDoubleFunction"))
{
//error if we get to this code block since it returned a non-zero value
}
//inspect the output arguments
double answer = mxGetScalar(*output_args);

recursive divison of number

I have following code to divide one number recursively by another number:
#include <iostream>
using namespace std;
int divide(int number,int dividend){
int answer=0;
if (number>dividend || number==dividend ){
answer+=1;
return divide(number-dividend,dividend);
}
return answer;
}
int main(){
cout<<divide(20,5)<<endl;
return 0;
}
but unfortunately I get zero as answer. Do you see what is wrong?
Answer is a local variable. When you run this code, the first call to divide creates an instance of the answer variable, sets it to 0, and then increments it to 1. Then, when you recursively call divide again, it creates a brand new instance of the answer variable, sets that instance to 0, and then increments that instance to 1.
In your final call to divide, it creates a brand new instance of the answer variable, sets that instance to 0, but since now number<=dividend it doesn't increment it, and it returns that instance of answer which is 0.
In the if branch you are incrementing answer but returning something unrelated (the result of the recursive call). I am sure, this is not what you want. Go from there.
You are recursively running the following code:
if (number>dividend || number==dividend ){
answer+=1;
return divide(number-dividend,dividend);
}
But once the recursive calling ends (which is number < dividend), you will ignore the if statement and return 0;
You do int answer=0; in the start of function call, so when the if statement is wrong, it returns 0 so you should define it as input parameter (by reference call) or make it global (not recommended) and do not set it to zero, just set it before your recursive function call.