目次
Debian システム上でプログラミングを学ぶ人がパッケージ化されたソースコードを読み込めるようになるための指針を示します。以下はプログラムに関する特記すべきパッケージと対応する文書パッケージです。
オンラインリファレンスは manpages
と manpages-dev
パッケージをインストールした後で "man name
" とタイプすると利用可能です。GNU
ツールのオンラインリファレンスは該当する文書パッケージをインストールした後で "info
program_name
" とタイプすると使えます。一部の GFDL 文書は DFSG に準拠していないと考えられているので
main
アーカイブに加えて contrib
や
non-free
アーカイブを含める必要があるかもしれません。
Please consider to use version control system tools. See 「Git」.
![]() |
警告 |
---|---|
" |
![]() |
注意 |
---|---|
ソースから直接コンパイルしたソフトウエアープログラムは、システムプログラムとかち合わないように、" |
![]() |
ヒント |
---|---|
"99ボトルのビールの歌" 作成のコード例はほとんど全てのプログラム言語に関する理解のための非常に好適です。 |
シェルスクリプトは実行ビットがセットされたテキストファイルで、以下に示すフォーマットのコマンドを含んでいます。
#!/bin/sh ... command lines
最初の行はこのファイル内容を読み実行するシェルインタープリタを指定します。
シェルスクリプトを読むのは Unix 的なシステムがどのように機能しているのかを理解する最良の方法です。ここでは、シェルプログラムに関する指針や心がけを記します。失敗から学ぶために "シェルの失敗" (http://www.greenend.org.uk/rjk/2001/04/shell.html) を参照下さい。
シェル対話モード (「シェルプロンプト」と「Unix 的テキスト処理」参照下さい) と異なり、シェルスクリプトは変数や条件文やループを繁用します。
Many system scripts may be interpreted by any one of POSIX shells (see 表1.13「シェルプログラムのリスト」).
The default non-interactive POSIX shell "/bin/sh
" is a
symlink pointing to /usr/bin/dash
and used by many system
programs.
The default interactive POSIX shell is /usr/bin/bash
.
全ての POSIX シェル間でポータブルとするために bashisms や
zshisms
を使うシェルスクリプトを書くのを避けましょう。checkbashisms
(1) を使うとこれがチェックできます。
表12.1 典型的 bashizms のリスト
推薦: POSIX | 回避すべき: bashism |
---|---|
if [ "$foo" = "$bar" ] ; then … |
if [ "$foo" == "$bar" ] ; then … |
diff -u file.c.orig file.c |
diff -u file.c{.orig,} |
mkdir /foobar /foobaz |
mkdir /foo{bar,baz} |
funcname() { … } |
function funcname() { … } |
8進表記: "\377 " |
16進表記: "\xff " |
"echo
"
コマンドはその実装がシェルビルトインや外部コマンド間で相違しているので次の注意点を守って使わなければいけません。
"-n
" 以外のどのコマンドオプション使用も避けます。
文字列中にエスケープシーケンスはその取扱いに相違があるので使用を避けます。
![]() |
注記 |
---|---|
" |
![]() |
ヒント |
---|---|
出力文字列にエスケープシーケンスを埋め込む必要がある場合には、" |
特別なシェルパラメーターがシェルスクリプト中ではよく使われます。
表12.2 シェル変数のリスト
シェル変数 | 変数値 |
---|---|
$0 |
シェルまたはシェルスクリプトの名前 |
$1 |
最初 (1番目) のシェル引数 |
$9 |
9番目のシェル引数 |
$# |
シェル引数の数 |
"$*" |
"$1 $2 $3 $4 … " |
"$@" |
"$1" "$2" "$3" "$4" … |
$? |
最新のコマンドの終了状態 |
$$ |
このシェルスクリプトの PID |
$! |
最近スタートしたバックグラウンドジョブの PID |
覚えておくべき基本的なパラメーター展開を次に記します。
表12.3 シェル変数展開のリスト
パラメーター式形 | var が設定されているときの値 |
var が設定されていないときの値 |
---|---|---|
${var:-string} |
"$var " |
"string " |
${var:+string} |
"string " |
"null " |
${var:=string} |
"$var " |
"string " (合わせて "var=string " を実行) |
${var:?string} |
"$var " |
"string " をstderr に出力
(エラーとともに exit する) |
ここで、これら全てのオペレーターのコロン ":
" は実際はオプションです。
":
" 付き = 演算子は存在と非ヌル文字列をテストします
":
" 無し = 演算子は存在のみをテストします
表12.4 重要なシェル変数置換のリスト
パラメーター置換形 | 結果 |
---|---|
${var%suffix} |
最短のサフィックスパターンを削除 |
${var%%suffix} |
最長のサフィックスパターンを削除 |
${var#prefix} |
最短のプレフィックスパターンを削除 |
${var##prefix} |
最長のプレフィックスパターンを削除 |
各コマンドは条件式に使えるエグジットステイタスを返します。
成功: 0 ("真")
エラー: 非0 ("偽")
![]() |
注記 |
---|---|
シェル条件文の文脈において "0" は "真" を意味しますが、C 条件文の文脈では "0" は "偽" を意味します。 |
![]() |
注記 |
---|---|
" |
覚えておくべき基本的な条件文の慣用句は次です。
"command &&
成功したらこのcommandも実行 || true
"
"command ||
もしcommandが成功しないとこのコマンドも実行 || true
"
次のような複数行のスクリプト断片
if [ conditional_expression ]; then if_success_run_this_command else if_not_success_run_this_command fi
ここで、シェルが "-e
" フラグ付きで起動された際にシェルスクリプトがこの行で誤って exit
しないようにするために、末尾の "|| true
" が必要です。
表12.5 条件式中のファイル比較演算子
式 | 論理真を返す条件 |
---|---|
-e file |
file が存在する |
-d file |
file が存在しディレクトリーである |
-f file |
file が存在し通常ファイルである |
-w file |
file が存在し書込み可能である |
-x file |
file が存在し実行可能である |
file1 -nt file2 |
file1 が file2 よりも新しい (変更) |
file1 -ot file2 |
file1 が file2 よりも古い (変更) |
file1 -ef file2 |
file1 と file2 は同デバイス上の同 inode 番号 |
表12.6 条件式中での文字列比較演算子のリスト
式 | 論理真を返す条件 |
---|---|
-z str |
str の長さがゼロ |
-n str |
str の長さが非ゼロ |
str1 = str2 |
str1 と str2 は等しい |
str1 != str2 |
str1 と str2 は等しく無い |
str1 < str2 |
str1 のソート順が str2 より前 (ロケール依存) |
str1 > str2 |
str1 のソート順が str2 より後 (ロケール依存) |
条件式中の算術整数比較演算子は "-eq
"
と "-ne
" と "-lt
" と
"-le
" と "-gt
" と
"-ge
" です。
POSIX シェル中で使われるループの慣用句があります。
"for x in foo1 foo2 … ; do コマンド ; done
" は "foo1
foo2 …
" リストの項目を変数 "x
" に代入し
"コマンド
" を実行してループします。
"while 条件 ; do コマンド ; done
" は"条件
"
が真の場合 "コマンド
" を繰り返します。
"until 条件 ; do コマンド ; done
" は"条件
"
が真でない場合 "コマンド
" を繰り返します。
"break
" によってループから脱出できます。
"continue
" によって次のループ初めに戻りループを再開します。
![]() |
ヒント |
---|---|
C 言語のような数字の繰り返しは " |
![]() |
ヒント |
---|---|
「ファイルに関してループしながらコマンドを反復実行」を参照下さい。 |
Some popular environment variables for the normal shell command prompt may not be available under the execution environment of your script.
For "$USER
", use "$(id -un)
"
For "$UID
", use "$(id -u)
"
For "$HOME
", use "$(getent passwd "$(id -u)"|cut
-d ":" -f 6)
" (this works also on 「最新の集中システム管理」)
シェルはおおよそ次のシーケンスでスクリプトを処理します。
シェルは1行読み込みます。
シェルは、もし "…"
や '…'
の中なら、行の一部を1つのトークンとしてグループします。
シェルは1行を次のによってトークンに分割します。
空白: space tab
newline
Metacharacters: | ; & ( )
"…"
や '…'
の中でない場合、シェルは各トークンを予約語に対してチェックしその挙動を調整します。
予約語: if then elif else fi for in
while unless do done case esac
"…"
や '…'
の中でない場合、シェルはエイリアスを展開します。
"…"
や'…'
の中でない場合、シェルはティルダを展開します。
"~
" → 現ユーザーのホームディレクトリー
"~user
" →
user
のホームディレクトリー
'…'
の中でない場合、シェルは パラメーター"をその値に展開します。
パラメーター: "$PARAMETER
"
or "${PARAMETER}
"
'…'
の中でない場合、シェルは コマンド置換を展開します。
"$( command )
" → "command
" の出力
"` command `
" → "command
" の出力
"…"
や '…'
の中でない場合、シェルは パス名のグロブを展開します。
*
→ あらゆる文字
?
→ 1文字
[…]
→ "…
" 中の1つ
シェルはコマンドを次から検索して実行します。
関数定義
ビルトインコマンド
"$PATH
" 中の実行ファイル
シェルは次行に進みこのプロセスを一番上から順に反復します。
ダブルクォート中のシングルクォートに効果はありません。
シェル環境中で "set -x
" を実行したり、シェルを "-x
"
オプションで起動すると、シェルは実行するコマンドを全てプリントするようになります。これはデバグをするのに非常に便利です。
Debian システム上でできるだけポータブルなシェルプログラムとするには、ユーティリティープログラムを essential パッケージで提供されるプログラムだけに制約するのが賢明です。
"aptitude search ~E
" はessential (必須) パッケージをリストします。
"dpkg -L パッケージ名 |grep '/man/man.*/'
"
は パッケージ名
パッケージによって提供されるコマンドのマンページをリストします。
表12.7 シェルスクリプト用の小さなユーティリティープログラムを含むパッケージのリスト
パッケージ | ポプコン | サイズ | 説明 |
---|---|---|---|
dash
|
V:894, I:995 | 191 | small and fast POSIX-compliant shell for sh |
coreutils
|
V:908, I:999 | 18062 | GNU コアユーティリティー |
grep
|
V:799, I:999 | 1245 | GNU grep , egrep and
fgrep |
sed
|
V:792, I:999 | 987 | GNU sed |
mawk
|
V:391, I:997 | 263 | 小さく高速な awk |
debianutils
|
V:919, I:999 | 243 | Debian 特有の雑多なユーティリティー |
bsdutils
|
V:610, I:999 | 355 | 4.4BSD-Lite 由来の基本ユーティリティー |
bsdextrautils
|
V:454, I:548 | 337 | 4.4BSD-Lite 由来の追加のユーティリティー |
moreutils
|
V:14, I:39 | 244 | 追加の Unix ユーティリティー |
![]() |
ヒント |
---|---|
Although |
See 「Unix 的テキスト処理」 for examples.
表12.8 List of interpreter related packages
パッケージ | ポプコン | サイズ | 文書 |
---|---|---|---|
dash
|
V:894, I:995 | 191 | sh: small and fast POSIX-compliant shell for
sh |
bash
|
V:821, I:999 | 7163 | sh: "info bash " provided by
bash-doc |
mawk
|
V:391, I:997 | 263 | AWK: small and fast awk |
gawk
|
V:313, I:402 | 2456 | AWK: "info gawk " provided by
gawk-doc |
perl
|
V:644, I:990 | 669 | Perl: perl (1) and html pages
provided by perl-doc and perl-doc-html |
libterm-readline-gnu-perl
|
V:2, I:30 | 379 | Perl extension for the GNU ReadLine/History Library:
perlsh (1) |
libreply-perl
|
V:0, I:0 | 171 | PerlのREPL: reply (1) |
libdevel-repl-perl
|
V:0, I:0 | 237 | PerlのREPL: re.pl (1) |
python3
|
V:726, I:931 | 80 | Python: python-doc が提供する
python3 (1) と html ページ |
tcl
|
V:28, I:268 | 22 | Tcl: tcl-doc が提供する
tcl (3) と詳細なマンページ |
tk
|
V:22, I:261 | 22 | Tk: tk-doc が提供する
tk (3) と詳細なマンページ |
ruby
|
V:112, I:254 | 29 | Ruby: ruby (1),
erb (1), irb (1),
rdoc (1), ri (1) |
When you wish to automate a task on Debian, you should script it with an interpreted language first. The guide line for the choice of the interpreted language is:
Use dash
, if the task is a simple one which combines CLI
programs with a shell program.
Use python3
, if the task isn't a simple one and you are
writing it from scratch.
Use perl
, tcl
,
ruby
, ... if there is an existing code using one of these
languages on Debian which needs to be touched up to do the task.
If the resulting code is too slow, you can rewrite only the critical portion for the execution speed in a compiled language and call it from the interpreted language.
Most interpreters offer basic syntax check and code tracing functionalities.
“dash -n script.sh” - Syntax check of a Shell script
“dash -x script.sh” - シェルスクリプトのトレース
“python -m py_compile script.py” - Syntax check of a Python script
“python -mtrace --trace script.py” - Trace a Python script
“perl -I ../libpath -c script.pl” - Syntax check of a Perl script
“perl -d:Trace script.pl” - Trace a Perl script
For testing code for dash
, try 「Readline wrapper」 which accommodates
bash
-like interactive environment.
For testing code for perl
, try REPL environment for Perl
which accommodates Python-like REPL (=READ + EVAL + PRINT + LOOP)
environment for Perl.
The shell script can be improved to create an attractive GUI program. The
trick is to use one of so-called dialog programs instead of dull interaction
using echo
and read
commands.
表12.9 ダイアログプログラムのリスト
パッケージ | ポプコン | サイズ | 説明 |
---|---|---|---|
x11-utils
|
V:169, I:556 | 712 | xmessage (1): window 中にメッセージや質問を表示 (X) |
whiptail
|
V:258, I:996 | 57 | シェルスクリプトからユーザーフレンリーなダイアログボックスを表示 (newt) |
dialog
|
V:13, I:113 | 1213 | シェルスクリプトからユーザーフレンリーなダイアログボックスを表示 (ncurses) |
zenity
|
V:74, I:356 | 167 | シェルスクリプトからグラフィカルなダイアログボックスを表示 (GTK) |
ssft
|
V:0, I:0 | 75 | シェルスクリプトフロントエンドツール (gettext を使った zenity や kdialog や dialog のラッパー) |
gettext
|
V:55, I:278 | 5825 | "/usr/bin/gettext.sh ": メッセージ翻訳 |
Here is an example of GUI program to demonstrate how easy it is just with a shell script.
This script uses zenity
to select a file (default
/etc/motd
) and display it.
GUI launcher for this script can be created following 「GUI からプログラムをスタート」.
#!/bin/sh -e # Copyright (C) 2021 Osamu Aoki <osamu@debian.org>, Public Domain # vim:set sw=2 sts=2 et: DATA_FILE=$(zenity --file-selection --filename="/etc/motd" --title="Select a file to check") || \ ( echo "E: File selection error" >&2 ; exit 1 ) # Check size of archive if ( file -ib "$DATA_FILE" | grep -qe '^text/' ) ; then zenity --info --title="Check file: $DATA_FILE" --width 640 --height 400 \ --text="$(head -n 20 "$DATA_FILE")" else zenity --info --title="Check file: $DATA_FILE" --width 640 --height 400 \ --text="The data is MIME=$(file -ib "$DATA_FILE")" fi
This kind of approach to GUI program with the shell script is useful only for simple choice cases. If you are to write any program with complexities, please consider writing it on more capable platform.
GUI filer programs can be extended to perform some popular actions on selected files using additional extension packages. They can also made to perform very specific custom actions by adding your specific scripts.
For GNOME, see NautilusScriptsHowto.
For KDE, see Creating Dolphin Service Menus.
For Xfce, see Thunar - Custom Actions and https://help.ubuntu.com/community/ThunarCustomActions.
For LXDE, see Custom Actions.
In order to process data, sh
needs to spawn sub-process
running cut
, grep
,
sed
, etc., and is slow. On the other hand,
perl
has internal capabilities to process data, and is
fast. So many system maintenance scripts on Debian use
perl
.
Let's think following one-liner AWK script snippet and its equivalents in Perl.
awk '($2=="1957") { print $3 }' |
これは次の数行のどれとも等価です。
perl -ne '@f=split; if ($f[1] eq "1957") { print "$f[2]\n"}' |
perl -ne 'if ((@f=split)[1] eq "1957") { print "$f[2]\n"}' |
perl -ne '@f=split; print $f[2] if ( $f[1]==1957 )' |
perl -lane 'print $F[2] if $F[1] eq "1957"' |
perl -lane 'print$F[2]if$F[1]eq+1957' |
最後のスクリプトは謎々状態です。Perl の次の機能を利用しています。
ホワイトスペースはオプション。
数字から文字列への自動変換が存在します。
Perl execution tricks via command line options:
perlrun
(1)
Perl special variables: perlvar
(1)
This flexibility is the strength of Perl. At the same time, this allows us to create cryptic and tangled codes. So be careful.
For more crazy Perl scripts, Perl Golf may be interesting.
表12.10 コンパイラ関連のパッケージのリスト
パッケージ | ポプコン | サイズ | 説明 |
---|---|---|---|
gcc
|
V:153, I:565 | 47 | GNU C コンパイラ |
libc6-dev
|
V:246, I:581 | 11954 | GNU C Library: Development Libraries and Header Files |
g++
|
V:56, I:503 | 14 | GNU C++ compiler |
libstdc++-10-dev
|
V:38, I:269 | 17587 | GNU Standard C++ Library v3 (development files) |
cpp
|
V:317, I:727 | 30 | GNU C プリプロセッサ |
gettext
|
V:55, I:278 | 5825 | GNU 国際化ユーティリティ |
glade
|
V:0, I:6 | 1209 | GTK User Interface Builder |
valac
|
V:0, I:5 | 717 | C# like language for the GObject system |
flex
|
V:8, I:82 | 1260 | LEX-compatible fast lexical analyzer generator |
bison
|
V:8, I:89 | 3116 | YACC互換のパーサジェネレータ |
susv2
|
I:0 | 16 | "The Single UNIX Specifications v2" を取得 |
susv3
|
I:0 | 16 | "The Single UNIX Specifications v3" を取得 |
golang
|
I:20 | 12 | Go programming language compiler |
rustc
|
V:3, I:13 | 7753 | Rust systems programming language |
haskell-platform
|
I:4 | 12 | 標準 Haskell ライブラリーとツール |
gfortran
|
V:8, I:75 | 16 | GNU Fortran 95 compiler |
fpc
|
I:3 | 102 | Free Pascal |
Here, 「Flex — 改良版 Lex」 and 「Bison — 改良版 Yacc」 are included to indicate how compiler-like program can be written in C language by compiling higher level description into C language.
C プログラム言語で書かれたプログラムをコンパイルする適正な環境を次のようにして設定できます。
# apt-get install glibc-doc manpages-dev libc6-dev gcc build-essential
GNU C ライブラリーパッケージである libc6-dev
パッケージは、C
プログラム言語で使われるヘッダーファイルやライブラリールーチンの集合である C
標準ライブラリーを提供します。
C のリファレンスは以下を参照下さい。
"info libc
" (C ライブラリー関数リファレンス)
gcc
(1) と "info gcc
"
各 C ライブラリー関数名
(3)
Kernighan & Ritchie 著, "The C Programming Language", 第2版 (Prentice Hall)
簡単な例の "example.c
" は"libm
"
ライブラリーを使って実行プログラム "run_example
" に次のようにしてコンパイル出来ます。
$ cat > example.c << EOF #include <stdio.h> #include <math.h> #include <string.h> int main(int argc, char **argv, char **envp){ double x; char y[11]; x=sqrt(argc+7.5); strncpy(y, argv[0], 10); /* prevent buffer overflow */ y[10] = '\0'; /* fill to make sure string ends with '\0' */ printf("%5i, %5.3f, %10s, %10s\n", argc, x, y, argv[1]); return 0; } EOF $ gcc -Wall -g -o run_example example.c -lm $ ./run_example 1, 2.915, ./run_exam, (null) $ ./run_example 1234567890qwerty 2, 3.082, ./run_exam, 1234567890qwerty
ここで、"-lm
" はsqrt
(3) のために
libc6
パッケージで提供されるライブラリー
"/usr/lib/libm.so
" をリンクするのに必要です。実際のライブラリーは
"/lib/
" 中にあるファイル名 "libm.so.6
" で、それは
"libm-2.7.so
" にシムリンクされています。
出力テキスト中の最後のパラメーターを良く見ましょう。"%10s
"
が指定されているにもかかわらず10文字以上あります。
上記のオーバーラン効果を悪用するバッファーオーバーフロー攻撃を防止のために、sprintf
(3) や
strcpy
(3) 等の境界チェック無しのポインターメモリー操作関数の使用は推奨できません。これに代えて
snprintf
(3) や strncpy
(3) を使います。
flex
(1) の入門書は "info flex
" の中にあります。
自分で作った "main()
" と "yywrap()
"
を供給する必要があります。そうでない場合にはあなたの flex プログラムは次のようでなければライブラリー無しにコンパイル出来ません。これというのは
"yywrap
" はマクロで、"%option main
" とすると
"%option noyywrap
" が暗示的に有効になるからです。
%option main %% .|\n ECHO ; %%
上記の代わりにとして、cc
(1) のコマンドラインの最後に (ちょうど AT&T-Lex
が"-ll
" 付きであるように) "-lfl
"
リンカーオプションを使いコンパイルすることが出来ます。この場合、"%option
" は必要なくなります。
Lint like tools can help automatic static code analysis.
Indent like tools can help human code reviews by reformatting source codes consistently.
Ctags like tools can help human code reviews by generating an index (or tag) file of names found in source codes.
![]() |
ヒント |
---|---|
Configuring your favorite editor ( |
表12.12 静的コード分析ツールのリスト
パッケージ | ポプコン | サイズ | 説明 |
---|---|---|---|
vim-ale
|
I:0 | 2591 | Asynchronous Lint Engine for Vim 8 and NeoVim |
vim-syntastic
|
I:3 | 1379 | Syntax checking hacks for vim |
elpa-flycheck
|
V:0, I:1 | 792 | modern on-the-fly syntax checking for Emacs |
elpa-relint
|
V:0, I:0 | 135 | Emacs Lisp regexp mistake finder |
cppcheck-gui
|
V:0, I:1 | 6196 | 静的 C/C++ コード分析ツール (GUI) |
shellcheck
|
V:2, I:11 | 18987 | シェルスクリプトのリントツール |
pyflakes3
|
V:1, I:14 | 24 | passive checker of Python 3 programs |
pylint
|
V:4, I:17 | 1976 | Python コード静的チェックソフト |
perl
|
V:644, I:990 | 669 | 静的コードチェックソフト付きのインタープリタ: B::Lint (3perl) |
rubocop
|
V:0, I:0 | 3247 | Ruby 静的コード分析ツール |
clang-tidy
|
V:1, I:8 | 21 | clang-based C++ linter tool |
splint
|
V:0, I:3 | 2320 | C プログラムを静的にバグのチェックするためのツール |
flawfinder
|
V:0, I:0 | 205 | C/C++ ソースコードを検査してセキュリティーの脆弱性を探すツール |
black
|
V:2, I:8 | 557 | uncompromising Python code formatter |
perltidy
|
V:0, I:4 | 2338 | Perl スクリプトのインデントとリフォーマット |
indent
|
V:0, I:9 | 426 | C language source code formatting program |
astyle
|
V:0, I:3 | 761 | Source code indenter for C, C++, Objective-C, C#, and Java |
bcpp
|
V:0, I:0 | 111 | C(++) beautifier |
xmlindent
|
V:0, I:1 | 53 | XML ストリームリフォーマッタ |
global
|
V:1, I:2 | 1896 | ソースコードの検索と閲覧のツール |
exuberant-ctags
|
V:3, I:25 | 345 | build tag file indexes of source code definitions |
デバッグはプログラミング活動において重要です。プログラムのデバッグ法を知ることで、あなたも意味あるバグリポートを作成できるような良い Debian ユーザーになれます。
Debian 上の第一義的デバッガは、実行中のプログラムを検査できるようにする
gdb
(1) です。
gdb
と関連プログラムを次のようにインストールしましょう。
# apt-get install gdb gdb-doc build-essential devscripts
gdb
のよいチュートリアルについては以下を参照ください:
“info gdb
”
“Debugging with GDB” in
/usr/share/doc/gdb-doc/html/gdb/index.html
次は gdb
(1) を"-g
" を使ってデバッグ情報を付けてコンパイルされた
"program
" に使う簡単な例です。
$ gdb program (gdb) b 1 # set break point at line 1 (gdb) run args # run program with args (gdb) next # next line ... (gdb) step # step forward ... (gdb) p parm # print parm ... (gdb) p parm=12 # set value to 12 ... (gdb) quit
![]() |
ヒント |
---|---|
多くの |
Since all installed binaries should be stripped on the Debian system by
default, most debugging symbols are removed in the normal package. In order
to debug Debian packages with gdb
(1),
*-dbgsym
packages need to be installed
(e.g. coreutils-dbgsym
in the case of
coreutils
). The source packages generate
*-dbgsym
packages automatically along with normal binary
packages and those debug packages are placed separately in debian-debug archive. Please refer to articles on Debian Wiki for more
information.
デバッグしようとしているパッケージに*-dbgsym
パッケージが無い場合は、次のようにしてリビルドした後でインストールする必要があります。
$ mkdir /path/new ; cd /path/new $ sudo apt-get update $ sudo apt-get dist-upgrade $ sudo apt-get install fakeroot devscripts build-essential $ apt-get source package_name $ cd package_name* $ sudo apt-get build-dep ./
必要に応じてバグを修正します。
例えば次のように、既存パッケージを再コンパイルする時は "+debug1
"
を後ろに付けたり、リリース前のパッケージをコンパイルする時は "~pre1
" を後ろに付けたりと、正規の
Debian バージョンとかち合わないようにパッケージバージョンを増やします。
$ dch -i
次のようにしてデバグシンボル付きでパッケージをコンパイルしてインストールします。
$ export DEB_BUILD_OPTIONS="nostrip noopt" $ debuild $ cd .. $ sudo debi package_name*.changes
パッケージのビルドスクリプトを確認して、バイナリーのコンパイルに確実に "CFLAGS=-g -Wall
"
が使われているようにします。
プログラムがクラッシュするのに出会った場合に、バックトレース情報をバグレポートに切り貼りして報告するのは良い考えです。
The backtrace can be obtained by gdb
(1) using one of the
following approaches:
Crash-in-GDB approach:
GDB からプログラムを実行します。
プログラムがクラッシュします。
GDB プロンプトで "bt
" と打ちます。
Crash-first approach:
For infinite loop or frozen keyboard situation, you can force to crash the
program by pressing Ctrl-\
or Ctrl-C
or executing “kill -ABRT PID
”. (See
「プロセスの停止」)
![]() |
ヒント |
---|---|
しばしば、一番上数行が " $ MALLOC_CHECK_=2 gdb hello |
表12.14 上級 gdb コマンドのリスト
コマンド | コマンド目的の説明 |
---|---|
(gdb) thread apply all bt |
マルチスレッドプログラムの全てのスレッドのバックトレースを取得 |
(gdb) bt full |
関数コールのスタック上に来たパラメーターを取得 |
(gdb) thread apply all bt full |
異常のオプションの組み合わせでバックトレースとパラメーターを取得 |
(gdb) thread apply all bt full 10 |
無関係の出力を切り最後の10のコールに関するバックトレースとパラメーターを取得 |
(gdb) set logging on |
gdb アウトプットをファイルに書き出す (デフォールトは
"gdb.txt ") |
次のように ldd
(1) を使ってプログラムのライブラリーへの依存関係をみつけだします。
$ ldd /bin/ls librt.so.1 => /lib/librt.so.1 (0x4001e000) libc.so.6 => /lib/libc.so.6 (0x40030000) libpthread.so.0 => /lib/libpthread.so.0 (0x40153000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
`chroot` された環境下で ls
(1) が機能するには、上記ライブラリーがあなたの `chroot`
された環境内で利用可能である必要があります。
「プログラム活動の追跡」を参照下さい。
There are several dynamic call tracing tools available in Debian. See 「プログラム活動の監視と制御と起動」.
GNOME プログラム preview1
が X エラーを受けると、次のようなメッセージが見つかります。
The program 'preview1' received an X Window System error.
このような場合には、プログラムを "--sync
" 付きで実行して、バックトレースを得るために
"gdk_x_error
" 関数上で停止するようにしてみましょう。
Debian にはメモリーリークを検出するプログラムがいくつか利用可能です。
表12.15 メモリーリーク検出ツールのリスト
パッケージ | ポプコン | サイズ | 説明 |
---|---|---|---|
libc6-dev
|
V:246, I:581 | 11954 | mtrace (1): glibc 中の malloc デバッグ機能 |
valgrind
|
V:6, I:38 | 77683 | メモリーデバッガとプロファイラ |
electric-fence
|
V:0, I:4 | 73 | malloc (3) デバッガ |
libdmalloc5
|
V:0, I:3 | 393 | メモリーアロケーションのデバグ用ライブラリー |
duma
|
V:0, I:0 | 293 | C および C++ プログラムのバッファオーバーランとアンダーランを検出するライブラリー |
leaktracer
|
V:0, I:2 | 56 | C++ プログラム用のメモリーリーク追跡ソフト |
表12.16 ビルドツールパッケージのリスト
パッケージ | ポプコン | サイズ | 文書 |
---|---|---|---|
make
|
V:147, I:574 | 1592 | make-doc が提供する "info make " |
autoconf
|
V:33, I:257 | 2025 | autoconf-doc が提供する "info autoconf |
automake
|
V:34, I:256 | 1837 | automake1.10-doc が提供する "info automake " |
libtool
|
V:29, I:240 | 1213 | libtool-doc が提供する "info libtool " |
cmake
|
V:16, I:116 | 28897 | cmake (1) cross-platform, open-source make system |
ninja-build
|
V:5, I:34 | 417 | ninja (1) small build system closest in spirit to Make |
meson
|
V:2, I:20 | 3451 | meson (1) high productivity build system on top of
ninja |
xutils-dev
|
V:1, I:10 | 1485 | imake (1), xmkmf (1), 他 |
Make
はプログラムのグループを管理するためのユーティリティーです。make
(1)
を実行すると、make
は"Makefile
"
というルールファイルを読み、ターゲットが最後に変更された後で変更された前提ファイルにターゲットが依存している場合やターゲットが存在しない場合にはターゲットを更新します。このような更新は同時並行的にされるかもしれません。
ルールファイルのシンタックスは以下の通りです。
target: [ prerequisites ... ] [TAB] command1 [TAB] -command2 # ignore errors [TAB] @command3 # suppress echoing
上記で、"[TAB]
" は TAB コードです。各行は make
による変数置換後シェルによって解釈されます。スクリプトを継続する行末には "\
"
を使います。シェルスクリプトの環境変数のための "$
" を入力するためには
"$$
" を使います。
ターゲットや前提に関するインプリシット (暗黙) ルールは、例えば次のように書けます。
%.o: %.c header.h
上記で、ターゲットは "%
" という文字を (1つだけ)
含んでいます。"%
"
は実際のターゲットファイル名の空でないいかなる部分文字列ともマッチします。前提もまた同様にそれらの名前が実際のターゲットファイル名にどう関連するかを示すために
"%
" を用いることができます。
表12.17 make の自動変数のリスト
自動変数 | 変数値 |
---|---|
$@ |
ターゲット |
$< |
最初の前提条件 |
$? |
全ての新規の前提条件 |
$^ |
全ての前提条件 |
$* |
"% " はターゲットパターンの軸にマッチします |
"make -p -f/dev/null
" を実行して自動的な内部ルールを確認下さい。
Autotools is a suite of programming tools designed to assist in making source code packages portable to many Unix-like systems.
Autoconf is a tool to produce a shell script
"configure
" from "configure.ac
".
"configure
" is used later to produce
"Makefile
" from "Makefile.in
"
template.
Automake is a tool to produce
"Makefile.in
" from "Makefile.am
".
Libtool is a shell script to address the software portability problem when compiling shared libraries from source code.
![]() |
警告 |
---|---|
システムファイルをあなたがコンパイルしたプログラムでインストールする時に上書きしてはいけません。 |
Debian は"/usr/local/
" とか "/opt
"
中のファイルに触れません。プログラムをソースからコンパイルする場合、Debian とかち合わないようにそれを
"/usr/local/
" の中にインストールします。
$ cd src $ ./configure --prefix=/usr/local $ make # this compiles program $ sudo make install # this installs the files in the system
オリジナルのソースを保有し、それが
autoconf
(1)/automake
(1)
と使用しあなたがそれをどう設定したかを覚えているなら、次のように実行してソフトウエアーをアンイストールします。
$ ./configure all-of-the-options-you-gave-it
$ sudo make uninstall
この代わりに、"/usr/local/
"
の下にだけインストールプロセスがファイルを置いたことが絶対に確実でそこに重要なものが無いなら、次のようにしてその内容を消すことが出来ます。
# find /usr/local -type f -print0 | xargs -0 rm -f
If you are not sure where files are installed, you should consider using
checkinstall
(8) from the checkinstall
package, which provides a clean path for the uninstall. It now supports to
create a Debian package with "-D
" option.
The software build system has been evolving:
Autotools on the top of Make has been the de facto standard for the portable build infrastructure since 1990s. This is extremely slow.
CMake initially released in 2000 improved speed significantly but was still build on the top of inherently slow Make.
Ninja initially released in 2012 is meant to replace Make for the further improved build speed but is also designed to have its input files generated by a higher-level build system.
Meson initially released in 2013 is the new popular and fast higher-level build system which uses Ninja as its backend.
See documents found at "The Meson Build system" and "The Ninja build system".
基本的な対話式動的ウェッブページは次のようにして作られます。
質問 (クエリー) はブラウザーのユーザーに HTML フォームを使って提示されます。
フォームのエントリーを埋めたりクリックすることによって次の符号化されたパラメーター付きの URL 文字列をブラウザーからウェッブサーバーに送信します。
"http://www.foo.dom/cgi-bin/program.pl?VAR1=VAL1&VAR2=VAL2&VAR3=VAL3
"
"http://www.foo.dom/cgi-bin/program.py?VAR1=VAL1&VAR2=VAL2&VAR3=VAL3
"
"http://www.foo.dom/program.php?VAR1=VAL1&VAR2=VAL2&VAR3=VAL3
"
URL 中の "%nn
" は16進数で nn
の値の文字と置き換えられます。
環境変通は次のように設定されます: "QUERY_STRING="VAR1=VAL1 VAR2=VAL2
VAR3=VAL3"
".
ウェッブサーバー上の CGI プログラム
("program.*
" のいずれでも) が環境変数
"$QUERY_STRING
" とともに起動されます。
CGI プログラムの STDOUT
(標準出力)
がウエブブラウザーに送られ対話式の動的なウェッブページとして表示されます。
セキュリティー上、CGI パラメーターを解釈する手作りの急ごしらえのプログラムは作らない方が賢明です。Perl や Python にはこのための確立したモジュールが存在します。PHP はこの様な機能とともに提供されます。クライアントでのデーターのストレージの必要がある場合、HTTP クッキーが使われます。クライアントサイドのデーター処理が必要な場合、Javascript が良く使われます。
詳しくは、Common Gateway Interface や The Apache Software Foundation や JavaScript を参照下さい。
http://www.google.com/search?hl=en&ie=UTF-8&q=CGI+tutorial を URL として直接ブラウザーのアドレスに入れ Google で"CGI tutorial" を検索するとグーグルサーバー上の CGI スクリプトが動いているのを観察する良い例です。
Debian パッケージを作りたい場合には、次を読みましょう。
基本的なパッケージシステムの理解には2章Debian パッケージ管理
基本的なポーティングプロセスの理解のために、「安定版システムへのパッケージ移植」
基本的な chroot 技術の理解のために、「Chroot システム」
debuild
(1), and sbuild
(1)
リコンパイルとデバグは「Debian パッケージのデバグ」
Guide for Debian
Maintainers (debmake-doc
パッケージ)
Debian Developer's
Reference (developers-reference
パッケージ)
Debian ポリシーマニュアル
(debian-policy
パッケージ)
debmake
や dh-make
や
dh-make-perl
等のパッケージングを補助するパッケージがあります。