GDB: print passed variable in Fortran subroutine - gdb

I am debugging prog.f90, which has a module mod.f90.
How do I print the argument variable values?
The module has a subroutine that gets called within a loop. It takes in the arguments, stores one in a local variable, calculates the new value of the argument, stores that in another local variable, then uses the local variables to test a certain condition. The new value of the argument gets passed back to main.
I am trying to print the values of all the variables while within the subroutine.
I can print the local variables, but printing the argument variables gives me a reference address.
(gdb)print temp_curr
$1 = 4
(gdb)print arg1
$2 = (REF TO -> ( real8 )) #0x7fffffffe0a0: -nan(0x8000000000000)
I tried...
(gdb)print $arg1
$3 = VOID
The subroutine looks something like this...
SUBROUTINE sub(arg1, arg2)
IMPLICIT NONE
REAL, INTENT(inout):: arg1, arg2
REAL :: temp_prev, temp_curr
temp_prev = arg1
arg1 = (a bunch of calculations)/arg2
temp_curr = arg1
IF (temp_curr < temp_prev) THEN
stop
END IF
END SUBROUTINE sub

The $arg1 variable in gdb is used only in gdb scrips (see GDB documentation). If you refer to the variable of the program being debugged use only arg1.
Try print MODULENAME::VARNAME if you want to print a variable (VARNAME) from another module (MODULENAME)

Related

Cannot access memory at address 0x48e7458d48c38958 with gdb

I am new to gdb and am using gdb to debug a project written by others. In this piece:
int CObjectRecognizer::String2StructInput(const StructInstanceInput &input, StructAlgoResult &aResult, SObjectRecInput &inputInfo)
{
try
{
map<string,string> inputmap = input.field;
if(inputmap.find("att") != inputmap.end())
{
inputInfo.m_att = atoi(inputmap["att"].c_str());
}
}
In gdb, I want to check the value of inputmap, but it gives an error message:
(gdb) next
899 map<string,string> inputmap = input.field;
(gdb) print inputmap
$1 = std::map with 140737488343904 elements<error reading variable: Cannot access memory at address 0x48e7458d48c38958>
(gdb)
Why can't it access memory at the particular address 0x48e7458d48c38958?
EDIT: following suggestion:
(gdb) print inputmap
$2 = std::map with 0 elements
It seems the map is empty. Why doesn't this line create a map?:
map<string,string> inputmap = input.field;
When a GDB line or function entry breakpoint is hit, or when you use the step or next commands, the target program is stopped at the beginning of the first statement of a line, before any code in that line has been executed.
This behavior is usually exactly what you want, for example if your program has a line a[i+j] = b[i] + c[j]; and you want to check for an array out-of-bounds access before it happens.
In your case, the line's statement declares an object whose address is known but whose contents won’t be defined until after the statement has been executed, including the call to its initializer. So print at this point shows the contents of the object’s known, but uninitialized, memory.
As #HolyBlackCat suggested in a comment, you can type next, and GDB will execute the current line's statement or statements, including any initializers and function calls, and stop just before the next line of source code to be executed in the current function[1].
At that point, print inputmap will show the correct value[2].
[1] or its caller, if you're at a return statement
[2] assuming it wasn’t a return statement

Issue with unit identifier when using Fortran modules in C++

My main program is C++ and I need to use modules of an existing Fortran program. I was able to link to it with g++.
Problem is, all subroutines inside the fortran codes are designed to be called by a Fortran main program. Globally, the initialization of global variables in the latter is done by directing an input file on command line:
main_fortran_prog < inputFile.ext
A given subroutine "foubar" require these variables to be set. For that, It reads from unit identifier 5 which is, according to documentation I read, for INPUT from the keyboard with the READ statement.
Now, I want to call the subroutine "foubar" in my C++ main which will initialize theses variables "real_fou" and "bool_flag" before the subroutine operation. Knowing that "foubar" will look out the value of real_fou and bool_flag in namelist from unit identifier 5 which I'm trying to avoid, what are the best options for me?
In sum: I want to migrate from a Fortran main to a C++ main. I'm trying avoid namelist reading from a file if possible.
List of files in my directory:
inputFile.ext:
! this file is used by fortran main program to initialize global variable
&justANamelist
real_fou = 0.3490834
bool_flag = .true.
/
afortmodule.f90
module afortmodule
implicit none
integer :: ios
logical, save :: bool_flag
real, save :: real_fou
contains
subroutine foubar() bind(c, name='foubar')
use iso_c_binding
call parseInput
write(*,*) "real_fou x 2 = " , real_fou * 2
end subroutine
subroutine parseInput
! Define the namelist
NAMELIST /justANamelist/ real_fou, bool_flag
! How to bypass this in cpp call ?
read(5,nml=justANamelist, IOSTAT=ios)
end subroutine
end module afortmodule
main_fortran.f90
! require < inputFile.ext
program mainfortranprog
use afortmodule
! call foubar to display real_fou x 2
call foubar
end program mainfortranprog
main_cpp.cpp
// This want to call the subroutine foubar in fortran module afort_module
// like main_fortran did but avoiding the commandline input like "< inputFile.ext"
#include <iostream>
using namespace std;
void call_foubar(void);
extern "C"{
void foubar (void);
bool bool_flag;
double real_fou;
}
int main (void){
real_fou = 3.13215163;
bool_flag = true;
//How can I use these values in namelist justANamelist
//in manner that I could be able to do:
call_foubar();
return 0;
}
void call_foubar(void){
// some codes here
foubar();
}

How to check return value of a function in Lauterbach(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

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);

How can I prevent "- : unit = ()" from appearing after my output in the ocaml toplevel?

This code
for i=0 to 5 do print_string "a" done;;
will output
aaaaaa- : unit = ()
But how can I output just "aaaaaa" without outputting the value of for-expression?
You are not really printing the value of the expression; it's just that you are within the session of the interpreter, which by default prints the value/type of the evaluated expression. If you compiled your program it would just print the a's.
You could compile your program; this way you won't get the type information from the interpreter -- which is : unit = ()