Tuesday, November 16, 2010

Explore Linux /proc File System (/proc directories, /proc files)

  • /proc/meminfo – Current utilization of primary memory on the system
  • /proc/misc – This file lists miscellaneous drivers registered on the miscellaneous major device, which is number 10
  • /proc/modules – Displays a list of all modules that have been loaded by the system
  • /proc/mounts – This file provides a quick list of all mounts in use by the system
  • /proc/partitions – Very detailed information on the various partitions currently available to the system
  • /proc/pci – Full listing of every PCI device on your system
  • /proc/stat – Keeps track of a variety of different statistics about the system since it was last restarted
  • /proc/swap – Measures swap space and its utilization
  • /proc/uptime – Contains information about uptime of the system
  • /proc/version – Version of the Linux kernel, gcc, name of the Linux flavor installed.

Explore Linux /proc File System (/proc directories, /proc files)

Wednesday, October 20, 2010

Vim Tips: Go Previos View and Current View

Often in Vim Editor we will move from one place of the code to another part of the code especially when we search for a particular string and on the way of searching we might lose the Previous view or position of the file in view editor.

The following keyboard shortcut is very useful to navigate to the preview view and current view in Vim editor.

CTRL-G  displays your location in the file and the file status
CTRL-O takes you back to older positions or previous view,
CTRL-I  takes you to  newer positions or current view.

Linux Command to Find RAM size

There are many commands to find RAM size using Linux and we will see all the commands in coming day. But for now we will see a command called Free

 fasil@FASIL:~$ free   
        total    used    free   shared  buffers   cached  
 Mem:    1161960  1129788   32172     0   147160   705716  
 -/+ buffers/cache:   276912   885048  
 Swap:   1306616    5576  1301040  
 fasil@FASIL:~$ free -h  
 free: invalid option -- 'h'  
 usage: free [-b|-k|-m|-g] [-l] [-o] [-t] [-s delay] [-c count] [-V]  
  -b,-k,-m,-g show output in bytes, KB, MB, or GB  
  -l show detailed low and high memory statistics  
  -o use old format (no -/+buffers/cache line)  
  -t display total for RAM + swap  
  -s update every [delay] seconds  
  -c update [count] times  
  -V display version information and exit  
 fasil@FASIL:~$ free -m  
        total    used    free   shared  buffers   cached  
 Mem:     1134    1103     31     0    143    689  
 -/+ buffers/cache:    270   

Free command is used to find the RAM size, Swap size and memory statistics

Delete Folders Recursively in Linux

Command to delete a folder or file in linux is so simple, sometime there will be a situation where you need  to delete all files or folders like .o or sometimes there will be hidden folder like .svn and .gitignore present in the Linux . To delete that folders recursively here is the linux command

To recursively delete subdirectories and all files, use the following syntax:

rm -rf /path/to/directory

You can also find all *.svn or *.gitignore or *.o  in many subdirectories and delete them:

The -i option prompt before any removal and the -f option ignore nonexistent files, never prompt for anything.
find . -type f -name "*.svn" -exec rm -i '{}' \;
find . -type f -name "*.gitignore" -exec rm -f '{}' \;

Monday, November 3, 2008

Commands to Retrieve Load, statistics and messages

# top                                # display and update the top cpu processes
# mpstat 1                           # display processors related statistics
# vmstat 2                           # display virtual memory statistics
# iostat 2                           # display I/O statistics (2 s intervals)
# systat -vmstat 1                   # BSD summary of system statistics (1 s intervals)
# systat -tcp 1                      # BSD tcp connections (try also -ip)
# systat -netstat 1                  # BSD active network connections
# systat -ifstat 1                   # BSD network traffic through active interfaces
# systat -iostat 1                   # BSD CPU and and disk throughput
# tail -n 500 /var/log/messages      # Last 500 kernel/syslog messages
# tail /var/log/warn                 # System warnings messages see syslog.conf

Hardware Information Accessing Commands

# cat /proc/cpuinfo                              # CPU model
# cat /proc/meminfo                            # Hardware memory
# grep MemTotal /proc/meminfo            # Display the physical memory
# watch -n1 'cat /proc/interrupts'           # Watch changeable interrupts continuously
# free -m                                                     # Used and free memory (-m for MB)
# cat /proc/devices                                     # Configured devices
# lspci -tv                                                 # Show PCI devices
# lsusb -tv                                              # Show USB devices
# lshal                                                  # Show a list of all devices with their properties
# dmidecode                                          # Show DMI/SMBIOS: hw info from the BIOS

Saturday, November 1, 2008

GCC Tips

View Compilation Plan

gcc -### <the rest of your command line goes here>

The GCC you run is a driver program for a bunch of other programs. With this parameter, gcc will produce (but not actually execute) the commands it would have used to accomplish the task you asked it to do. This way, you can see the gory details of what's going on behind the scenes. What library is being used? What is -mcpu set to? It's all there.

