I am trying to modify some open source code, but I having trouble approaching how to do so
The open source program that I am working with is called lessfs, and it has about four C files with up to 3000 lines of code. I am only concerned with one portion of the source code in order to modify.
Therefore, my question is, if I would like to add some print statements inside the code, how would I be able to see it?
Would I have to run the program or compile the program and run them individually? Or would it just be better to email the author and figure out a way to see certain items being printed while the program is running?
I have tried using GDB, but how do you do that when you are dealing with multiple large files?
I posted another form similar to this here:
Thanks for your time in advance
Also, for more information about the source code that I am working with can be found here:
http://www.lessfs.com/wordpress/
If you add some print statement you have to compile the program again and then run it in order to see the printed text. I think this is obvious for a compiled languages.
Instead of print statements use GDB with breakpoints in order to inspect the values of the variables or the flow of the program.
Related
I have been writing a program for multi-element diffusion. Because the method I am using is poorly optimized in MatLab, I have been programming in C, as that is fast for my purpose. I am, however, much more an engineer than a programmer.
The model consists of many functions and each of those functions I have tested separately for expected inputs. However, now that everything is together, my program gets stuck. Most likely in one of the (many) while loops.
If I can find which loop it gets stuck in, I can see if it gets a wrong input, or if I made a mistake in the loop itself that I missed during testing.
If it were simply looping a few times, I could add a print statement of sorts in each loop, but since it iterates a few million to more than a billion times, that won't work. And if I try to run it with just a few hundred iterations, the problem doesn't occur.
I was hoping that in the IDE there is an option to see which function is currently being executed, but I can't find it in the one I am using (Pelles C).
Is there an option in Pelles C (or if not there in another IDE) that shows which function is currently active? Or is there a different way to find where it gets stuck?
I have been trying to get the debugger to tell me where it is stuck, but even though it gives me a lot of information about things I have no clue about, it doesn't seem to tell me what I want to know.
Compile your program with -g flag and run it using gdb`
gdb ./test
whenever it gets stuck, run 'where' or 'bt' in gdb's command line, to find the exact line where it gets stuck. Also put -Wall flag, it will show all warnings.
This question already has an answer here:
How to stop program in gdb at writing to a particular file known by its name
(1 answer)
Closed 5 years ago.
While debugging my program I want to observe changes which the program is making to files and I want debugger to tell me when that happens
I am debugging a program which either creates or modifies certain files (let's say text files). A file can be modified from multiple places in the program and it can be modified a multiple number of times. The sequence of modification is important. I am looking for a way to set a watchpoint on the file just like we set on variables. Basically I want the program to break whenever the file is modified so that I can analyze the file content and observe the program further. Is there a way to achieve this?
I don't think you're going to be able to do this easily. It's a novel concept, I've never heard of it at least.
Files are not typically things in the "scope" understood and managed by a debugger, so there's no way to tell a debugger to do this. Also, it's not quite how the single-step/debugging model works under the hood either, since files are a pretty high-level concept and breaking relies on stopping execution once it reaches a particular address.
You would need to put conditional breakpoints in all functions that do I/O to the file in question, and somehow come up with a condition to filter out only the file(s) you're interested in.
Two approaches I can think of:
Set a conditional break-point for your read/write functions that will break on your specified file descriptor / file pointer.
Write wrappers for the read/write syscalls that logs activity for file descriptor and load it as a shared library using LD_PRELOAD.
We have a assignment and the teacher doesn't go into depth with explaining things so I'm a bit confused since I haven't really done much programming before. We have to write a program that when it's done being executed it's able to read its source file and can make another text file which is the same as its source file but the text file has a line number. My problem is I don't understand how to begin it. Could someone give me an example how to get started and what steps to take? I'm not asking for someone to do the programming for me just give an example. Thanks in advance.
Roughly the steps you'll want to take are:
Read each line of the input text file
Prepend the line number to the beginning of each line.
Write your modified lines into a new text file.
There's a lot of good information on how to read/write to files here, and string concatenation (for how to prepend the line number) here. You may also want to look into for loops so that you can hit every line in the input file.
There are really two parts to your question: "Who am I?" (what file are you) and "Write a copy of myself with line numbers"
The part that you describe above is the first -- "Who am I?" and for that, something external to your source code has to provide the info because the language itself can reside in any file.
Often, there is information available about what's being compiled made available by the preprocessor (just like it sounds, it's something that is run before compiling your source code). In this case, "preprocessor macros" commonly give you this sort of environmental data.
Take a look at this link for GNU C: https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html to start researching what is available under what conditions. Your compiler, if not gcc, should have similar docs.
I want gdb to dump the code it executes line by line. Just like step command as it shows the current line but I do not want do step through the entire code as it is too big.
So I want to automate it.
The reason I want to do this because my code behaves differently in two scenarios and I want to see where actually the diff arises so I plan to take dump for two different scenarios in two different file then take a diff.
I know it may not be the best way to debug something but trust me I have tried a lots of stuff to find the bug but no use and I think this can help me in a great way.
Thanks in advance !!
With GDB you can set breakpoints that will allow you to follow step by step the code in a specific area of your programm.
I am writing a relatively simple C program in Visual C++, and have two global variables which I would like to know the values of as the program runs. The values don't change once they are assigned, but my programming ability is not enough to be able to quickly construct a text box that displays the values (I'm working in Win32) so am looking for a quick routine that can perhaps export the values to a text file so I can look at them and check they are what they ought to be. Values are 'double'.
I was under the impression that this was the purpose of the debugger, but for me the debugger doesn't run as the 'file not found' is always the case.
Any ideas how I can easily check the value of a global variable (double) in a Win32 app?
Get the debugger working. You should maybe post another question with information about why it won't work - with as much info as possible.
Once you have done that, set a breakpoint, and under Visual C++ (I just tried with 2010), hover over the variable name.
You could also use the watch window to enter expressions and track their values.
If your debugger isn't working try using printf statements wherever the program iterates.
Sometimes this can be a useful way of watching a variable without having to step into it.
If however you wish to run through the program in debug mode set a breakpoint as suggested (in VS2010 you can right click on the line you want to set a breakpoint on).
Then you just need to go to Toolbars -> Debug Toolbar.
I usually like to put #ifdef _DEBUG (or write an appropriate macro or even extra code) to do the printing, and send to the output everything that can help me tracking what the program's doing. Since your variables are never changing, I would do so.
However, flooding the console with lots of values is bad imo, and in such cases I would rely on assertions and the debugger - you should really see why it's not working.
I've done enough Python and Ruby to tell you that debugging a complex program when all you have is a printf, although doable, is extremely frustrating and takes way longer than what it should.
Finally, since you mention your data type is double (please make sure you have a good reason for not using floats instead), in case you add some assertion, remember that == is to be avoided unless you know 100% that == is what you really really want (which is unlikely if your data comes from calculations).