Reading the manual is the first skill needed to program under GNU/Linux and similar OSes.
$man man invokes the manual.
Arrow keys navigate the manual, / opens up the search input, q quits the manual.
$man find looks up a specific page of the manual.
$man printf If man gives us documentation on a different topic than we want, we can restrict its search to a specific section: $man 3 printf $man 2 fork
For OS-related functions and general C coding, sections 2 and 3 of the manual are the most useful.
If man doesn't answer your question, look it up on the wider internet. For specific programming issues, stackoverflow is an invaluable resource.
screen and tmux are terminal multiplexers.
This very short tutorial talks about screen. The same key actions can be performed in tmux, by using its command key. The screen command key is Control-a, while for tmux, it's Control-b. However, screen and tmux use different command-line parameters.
For brevity, Control-a is shortened to C-a
screen provides multiple virtual consoles when a single console is, in fact, available.
screen can be used when having a single ssh connection to a linux machine, to get multiple command lines.
Operating screen is simple:
$screen $pwd $screen -x
$screen -ls$killall screen This will end all the processes inside all the screen sessions you have open, resulting in data loss.
Intermediate tmux tutorial, including window splitting:
http://lukaszwrobel.pl/blog/tmux-tutorial-split-terminal-windows-easily
wget is a powerful console-based URL downloader. Simply feed it an URL, and it will download it in the current path:
$wget 'http://profs.info.uaic.ro/~eugennc/teaching/cn/res/make.sh' It is best to enclose the URL with ' characters. Otherwise, the console might decide to evaluate the url, instead of feeding it unchanged to wget.
Copy/paste: On GNU/Linux, simply selecting text with the mouse copies it. Middle-mouse-button pastes.
putty uses right-mouse-button to paste.
gcc is used to make binaries from C code.
A simple usecase for gcc is:
$gcc hello.c -o hello.bin -lm -ggdb
The various tokens mean:
strace is a diagnostic tool that displays system calls made by a binary. It is often the first step in debugging a program.
strace ./hello.bin strace is a wrapper for the program. It launches it, then, as it executes, it displays the system calls as they are made.
$cat hello.c
#include<stdio.h>
int main(void)
{
printf("Hello!\n");
return 0;
}
$gcc hello.c -o hello.bin -ggdb
$strace ./hello.bin
strace ./hello.bin
execve("./hello.bin", ["./hello.bin"], [/* 44 vars */]) = 0
brk(NULL) = 0x1da4000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f66eee71000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=122073, ...}) = 0
mmap(NULL, 122073, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f66eee53000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\3\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1685264, ...}) = 0
mmap(NULL, 3791264, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f66ee8b4000
mprotect(0x7f66eea49000, 2093056, PROT_NONE) = 0
mmap(0x7f66eec48000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x194000) = 0x7f66eec48000
mmap(0x7f66eec4e000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f66eec4e000
close(3) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f66eee51000
arch_prctl(ARCH_SET_FS, 0x7f66eee51700) = 0
mprotect(0x7f66eec48000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ) = 0
mprotect(0x7f66eee74000, 4096, PROT_READ) = 0
munmap(0x7f66eee53000, 122073) = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 13), ...}) = 0
brk(NULL) = 0x1da4000
brk(0x1dc5000) = 0x1dc5000
write(1, "Hello!\n", 7Hello!
) = 7
exit_group(0) = ?
+++ exited with 0 +++
write(1, "Hello!\n", 7) call display is broken up by stdout.
gdb is a powerful debugger. At the very least, it can set breakpoints and pause code execution, inspect variables and print stack traces.
After building a binary with -ggdb
$gcc hello.c -o hello.bin -ggdb we can run it with $gdb ./hello.bin
gdb launches an interactive console, where commands can be executed:
(gdb) file ./hello.bin loads a binary file, if we've accidentally run gdb without arguments. (gdb) run runs the program.(gdb) run arg1 arg2 runs the program with arguments(gdb) break 4 (gdb) b 4 add a breakpoint at line 4.(gdb) clear 4 clears a breakpoint at line 4.(gdb) print argv (gdb) p argv[0] prints variables.(gdb) continue (gdb) c continues the execution.(gdb) next (gdb) n continues the execution until the next line is reached.
(gdb) n 5 continues the execution until the 5th line is reached.(gdb) stepi (gdb) s steps into the next line. If that line contains a function, gdb will go into its code.(gdb) up (gdb) down navigate up and down frames. If we're in a function, we can press up to see the code where that function is called. (gdb) backtrace (gdb) bt 5 (gdb) bt full (gdb) bt full 10 prints frames, starting from the current one, up the stack. (gdb) quit (gdb) q quits gdb. C-d (Ctrl-d) works as well.
(gdb) set follow-fork-mode child tells gdb to follow the child process after a fork. This allows child-process code debugging.
$gdb -tui ./hello.bin
M-x gdb
Intermediate gdb tutorial:
https://www.cs.cmu.edu/~gilpin/tutorial/
gdb fork debugging:
https://sourceware.org/gdb/onlinedocs/gdb/Forks.html
Makefiles are scripts/configuration files that are used, most often, to describe how the binaries of a software project should be made (compiled/linked).
A series of advanced features are presented in the example Makefile:
.cpp.o:
$(CC) $(GENERALFLAGS) $(CPPFLAGS) $< -o $@
is a rule that makes objects out of source files. The rule fires for each .cpp file; it generates an .o object name from the .cpp file name, then replaces the $< variable with the .cpp file name, and the $@ variable with the .o file name.
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $@ $(GENERALFLAGS) $(LDFLAGS)
specifies that, in order to be able to make the executable, all the objects need to first be made, themselves. After that requirement is satisfied, $(CC) is used to link the objects together in a binary executable.
A more advanced Makefile example:
Makefile
Emacs is a powerful and reliable mostly-text editor. It is extremely configurable, but requires a great deal of learning.
Its basic strengths are powerful keyboard shortcuts, on-the-fly recording and replaying of macros, and rapid regexp search and replace.
s means pressing s.
S means entering capital s.
C-s means pressing Ctrl and s at the same time.
C-x s means pressing Ctrl and x at the same time, releasing both, then pressing s.
C-x-s means pressing Ctrl and x, then keeping Ctrl pressed and pressing s.M-x by pressing alt and x, releasing, then entering the name of the mode. For instance, gdb is launched like M-x gdb This means pressing alt and x at the same time, releasing them, then typing gdb, then pressing enter. Then pressing enter again, as the gdb minor mode asks for custom parameters.C-x-f Not C-x f
This opens up a small dialogue (in Emacs terms, a minibuffer), where the file name can be typed. Upon typing the whole name and pressing enter, the file is opened in Emacs.C-x-s saves a file. C-x s saves all files currently open and modified. Emacs makes periodic saves, and saves the old version of files with the ~ extension (the standard backup extension).C-x-c exits EmacsC-g interrupts the current action. If you've found yourself stuck, press it a couple of times.C-s searches for a string.
C-M-s searches for a regular expression. That is Ctrl-Alt-s .C-r searches backward.M-% replaces a string.C-M-% replaces a regexp.f3 starts recording a macro. f4 ends the recording of a macro (if currently recording one). Otherwise, it executes the last recorded macro. Emacs macros are exceptionally powerful. As they replay the same keyboard inputs, you can call various Emacs functions, like search, word skip, and calling functions through M-x, repeatedly. It is, perhaps, Emacs' most powerful feature. A simple .emacs and .emacs.d configuration. Install this in the home directory. i.e.: ~/.emacs and ~/.emacs.d .
emacs.zip