You can pipe this output to a file and execute that to compile a program, making it easy to experiment with tweaks to the linker or assembler.

Reading specs from /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/specs
Configured with: ../configure --prefix=/opt/timesys/toolchains/ppc7xx-linux --mandir=/opt/timesys/toolchains/ppc7xx-linux/share/man --infodir=/opt/timesys/toolchains/ppc7xx-linux/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++ --with-sysroot=/here/workdir/i386-x-ppc7xx/deleteme --disable-libgcj --build=i686-timesys-linux --host=i686-timesys-linux --target=powerpc-linux --program-prefix=ppc7xx-linux-
Thread model: posix
gcc version 3.4.1 20040714 (TimeSys 3.4.1-7)
/opt/timesys/toolchains/ppc7xx-linux/libexec/gcc/powerpc-linux/3.4.1/cc1 -quiet -v -D__unix__ -D__gnu_linux__ -D__linux__ -Dunix -D__unix -Dlinux -D__linux -Asystem=linux -Asystem=unix -Asystem=posix -I/opt/timesys/toolchains/ppc7xx-linux/powerpc-linux/include/nptl file.c -quiet -dumpbase file.c -auxbase file -version -o /tmp/ccShiHn4.s
ignoring nonexistent directory "/here/workdir/i386-x-ppc7xx/deleteme/usr/local/include"
ignoring nonexistent directory "/here/workdir/i386-x-ppc7xx/deleteme/usr/include"
#include "..." search starts here:
#include <...> search starts here:
/opt/timesys/toolchains/ppc7xx-linux/powerpc-linux/include/nptl
/opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/include
/opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/include
End of search list.
GNU C version 3.4.1 20040714 (TimeSys 3.4.1-7) (powerpc-linux)
compiled by GNU C version 3.2.2 20030222 (Red Hat Linux 3.2.2-5).
GGC heuristics: --param ggc-min-expand=47 --param ggc-min-heapsize=32138
/opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/bin/as -mppc -many -V -Qy -o /tmp/ccWeV3a3.o /tmp/ccShiHn4.s
GNU assembler version 2.15.90.0.3 (powerpc-linux) using BFD version 2.15.90.0.3 20040415
/opt/timesys/toolchains/ppc7xx-linux/libexec/gcc/powerpc-linux/3.4.1/collect2 --eh-frame-hdr -V -Qy -L/opt/timesys/toolchains/ppc7xx-linux/powerpc-linux/lib/nptl --rpath-link /opt/timesys/toolchains/ppc7xx-linux/powerpc-linux/lib/tls -m elf32ppclinux -dynamic-linker /lib/ld.so.1 -o file /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/lib/crt1.o /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/lib/crti.o /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/crtbegin.o -L/opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1 -L/opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/lib /tmp/ccWeV3a3.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/crtsavres.o /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/crtend.o /opt/timesys/toolchains/ppc7xx-linux/lib/gcc/powerpc-linux/3.4.1/../../../../powerpc-linux/lib/crtn.o
GNU ld version 2.15.90.0.3 20040415
Supported emulations:
elf32ppclinux
elf32ppc
elf32ppcsim

Pre-Process, Retain Comments

gcc -C -E <file-name.c> -o file

Some engineers love to do coding in macros. The rest of us would like to break their fingers. This command will run the file through the pre-processor, expanding all macros, but retaining all comments. Stick a comment like "LOOK HERE" and search for that so you reduce the amount of time you spend looking for the offending code.

See what Files the Linker is Using

gcc -Wl,-t <parameters>

Displays what files the linker opens in what order. When looking in archive files, the archive file is displayed in para theses, followed by the file in the archive. Very handy when working through a legacy project that depends on files linking in a certain order that suddenly breaks because of a small (probably viewed as not noteworthy) change in a makefile somewhere.

/usr/bin/ld: mode elf_i386
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/../../../crt1.o
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/../../../crti.o
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/crtbegin.o
/tmp/cc37FxnS.o
-lgcc_s (/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/libgcc_s.so)
/lib/libc.so.6
(/usr/lib/libc_nonshared.a)elf-init.oS
-lgcc_s (/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/libgcc_s.so)
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/crtend.o
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/../../../crtn.o

Print Pre-defined Macros

gcc -E -dM - < /dev/null | cut -c 9- | sort

Very handy when porting code. Lets you know if your target processor has some missing defines or if something is different (line __INT_MAX__) that can have interesting effects on your project. Diff the output from the old to the new compiler so you can easily see the differences, makes it easy to spot problems before getting started.

Sample output, from a compiler targeting an ARM processor.

