Showing posts with label c. Show all posts
Showing posts with label c. Show all posts
Mar 12, 2016
Jul 16, 2014
Clewn
Clewn provides Gdb support within Vim - Clewn is a standalone program controlling Vim through a NetBeans interface.
List of default key mappings:
CTRL-Z send an interrupt to GDB and the program it is running
B info breakpoints
L info locals
A info args
S step
I stepi
CTRL-N next: next source line, skipping all function calls
X nexti
F finish
R run
Q quit
C continue
W where
CTRL-U up: go up one frame
CTRL-D down: go down one frame
cursor position: ~
CTRL-B set a breakpoint on the line where the cursor is located
CTRL-E clear all breakpoints on the line where the cursor is located
mouse pointer position: ~
CTRL-P print the value of the variable defined by the mouse pointer
position
CTRL-X print the value that is referenced by the address whose
value is that of the variable defined by the mouse pointer
position
CTRL-K set a breakpoint at assembly address shown by mouse position
CTRL-H clear a breakpoint at assembly address shown by mouse position
CTRL-J add the selected variable at mouse position to the watched
variables window
Note: You can tune Vim behavior when using netBeans keys with the mouse
pointer position, by setting Vim balloondelay to a smaller value than its
default. The following setting has been reported to give better results:
:set balloondelay=100 ~
Note: After browsing your code, you can always return rapidly to the line of
code where GDB is currently stopped, by using CTRL-U and CTRL-D.
Jun 25, 2014
Jun 24, 2014
Elfutils
https://access.redhat.com/site/documentation/en-US/Red_Hat_Developer_Toolset/1/html/User_Guide/chap-elfutils.html
sudo apt-get install elfutils
eu-readelf --all /lib/x86_64-linux-gnu/libc.so.6
gcc dumpspecs
gcc -dumpspecs
# ...
# *startfile:
# %{!mandroid|tno-android-ld:%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}} crti.o%s # %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s};:%{shared: crtbegin_so%O%s;: %{static: crtbegin_static%O%s;: crtbegin_dynamic%O%s}}}
# ...
http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Spec-Files.html#Spec-Filesld --verbose
#...
# OUTPUT_ARCH(i386:x86-64)
# ENTRY(_start)
# SEARCH_DIR("/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); # SEARCH_DIR("=/usr/lib");
SECTIONS
# ...
https://sourceware.org/binutils/docs/ld/
Core dump
Allow core dump
Permanent:
Permanent:
sudo vi /etc/security/limits.conf
#add line
#YOUR_USERNAME soft core 1000000
Temporary:ulimit -c unlimited
Core dumpsleep 30
# Type Control-\
Quit (core dumped)
kill -ABRT <PID>
# 11602 abort (core dumped)
In Cabort()
Core dump misccat /proc/sys/kernel/core_pattern
man 5 core
cat /proc/PID/coredump_filter
Apr 29, 2014
Resources that have influenced me as a C programmer
From http://www.reddit.com/r/C_Programming/comments/249cmt/resources_that_have_influenced_me_as_a_c/
http://stackoverflow.com/questions/tagged/c
The GNU C Library Reference. Also commonly available as 'info libc'. It should be read like a book. Really.
The Kernel CodingStyle, has some pretty good advice.
What every programmer should know about memory is very relevant for high-speed processing. This is the LWN article.
Kernel design patterns. Another great LWN link. I always smile a little whenever I use the container_of() macro.
Recursive Make Considered Harmful. Makes a fine beverage when mixed with Autotools.
Gnulib - the GNU Portability Library. Add in small quantities.
GNU C Language Extensions. I try to stay pretty compiler-agnostic, but there are some nifty things here. I would have never known about zero-length arrays if not for this.
Linux Device Drivers, 3rd edition.. Definitely worth reading.
The Linux Programming Interface. Likewise.
K&R Second Edition. Almost forgot this one!
GNU Screen. Nothing specific to C programming, but this utility deserves mention.
More than anything, man pages, 'info libc', reading header files, etc. Ways to look things up without first turning to Google, which often links to low signal-to-noise resources.
http://stackoverflow.com/questions/tagged/c
The GNU C Library Reference. Also commonly available as 'info libc'. It should be read like a book. Really.
The Kernel CodingStyle, has some pretty good advice.
What every programmer should know about memory is very relevant for high-speed processing. This is the LWN article.
Kernel design patterns. Another great LWN link. I always smile a little whenever I use the container_of() macro.
Recursive Make Considered Harmful. Makes a fine beverage when mixed with Autotools.
Gnulib - the GNU Portability Library. Add in small quantities.
GNU C Language Extensions. I try to stay pretty compiler-agnostic, but there are some nifty things here. I would have never known about zero-length arrays if not for this.
Linux Device Drivers, 3rd edition.. Definitely worth reading.
The Linux Programming Interface. Likewise.
K&R Second Edition. Almost forgot this one!
GNU Screen. Nothing specific to C programming, but this utility deserves mention.
More than anything, man pages, 'info libc', reading header files, etc. Ways to look things up without first turning to Google, which often links to low signal-to-noise resources.
Labels:
c
Apr 14, 2014
Shared libraries and dynamic linker
cc -g -c prog.c mod1.c mod2.c mod3.c
cc -g -o prog_nolib prog.o mod1.o mod2.o mod3.o
strip(1) - discard symbols from object files.
Static lib:
ar r libdemo.a mod1.o mod2.o mod3.o
ar tv libdemo.a
# rw-rw-r-- 1000/1000 1003352 Apr 14 11:24 2014 mod1.o
# rw-rw-r-- 1000/1000 403352 Apr 14 11:24 2014 mod2.o
# rw-rw-r-- 1000/1000 43352 Apr 14 11:24 2014 mod3.o
cc -g -o prog prog.o libdemo.a
# or
cc -g -o prog prog.o -Lmylibdir -ldemo
./prog
Dinamic lib:
gcc -g -c -fPIC -Wall mod1.c mod2.c mod3.c
gcc -g -shared -o libfoo.so mod1.o mod2.o mod3.o
# or
gcc -g -fPIC -Wall mod1.c mod2.c mod3.c -shared -o libfoo.so
gcc -g -Wall -o prog prog.c libfoo.so
LD_LIBRARY_PATH=. ./prog
nm(1) - list symbols from object files
readelf(1) - displays information about ELF files
objdump(1) - display information from object files
ldd(1) - print shared library dependencies
ldconfig(1) - configure dynamic linker run-time bindings
nm mod1.o
readelf -s mod1.o
objdump --all-headers libfoo.so
readelf -d libfoo.so
Process shared libs:
cat /proc/PID/maps
Shared libs that prog required:
ldd /usr/sbin/tcpdump
# linux-vdso.so.1 => (0x00007fffe17ff000)
# libcrypto.so.1.0.0 => /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007ff285828000)
# libpcap.so.0.8 => /usr/lib/x86_64-linux-gnu/libpcap.so.0.8 (0x00007ff2855ed000)
# libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff285224000)
# libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff285020000)
# libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007ff284e07000)
# /lib64/ld-linux-x86-64.so.2 (0x00007ff28606f000)
Where function defined:
nm -A /usr/lib/lib*.so 2> /dev/null | grep ' crypt$'
All system shared libs:
ldconfig –p
#1768 libs found in cache `/etc/ld.so.cache'
# libzvbi.so.0 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libzvbi.so.0
# libzvbi-chains.so.0 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libzvbi-chains.so.0
# libzltext.so.0.13 (libc6,x86-64) => /usr/lib/libzltext.so.0.13
# libzlcore.so.0.13 (libc6,x86-64) => /usr/lib/libzlcore.so.0.13
# libzip.so.2 (libc6,x86-64) => /usr/lib/libzip.so.2
#...
Path: LD_LIBRARY_PATH, DT_RPATH, DT_RUNPATH, $ORIGIN
gcc -Wl,-rpath,'$ORIGIN'/lib ...
Run-time symbol resolution:
gcc -g -c -fPIC -Wall -c foo.c
gcc -g -shared -o libfoo.so foo.o
gcc -g -o prog prog.c libfoo.so
LD_LIBRARY_PATH=. ./prog
# main-xyz
Monitoring dynamic linker:
LD_DEBUG=help ./prog
LD_DEBUG=all ./prog
Labels:
c
Feb 3, 2014
GCC, DDD
gcc
ddd
X86 Registers
# -S to output assembly
gcc -O0 -S hello.c
# -masm=intel or -masm=att
ddd
gcc -g -c sample sample.c
ddd sample
ddd --disassemble sample
X86 Registers
Jan 23, 2014
Kernel. Modules, program exec, syscalls, elf
man 2 syscalls
system_call()
strace -e trace=open date
strace -c date
ltrace -T -S rmdir t
ltrace -c -S rmdir t
ldd `which python`
Elf
# view headers, section...
readelf -a a.out
readelf --sections=vmlinux a.out
readelf --dynamic a.out
# disassembly
objdump -M intel -S a.out
# list symbols from object files
nm a.out
# dependencies resolved from this list
cat /proc/kallsyms | grep printk
cat /boot/System.map-3.13.0 | grep printk
stat a.out
file a.out
Modules
# list modules
lsmod
# Modules dependencies
cat /lib/modules/3.12.7/modules.dep | grep vboxdrv
man depmod
# info
modinfo vboxdrv
# view system device information
systool -v -m vboxdrv
struct modulesys_init_module() -> load_module()
sys_delete_module()
request_module()
hello world module
man 2 init_module
man 2 delete_module
# which module loader
cat /proc/sys/kernel/modprobe
sudo sysctl -a | grep modprobe
man modprobe
man insmod
man rmmod
ll /sbin/udevd
udevadm monitor
cat /proc/sys/kernel/taintined
dmesg | grep taint
Jan 11, 2014
MALLOC_CHECK_ and MALLOC_PERTURB_
http://l3net.wordpress.com/2013/06/19/malloc_check_-and-malloc_perturb_/
Recent versions of Linux glibc (2.x) include a a malloc() implementation tunable using environment variables. This allows the user to diagnose allocation problems such as heap corruption, double free etc.
When MALLOC_CHECK_ is set to 3, a diagnostic message is printed on stderr and the program is aborted. A value of 0 disables the diagnostic – see man malloc for more details.
Setting the MALLOC_PERTURB_ environment variable causes the malloc functions in libc to return memory which has been wiped and initialized with the byte value of the environment variable. Setting MALLOC_PERTURB_ to zero disables the feature.
No special arguments need to be passed during program compilation. These diagnostics work on any precompiled programs. Unlike valgrind, the speed of execution is not affected.
Recent versions of Linux glibc (2.x) include a a malloc() implementation tunable using environment variables. This allows the user to diagnose allocation problems such as heap corruption, double free etc.
When MALLOC_CHECK_ is set to 3, a diagnostic message is printed on stderr and the program is aborted. A value of 0 disables the diagnostic – see man malloc for more details.
Setting the MALLOC_PERTURB_ environment variable causes the malloc functions in libc to return memory which has been wiped and initialized with the byte value of the environment variable. Setting MALLOC_PERTURB_ to zero disables the feature.
No special arguments need to be passed during program compilation. These diagnostics work on any precompiled programs. Unlike valgrind, the speed of execution is not affected.
export MALLOC_CHECK_=3
export MALLOC_PERTURB_=$(($RANDOM % 255 + 1))
Jan 10, 2014
Valgrind overview: runtime memory checker and a bit more aka использование #valgrind на селе
valgrind --tool=callgrind ls
http://www.youtube.com/watch?v=dKngHt9CvokAlleyoop is a Valgrind front-end for the GNOME environment, made by Jeffrey Stedfast.
Jan 22, 2013
Jan 17, 2013
Jan 11, 2013
Compiling C
-O3 indicates optimization level three, which tries every trick known to build faster code. If, when you run the debugger, you find that too many variables have been optimized out for you to follow what’s going on, then change this to -O0.
-Wall adds compiler warnings. This works for gcc, Clang, and icc. For icc, you might prefer -w1, which displays the compiler’s warnings, but not its remarks.
-Werror your compiler will treat warnings as errors.
-I adds the given path to the include search path, which the compiler searches for header files you #included in your code.
-L adds to the library search path. Order matters:
-g adds symbols for debugging.
pkg-config
-Wall adds compiler warnings. This works for gcc, Clang, and icc. For icc, you might prefer -w1, which displays the compiler’s warnings, but not its remarks.
-Werror your compiler will treat warnings as errors.
-I adds the given path to the include search path, which the compiler searches for header files you #included in your code.
-L adds to the library search path. Order matters:
gcc -I/usr/local/include use_useful.c -o use_useful -L/usr/local/lib -luseful
If you have a file named specific.o that depends on the Libbroad library, and Libbroad depends on Libgeneral, then you will need: gcc specific.o -lbroad -lgeneral-g adds symbols for debugging.
-std=gnu11 is gcc-specific, and specifies that gcc should allow code conforming to the C11 and POSIX standards
pkg-config
pkg-config --libs gsl libxml-2.0
# -lgsl -lgslcblas -lm -lxml2
pkg-config --cflags gsl libxml-2.0
# -I/usr/include/libxml2
gcc `pkg-config --cflags --libs gsl libxml-2.0` -o specific specific.c
# gcc -I/usr/include/libxml2 -lgsl -lgslcblas -lm -lxml2 -o specific specific.c
Runtime linking
export LD_LIBRARY_PATH=libpath:$LD_LIBRARY_PATH
# or
LDADD=-Llibpath -Wl,-Rlibpath
Labels:
c
Jan 10, 2013
Compiling C from stdin
allheads.h:
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <gsl/gsl_rng.h>
Paste this onto your command line, or add it to your .bashrc, .zshrc, or wherever applicable:
go_libs="-lm"
go_flags="-g -Wall -include allheads.h -O3"
alias go_c="c99 -xc '-' $go_libs $go_flags"
And now we can use a here document to paste short C programs onto the command line:
go_c << '---'
int main(){printf("Hello from the command line.\n");}
---
# and then run:
./a.out
Jan 7, 2013
Head First C - David Griffiths
Exit status of last program
echo $?
&x - location in memory of x variable
You can also call sizeof for a data type, such as sizeof(int).
You can declare a char pointer as const char * to prevent the code from using it to modify a string.
man strstr
info coreutils 'printf'
Q: But what if I want to pass negative numbers as command-line arguments like set_temperature -c -4? Won’t it think that the 4 is an option, not an argument?
A: In order to avoid ambiguity, you can split your main arguments from the options using --. So you would write set_temperature -c -- -4. getopt() will stop reading options when it sees the --, so the rest of the line will be read as simple arguments.
(./bermuda | ./geo2json) < spooky.csv > output.json
If you genuinely want to share variables, you should declare them in your header file and prefix them with the keyword extern:
extern int passcode;
make takes away a lot of the pain of compiling files. But if you find that even it is not automatic enough, take a look at a tool called autoconf
The make tool can do far, far more than we have space to discuss here. To find out more about make and what it can do for you, visit the GNU Make Manual
Remember: when you're assigning struct variables, you are telling the computer to copy data.
If you use the typedef command, you can normally skip giving the struct a proper name. But in a recursive structure, you need to include a pointer to the same type. C syntax won't let you use the typedef alias, so you need to give the struct a proper name. That's why the struct here is called struct island.
The nm command lists the names that are stored inside the archive.
nm /usr/lib/libruby1.8-static.a
When you bind a socket to a port, the operating system will prevent anything else from rebinding to it for the next 30 seconds or so, and that includes the program that bound the port in the first place. Use reuse option (p. 477)
MUT-EX = MUTually EXclusive.
The make tool knows quite a lot about C compilation, and it can use implicit rules to build files without you telling it exactly how. For example, if you have a file called fred.c, you can compile it without a makefile by typing:
> make fred
cc fred.c -o fred
Purchase at Amazon
https://bitbucket.org/st1tch/head-first-c
Author repository: https://github.com/dogriffiths/HeadFirstC
Subscribe to:
Posts (Atom)







