How to adjust source code highlighting in GDB cli? - c++

I am using basic GDB CLI tool, no any TUI frontends. It highlights some parts of code with the same color as my terminal background making them indistinguishable. I know it is possible to disable source code highlighting but I would like to have it.
I didn't find much about this in documentation besides the fact that either GNU Source Highlight library, or Python Pygments package provide highlighting, but not a single word about how to check which of them GDB actually use or how to configure them and adjust colors.

Edit esc.style in /usr/share/source-highlight/esc.style
GDB uses source-highlight which should not be confused with similar tool called just 'highlight' and provided by some distributions including Debian and Ubuntu. It is possilbe to check if GDB is actually linked with it: there should be --enable-source-highlight line in gdb --configuration output. It is documented in info source-highlight and is configured by .lang and .style files. esc means 'escape' and used for output in terminal, esc.style usage is hardcoded in GDB sources, it would be more correct to check terminfo and use esc256.style if appropriate, but it is written the way it is written.

Related

Why can't I use ament_cpplint with cpplint plugins for various IDEs?

I am working with C++ code with ROS2. ROS2 has code style standards and one of the recommended linters is ament_cpplint, which is slightly different from cpplint
Most IDEs/Editors for C++ (Clion, Atom, VS Code, Sublime Text) have plugins for cpplint which will run periodic linter checks as you are editing files. You usually have to set the path to the cpplint executable on your computer. I've tried to set the path to ament_cpplint executable instead but it just won't work. I've tried this with multiple IDEs and plugins by now and none of them will work.
I am not sure what's causing this, Google's cpplint works with IDEs. And I can run $ ament_cpplint from the command line just fine, but I can't get it to work in an IDE.
This might be too late for you, but for others.
You need to pass the path to the cpplint.py executable that is inside the ament_cpplint package.
For ros2 galactic, it is located here:
/opt/ros/galactic/lib/python3.8/site-packages/ament_cpplint/cpplint.py

How to setup custom breakpoints in the C++ program?

I'm working on a project, where I cannot disclose the details of the code. So, the application is all written in C and C++. Since, a particular file which wanted to debug has a lot of dependencies and exports, I need to debug the whole project. How do I set breakpoints in the code itself so that the debugging would stop at that particular point? I'm using Ubuntu 14.04 (since the project is compatible with this environment) and gdb debugger.
I've tried using
#include <csignal>
// Generate an interrupt
std::raise(SIGINT);
But I keep getting error
error: ‘raise’ is not a member of ‘std’
Even this also didn't work
#include <signal.h>
raise(SIGINT);
Plus the debugging wont stop at that point, so that I could foresee the function at that point. I only want to debug it from console, rather using any IDE.
Since the programfile I want to debug has lot many header files which it imports, I'm unable to make a executable to use gdb. So, while make clean build of my MakeFile I want to debug the particular program file at a particular function. So, for that I want to add breakpoints in the program. I cannot use any GUI for debugging since I should not use.
Have you tried to use GDB Commands?
b lineno - set a break point at line 'lineno'
b srcfile:lineno - set a break point in source file 'srcfile' at line 'lineno'
Read more about debugging with gdb. Be sure to compile all your code with DWARF debug information (so use g++ -Wall -Wextra -g to compile it with GCC).
GDB is extensible and you can define your own gdb commands at startup in your init file, probably .gdbinit and put some initial commands there.
BTW, on Linux, debugging (so the gdb debugger) is using ptrace(2) facilities. And you can use gdb non-interactively on the command line, using scripts.
How do I set breakpoints in the code itself
I don't recommend adding specific C code for breakpoints. So don't do that in your C code. But see also this.
Perhaps you want some backtrace library, like Ian Taylor's libbacktrace ?
I cannot use any GUI for debugging
You don't need to. You'll use gdb on the command line. With an appropriate gdb script, you can even use it non-interactively (e.g. in a Makefile)
I only want to debug it from console, rather using any IDE.
Please realize that IDEs are only glorified source code editors capable of running other external tools (including the GCC compiler and the gdb debugger). You certainly don't need -on Linux- any IDE to run a compiler or a debugger (but IDEs could be convenient, but not necessary, for that), because you can (and should) run your compiler, your debugger, your build automation tool, on the command line.
Since the program file I want to debug has lot many header files which it imports, I'm unable to make a executable
You should fix that first. You need to make an executable. BTW, there is no "import" involved at run time, since header files are relevant only at compile time. Read more about the cpp preprocessor. You probably should invoke GCC (e.g. the g++ compiler, since you have C++ code) with appropriate preprocessor options (sometimes, tools like pkg-config are useful for that). You probably should use some build automation tool such as GNU make (with your Makefile; see this for inspiration) or ninja. You could add ad hoc gdb commands to your build procedure (e.g. with some additional and specific rules and/or recipes in your Makefile).
First, make sure you have compiled with -g. There are other gdb specific flags in gcc. You could add them in too.
Try using ddd, the graphical version of gdb. Great tool if you don't know the gdb command line. Just open up the relevant source file, select the line then click on breakpoint on the toolbar. It will tell you on the console section, what command was actually executed (good way to learn). There is a floating button list with run, next etc. for stepping through your code.
ddd will work on most of the gcc toolchain.
EDIT:
Say your code is made up of 2 files main.cpp and child.cpp. main.cpp contains main(). The executable is called a.out.
To start
ddd a.out &
It will open in main.cpp. To put a breakpoint in child.cpp, click on File/Open Source... and select child.cpp. Then scroll to where you want a breakpoint. Put your cursor on the line, then click on break in the toolbar.
To run, either type run in the gdb window below or click on Run in the floating button dialog.

