Generate intermediate files ( assembling, preprocessing) in gcc

Let’s start with some GCC commands as shown below. How-ever it won’t give you all the resulted files in each stage, for ex: preprocessor files. For that, I use below GCC option to generate different output files of my source. As mentioned in “man’ page it will generate “.i”, “s”, “.o” files .. Ex: #Examine …

Read more

Kernel module to print process pid, stack..etc information..

When browsing through my filesystem,  I saw a kernel module which I wrote a way back to print information about the current process and its stack, pid..etc.. Also, this module prints all the processes running in the system. So I loaded that into my running system and it was obedient 🙂

I had called him ‘process_informer.ko’..

[root@humbles-lap hello]# make
make -C /lib/modules/2.6.30.10-105.2.23.fc11.x86_64/build M=/misc/my_kernel_modules/hello modules
make[1]: Entering directory `/usr/src/kernels/2.6.30.10-105.2.23.fc11.x86_64′
CC [M]  /misc/my_kernel_modules/hello/process_informer.o
Building modules, stage 2.
MODPOST 1 modules
CC      /misc/my_kernel_modules/hello/process_informer.mod.o
LD [M]  /misc/my_kernel_modules/hello/process_informer.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.30.10-105.2.23.fc11.x86_64′
[root@humbles-lap hello]#

[root@humbles-lap hello]# lsmod |grep process
[root@humbles-lap hello]# insmod process_informer.ko
[root@humbles-lap hello]# lsmod |grep process
process_informer       12427  0
[root@humbles-lap hello]#

[root@humbles-lap hello]# modinfo process_informer.ko
filename:       process_informer.ko
license:        GPL
description:    Module, for fun
version:        1.0.0
author:         Humble Chirammal
srcversion:     C62BE5C41BEC6E733271884
depends:        
vermagic:       2.6.30.10-105.2.23.fc11.x86_64 SMP mod_unload
[root@humbles-lap hello]#

[root@humbles-lap hello]# rmmod process_informer.ko
[root@humbles-lap hello]# lsmod |grep process

Jun  1 00:01:17 humbles-lap kernel: [ 6225.683529]  Hi World, somebody loaded me  ==> __init()
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683533]
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683534]  I am informed to track current process
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683538]
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683539]       Current process is : ‘insmod’
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683540]        pid : ‘8875’
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683541]       Kernel release : ‘2.6.30.10-105.2.23.fc11.x86_64’
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683544]      ‘RUNNABLE’ OR ‘UNRUNNABLE’
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683547]       PRIO : 120  STATE: 0
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683550]
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683551]       My thread_info located at : ffff88009d0dc000
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683552]
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683555]      Process 0 is  : swapper/0     pid :: 0
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683558]      Process 1 is  : systemd     pid :: 1
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683561]      Process 2 is  : kthreadd     pid :: 2
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683565]      Process 3 is  : ksoftirqd/0     pid :: 3
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683568]      Process 4 is  : migration/0     pid :: 6
Jun  1 00:01:17 humbles-lap kernel: [ 6225.683571]      Process 5 is  : watchdog/0     pid :: 7

************Truncated..

Jun  1 00:01:17 humbles-lap kernel: [ 6225.684065]      Process 157 is  : su     pid :: 7296
Jun  1 00:01:17 humbles-lap kernel: [ 6225.684068]      Process 158 is  : bash     pid :: 7306
Jun  1 00:01:17 humbles-lap kernel: [ 6225.684071]      Process 159 is  : bash     pid :: 7398
Jun  1 00:01:17 humbles-lap kernel: [ 6225.684074]      Process 160 is  : su     pid :: 7419

Jun  1 00:02:08 humbles-lap kernel: [ 6225.684117]      Process 174 is  : sleep     pid :: 8856

Jun  1 00:02:08 humbles-lap kernel: [ 6276.449168]   Bye Guys, he is kind enough to unload me      ==> __exit()

Hmmmm.. there are ‘174’ processes in my system …

How can I get a process usage using glibc ( getrusage() ) functions ?

It is always possible to fetch different information about a process from “/proc/” filesystem. I can point some important files from “/proc” filesystem to get statistics about a process. /proc/pid/stat /proc/pid/statm /proc/pid/status You can substitute the “pid” of the process in above and get the status of a process. I can see some of the …

Read more

Examine/display memory and register in gdb

This is going to be a small demonstration or ‘tip’ to analyze registers and memory via gdb when debugging a program. These commands are pretty much useful when debugging a program. It has its own use cases.

Examine registers:

$info registers is the command which can be used to see current register values at the moment from gdb prompt. Below command can be used as a short cut to view registers:

(gdb) i r rax 0x1 1 rbx 0x7fff955a9df0 140735699131888 rcx 0xffffffffffffffff -1 rdx 0x7fff955a9e70 140735699132016 rsi 0x7fff955a9df0 140735699131888 rdi 0x16 22 rbp 0x7fff955a9e70 0x7fff955a9e70 rsp 0x7fff955a9dc0 0x7fff955a9dc0 r8 0x7fff955a9dd0 140735699131856 r9 0x1 1 r10 0x7fff955a9ef0 140735699132144 r11 0x293 659 r12 0x7fff955a9ef0 140735699132144 r13 0x0 0 r14 0x1 1 r15 0x0 0 rip 0x37e78da373 0x37e78da373 eflags 0x293 [ CF AF SF IF ] cs 0x33 51 ss 0x2b 43 ds 0x0 0 es 0x0 0 fs 0x0 0 gs 0x0 0

Register information can be fetched individually . For ex: “Stack pointer” and “Instruction pointer” information can be fetched by:

(gdb) i r $sp sp: 0x7fff955a9dc0 (gdb) i r $rip rip 0x37e78da373 0x37e78da373 (gdb)

Examining memory :

This is pretty much useful when debugging a program:

“x” is the command which can be used for the same purpose.. The general format of ‘x’ command as shown here.

(gdb) help x

Examine memory: x/FMT ADDRESS.

ADDRESS is an expression for the memory address to examine. FMT is a repeat count followed by a format letter and a size letter. Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal), t(binary), f(float), a(address), i(instruction), c(char) and s(string). Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes). The specified number of objects of the specified size are printed according to the format.

Defaults for format and size letters are those previously used. The default count is 1. The default address is the following the last thing printed with this command or “print”. (gdb)

In short :

Formats: o – octal d – decimal x – hexadecimal u – unsigned integer s – string t – binary

Units: b – byte h – half w – word g – double word

Example use of ‘x’ command:

“3” words of memory ‘above’ stack pointer can be displayed by:

But why I used “above” here? ‘Ans’: It is homework/assignment for you 🙂

(gdb) x/3xw $sp 0x7fff955a9dc0: 0x00000000 0x00000000 0x0041ecb1 (gdb)

“2” machine instructions from 0x37e78da373/eip

(gdb) x/2i 0x37e78da373 => 0x37e78da373 : mov (%rsp),%rdi 0x37e78da377 : mov %rax,%rdx (gdb)

To display a string you can use: ‘ I selected a random address’, so it may not give a human-readable example string as output.

(gdb) x/s 0x0041ecb1 0x41ecb1: “A\211\307藟\001” (gdb)

I hope this helps.

Find integer array size in C programming

Someone asked me once “how can I get integer array size in C programming and use it in the code?”.Unfortunately there is no defined standard function to get that in C. Rather you could use below method .Suppose you have defined an integer array ( static ) called , int intArray[10];Its size can be derived …

Read more