-1

I ran into a problem when I tried to debug a c++ console project in Codeblocks

Debugging went fine until I included <ncurses.h> and I included the library with -lncurses

I took a code sample from another stackoverflow page if someone would want to give it a try. This code sample compiles and runs fine but when I try to debug it, it closes the console window when I execute the line: w = initscr();

#include <stdlib.h>
#include <curses.h>
//#include <locale.h>
#include <ctype.h>
#include <unistd.h>

int main( void ) {
    WINDOW *w;
    int     c;
    /* This is optional. This tells the C libraries to use user's locale settings. */
    // setlocale( LC_ALL, "" );
    /* Initialize curses. */
    w = initscr();
    if ( w == NULL ) {
        fprintf( stderr, "Error initializing curses library.\n" );
        return EXIT_FAILURE;
    }
    raw();              /* Terminal in raw mode, no buffering */
    noecho();           /* Disable echoing */
    //nonl();           /* Disable newline/return mapping */
    keypad( w, FALSE ); /* FALSE: CSI codes, TRUE: curses codes for keys */
    timeout(1);
    printw( "Press Q to quit.\n" );
    do {
        c = getch();
        if ( isprint( c ) ) {
            printw( " Received character '%c' ", c );
        }
        usleep( 100000 );
        printw( "." );
    } while ( c != 'Q' );
    /* Done. */
    endwin();
    return EXIT_SUCCESS;
}

The build log looks fine to me:


-------------- Clean: Debug in keyboard (compiler: GNU GCC Compiler)---------------

Cleaned "keyboard - Debug"

-------------- Build: Debug in keyboard (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -g -I/usr/lib/x86_64-linux-gnu/wx/include/gtk3-unicode-3.0 -I/usr/include/wx-3.0 -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread -DCB_VERSION="\\\"Code::Blocks Release 20.03 rev 11997 2020-04-18, 19:47:24\\\"" -I/usr/include -c /home/data/VBOX/VBOX_DATA/CodeBlocks/keyboard/main.cpp -o obj/Debug/main.o
g++ -L/usr/lib/ -o bin/Debug/keyboard obj/Debug/main.o  -L/usr/lib/x86_64-linux-gnu -pthread   -lwx_gtk3u_xrc-3.0 -lwx_gtk3u_html-3.0 -lwx_gtk3u_qa-3.0 -lwx_gtk3u_adv-3.0 -lwx_gtk3u_core-3.0 -lwx_baseu_xml-3.0 -lwx_baseu_net-3.0 -lwx_baseu-3.0   -lncurses
Output file is bin/Debug/keyboard with size 23.88 KB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

The last lines from the debugger window:

Debugger name and version: GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2

[debug]>>>>>>cb_gdb:
[debug]> set width 0
[debug]>>>>>>cb_gdb:
[debug]> set height 0
[debug]>>>>>>cb_gdb:
[debug]> set breakpoint pending on
[debug]>>>>>>cb_gdb:
[debug]> set print asm-demangle on
[debug]>>>>>>cb_gdb:
[debug]> set unwindonsignal on
[debug]>>>>>>cb_gdb:
[debug]> set print elements 200
[debug]>>>>>>cb_gdb:
[debug]> set disassembly-flavor intel
[debug]>>>>>>cb_gdb:
[debug]> catch throw
[debug]Catchpoint 1 (throw)
[debug]>>>>>>cb_gdb:
[debug]> directory /home/data/VBOX/VBOX_DATA/CodeBlocks/keyboard/
[debug]Source directories searched: /home/data/VBOX/VBOX_DATA/CodeBlocks/keyboard:$cdir:$cwd
[debug]>>>>>>cb_gdb:
[debug]> tty /dev/pts/0
[debug]>>>>>>cb_gdb:
[debug]> run
[debug]Starting program: /home/data/VBOX/VBOX_DATA/CodeBlocks/keyboard/bin/Debug/keyboard 
[debug][Thread debugging using libthread_db enabled]
[debug]Using host libthread_db library "/usr/lib/x86_64-linux-gnu/libthread_db.so.1".
[debug][Inferior 1 (process 20484) exited with code 01]
[debug]>>>>>>cb_gdb:

[Inferior 1 (process 20484) exited with code 01]

[debug]> quit

Debugger finished with status 0

What could be wrong here?

hennep
  • 545
  • 4
  • 12
  • why `tty /dev/pts/0`? It's a pseudo tty which may already be in use. Regardless, it's not a vty. – Bib Nov 25 '21 at 19:36
  • No idea whay that's used, in settings I've specified xterm – hennep Nov 25 '21 at 19:42
  • Either remove it, or set it to the tty of an running xterm. I'd just remove it, since stopping it causes problems with other tty's, or rather, you can't. – Bib Nov 25 '21 at 19:49
  • 1
    I do not see any evidence of a crash. The program terminates with an exit code of 1. That is not a crash. – Sam Varshavchik Nov 25 '21 at 19:52
  • It's because `gdb` cannot write to `/dev/pts/0`, if he removes that, it works just fine. – Bib Nov 25 '21 at 19:59
  • I don't know where to change that tty line. In the meantime I have tried qterminal instead of xterm, that did not make any difference. @Sam, The program exits when I single step on the "initscr" line. It should run until the 'Q' is pressed on the keyboard. – hennep Nov 25 '21 at 20:23
  • @hennep as for the "tty line", check [.gdbinit](https://stackoverflow.com/questions/9573683/where-is-gdbinit-is-located-and-how-can-i-edit-it/13637929) and debug settings in your IDE. – alagner Nov 25 '21 at 20:28
  • Only one init file /etc/gdb/gdbinit and it is empty. When I look at the start of the debug window, it shows: "[debug]Using terminal's PID as console PID 22105, TTY /dev/pts/0", so it is set to the running term. – hennep Nov 25 '21 at 20:40

1 Answers1

-1

I got ncurses to work in the debugger after I changed the access rights of the device /dev/pts/1 with: chmod 777 /dev/pts/1 (Note that pts/0 was already in use by another terminal to change the access rights)

That is a confirmation that the problem is caused by the terminal but it is not a permanent situation that I want, so I tried to revert it to:

crw--w----  1 hennep tty  136, 1 nov 26 11:05 1

When I execute "chmod 620 /dev/pts/1" it looks the same but I can still run the debugger without any problems. That might be good news but something has changed in the access rights and I don't know what I have messed up :-(

hennep
  • 545
  • 4
  • 12