Can I set a breakpoint with gdb as I'm calling it?

I'm in the middle of a large debugging project, and every time I start running gdb I have to type b 253.
It would be really nice if I could set my run script so that gdb loads with that breakpoint already set.
To be more explicit: Here are the contents of run.csh:
gdb --args path/to/program arg1 arg2
Can I modify this so that, once I run it, I can just type r and the program breaks on line 253?
Yes. Read documentation of gdb.
You can extend GDB. You can have Canned Sequences of Commands.
You can define or use extensions in Python, in Guile. See also this.
(you might need to recompile GDB itself from source, since sadly not all usual gdb are configured with Guile support)
You can have your .gdbinit file (read about startup files and command files). Btw you might prefer to break in function names, not in line numbers there. Read more about specifying locations.
Actually, many large projects have some .gdbinit (perhaps generated) in their source repository.
Be sure to use a recent version of GDB. The latest one (in March 2018) is GDB 8.1

How to use the documentation of C++ code inside an IDE on linux?

Up to now i tried Eclipse, KDevelop and Code::Blocks.
Code::Blocks (12.11) seems not to be able to display documentation at all.
Eclipse (4.3.2) is able to display the documentation of at least the standard libraries during code completion and on hover, but it looks like there is no way to generate or add custom documentation. By now I was able to use DoxygenCPPInfo to convert the xml documentation to a "Java Serialization Data" file, which is useable by libhover. But the documentation is only visible on hover, but not on code completion.
KDevelop (4.7) does only show the comment, which normally contains the documentation, on hover and a heavily shortened version on code completion.
Is there another IDE or something else I could do to benefit from in code documentation while writing new code?
DoxygenCPPInfo can be compiled using the following files from eclipse-linuxtools:
ClassInfo.java
FunctionInfo.java
LibHoverInfo.java
MemberInfo.java
TypedefInfo.java
DoxygenCPPInfo.java
The xml documentation needs to be in one file to be used with DoxygenCPPInfo. This can be done by using xsltproc with combine.xslt and index.xml as input files. The final output of DoxygenCPPInfo can be placed in workspace/.metadata/.plugins/org.eclipse.linuxtools.cdt.libhover/CPP/ and will be loaded on next start of eclipse using that workspace. The documentation is only shown on hover and not on code completion.
You want to document a function in one file and use that function in another file. While using that function you want to reference the documentation written earlier in a tooltip without having to compile the first file. This can be done easily in kdevelop ide. There are not much IDEs I have come across that provide such ease of cross-referencing. Eclipse lib-hover plugin for C but it is clunky and I have had trouble working with that earlier. Here is the link to kdevelop-handbook .
Documenting in doxygen style in kdevelop

How to configure Vim for C++ development?

I'm learning C++ using Vim as an editor on Windows XP, however I found a issue that I have listed below.
I have downloaded and installed c.vim and it is a essential file, however when I start vim it shows the message C/C++ template file 'C:\Program Files\Vim\vimfiles\c-support/templates/Templates' does not exist or is not readable, How do i fix this problem?
How would i make vim compile a C++ STL file?
For your first problem: I suspect that you didn't extract all the files in the archive (that c.vim came in). The c.vim documentation (README.csupport) says:
The subdirectories in the zip archive
cvim.zip mirror the directory
structure which is needed below the
local installation directory
$HOME/.vim/ for LINUX/UNIX
($VIM/vimfiles/ for Windows)
This means that you need to uncompress the entire archive as it is into your vimfiles directory.
There are some other steps to follow, detailed in the documentation.
As for your second issue: you need a Makefile to do that. If you have never done this before, I suggest using cmake to generate a Makefile. You will also need GNU tools for Windows; Cygwin or MinGW are the most popular choices. I haven't use them, it is easier to do all this on some *nix OS :).
When done, use :cd (if you are not in your working directory), and :make. Use :cl to list the compiler output, :cn to jump to the next error. There are some other useful commands for compiling. You might find these resources useful:
StackOverflow: Recommended plugins for C coding
Compiling from Vim
C editing with Vim
Also, I found the Nerd Commenter a very useful companion.
I found that Vim acts somewhat like alien on Windows; it is designed for an *nix-like operating system. I think it is possible to craft a similar environment for it, and use it mostly successfully, but it is so much easier to do on some linux, as it is "instantly home" there.
Anyway, if you wish to stick with Windows, I think you can find a way to accomplish what you want. Good luck.
Download Vim and install to your computer.
Download c.vim and extract to $Vim\vimfiles\
Download MinGW and install to your computer, make sure that you check C++ Compiler at Select Components.
Add C:\MinGW to system Path variable
Edit _vimrc file, add set makeprg=mingw32-make after line behave mswin
Test your Vim with hello world, use !g++ c:\full_path\filename.cpp -o c:\full_path\output.exe.