存档

2013年1月 的存档

建立 Lisp 开发环境

2013年1月30日 1 条评论

Slime 是 Emacs 下的一个写 Common Lisp 的一个插件

Download

如果是从源代码安装的话, 需要从 Slime 的官网上下载安装包, 然后进行安装.

Install

在 Gentoo 上安装很简单:

# emerge -av slime

Configuration

(setq inferior-lisp-program "/usr/bin/sbcl")
(add-to-list 'load-path "/usr/share/emacs/site-lisp/slime/")
(require 'slime)
(slime-setup)

装好后, 在我的机器上, 会显示以下错误

  ASDF 2 is not compatible with ASDF-BINARY-LOCATIONS, which you are using. 
ASDF 2 now achieves the same purpose with its builtin ASDF-OUTPUT-TRANSLATIONS, 
which should be easier to configure. Please stop using ASDF-BINARY-LOCATIONS, 
and instead use ASDF-OUTPUT-TRANSLATIONS. See the ASDF manual for details. 
In case you insist on preserving your previous A-B-L configuration, but 
do not know how to achieve the same effect with A-O-T, you may use function 
ASDF:ENABLE-ASDF-BINARY-LOCATIONS-COMPATIBILITY as documented in the manual; 
call that function where you would otherwise have loaded and configured A-B-L..

Gentoo 中 asdf 默认的配置文件(/etc/gentoo-init.lisp)有点问题, 注释掉以下几行 就可以了

(asdf:oos 'asdf:load-op :asdf-binary-locations)
(setf asdf:*centralize-lisp-binaries* t)
(setf asdf:*source-to-target-mappings* '((#p"/usr/lib/sbcl/" nil) (#p"/usr/lib64/sbcl/" nil)))

修改为

;; (asdf:oos 'asdf:load-op :asdf-binary-locations)
;; (setf asdf:*centralize-lisp-binaries* t)
;; (setf asdf:*source-to-target-mappings* '((#p"/usr/lib/sbcl/" nil) (#p"/usr/lib64/sbcl/" nil)))

注意该配置文件是在 /etc/sbclrc 定义的

;;; The following is required if you want source location functions to
;;; work in SLIME, for example.
 
(setf (logical-pathname-translations "SYS")
    '(("SYS:SRC;**;*.*.*" #p"/usr/lib64/sbcl/src/**/*.*")
      ("SYS:CONTRIB;**;*.*.*" #p"/usr/lib64/sbcl/**/*.*")))
 
;;; Setup ASDF2
(load "/etc/gentoo-init.lisp")

Enjoy

使用 M-x slime 就可以启动 slime 了, 启动画面做得很炫

分类: Emacs, Lisp 标签: ,

How to install spice client on Raspberry Pi

2013年1月7日 没有评论

Download source

First, we need to download the source of spice client from spice official website, the latest stable version is 0.14.

$ wget http://spice-space.org/download/gtk/spice-gtk-0.14.tar.bz2

Install dependencies packages

Spice client depends many other packages, e.g. jpeg, gtk, audio … We must install these packages before compiling source.

Furthermore, since we install spice client from source, packages related to compile are also needed, e.g. gcc, autoconf, libtool

The celt package in raspbian’s repository(0.7.1) is newer than Spice client requires(0.5.1), so we have to install this required version from source.

$ sudo apt-get install libogg-dev
$ wget https://launchpadlibrarian.net/59154526/celt_0.5.1.3.orig.tar.gz
$ tar xf celt_0.5.1.3.orig.tar.gz
$ cd celt-0.5.1.3
$ ./configure --prefix=/usr
$ make && sudo make install

All packages we need to install are following:

$ sudo apt-get install build-essential autoconf libtool intltool libspice-protocol-dev libgtk2.0-dev libssl-dev libpulse-dev gobject-introspection libgirepository1.0-dev libjpeg8-dev pulseaudio

Compile and install source

This step, we are ready to compile and install Spice client.

A accelerated X driver for Raspberry Pi are being developped, and a test version has been released. The driver takes advantage of hardware acceleration for display. So spice client could run much smoother.

But by default, spice-gtk uses cairo as display backend, which does not use hardware acceleration. In order to use hardware acceleration, we must configure “–with-gtk=2.0”.

$ tar xf spice-gtk-0.14.tar.bz2
$ cd spice-gtk-0.14
$ ./configure --prefix=/usr --disable-maintainer-mode --disable-static --enable-introspection --without-python --without-sasl --disable-polkit --disable-vala --enable-smartcard=no --with-gtk=2.0
$ make
$ sudo make install

Ok, if no errors occur, we have completed the compilation and installation, it’s time to enjoy it.

Done, connect to server

Now you can connect to your VM using following command:

$ spicy -h spice_server -p port

That’s all, enjoy it!

调试 C 程序的奇淫技巧

2013年1月5日 没有评论

调试时不要开启任何优化选项

如果在编译的时候开启了一些优化选项, 比如 -O2, -O3 什么的, 有的变量/函数会被 gcc 自动优化掉, 比如以下的代码片段:

    for (i = 0; i < msg->num_of_channels; i++) {
        channel_new_t *c;
 
        c = g_new(channel_new_t, 1);
        c->session = g_object_ref(session);
        c->type = msg->channels[i].type;
        c->id = msg->channels[i].id;
        /* no need to explicitely switch to main context, since
           synchronous call is not needed. */
        /* no need to track idle, session is refed */
        g_idle_add((GSourceFunc)_channel_new, c);
    }

当我 break 到这行, 想查看 ichannels[i] 的值时, 开启优化选项和 未开启的区别如下:

// CFLAGS="-g"
1430            c->type = msg->channels[i].type;
(gdb) p i
$9 = <optimized out>
 
// CFLAGS="-g -O0"
1430            c->type = msg->channels[i].type;
(gdb) p i
$1 = 0
(gdb) p msg->channels[0]
$2 = {type = 5 '\005', id = 0 '\000'}

要关闭 gcc 的优化, 通常可以通过以下途径达到:

  • 如果程序的 configure 脚本提供了 “–enable-debug”, 打开它
  • 在正式编译代码的的时候, 定制 CFLAGS 参数, 去掉优化选项(使用 -O0), e.g. make CFLAGS=”-O0 -g”

core dump

默认情况下, 当程序发生 Segmentation fault 时, 内核只是简单的把该进程结束, 不做任何事情, 如果想要保留出错时候的上下文, 可以使用 ulimit -c unlimited 设置 dump core 文件 的大小, 使其不为0(或者用 setrlimit()设置).

这样, 程序 Segmentation fault 的时候, 会在当前目录下(或者 /var/***) 下 生成一个 名为 core(或者 core.*) 的文件, 用 gdb 工具就可以看到程序到底为什么 Segmentation fault 了.

例如下面的程序, 明显的有一个非法内存访问:

#include <stdio.h>
 
int main(int argc, char *argv[])
{
    char *s = NULL;
    sprintf(s, "foo");
    return 0;
}

编译运行后, 会得到一个 名为 core 的文件, 用 gdb 打开, 然后 gdb 会停在程序 出错的地方, 查看 s 的值, 可以看到 s 的值为 0x0, 即传递了一个非法的内存引用给 sprintf() 函数引发了异常.

$ ulimit -c unlimited
$ gcc -o test test.c -g
$ ./test
Segmentation fault (core dumped)
 
$ gdb test core
Core was generated by `./test'.
Program terminated with signal 11, Segmentation fault.
#0  0x0000000000400512 in main (argc=1, argv=0x7fff7eed3048) at test.c:6
6       sprintf(s, "foo");
(gdb) bt
#0  0x0000000000400512 in main (argc=1, argv=0x7fff7eed3048) at test.c:6
(gdb) p s
$1 = 0x0

分类: programming 标签: , ,