博文

目前显示的是 三月, 2011的博文

关于setjmp和longjmp

前段时间看C专家编程遇到setjmp和longjmp,没有特别在意.这两天看深入理解计算机系统,再次提到setjmp和longjmp.觉得很巧妙,写个小程序测试一下其特性:   1 //longjmp每一次都跳到setjmp的地方,正所谓是保存了当时的堆栈情况   2 //如此看来,setjmp和longjmp的配对,和fork()函数有可比性:   3 //fork()一个函数执行两次,返回两个不同的值,一个是0,一个是subprocess id.   4 //而,longjmp则跳转到setjmp的位置,setjmp返回的值第一次是0,以后就由longjmp的参数决定   5   6 #include <stdio.h>   7 #include <setjmp.h>   8 #include <stdlib.h>   9  10 sigjmp_buf buf;  11  12 void fun()  13 {  14         printf("%s..........",__func__);  15         longjmp(buf, 2);  16         exit(0);  17 }  18 void bar()  19 {  20         printf("%s\n",__func__);  21         exit(0);  22 }  23  24 void main()  25 {  26  27         int n;  28         printf("Starting.........");  29         n=setjmp(buf);//第一次返回为0,下一次调用的情景是遇到longjmp,返回值为longjmp的第二个参数  30         printf("Restarting.........");  31         if(n ==1)  32                 fun();  33         if(n ==2 )  34                 bar();  3

在Android Console中添加Busybox和Bash

整个过程分两部分: 第一部分:将busybox 和 bash 通过adb push 推到/data目录下 第二部分:更改init.rc,将PATH环境写入ramdisk.img中 第一部分: 下载bash和busybox,然后按照下面的步骤,step by step:(由于bash和busybox的链接大部分已经失效,我手动编译了两个,作为附件放在页面底部) #adb shell mkdir /data/busybox #adb push busybox /data/busyobx #adb push bash /data/busybox //adb shell,进入android #cd /data/busyobx #chmod +x busybox bash #./busybox --install //将程序安装在当前目录下 #export PATH=/data/busybox:$PATH 至此,已经可以使用busybox和bash了。但是很麻烦,每次都需要在前面加上busybox。 因此,最好是将环境变量直接在初始化的时候就指定,因此需要更改ramdisk.img中的init.rc 第二部分 更改init.rc 1)将ramdisk.img复制其他目录,名称改为ramdisk.img.gz,解压 #gunzip ramdisk.img.gz //新建一个文件夹ramdisk,进入 #cpio -i -F ../ramdisk.img 这时,可到ramdisk中看看去~ 2)修改init.rc,在PATH中加上busybox 路径 3)重新打包成镜像,并使用新镜像启动emulator #cpio -i -t -F ../ramdisk.img > list #cpio -o -H newc -O rd_busybox.img < list //使用 -ramdisk 参数,指定所使用的镜像文件 --over--- bash: https://docs.google.com/uc?id=0ByKC30p2j_DDMmM3Zjk5YzItM2U2NC00NGZkLTk4MGYtMmU3ZDgyNTY0MWUz&export=download&hl=zh_CN busybox: 这里面有一堆  http://www.busybox.net

在ubuntu中通过ndiswrapper使用无线网卡

首先,科普一下ndiswrapper: NDISwrapper is a free software driver wrapper that enables the use of Windows XP drivers for network devices ( PCI cards , USB modems , and routers ), on Unix-like operating systems. NDISwrapper works by implementing the Windows kernel and NDIS APIs , and dynamically linking the Windows drivers to this implementation. It therefore works only on systems based on the architectures supported by Windows, namely IA-32 or x86-64 . 对于那些厂商提供Linux驱动的网络硬件,就不许要这个ndiswrapper啦。我使用的是NETGEAR,是老美生产的无线网卡,但不提共LINUX的驱动。具体型号是NETGEAR WNA3100,之前在Ubuntu10.10上,使用自编译的ndswripper,老连不上。但是使用debian可以。 今天,由于Debian老出问题,而且ubuntu10.10也推出了中国定制版,于是,便安装了Ubuntu。在图书馆,都使用无线的网络。因此,要安装包,必须先到xp下面下载后,在重启进入Ubuntu安装,相当麻烦。 由于NETWEAR官方没有出LINUX的驱动,因此需采用ndiswrapper这个工具来加载windows驱动。在ubuntu中,已经自带了ndiswrapper程序,位于/usr/sbin/ndiswrapper-1.9. 具体使用的方法,可以查看Help. 先ndiswrapper-1.9 -i bcmwlhigh5.inf.inf #安装windows driver. 然后ndiswrapper-1.9 -m #将配置信息写到modprobe中。 然后sudo modprobe ndiswrapper #将模块插入到KERNEL中。 这个时候,基本上就OK了

在ubuntu中使用无线网卡

