0

Every time comes the debugging part with the GDB, I feel quite lost. To a novice like me, GDB is not so handy compared with debuggers in IDEs of other languages and the graphic debugger is not working. I've tried to debug load() and indexes() with GDB. I tried to set breakpoints at load and run curl -i http://localhost:8080/hello.html, but nothing happened. What exact steps do I need to take to debug? More details will be appreciated. Thanks in advance!

Snippets of my problematic code:

load()

bool load(FILE* file, BYTE** content, size_t* length)
{
    // TODO
    BYTE* cursor;
    *content = realloc(*content, 1);
    if (*content == NULL)
    {
        return false;
    }
    cursor = *content;

    while (fread(cursor, 1, 1, file) == 1)
    {
        (*length)++;
        cursor = realloc(cursor, (*length) + 1);
        if (cursor == NULL)
        {
            return false;
        }
    }

    return true;
}

indexes()

char* indexes(const char* path)
{
    // TODO
    const char* php = "index.php";
    const char* html = "index.html";
    char* full_path = calloc(1, sizeof(*path) + sizeof(*php));
    if (full_path == NULL)
    {
        return NULL;
    }
    full_path = strcpy(full_path, path);
    full_path = strcat(full_path, php);
    if (access(full_path, F_OK) == 0)
    {
        return full_path;
    }
    else
    {
        free(full_path);
        full_path = calloc(1, sizeof(*path) + sizeof(*html));
        full_path = strcpy(full_path, path);
        full_path = strcat(full_path, html);
        if (access(full_path, F_OK) == 0)
        {
            return full_path;
        }
    }


    return NULL;
}

EDIT: update the debugging pictures.

enter image description here enter image description here

1

Here's what I do. Use debug50 instead of the command line gdb. If you haven't used that before:

Run update50 to be sure you are up to date (the IDE is on version 81 at the moment). Then, here's part of this year's class where Prof Malan explains how debug50 works. https://video.cs50.net/2016/fall/lectures/2?t=22m20s (if it doesn't go to that timestamp, click the 3-bar icon on the right and scroll down to the debug50 section).

So, to debug server, first, add breakpoints to the indexes and load functions (as described in the video).

Then, have 2 terminal tabs available. In the first one, run

debug50 ./server public

That will launch server and open the debug pane.

In the second terminal tab, run

telnet localhost 8080

You can then enter a request like GET / HTTP/1.1 and then hit return twice.

Over in your debug, you should see your server program running and then stopping at indexes. You can then step through.

Here's a screenshot of what I mean. I've got the editor in the top, showing the code, and showing the breakpoint on the load function. I've launched debug50 ./server public in the bottom first tab and telnet localhost 8080 in the second tab (which you can see). On the right is the debug panel, showing me that the program has stopped at load, and giving me the ability to step into the function and follow it through.

Debugging server

| improve this answer | |
  • No, still nothing happened in the gdb terminal. I cannot see anything about the code. – Kevin King Oct 13 '16 at 7:07
  • So debug50 iisn't working at all? Try debug50 -HUP to reset it. Then try again. And just to be sure, you won't see anything in the terminal when you are running the debugger. You should see your editor window with your server.c code highlighted as it moves through, and you navigate using the buttons in the debugger panel on the right side. – curiouskiwi Oct 13 '16 at 8:00
  • How to set breakpoints? Using gdb commands? Then the first two command lines you suggest would not work. – Kevin King Oct 13 '16 at 8:13
  • I edited my answer to add more info. – curiouskiwi Oct 13 '16 at 8:18
  • I've tried your method step by step, but get different results. Still nothing happened on the right debugging panel. The screenshots have been updated. Thank you! – Kevin King Oct 13 '16 at 9:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .