Sep 02 2010

Grepping for tabs

Category: Uncategorizedwuxicn @ 10:19 AM

在grep中无法转义\t,因此grep直接将\t认为是字母t,而不是我们希望的tab。
要想在grep中用tab,需要直接在终端输入tab,输入方法是:Ctrl+v 然后在按 tab.

Tags: , ,


Aug 13 2010

Linux下写动态链接库.so

Category: Uncategorizedwuxicn @ 9:54 PM

Linux下的动态链接库是.so文件,即:Shared Object,下面是一个简单的例子说明如何写.so以及程序如何动态载入.so中的函数和对象。

testso.h:

#ifndef _TESTSO_H
#define _TESTSO_H
extern "C" {
    int myadd(int a, int b);
    typedef int myadd_t(int, int); // myadd function type
}
#endif // _TESTSO_H

testso.cpp:

#include "testso.h"

extern "C"
int myadd(int a, int  b)
{
    return a + b;
}

编译so:

g++  -shared  -fPIC  -o testso.so testso.cpp

注意,-shared参数和-fPIC参数非常重要:
-shared 告诉gcc要生成的是动态链接库;
-fPIC 告诉gcc生成的生成的代码是非位置依赖的,方面的用于动态链接。

Continue reading “Linux下写动态链接库.so”

Tags: , , ,


Jul 05 2010

OpenSSH公钥授权方式

Category: Uncategorizedwuxicn @ 7:49 PM

用公钥授权(Public key Authentication)的方式可以实现ssh服务器与本机之间的配对,实现不用输入密码联入SSH服务器。

具体做法:

1) 在本机(ssh客户机)生成密钥文件:

client$ cd ~/.ssh
client$ ssh-keygen -q -f id_rsa -t rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

在提示输入密码阶段时(Enter passphrase…),直接Enter,这样以后在本机就不用输入密码了。
这样,在.ssh/目录下就会生产2个文件:id_rsaid_rsa.pub .
注意:保密好这两个文件!

2) 分发公钥文件:

将你的公钥文件 (id_rsa.pub) 分发到你的SSH服务器上:

# Step 1: 将你的id_rsa.pub文件上传到服务器上
client$ scp ~/.ssh/id_rsa.pub user@your.ssh.server:~/.ssh/

# Step 2: 在服务器上安装公钥文件
server$ chmod 700 ~/.ssh
server$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
server$ chmod 600 ~/.ssh/authorized_keys
server$ rm ~/.ssh/id_rsa.pub

注意:这个地方要用>>追加到authorized_keys文件末尾,以免冲到以前的一些公钥授权。

好了,现在大功告成了,enjoy!
PS: 以上方法只保证在OpenSSH服务器上有效,其他SSH服务器不知道。

Tags: ,


Apr 25 2010

[zz]Guide: Function Calling Conventions

Category: Uncategorizedwuxicn @ 9:55 PM

Guide: Function Calling Conventions

GCC follows certain rules in generating and calling its functions. If you are writing portable C or C++ code, you never need to know about these rules. However, if you are writing assembly language or nonportable code that depends on these rules, you need to know what they are. This document attemps to describe them, and gives some examples.

Notes

This document assumes a familiarity with assembly language. The assembler code used here is written in the AT&T syntax, as used by GNU as. If you’re using an Intel-syntax assembler, like nasm, you’ll have to translate appropriately.

What’s described here are GCC’s standard calling conventions. Many can be changed by using options like -mregparm, but that’s outside the scope of this document.

These conventions apply to C. C++ introduces several additional complications (such as class pointers and name mangling), some of which can change between compiler versions. Thus, I suggest that asm functions called from C++ code be declared as extern "C". This will cause C calling conventions to be used.

Writing Assembly-Language Functions

Naming

In DJGPP, a function’s assembly-language name is the same as its C name, with an underscore (“_“) prepended. Thus, the C function foo would be named _foo in assembly language. (This is in fact true for all symbol names, such as variables.) C++ has some much more complicated rules.

Registers

GCC requires that some registers not change across a function call. If you want to use these registers in an assembly function, you must save and restore their values. They are:

  • %ebx
  • %esi
  • %edi
  • %ebp
  • The segment registers %ds%es and %ss

Other registers are available for your use (though some have other special uses; read on).

  • Integers (of any size up to 32 bits) and pointers are returned in the %eax register.
  • Floating point values are returned in the 387 top-of-stack register, st(0).
  • Return values of type long long int are returned in %edx:%eax (the most significant word in %edx and the least significant in %eax).
  • Returning a structure is complicated and rarely useful; try to avoid it. (Note that this is different from returning a pointer to a structure.)

If your function returns void (e.g. no value), the contents of these registers are not used.

          Last argument
	  ...
4(%esp)	  First argument
(%esp)    Return address

  • Integers up to 32 bits and pointers are pushed as a single longword.
  • long long int is pushed as two longwords; the least significant is pushed last (and so is located first in memory).
  • float and double are pushed as a double-precision value, occupying 8 bytes.
  • long double is pushed as an extended-precision value followed by 2 bytes of padding, totalling 12 bytes.
  • As before, structures are more complicated and best avoided.

These rules also apply to functions which take a variable number of arguments (like printf). As with any variadic function, the function must find its own way of determining how many arguments were actually passed (usually based on one of the required args).

The stack below the return address is available for temporary storage, but be sure to decrement %esp appropriately. Memory below %esp may be overwritten asynchronously, by interrupt handlers and such. Restore its value when exiting, so that the return works correctly. You may also push and pop at will.

You may modify your arguments in place if you wish; they will not be reused by the caller. Do not, however, attempt to pop them; the caller handles this.

Calling C Functions From Assembly Language

An assembly language function may wish to call a function written in C, either your own or one from the standard library. The same rules already explained apply; you just see them from the other side.

First, you push the function’s arguments (if any) onto the stack, last argument first. See above for the formats used. (Floating point values are usually most easily handled by making space on the stack and then executing a store instruction; i.e. subl $8,%esp; fstpl (%esp).)

Use a simple call instruction to call the function.

You are responsible for removing the arguments you have pushed. They may have changed, so you may not reuse them. You need not, however, discard them at once; it may be more convenient when calling several functions to leave the arguments on the stack and pop them all together at the end. addl n,%esp is an efficient way to do this. It may also be convenient in this case to use %ebp as a frame pointer, since it need not change all the time. (The C compiler does this.)

The return value may be found as detailed above.

Expect the registers %eax%ecx, and %edx, as well as the floating-point stack, to have changed. Standard library functions may modify the %gs register, and the_far* functions may modify %fs. Other registers will be preserved.

Conclusion

These are the basic calling conventions used by GCC; however, there are special cases, optional modifications, etc. that can apply in situations not covered here. In this case, gcc -S is your best friend – from assembly output, you can usually figure out the rules. Also helpful is the GCC source: see i386.h and i386.md inconfig/i386. They are well commented.

Examples

(略,见原文)

原文:http://www.delorie.com/djgpp/doc/ug/asm/calling.html

Tags: , ,


Apr 21 2010

使用SSH隧道翻墙!

Category: Uncategorizedwuxicn @ 4:38 PM
使用SSH隧道翻墙你必须有一个国外可以通过SSH登录的空间。

做法很简单(感谢Sprayfly):
1. 将下面内容存成文件tunnel.sh:

#!/bin/bash
# --------------------------------------
#
#     Title: SSH HTTP Proxy Script
#    Author: Jonathan Lumb
#     Email: jonolumb (at) gmail (dot) com
#  Homepage: http://sprayfly.com
#      File: tunnel.sh
#   Created: July 05, 2009
#
#   Purpose: Establishes/closes a secure HTTP proxy
#
# --------------------------------------
########### Setup SSH Proxy #############
# You will need to have your server setup to run with public and private keys
export SSH_HOST=username@host
############ End of Setup #############

if [ ! -f /tmp/.tunnel ]
then
    echo "创建 SSH 隧道"
    ssh -f -D 9999 $SSH_HOST "if [ -f ~/.tunnel ]; then rm ~/.tunnel; fi; while [ ! -f ~/.tunnel ]; do echo > /dev/null; done" &
    touch /tmp/.tunnel
else
    echo "关闭 SSH 隧道"
    ssh $SSH_HOST "touch ~/.tunnel"
    rm /tmp/.tunnel
fi

exit

然后给这个文件增加执行权限:chmod +x tunnel.sh

2. 建立SSH的RSA授权文件:ssh-keygen -t rsa注意:密码留空。
这样在本地的 ~/.ssh/ 下就会产生两个文件 id_rsaid_rsa.pub,将这两个文件的访问权限设置为0700:chmod 0700 id_rsa id_rsa.pub
然后将密钥授权文件id_rsa.pub文件传到远程的~/.ssh/ 下,命名为:authorized_keys,操作:
scp id_rsa.pub username@host.com:~/.ssh/authorized_keys

3. 做完上面两步后就可以开启你的SSH tunnel了:
开启/关闭SSH隧道:./tunnel.sh
然后在浏览器中设置socks代理(http代理必须留空):127.0.0.1端口:9999

补充说明一点:要关闭ssh隧道还可以在远程的 $HOME 目录下建立一个 .tunnel 文件,这样本地ssh隧道就会自动关闭了,建立方法:touch .tunnel

Tags: , , ,


Apr 13 2010

ANSI Escape Sequences (ANSI Escape codes)

Category: Uncategorizedwuxicn @ 1:03 PM

ANSI Escape Sequences 定义了控制终端(Terminal)中的颜色显示、光标位置、键盘重映射的功能。直接向终端发送特定的ASCII字符串就能对特定控制功能进行修改。

