Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Nov 11, 2010

讓你的封包,送出時自動加上 VLAN ID

在 Linux 下,使用 VLAN 的方法

1. insert 802.1q kernel module
sudo modprobe 8021q 


2. create a vlan id on NIC by vconfig

sudo vconfig add eth0 111
Added VLAN with VID == 111 to IF -:eth0:-
3. start up eth0.111
sudo ifconfig eth0.111 192.168.1.2 up

4. 測試前的準備:
sudo ifconfig eth0.111 192.168.1.2

eth0.111  Link encap:Ethernet  HWaddr 00:21:70:ff:d7:88  
          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::221:70ff:feff:d788/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:238 (238.0 B)
5. 利用 wireshark 抓出來的封包


此例中,在eth0上加上 vlanid 111,成功之後,會多出一個 eth0.111 的network device,只要是從這個 device 出去的封包,會自動加上 802.1Q 的 header

May 4, 2010

讓 Chrome 支援 Java

  1. mkdir /opt/google/chrome/plugins
  2. cd /opt/google/chrome/plugins
  3. ln -s `dpkg -L sun-java6-bin | grep libnpjp2.so` .

Apr 27, 2010

auto-update tags

工作後,大部份的時間都在 windows 上 Coding,問了蠻多人,大多在 Windows 上寫Coding的人,大多是用 Source Insigne、Ultra Edit。自已本身是用Source Insigne一開始使用就有種,無痛上手的感覺,使用起來,完全沒有門檻,當然也試過 Vim + winCtags,光是要想辨法建tags,就搞了好久,後來就直接放棄。
而自已本身大多還是習慣在Linux的環境下,不外呼,vim + ctags + cscope + Tlist + ........ ,在這多種組合下,用的還算順手,但對於 tag 的 auto update,一直到最近才找到如何處理。

可以利用下面的 vim function,當存檔時,會自助幫你執行 append 新的 tag,但沒辨法利用 set tags 的變數來決定我 tags的位置,目前是直接放棄這個方式。
function! UPDATE_TAGS()
let _f_ = expand("%:p")
let _cmd_ = '"ctags -a -f /dvr/tags --c++-kinds=+p --fields=+iaS --extra=+q " ' . '"' . _f_ . '"'
let _resp = system(_cmd_)
unlet _cmd_
unlet _f_
unlet _resp
endfunction
autocmd BufWritePost *.cpp,*.h,*.c call UPDATE_TAGS()

AutoTag主要是提供一支 autotags.vim的plugin,使用方法很簡單,只要將此plugin放入你.vim/plugin內就可以,而它是使用python來implement auto update的功能,這對只會寫 python 看不懂 vim script在做什麼的我,因該也算是一種好處吧 ,哈哈。

今天,為了試看看,autotags.vim用在大型 project 速度上怎樣,我拿 linux kernel 2.6.31 來測試,做出來的 tags 約 85MB (arch/下只剩 powerpc的目錄),我隨便開啟一個檔案,編輯後存檔,大約花了 5 秒做 auto update的動作(CPU是C2D E6600 @ 2.4G),雖然花了5秒,但沒想像中的那麼慢,或許之後,可以思考如何去切而不是整個都包到同一個 tags 內。

在autotags.vim裡,有一個變數 __maxTagsFileSize 用來限定,tags的大小,預設是 7MB,如果你的 tags 超過這個大小,會直接放棄 update 的動作,當然,可以手動把它設大一點 :)。

除了 ctags,cscope也是一個很好用的 source browsing,支援比ctags更多的功能,但它的 cscope.out ,好像不支援 append 的功能,如要更新,要全部重做一次。Automatically create and update cscope database提供了一組 Hot-Key,來幫忙不用退出vim,"一指"完成。
Hot-Key如下:
nmap :!find . -iname '*.c' -o -iname '*.cpp' -o -iname '*.h' -o -iname '*.hpp' > cscope.files
\ :!cscope -b -i cscope.files -f cscope.out
\ :cs reset

Dec 27, 2009

linux's core dump

core dump 的目地是當你的程式跑到一半當掉(異常中止跳出或freeze)時,我們可從 core 中得到最後掛在那裡,在加上 backtrace 就能快速的找出那裡有 bug :).

先認定 core file size: ulimit -a
如果是 0 的話,我們需要先做一些動作,才有辨法開啟 core dump 的功能.
1. sudo vi /etc/security/limits.conf, 加入這行
* soft core unlimited
2. 開一個新的 terminal,輸入 ulimit -c,此時因該還是0, 利用 ulimit -c unlimited,更改成 unlimited。

設定完 core file size 之後,我使用下面程式來測試 debgu 的效果

/*
* file name: wrong.c
* gcc -g wrong.c -o wrong
*/

#include
#include

void func3()
{
//go to dia;
char *x = 0x0;
*x = 1;
printf("%s", x);
strcpy(x, "This is wrong");
}

void func2(void)
{
func3();
}

void func1(void)
{
func2();
}


int main(void)
{
func1();
return 0;
}

執行結果為:
程式記憶體區段錯誤 (core dumped)
如沒有意外的話,因該可以看到多出了一個 core 的檔案
$file core
core: ELF 32-bit LSB core file Intel 80386, version 1 (SYSV), SVR4-style, from './wrong'
有了 core file 之後,我們就可以利用 gdb 來做 backtrace
$ gdb wrong core
Core was generated by `./wrong'.
Program terminated with signal 11, Segmentation fault.
#0 0x08048264 in func3 () at wrong.c:8

warning: Source file is more recent than executable.
8
我們大概可以知道是停在 func3 裡,透過 print 可知道,發生了什麼事
(gdb) p x
$1 = 0x0
(gdb) p *x
Cannot access memory at address 0x0
另外,最好用的當然是 backtrace
(gdb) where
#0 0x08048264 in func3 () at wrong.c:8
#1 0x080482a4 in func2 () at wrong.c:15
#2 0x080482b1 in func1 () at wrong.c:20
#3 0x080482be in main () at wrong.c:26
(gdb) help where
Print backtrace of all stack frames, or innermost COUNT frames.
With a negative argument, print outermost -COUNT frames.
Use of the 'full' qualifier also prints the values of the local variables.
where 指令,我們能很清楚的知道是從
main -> func1 -> func2 -> func3
另外還有一種情況是,程式掉進 trap,而不會觸發 SIGEGV signal,此時可以利用 xiaosuo 寫的 dumper 讓你所想要的程式觸發 signal 產生 core file。

將 wrong.c 中的 func3 改成

void func3()
{
//go into loop;
while(1){
sleep(1);
}
}

compiler完之後,讓 wrong_loop 於背景執行
./wrong_loop &
[1] 31908
當然,我們都知道,它是不會中止的,此時可以利用 dumper 來中止 wrong_loop 且產生 core file
$ ./dumper 31908 -k
Start injecting(31908)...OK
[1]+ 不合法的命令 (core dumped) ./wrong_loop
$
$ file core
core: ELF 32-bit LSB core file Intel 80386, version 1 (SYSV), SVR4-style, from './wrong_loop'
產生了 core 我們就可利用 GDB 來debug
$ gdb wrong_loop core
.....
Program terminated with signal 4, Illegal instruction.
#0 0xbfe0f4dc in ?? ()
(gdb) where
#0 0xbfe0f4dc in ?? ()
#1 0x08048266 in func3 () at wrong_loop.c:8
#2 0x08048273 in func2 () at wrong_loop.c:14
#3 0x08048280 in func1 () at wrong_loop.c:19
#4 0x0804828d in main () at wrong_loop.c:25
(gdb)

Reference: