Eclipse IDE debugger : step into isn't working properly

191 views Asked by At

All the functions used in the main file are correctly defined. However when I try to enter the debugging mode, for some reason IDE isn't stepping into said functions when asked to. Instead it acts like a step over and ignores the lines until the very last line, where out of the blue all variables appear in the window ; prior to that, no variable is present whatsoever.

Here's the main file :

#include "pointers.h"

int main(void){
    whatisapointer();
    whatisthesizeofapointer();
    whatareNpointersplacedsuccessively(6);
    return 0;
}

the pointers.c file used to define the functions in main :

#include "pointers.h"

void whatisapointer(){
    int *pointer;                   
    pointer = allocateOneInteger(); 
    *pointer = 42;                  
}

void whatisthesizeofapointer(){
    int a = sizeof(char);
    int b = sizeof(int);
    int c = sizeof(char*);
    int d = sizeof(int*);
}

void whatareNpointersplacedsuccessively(int N){
    int i, *Npointer, *current;
    char *Npointerchar, *currentchar;
    Npointer = allocateNInteger(N);
    current = Npointer;
    current = Npointer+1;
    current = Npointer+2;
    for(i=0;i<N;i++) *(Npointer+i) = i;
    N=2*N;
    Npointerchar = allocateNChar(N);
    currentchar = Npointerchar;
    currentchar = Npointerchar+1;
    currentchar = Npointerchar+2;
    for(i=0;i<N;i++) Npointerchar[i] = 65+i;
    Npointer[N-3] = 0x68676665;
    current = Npointer+N-3;
}


int* allocateOneInteger(){
    return (int*)malloc(1*sizeof(int));
}

int* allocateNInteger(int N){
    return (int*)malloc(N*sizeof(int));
}

char* allocateNChar(int N){
    return (char*)malloc(N*sizeof(char));
}


void echange(int a,int b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
}

void echangep(int *pa,int *pb)
{
    int temp;
    temp=*pa;
    *pa=*pb;
    *pb=temp;
}

/* Fonction allocateNFloat
 */
void allocateNFloat(int N){

}

And the makefile :

CC := gcc
FLAGS := -g -Wall -Werror

all : prog

prog : pointers.o mainpointers.o
        $(CC) pointers.o mainpointers.o -o prog

fonctions.o : pointers.c pointers.h
        $(CC) -c pointers.c $(FLAGS) -o pointers.o
        
mainpointers.o : mainpointers.c pointers.h
        $(CC) -c mainpointers.c $(FLAGS) -o mainpointers.o

clean : 
            rm -f *.o
            rm -f prog

I've done some research, none of which has helped me solve this issue.

1

There are 1 answers

8
lockcmpxchg8b On

You're very likely finding that the compiler is optimizing away your code, because the functions have no side-effects. (If you're not familiar with the concept of side effects, the gist is 'changes to program state that might influence other parts of the program'---compilers aggressively prune away side-effect-free code because, by definition, doing so doesn't change the behavior of the program.)

Even allocating memory isn't a side affect, because correct code should never be able to tell whether another part of the program has allocated memory.

The easiest side-effect is to print out the result of any intermediate computation you want to inspect via the debugger; that will force the optimizer to leave the code in place.

E.g.,

int foo() //no side effects, will be optimized to an empty function or pruned.
{
    char *test = malloc(4096);
    strcpy(test, "My test string");
    return 0;
}

int foo() //dumb side effects
{
    char *test = malloc(4096);
    strcpy(test, "My test string");
    printf("%02x", ((int)test & 0xff) ^ (test[0]) );//make output depend on the operations you wish to observe
    return 0;
}

An alternate means to verify that the compiler is pruning away your code would be to disassemble the resulting object file...you should see that the functions consist of a single 'return' instruction...which is what the debugger is showing by highlighting the closing curly brace.