How to debug against a same loop that generates different outputs? - loops

I'm learning debugging. For this purpose I'm using x64dbg. So I understood how to place breakpoints (according to strings) and how to "block" the execution of the program near the assembly code that interests me.
Unfortunately I came across some strange cases.
In some cases the "flow" is not linear. The flow "follows" a sort of loop. With each passage of this loop "something new" is written. For example. Let's say that the software has to write HELLO. At the first turn just an H appears, at the second turn just a HE, etc etc... and I can see this in the comments column of x64dbg.
The stranger thing is that if the program has to write PIZZA, the program crosses the SAME loop that previously wrote hello HELLO... but this time P appears first, then PI, then PIZZ etc etc… and I can see this in the comments column of x64dbg.
In other words, any operation I perform in the software the SAME piece of code is always executed, this code is executed hundreds of times (in loop) and each step contributes to a little piece of the final result (which changes).
How is it possible ? What should I do ?

Related

Find where program is stuck C

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.

C Program slowing down after a couple read-ins

So i have to write a C program for class, that solves a connect four like game, that roughly said resolves matches of same colored pieces, lets pieces above fall down, then checks again for matches, if no matches were found it drops the next piece and so on.
My given input is a text file, where each line holds information about the color and the x-position, where the piece is supposed to fall down.
My implementation is based on AVL-trees and my program works fine for inputs roughly up to 2.000.000 lines and solves it in about 3 seconds.
One bigger example file i have to solve is roughtly 2.600.000 lines long but doesnt terminate.
The program reads in about 1.000.000 lines per second but right after about the 2.000.000 mark its slows down tremendously and only reads in a couple 100 lines per second.
Its one of my first "bigger" projects and has about 900 lines of code, so i dont see the point in posting it here (unless someone really wants to see it)
I am really clueless what the cause could be, so maybe someone of you has an idea or can point me in a direction for things i have to look out for.
Add logging with timestamps to spot where the slowdown occurs. You need to isolate the code that has the performance issue.
The first step to getting better is to measure where you currently are. Once you know how long the current process takes, you can decide if a change you make helps, hurts or makes no change.
There's a vast world of software optimization, look around for expensive loops. Read up on big oh notation. If you have a loop that processes a loop that processes a loop O(n^3), what seems fast with 100 iterations each can slow down dramatically when each loop is a million.
If you have a performance profiler it can help organize the timestamps from the logs it creates so that the code that's taking the most time is highlighted. Many times what happens in cases like this is that something that seems good enough when you start out isn't good enough once you start to scale.
Nobody says you have to add log to every single line of your source code. That can be overwhelming. Figuring out what you don't have to focus on can be very helpful.

What do "single-entry" and "single-exit" mean for a statement?

From An Integrated Approach to Software Engineering
By Pankaj Jalote
Clearly, no meaningful program can be written as a sequence of simple
statements without any branching or repetition (which also involves
branching). So, how is the objective of linearizing the control flow
to be achieved? By making use of structured constructs. In
structured programming, a statement is not a simple assignment
statement, it is a structured statement. The key property of a
structured statement is that it has a single-entry and a
single-exit. That is, during execution, the execution of the (structured) statement starts from one defined point and the execution
terminates at one defined point. With single-entry and single-exit
statements, we can view a program as a sequence of (structured)
statements. And if all statements are structured statements, then
during execution, the sequence of execution of these statements will
be the same as the sequence in the program text. Hence, by using
single-entry and single-exit statements, the correspondence between
the static and dynamic structures can be obtained.
The most commonly used single-entry and single-exit statements are
Selection: if B then S1 else S2
if B then S1
Iteration: While B do S
repeat S until B
Sequencing: S1; S2; S3;...
What do "single-entry" and "single-exit" mean in a structured statement?
Why are statements listed at the end are single-entry and single-exit?
For example, in if B then S1 else S2, why is it single exit, given it can terminates at either S1 or S2?
Can you give a statement which is not single entry?
Can you given a statement which is not single exit?
In many languages, the only statements that do not have a single entry are those which happen to contain labels for use with goto or switch statements located outside them, and the only statements that do not have a single exit are those which contain a goto to an outside location, trigger an exception, or otherwise force stack unwinding. Note that for any particular call of a function, the only "normal" exit point will be the code immediately following that call.
The notion of single entry/single exit may be unclear to those who have never worked with code that didn't use such an approach. Examples of the latter may be found when writing code for the platforms like the Atari 2600 where RAM space is often at an absolute premium. If a piece of code will be invoked from the code that shows the title screen, or from within the game logic, and there one can't afford the two bytes of stack space necessary for a subroutine-call instruction, it would not be uncommon to jump to the code (rather than using a "JSR" [jump to subroutine] instruction), and have the code exit by checking whether a game is in progress and jumping back to the appropriate spot in the "show title screen" or "perform game logic" code. Such a design may be awkward to maintain if it becomes necessary to invoke it from more places in the code, but such techniques may be necessary if RAM is really tight (e.g. one only has 128 bytes total, as on the Atari 2600).