__APCS_32__ 1
__arm__ 1
__ARM_ARCH_4T__ 1
__ARMEL__ 1
__CHAR_BIT__ 8
__CHAR_UNSIGNED__ 1
__DBL_DENORM_MIN__ 4.9406564584124654e-324
__DBL_DIG__ 15
__DBL_EPSILON__ 2.2204460492503131e-16
__DBL_HAS_DENORM__ 1
__DBL_HAS_INFINITY__ 1
__DBL_HAS_QUIET_NAN__ 1
__DBL_MANT_DIG__ 53
__DBL_MAX_10_EXP__ 308
__DBL_MAX__ 1.7976931348623157e+308
__DBL_MAX_EXP__ 1024

Mixed Assembler and Source Output

gcc -g somefile.c -o somefile
objdump -S somefile 

Prints out each line in the program and the corresponding assembly code. Very handy when you're trying to see that the processor is generating the correct code, with the instructions you're expecting. You can also see the effects of optimization, but would recommend doing this for a small amount of code because when the optimization level is high, there's a much lower relationship between line of code and generated assembler.

Here's an example of what objdump produces for a few lines of code:

gpvSharedMemory = shmat(hSharedMemory, NULL, 0);
10000958:       80 7f 00 10     lwz     r3,16(r31)
1000095c:       38 80 00 00     li      r4,0
10000960:       38 a0 00 00     li      r5,0
10000964:       48 01 09 31     bl      10011294 <shmat@plt>
10000968:       7c 60 1b 78     mr      r0,r3
1000096c:       3d 20 10 01     lis     r9,4097
10000970:       90 09 11 d0     stw     r0,4560(r9)
if (errno != 0) {
10000974:       48 01 08 d1     bl      10011244 <__errno_location@plt>
10000978:       7c 60 1b 78     mr      r0,r3
1000097c:       7c 09 03 78     mr      r9,r0
10000980:       80 09 00 00     lwz     r0,0(r9)
10000984:       2f 80 00 00     cmpwi   cr7,r0,0
10000988:       41 9e 00 50     beq-    cr7,100009d8 <main+0x10c>

Specify Language

gcc -x c a-c-source-file.with-a-non-standard-extension -o test.out

Great for legacy projects where where the file extensions don't match with GCC's expectations, while less of a problem since many projects got their start with GCC, this still is an issue with long-running projects that years back, used some other compiler. This stays in effect for the following file on the command line.

List Include File Dependencies

There's a whole family of things around -M. These produce a rule that could be used in a make file, with the included files as dependencies.

gcc -M <file name>

This shows you all includes, even those on the system path. Useful if you're doing porting work or validating if your compiler is working as expected and getting the files from the right place. You'll see something like this for a basic hello world program

hello.o: hello.c /usr/include/stdio.h /usr/include/features.h \
/usr/include/sys/cdefs.h /usr/include/gnu/stubs.h \
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/include/stddef.h \
/usr/include/bits/types.h /usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h /usr/include/libio.h \
/usr/include/_G_config.h /usr/include/wchar.h /usr/include/bits/wchar.h \
/usr/include/gconv.h \
/usr/lib/gcc-lib/i386-redhat-linux/3.3.3/include/stdarg.h \
/usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h
gcc -MM <file name>  

Like -M, but no system files. Great to see if your project is configured and working as expected.

gcc -M -MG <file name>  

The prior -M and -MM commands will stop if header files can be located. The parameter -MG will just produce the dependency list with the missing file. Engineers that have projects that generate header files as part of the build find -MM very handy.

gcc -M -MT '<target>' <file name> 

By default, the target will be the <file name>.o This command will make the default the value of <target>.

If the command was

gcc -M -MT '$(target)' hello.c

You would see

$(target): hello.c 

Symbol Trace

gcc -Wl,-y,printf hello.c

This is very handy when you want to understand the linker is finding a definition of a symbol. Some projects have name collisions or link order dependencies. This lets you see precisely what the linker is doing.

Given a hello world program, you would see output like

/tmp/ccwZx5UV.o: reference to printf
/lib/libc.so.6: definition of printf

The reference is in a temporary file created during the compilation process. If you were linking several object files together explicitly, you would see the name of the object file where printf was referenced.

Saving temporary files

gcc -save-temps hello.c

Saturday, October 25, 2008

Change directory command - cd

cd  - Change Directory - change the current working directory to a specific Folder.

Examples

Move to the sybase folder
$ cd /usr/local/sybase
$ pwd
/usr/local/sybase


Change to another folder
$ cd /var/log
$ pwd
/var/log


Quickly get back
$ cd –
$ pwd
/usr/local/sybase


move up one folder
$ cd ..
$ pwd
/usr/local/


$ cd ../../..
will move three levels backward



Man Pages

Syntax 
      cd [Options] [Directory]

Key
    -P    Do not follow symbolic links
    -L    Follow symbolic links (default)
If directory is not given, the value of the HOME shell variable is used.

If the shell variable CDPATH exists, it is used as a search path.
If directory begins with a slash, CDPATH is not used.

If directory is `-', this will change to the previous directory location (equivalent to $OLDPWD ).

The return status is zero if the directory is successfully changed, non-zero otherwise.