首先,科普一下ndiswrapper: NDISwrapper is a free software driver wrapper that enables the use of Windows XP drivers for network devices (PCI cards, USB modems, and routers), on Unix-like operating systems. NDISwrapper works by implementing the Windows kernel and NDIS APIs, and dynamically linking the Windows drivers to this implementation. It therefore works only on systems based on the architectures supported by Windows, namely IA-32 or x86-64. 对于那些厂商提供Linux驱动的网络硬件,就不许要这个ndiswrapper啦。我使用的是NETGEAR,是老美生产的无线网卡,但不提共LINUX的驱动。具体型号是NETGEAR WNA3100,之前在Ubuntu10.10上,使用自编译的ndswripper,老连不上。但是使用debian可以。 今天,由于Debian老出问题,而且ubuntu10.10也推出了中国定制版,于是,便安装了Ubuntu。在图书馆,都使用无线的网络。因此,要安装包,必须先到xp下面下载后,在重启进入Ubuntu安装,相当麻烦。 由于NETWEAR官方没有出LINUX的驱动,因此需采用ndiswrapper这个工具来加载windows驱动。在ubuntu中,已经自带了ndiswrapper程序,位于/usr/sbin/ndiswrapper-1.9. 具体使用的方法,可以查看Help. 先ndiswrapper-1.9 -i bcmwlhigh5.inf.inf #安装windows driver. 然后ndiswrapper-1.9 -m #将配置信息写到modprobe中。 然后sudo modprobe ndiswrapper #将模块插入到KERNEL中。 这个时候,基本上就OK了

ubuntu 10.10 安装systemtap 补充

