I am not an experienced programmer but I recently have to check C code translated from Matlab, in order to ensure that with the same data set, C and Matlab are giving the identical results.
Watches are what I am able to use until now but that requires me to sit in front of the screen and watch the values of a variable change at each loop. Is there a way to trace and log the values of a variable into a .txt file so that I can plot it later to compare with the results from Matlab?
I have already tried to write the values into some .txt file with 'fwrite' but there is not the ideal solution as I have to do that in the C code that I want to check. Embarrassed I am currently reading about tracepoint of gdb and will try to use that but I am still unsure if that is what I need. If you have some tips about what else I try.
I´m using Windows 7 and work with CodeBlocks.
Thanks!
Christina
You can set a breakpoint command to run when your watchpoint hits; have it log and execute a continue command to let the program keep running.
Related
so basically I have a C program which does a lot of computation based on an input .txt file and outputs a value. I want to run it 100 times and then work out the average, obviously this would be tedious to do individually.
So I've tried to research a bit about scripting etc and I've found things like this:
https://answers.yahoo.com/question/?qid=20091206100348AAaJPP8
Am I supposed to just do this in my command prompt? (I'm on Windows btw)
Thanks for any help :)
You're on Windows, so you can use a DOS batch script (.bat) to run your program N times using a loop (or N separate commands if that's easier for you). Use the >> symbol at the end of the command to append the output to a file. See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true for more info on this, and search google for dos bat file for help on getting started with writing batch scripts.
Try this:
Have the program append the data into the text or csv file concerned and then write another program where you can run the program for a defined number of times. Use the function system(). It accepts a string as argument and executes it in the CUI.
Hope that helps.
I'm trying to write a makefile to replace one of the scripts used in building a fairly large application.
The current script compiles one file at a time, and the primary reason for using make is to parallelise the build process. Using make -j 16 I currently get a factor of 4 speedup on our office server.
But what I've lost is some readability of the output. The compilation program for a file bundles up a few bits and pieces of work, including running custom pre-compilers, and running the gcc command. Each of these steps outputs some information, and I would prefer it to buffer the output from the command, and then show the whole lot in one go.
Is it possible to make make do this?
If you upgrade to GNU make 4.0, then you can use the built-in output synchronization feature to get what you want.
If you don't want to upgrade, then you'll have to modify each of your recipes to be wrapped with a small program that manages the output. Or you can set the SHELL variable to something that does it for you. Searching the internet should give you some examples.
A simple way to accomplish this is to send all the log output a to log directory with each file named, say:
log_file_20131104_12013478_b.txt // log_file_<date>_<time>_<sequence letter>.txt
and then simply cat them all together as your last make job in the dependency chain:
cat log_dir/log_file_20131104_12013478_*.txt > log_file_20131104_12013478.txt
With makepp this is the default behaviour as soon as you use -j. All the individual outputs (and entering dir messages) get collected and are output together as soon as the command terminates.
As I have learnt that a Perl debugger can be controlled dynamically using the file .perldb in your home directory, I was wondering if there is a C debugger also which can be manipulated in the same way. So I could set breakpoints at a certain point and print some variables and so on.
You can set breakpoints and print values in GDB via commands in ~/.gdbinit, yes.
How do you extract data from gdb so you can examine it in another program?
I am using gdb to debug a program. To see what is in array udata, I have created a source file called printudata with the following contents:
print udata[0]
print udata[1]
print udata[2]
...
print udata[143]
From within gdb I can execute that using source command and get output like this:
(gdb) source printudata
$399 = 1
$400 = 2.5
$401 = .3-10
...
$542 = <number>
So far, that is the best I can do for examining memory.
The only thing I can think of to do with this is (learn regular expressions and) strip off everything up to the equal sign so I can paste this into a spreadsheet which will tell me whether it's correct.
Is this the really the best way to get output from gdb? I am learning all this on my own and only have the basic, free tools that come with Linux (and am a beginner with all the above listed technologies)
You can print an array if it is really an array like this:
p udata
But, if udata is really a pointer, then you can use a cast to make gdb print it like an array.
p *(double(*)[144])udata
If you really want the line at a time output of your current "script", you can define a function and use a loop:
define print_udata
set $i=0
while ($i < 144)
p udata[$i]
set $i=$i+1
end
end
To log the output to a file, you can enable/disable logging:
set logging on
...gdb commands...
set logging off
The output will be in a file called gdb.txt.
In addition to the above, gdb has the "output" and "printf" commands. These don't enter the value into the value history, and they let you control the output much more precisely.
gdb has built-in scripting in both its own scripting language and in python. You can even script GDB from within a python program. You can use any of those options to write the data to a file.
More information about python & gdb here.
I have set of data point (x_i,y_i) from a text file. How can I write a C-program that reads those data, send the data to Sage, computes the Pearson correlation and send the result back to C. I have no idea how can I use C to give input to some Linux-program and read its output to a variable.
OK, let me get it straight: you are working on a C program, and within that program you need to calculate Pearson's correlation coefficient. You'd like to pass these calculations to Sage rather than code them yourself.
Now, I don't know Sage, but I guess it is possible to run it from command line. Assuming that you can prepare an input file or files for Sage, and run the calculations in Sage producing an output, I would then use the system from stdlib.h (man 3 system) to call the command line. Here is the outline of the steps in your C program:
prepare temporary files for Sage's input and output
construct the command line of Sage
use system() to run the command line
parse the temporary file name where Sage stored it's output.
That said, I would not do it using Sage. Pearson correlation coefficient is easy enough to implement in C, and if you do it, your program will not depend on the whole Sage installation.