所有控制序列都是以 ESC键 的ASCII码字符 和 [ 字符开始的,即: ESC [
ESC键 的 ASCII 码是 0x1b (oct 033),并且 [ 字符的ASCII是 0x5b (oct 0133),所以所有控制序列开头2字节都是: 0x1b 0x5b
我们可以用 echo -e 命令来进行测试(注意,必须带 -e 参数,否则echo不会将字符进行转义),ESC 字符可以写成 \x1b 或者 \033,
如: echo -e "\x1b[1;31mHello world\x1b[0m" 将会输出 红色粗体的:Hello world

控制序列的格式是这样的:

ESC [ ... Char

其中:
ESC是:ESC键 的 ASCII 字符:\x1b 或者 \033.
... Char是:以一个字符结尾的控制串,结尾字符区分大小写,可以是 H、f、m等,具体字符所表示的控制意义与格式见表1. [1]

Further reading: [3] [4]

 

References:

[1] ASCII Table: ANSI Escape sequences. http://ascii-table.com/ansi-escape-sequences.php
[2] The 256 color mode of xterm. http://www.frexx.de/xterm-256-notes/
[3] Wikipedia: ANSI escape cod. http://en.wikipedia.org/wiki/ANSI_escape_code
[4] Xterm Control Sequences. http://www.xfree86.org/current/ctlseqs.html
Continue reading “ANSI Escape Sequences (ANSI Escape codes)”

Tags: ,


Mar 31 2010

Make your own boot-up img with “dd”

Category: Uncategorizedwuxicn @ 10:29 PM

Assume that you have a boot file ‘boot.bin’, then you can use it to create your own boot-up img(disk) with “dd” command under linux(or BSD).

the only thing you need to do is to open a terminal and type:

dd if=boot.bin of=boot.img bs=512 count=1

done!

or if you want a boot-up floppy disk img:

dd if=boot.bin of=boot.img bs=512 count=1
dd if=/dev/zero of=boot.img seek=1 bs=512 count=2879

then you have a 1.44M disk img.

Tags:


Feb 10 2010

HipHop for PHP

Category: Uncategorizedwuxicn @ 10:35 AM

Facebook公布了他提高PHP性能的法宝——HipHop for PHP编译器。HipHop通过将PHP代码编译成C++代码,然后再调用g++对其进行编译,大大提高了程序的性能。

项目负责人赵海平(北大1987届遗传与分子生物专业,普林斯顿计算机科学博士)的话说,HipHop项目对 Facebook影响巨大。它目前已经支撑了Facebook 90%的Web流量。由于HipHop,Facebook Web服务器上的CPU使用平均减少了50%,从而大大减少了服务器的需求。

现在Facebook将其开源,以推动PHP开发大型复杂网站。
这里是官方博客:http://developers.facebook.com/news.php?story=358blog=1 (注:国内的局域网无法访问,河蟹的局域网又一次阻止了我们学习新的技术!)

HipHop的原理简单说明

我个人认为,PHP本身设计之初就是非常类似C的语法的(虽然还有一些动态语言的语法成分,但大多数语法和C还是非常像的),这点对转换带来了方便和可能。最大的一个难点问题就是,由于PHP是动态语言,弱类型的语言,变量的类型是可以随时改变的,而C++是强类型语言,变量的类型是必须事前确定的,如何解决好这个是HipHop的关键问题。

HipHop的做法:(这个是CSDN上别人总结的,写得很简略)
1. 静态分析。收集声明关系和依赖关系等信息。
2. 类型推演。选择最合适的类型,是C++的标量?还是String, Array, classes, Object或者Variant。
3. 代码生成。大部分直接将PHP语句和表达式对应为C++的语句和表达式。
Continue reading “HipHop for PHP”

Tags: , , , ,


Jan 15 2010

linux下的SSH文件传输协议——SCP

Category: Uncategorizedwuxicn @ 9:22 AM

SCP全写是:Secure Copy,是基于SSH协议的文件拷贝方法,可以在本机与远程主机或两个远程主机之间进行文件拷贝。SCP的实现需要通过SCP协议以及SCP程序。[1]

SCP命令的基本用法:

scp user1@host1:src-files...  user2@host2:taget-files...

如果源或者目标是本机,user和host可省,例如从本机考文件到目标机的test/目录:

scp *.txt wuxi@psdev-64:~/test/

参考文献:

[1] Secure copy on Wikipedia: http://en.wikipedia.org/wiki/Secure_copy

Tags:


Jan 09 2010

在公共账号的服务器上创建自己的工作环境

Category: Uncategorizedwuxicn @ 5:02 PM

在公司,跑程序需要在测试服务器上,而测试服务器是用的公共账号,因此我们自己不能随便改服务器上的设置(比如.bashrc和.vimrc等),否则会影响到其他人,实在不方便。

所以我写了一个脚本,将公共账户临时变为我个人的账户,这样就可以随意的修改配置了,缺点就是每次登录都运行一遍脚本。

方法很简单:
Continue reading “在公共账号的服务器上创建自己的工作环境”

Tags: ,


Next Page »