Archive for unix

Find Machine MAC Address

What is a MAC Address

* MAC stands for Media Access Control.
* It's a unique number assigned to each network interface card (NIC).
* Also called physical address, hardware address, network adapter address
* Used in media access control protocol sublayer.

How to Find a Machine MAC Address

Most Unix Machines

* /sbin/ifconfig -a

 
eth0      Link encap:Ethernet  HWaddr 00:30:A6:C0:1F:C0
eth1      Link encap:Ethernet  HWaddr 00:30:B6:C0:F7:FA
 

Windows Machines

* ipconfig /all

 
Ethernet adapter Local Area Connection:
 
        Media State . . . . . . . . . . . : Media disconnected
        Description . . . . . . . . . . . : Broadcom NetXtreme 57xx Gigabit Cont
roller
        Physical Address. . . . . . . . . : 00-1F-23-1D-00-3D
 
Ethernet adapter Wireless Network Connection:
 
        Connection-specific DNS Suffix  . : home
        Description . . . . . . . . . . . : Intel(R) PRO/Wireless 3945ABG Networ
k Connection
        Physical Address. . . . . . . . . : 00-1F-AE-23-28-67
 

Reference

* http://en.wikipedia.org/wiki/MAC_address
* http://www.coffer.com/mac_info/locate-unix.html

Bash References

http://tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.linuxconfig.org/Bash_scripting_Tutorial

Setup RPMForge on RHEL 4

 
# For RHEL 4
rpm -Uhv http://apt.sw.be/redhat/el4/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el4.rf.i386.rpm
# For RHEL 5
rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
yum update
 

References

http://dag.wieers.com/rpm/FAQ.php#B

GCC

Environment Variables

 
export C_INCLUDE_PATH=/opt/gdbm-1.8.3/include
export CPLUS_INCLUDE_PATH=/opt/gdbm-1.8.3/include
export LIBRARY_PATH=/opt/gdbm-1.8.3/lib
 

Include Path Search order

* Command line options (-I) from left to right
* Env variables (C_INCLUDE_PATH, CPLUS_INCLUDE_PATH)
* Standard default directories
- /usr/local/include
- /usr/include

Library Path Search order

* Command line options (-L) from left to right
* Env variables (LIBRARY_PATH)
* Standard default directories
- /usr/local/lib
- /usr/lib

Extended Search Path

* Multiple -I or -L entries. For example, -I. -I/opt/gdbm-1.8.3/include -I/net/include -L. -L/opt/gdbm-1.8.3/lib -L/net/lib .....
* Use colon as separator in env variables. For example,
C_INCLUDE_PATH=.:/opt/gdbm-1.8.3/include:/net/include
LIBRARY_PATH=.:/opt/gdbm-1.8.3/lib:/net/lib

Shared Library Path

LD_LIBRARY_PATH=/opt/gdbm-1.8.3/lib:/opt/gtk-1.4/lib
export LD_LIBRARY_PATH

ANSI/ISO Compliance

# Use -ansi
gcc -Wall -ansi pi.c

# Use -pedantic AND -ansi for strict ANSI/ISO compliance
gcc -Wall -ansi -pedantic gnuarray.c

Other Standards

-std=c89 or -std=iso9899:1990
-std=iso9899:199409
-std=c99 or -std=iso9899:1999

Recommended warning options

gcc -ansi -pedantic -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings

C Preprocessor (CPP)

* Expands macros before source files are compiled.
* Preprocess without compiling: -save-temps

 
# Use -E options
gcc -E hello.c
 
# Use -save-temps options
gcc -c -save-temps hello.c
 

Define macros

 
#ifdef TEST
  printf ("Test mode\n");
#endif
 

* Compile with gcc -Wall -DTEST dtest.c -o dtest will print "Test mode".
* Define in source files or header files

#define TEST "Hello, World!"

* Print predefined macros:

cpp -dM /dev/null

* Macro with values

 
printf ("Value of NUM is %d\n", NUM);
gcc -Wall -DNUM=100 dtestval.c
 

Debugging Core Dump

 
# Compile with -g option
gcc -Wall -g null.c -o null
 
# Set core dump file limit
ulimit -c unlimited
 
# Use gdb
gdb null core
(gdb) backtrace
(gdb) break main
(gdb) run
(gdb) step
(gdb) set variable p = malloc(sizeof(int))
(gdb) set variable *p = 255
(gdb) step
(gdb) step
(gdb) finish
(gdb) continue
 

Profiling

* Compile and link with -pg option

 
gcc -Wall -c -pg collatz.c
gcc -Wall -pg collatz.o
 

* Execute

 
./a.out
 

* Profile

 
gprof a.out
 

Compile C++

 
g++ -Wall hello.cc -o hello
 
# Compile for a specific CPU. Not portable!
g++ -Wall -march=pentium4 hello.cc -o hello
 

Debug/Kill a Hanged Program

 
# Compile with -g option
gcc -Wall -g loop.c
 
# Run it
./a.out
 
# Find pid
ps -x
 
# Attach gdb
gdb a.out
(gdb) attach 27189
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb)
 

Create a Library with GNU Archiver

 
# Compile. No link (-c)
gcc -Wall -c hello_fn.c
gcc -Wall -c hello_fn.c
 
# Archive. Create and replace (cr)
ar cr libhello.a hello_fn.o bye_fn.o
 
# List content
ar t libhello.a
 
# Use lib
gcc -Wall hellolib.c libhello.a -o hellolib
# or -L. (include current dir) -lhello (shortcut library link option)
gcc -L. hellolib.c -lhello -o hellolib
 

C++ Standard Library Template

* libstdc++

Providing Own Template

* Follow "Inclusion Compilation Model": place template definition in header files.

How Compiler Works

Overview

* Preprocessing: expands macros

 
cpp hello.c > hello.i
 

* Compilation: source code to assembly code

 
gcc -Wall -S hello.i
 

* Assembly: assembly code to machine code

 
as hello.s -o hello.o
 

* Linking: create final executable

 
ld
  -dynamic-linker \
  /lib/ld-linux.so.2 \
  /usr/lib/crt1.o \
  /usr/lib/crti.o \
  /usr/lib/gcc-lib/i686/3.3.1/crtbegin.o \
  -L/usr/lib/gcc-lib/i686/3.3.1 \
  hello.o \
  -lgcc \
  -lgcc_eh \
  -lc \
  -lgcc \
  -lgcc_eh \
  /usr/lib/gcc-lib/i686/3.3.1/crtend.o \
  /usr/lib/crtn.o
 

Examine Compiled Files

* file

$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped

Examine Symbol Table

* nm
- T: defined
- U: undefined

Finding Dynamically Linked Libraries

* ldd

 ldd hello
        linux-gate.so.1 =>  (0x00362000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00169000)
        libm.so.6 => /lib/libm.so.6 (0x0077c000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00101000)
        libc.so.6 => /lib/libc.so.6 (0x0063a000)
        /lib/ld-linux.so.2 (0x0061d000)

References

* http://www.network-theory.co.uk/gcc/intro/