前几天,看到yufeng的日志( http://blog.yufeng.info/archives/1098) 有介绍ubuntu10.10安装systemtap的方法,尝试并安装成功( http://haifeng-hust.blogspot.com/2011/03/ubuntu1010systemtap.html)。 但是这种方法安装出来的stap在pass3编译ko的时候经常出问题。因此利用deb包半手工安装。 在 http://ftp.debian.org/debian/pool/main/s/systemtap/ 的页面中有两个版本1.4.1和1.2.5.就目前10.10版本的ubuntu,安装1.4.1时依赖包libdw1需要1.4.9版本,但是对于deb包,最高的就是1.4.7版本。因此只要降低systemtap的版本。 由于在安装1.4.7的时候,没有安装成功,这时候,无论安装什么包都会提示sudo apt-get install -f,非常烦人。除去提示的方法就是用sudo dpkg -r 卸载刚才没有安装成功的包。 到 http://ftp.debian.org/debian/pool/main/s/systemtap/ 上面下载1.2.5版本的三个包systemtap-runtime systemtap-common systemtap.然后在安装systemtap1.2.5的时候,会发生libsqlite依赖包版本过低的问题。因此需要升级libsqlite,升级包地址请猛击: http://packages.debian.org/squeeze/i386/libsqlite3-0/download 然后安装之。这时候,就可以安装systemtap了。 然后测试: sudo tap -ve 'probe begin{printf("Hello world\n");exit();}' sudo tap -ve 'probe kernel.function("sys_open").return{printf("%s\n",execname());exit();}' 另外一个比较纠结的问题是,有时候会莫名其妙重启系统。另外,这个stap的版本较老,一些参数比如

使用VNCServer

图片
通过VNCServer,可以远程使用桌面,而不用老是对着黑漆漆的tty。安装步骤如下: 安装vnc4server haifeng@Tek-life:~$ sudo apt-get install vnc4server 然后配置vncserver haifeng@Tek-life:~$ vim .vnc/xstartup 1 #!/bin/sh 2 3 # Uncomment the following two lines for normal desktop: 4 unset SESSION_MANAGER 5 # exec /etc/X11/xinit/xinitrc 6 7 #[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup 8 #[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources 9 xsetroot -solid grey 10 #vncconfig -iconic & 11 x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" & 12 exec /etc/X11/xinit/xinitrc 13 #x-window-manager & 14 #gnome-session & 好了,配置结束 创建密码 haifeng@Tek-life:~$ vncpasswd Password: Verify: 然后,开个VNC服务 haifeng@Tek-life:~$ vncserver :20 xauth: /var/run/gdm/auth-for-haifeng-OFLDyx/database not writable, changes will be ignored New 'Tek-life:20 (haifeng)' desktop is Tek-life:20 Starting applications specified in /home/haifeng/.vnc/xstartup Log file is /home/haifeng/.vnc/Tek-lif

SystemTAP语法

写得不错,给个链接: http://blog.csdn.net/linyt/archive/2010/02/10/5305332.aspx 读书笔记稍后奉上

在ubuntu10.10中安装Systemtap

SystemTAP是个好东西。属于居家必备的工具之一。这两天安装了一下,其中还蛮折腾的。 安装步骤: sudo apt-get install systemtap Ubuntu Desktop默认没有安装kernel debug info的包,systemtap无法追踪内核信息。查看内核版本 haifeng@Tek-life:~$ uname -a Linux Tek-life 2.6.35-27-generic #48-Ubuntu SMP Tue Feb 22 20:25:29 UTC 2011 i686 GNU/Linux 从这里下载对应的kernel debug info包,安装 sudo dpkg -i linux-image-2.6.35-27-generic-dbgsym_2.6.35-27.48_i386.ddeb 至此内核追踪已经可以执行,但module的信息还需要多做些工作 sudo apt-get install elfutils 将以下内容保存为script.sh,并改变为可执行 1 #!/bin/bash 2 for file in `find /usr/lib/debug -name '*.ko' -print` 3 do 4 buildid=`eu-readelf -n $file| grep Build.ID: | awk '{print $3}'` 5 dir=`echo $buildid | cut -c1-2` 6 fn=`echo $buildid | cut -c3-` 7 rm -rf /usr/lib/debug/.build-id 8 mkdir -p /usr/lib/debug/.build-id/$dir 9 ln -s $file /usr/lib/debug/.build-id/$dir/$fn 10 ln -s $file /usr/lib/debug/.build-id/$dir/${fn}.debug 11 done 基本上就OK了。 测试: haifeng@Tek-life:~$ stap -l 'module("*").function("*&qu

让oklinux跑起来

oklinux是让修改过的linux跑在okl4虚拟机上。网上缺乏针对于oklinux的运行总结。在下不才,折腾好久,才编译运行成功,遂记录之,以方便后来者。 下载地址: okl4:http://wiki.ok-labs.com/downloads/release-3.0/okl4_3.0.tar.gz linux:http://wiki.ok-labs.com/downloads/release-3.0/oklinux_2.6.24.9-patch.4.tar.gz 分别解压okl4和linux.在okl4的目录中建一个指向Linux目录的软连接: $cd okl4_3.0 $ln -s ../linux-2.6.24.9-patch.4 linux 编译源码还需要交叉编译器。交叉编译器的下载地址 EABI:http://wiki.ok-labs.com/downloads/release-3.0/arm-linux-gnueabi-4.2.4.tar.gz OABI:http://www.ertos.nicta.com.au/downloads/tools/arm-linux-3.4.4.tar.gz 将这两个交叉交叉编译器解压后,更改okl4_3.0/tools/toolchains.py中的: gnu_arm_eabi_toolchain . gnu_arm_nptl_toolchain . arm_linux_toolchain,使之指向自己本机中编译器的正确位置。 然后,进入okl4_3.0,运行: ./tools/build.py machine=versatile project=linux wombat=true TOOLCHAIN=gnu_arm_eabi_toolchain pistachio.TOOLCHAIN=gnu_arm_toolchain PYFREEZE=False kdb_serial=True KDB_BREAKIN=False 可能出现的错误以及解决方法: 1.提示arm-linux-g++找不到。 在PATH中加上相应的arm-linux-g++的bin目录就可以了。 export PATH=$PATH:/path/to/bin/ 2.提示phthon版本不正确。 okl4采用python2.4版本。因此,安装python2

汇编代码杂记

一直没有在汇编中写过代码。而且读一些复杂的代码,还有一些吃力。并且自己在ARM体系结构方面的基本功非常薄弱,仅仅是看过杜春雷同学写的那本《ARM体系结构与编程》的教材,偶尔看一下网上同学总结的一些ARM指令的野史笔记。 由于要追踪一下项目中页表的建立流程,由于枝叶比较繁茂,并且深入汇编代码之中,因此,不能充分利用野蛮并且笨拙的Printk,于是便需要在汇编中调用自己写的C函数,然后让流程重新回到汇编中,继续徜徉。 让汇编调用C中的函数,在网上的文章一抓一大把,可是让C中的函数重新返回到汇编中,不着痕迹得继续运行,却没有这样的总结性文章。无奈之下,重新从download.csdn.net中下载到ARM指令集(以前看过,但由于一次误操作,将之永久驱逐出俺的硬盘中),By the way,这个ARM指令集确实不错,有需要的同学到csdn资源中心去自己寻找吧。下面把俺的情况说一下: ENTRY(cpu_arm926_set_pte_ext) mov r3, r1 eor     r3, r3, #L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_WRITE | L_PTE_DIRTY bic     r2, r3, #PTE_SMALL_AP_MASK bic     r2, r2, #PTE_TYPE_MASK orr     r2, r2, #PTE_TYPE_SMALL tst     r3, #L_PTE_USER                 @ User? orrne   r2, r2, #PTE_SMALL_AP_URO_SRW tst     r3, #L_PTE_WRITE | L_PTE_DIRTY  @ Write and Dirty? orreq   r2, r2, #PTE_SMALL_AP_UNO_SRW tst     r3, #L_PTE_PRESENT | L_PTE_YOUNG        @ Present and Young? movne   r2, #0 b  hypervisor_set_pte 我要做的事情是,在hypervisor_set_pte之前调用一个自写的C函数。由于后面还有b hypervisor_set_pte.而hypervisor_set_pte这个函数返回后,返回到lr所指定的地址中