Backtracking algorithm crashes with hard cases (C)

I am actually making an algorithm that takes as an input a file containing tetriminoses (figures from tetris) and arranges them in smallest possible square.
Still I encounter a weird problem :
The algorithm works for less than 10 tetriminoses (every time) but start crashing with 11, 12... (I concluded that it depends on how complicated the solution is, as it finds some 14 and 15 solutions).
But the thing is, if I add an optimisation flag like -Ofast (the program is written in C) it works for every input I give him no matter how much time it takes (sometimes more than a hour..).
First I had a lot of leaks (I was using double linked list) so I changed for an Int Array, no more leaks, but same problem.
I tried using the debugger but it makes no sense (see picture) :
Debugger says my variables do not exist anymore but all I do is increment or decrement them.
For this example it just stopped while everything is fine (values of variables are correct)
Here is the link of my main function (the one that do the backtracking):
https://github.com/Caribou123/fillitAG/blob/master/19canplace/solve.c
The rest of program (same repository) consist of functions to put tetriminoses in my array, remove them from it, or print the result.
Basically I try placing a tetri, if I have enough space, I place the next one, otherwise I remove the last one and place it to the next available position etc..
Also I first thought that I were trying to place something outside of the Array, so now my Array is way bigger than it should be and filled with -1 for invalid cases (so in the worst case I just rewrite a -1), 0 for free ones, and integers values from 1 to 26 for figures.
The fact that the program works with the flag -Ofast really troubles me, as the algorithm seems to work perfectly, what could cause my program to crash ?
Here is how I tracked the number of recursions, by adding two static variables
And here is the output
(In case you want to test it yourself, use the 19canplace folder, and compile with : gcc *.c libft/libft.a)
Thanks in advance for your time,
Artiom

Proccessing "JACK audio" data with C?

My question is slightly abstract but with good grounds. I have successfully ran a JACK script written in C that loops the microphone audio data to the speaker, However I would like to know how to alter the stream of audio my self during playback, perhaps one thing I'd like to try is filter the high(or low) frequencies (CUT them completely off). From my understanding audio comes through as an analog signal and converted to a digital value (within a certain range).
I'm guessing I'm forced to go about this one of two ways, I think one way is to process each value and check if it below the frequency (or above the frequency) I don't want and then alter the value to 0(or the previous value from the last loop cycle to prevent blank spots in the audio during playback). The second way i'm guessing is that JACK presents the buffer with a full array of values that are assigned by frequency spectrum. How do I go about doing this? (In the future I want to do other things with the raw data but I think this is a great start to get familiar with raw audio processing)
Here is my simplified code: http://pastebin.com/Hmiumqkz
You can see that I tried printing the in value as its supposed to be a "float" I thought I might be able to filter frequencies from there but I'm not sure as I don't get anything printed in the console when i run this code it just loops back the mic to speaker but with out any printing to the console.....
NOTES: I have already successfully compiled and tested programs that used the Gstreamer, ALSA, NAudio, irrKang, and the Phonon libraries, they don't allow me to have the cross compatibility i need between OSs and the raw audio data I require for my project, all i ask is to please think twice before lazily report for me to use "other libraries" only for the sake of it being "easier" but I have already tried them and they all fail me.
You haven't really asked a question that can be answered here on SO, so I'll point you to some outside resources.
Here is a tutorial for designing EQs based on the popular RBJ filters:
http://blog.bjornroche.com/2012/08/basic-audio-eqs.html
Most of it is written in C-like psuedocode and will walk you through step-by-step.
Here is the correct answer (You'll notice a printf() function in the proccess(){} call back function) the for loop prints out the the current frames in the buffer(Frequency domain but the for() loop is printing over time so its the time-domain as well -- its both frequency and time)
http://pastebin.com/axDLw7cc

Resources