diff --git a/en_US.ISO8859-1/books/developers-handbook/kerneldebug/chapter.sgml b/en_US.ISO8859-1/books/developers-handbook/kerneldebug/chapter.sgml
index cd5073ccd7..c699ac69eb 100644
--- a/en_US.ISO8859-1/books/developers-handbook/kerneldebug/chapter.sgml
+++ b/en_US.ISO8859-1/books/developers-handbook/kerneldebug/chapter.sgml
@@ -1,830 +1,830 @@
PaulRichardsContributed by JörgWunschKernel DebuggingObtaining a Kernel Crash DumpWhen running a development kernel (eg: &os.current;), such as a
kernel under extreme conditions (eg: very high load averages,
tens of thousands of connections, exceedingly high number of
concurrent users, hundreds of &man.jail.8;s, etc.), or using a
new feature or device driver on &os.stable; (eg:
PAE), sometimes a kernel will panic. In the
event that it does, this chapter will demonstrate how to extract
useful information out of a crash.A system reboot is inevitable once a kernel panics. Once a
system is rebooted, the contents of a system's physical memory
(RAM) is lost, as well as any bits that are
on the swap device before the panic. To preserve the bits in
physical memory, the kernel makes use of the swap device as a
temporary place to store the bits that are in RAM across a
reboot after a crash. In doing this, when &os; boots after a
crash, a kernel image can now be extracted and debugging can
take place.A swap device that has been configured as a dump
device still acts as a swap device. Dumps to non-swap devices
(such as tapes or CDRWs, for example) are not supported at this time. A
swap device is synonymous with a swap
partition.To be able to extract a usable core, it is required that at
least one swap partition be large enough to hold all of the bits
in physical memory. When a kernel panics, before the system
reboots, the kernel is smart enough to check to see if a swap
device has been configured as a dump device. If there is a
valid dump device, the kernel dumps the contents of what is in
physical memory to the swap device.Configuring the Dump DeviceBefore the kernel will dump the contents of its physical
memory to a dump device, a dump device must be configured. A
dump device is specified by using the &man.dumpon.8; command
to tell the kernel where to save kernel crash dumps. The
&man.dumpon.8; program must be called after the swap partition
has been configured with &man.swapon.8;. This is normally
handled by setting the dumpdev variable in
&man.rc.conf.5; to the path of the swap device (the
recommended way to extract a kernel dump).Alternatively, the dump device can be hard-coded via the
dump clause in the &man.config.5; line of
a kernel configuration file. This approach is deprecated and should
be used only if a kernel is crashing before &man.dumpon.8; can be executed.Check /etc/fstab or
&man.swapinfo.8; for a list of swap devices.Make sure the dumpdir
specified in &man.rc.conf.5; exists before a kernel
crash!&prompt.root; mkdir /var/crash
&prompt.root; chmod 700 /var/crashAlso, remember that the contents of
/var/crash is sensitive and very likely
contains confidential information such as passwords.Extracting a Kernel DumpOnce a dump has been written to a dump device, the dump
must be extracted before the swap device is mounted.
To extract a dump
from a dump device, use the &man.savecore.8; program. If
dumpdev has been set in &man.rc.conf.5;,
&man.savecore.8; will be called automatically on the first
multi-user boot after the crash and before the swap device
is mounted. The location of the extracted core is placed in
the &man.rc.conf.5; value dumpdir, by
default /var/crash and will be named
vmcore.0.In the event that there is already a file called
vmcore.0 in
/var/crash (or whatever
dumpdev is set to), the kernel will
increment the trailing number for every crash to avoid
overwriting an existing vmcore (eg:
vmcore.1). While debugging, it is
highly likely that you will want to use the highest version
vmcore in
/var/crash when searching for the right
vmcore.If you are testing a new kernel but need to boot a different one in
order to get your system up and running again, boot it only into single
user mode using the flag at the boot prompt, and
then perform the following steps:&prompt.root; fsck -p
&prompt.root; mount -a -t ufs # make sure /var/crash is writable
&prompt.root; savecore /var/crash /dev/ad0s1b
&prompt.root; exit # exit to multi-userThis instructs &man.savecore.8; to extract a kernel dump
from /dev/ad0s1b and place the contents in
- /var/crash. Don't forget to make sure the
+ /var/crash. Do not forget to make sure the
destination directory /var/crash has enough
- space for the dump. Also, don't forget to specify the correct path to your swap
+ space for the dump. Also, do not forget to specify the correct path to your swap
device as it is likely different than
/dev/ad0s1b!The recommended, and certainly the easiest way to automate
obtaining crash dumps is to use the dumpdev
variable in &man.rc.conf.5;.Debugging a Kernel Crash Dump with gdbOnce a dump has been obtained, getting useful information
out of the dump is relatively easy for simple problems. Before
launching into the internals of &man.gdb.1; to debug
the crash dump, locate the debug version of your kernel
(normally called kernel.debug) and the path
to the source files used to build your kernel (normally
/usr/obj/usr/src/sys/KERNCONF,
where KERNCONF
is the ident specified in a kernel
&man.config.5;). With those two pieces of info, let the
debugging commence!To enter into the debugger and begin getting information
from the dump, the following steps are required at a minimum:&prompt.root; cd /usr/obj/usr/src/sys/KERNCONF
&prompt.root; gdb -k /boot/kernel/kernel.debug /var/crash/vmcore.0You can debug the crash dump using the kernel sources just like
you can for any other program.This first dump is from a 5.2-BETA kernel and the crash
comes from deep within the kernel. The ouput below has been
modified to include line numbers on the left. This first trace
inspects the instruction pointer and obtains a back trace. The
address that is used on line 41 for the list
command is the instruction pointer and can be found on line line
17. Most developers will request having at least this
information sent to them if you are unable to debug the problem
yourself. If, however, you do solve the problem, make sure that
your patch winds its way into the source tree via a problem
report, mailing lists, or by being able to commit it! 1:&prompt.root; cd /usr/obj/usr/src/sys/KERNCONF
2:&prompt.root; gdb -k kernel.debug /var/crash/vmcore.0
3:GNU gdb 5.2.1 (FreeBSD)
4:Copyright 2002 Free Software Foundation, Inc.
5:GDB is free software, covered by the GNU General Public License, and you are
6:welcome to change it and/or distribute copies of it under certain conditions.
7:Type "show copying" to see the conditions.
8:There is absolutely no warranty for GDB. Type "show warranty" for details.
9:This GDB was configured as "i386-undermydesk-freebsd"...
10:panic: page fault
11:panic messages:
12:---
13:Fatal trap 12: page fault while in kernel mode
14:cpuid = 0; apic id = 00
15:fault virtual address = 0x300
16:fault code: = supervisor read, page not present
17:instruction pointer = 0x8:0xc0713860
18:stack pointer = 0x10:0xdc1d0b70
19:frame pointer = 0x10:0xdc1d0b7c
20:code segment = base 0x0, limit 0xfffff, type 0x1b
21: = DPL 0, pres 1, def32 1, gran 1
22:processor eflags = resume, IOPL = 0
23:current process = 14394 (uname)
24:trap number = 12
25:panic: page fault
26 cpuid = 0;
27:Stack backtrace:
28
29:syncing disks, buffers remaining... 2199 2199 panic: mi_switch: switch in a critical section
30:cpuid = 0;
31:Uptime: 2h43m19s
32:Dumping 255 MB
33: 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240
34:---
35:Reading symbols from /boot/kernel/snd_maestro3.ko...done.
36:Loaded symbols for /boot/kernel/snd_maestro3.ko
37:Reading symbols from /boot/kernel/snd_pcm.ko...done.
38:Loaded symbols for /boot/kernel/snd_pcm.ko
39:#0 doadump () at /usr/src/sys/kern/kern_shutdown.c:240
40:240 dumping++;
41:(kgdb)list *0xc0713860
42:0xc0713860 is in lapic_ipi_wait (/usr/src/sys/i386/i386/local_apic.c:663).
43:658 incr = 0;
44:659 delay = 1;
45:660 } else
46:661 incr = 1;
47:662 for (x = 0; x < delay; x += incr) {
48:663 if ((lapic->icr_lo & APIC_DELSTAT_MASK) == APIC_DELSTAT_IDLE)
49:664 return (1);
50:665 ia32_pause();
51:666 }
52:667 return (0);
53:(kgdb)backtrace
54:#0 doadump () at /usr/src/sys/kern/kern_shutdown.c:240
55:#1 0xc055fd9b in boot (howto=260) at /usr/src/sys/kern/kern_shutdown.c:372
56:#2 0xc056019d in panic () at /usr/src/sys/kern/kern_shutdown.c:550
57:#3 0xc0567ef5 in mi_switch () at /usr/src/sys/kern/kern_synch.c:470
58:#4 0xc055fa87 in boot (howto=256) at /usr/src/sys/kern/kern_shutdown.c:312
59:#5 0xc056019d in panic () at /usr/src/sys/kern/kern_shutdown.c:550
60:#6 0xc0720c66 in trap_fatal (frame=0xdc1d0b30, eva=0)
61: at /usr/src/sys/i386/i386/trap.c:821
62:#7 0xc07202b3 in trap (frame=
63: {tf_fs = -1065484264, tf_es = -1065484272, tf_ds = -1065484272, tf_edi = 1, tf_esi = 0, tf_ebp = -602076292, tf_isp = -602076324, tf_ebx = 0, tf_edx = 0, tf_ecx = 1000000, tf_eax = 243, tf_trapno = 12, tf_err = 0, tf_eip = -1066321824, tf_cs = 8, tf_eflags = 65671, tf_esp = 243, tf_ss = 0})
64: at /usr/src/sys/i386/i386/trap.c:250
65:#8 0xc070c9f8 in calltrap () at {standard input}:94
66:#9 0xc07139f3 in lapic_ipi_vectored (vector=0, dest=0)
67: at /usr/src/sys/i386/i386/local_apic.c:733
68:#10 0xc0718b23 in ipi_selected (cpus=1, ipi=1)
69: at /usr/src/sys/i386/i386/mp_machdep.c:1115
70:#11 0xc057473e in kseq_notify (ke=0xcc05e360, cpu=0)
71: at /usr/src/sys/kern/sched_ule.c:520
72:#12 0xc0575cad in sched_add (td=0xcbcf5c80)
73: at /usr/src/sys/kern/sched_ule.c:1366
74:#13 0xc05666c6 in setrunqueue (td=0xcc05e360)
75: at /usr/src/sys/kern/kern_switch.c:422
76:#14 0xc05752f4 in sched_wakeup (td=0xcbcf5c80)
77: at /usr/src/sys/kern/sched_ule.c:999
78:#15 0xc056816c in setrunnable (td=0xcbcf5c80)
79: at /usr/src/sys/kern/kern_synch.c:570
80:#16 0xc0567d53 in wakeup (ident=0xcbcf5c80)
81: at /usr/src/sys/kern/kern_synch.c:411
82:#17 0xc05490a8 in exit1 (td=0xcbcf5b40, rv=0)
83: at /usr/src/sys/kern/kern_exit.c:509
84:#18 0xc0548011 in sys_exit () at /usr/src/sys/kern/kern_exit.c:102
85:#19 0xc0720fd0 in syscall (frame=
86: {tf_fs = 47, tf_es = 47, tf_ds = 47, tf_edi = 0, tf_esi = -1, tf_ebp = -1077940712, tf_isp = -602075788, tf_ebx = 672411944, tf_edx = 10, tf_ecx = 672411600, tf_eax = 1, tf_trapno = 12, tf_err = 2, tf_eip = 671899563, tf_cs = 31, tf_eflags = 642, tf_esp = -1077940740, tf_ss = 47})
87: at /usr/src/sys/i386/i386/trap.c:1010
88:#20 0xc070ca4d in Xint0x80_syscall () at {standard input}:136
89:---Can't read userspace from dump, or kernel process---
90:(kgdb)quitThis next trace is an older dump from the FreeBSD 2 time
frame, but is more involved and demonstrates more of the
features of gdb. Long lines have been folded
to improve readability, and the lines are numbered for
reference. Despite this, it is a real-world error trace taken
during the development of the pcvt console driver. 1:Script started on Fri Dec 30 23:15:22 1994
2:&prompt.root; cd /sys/compile/URIAH
3:&prompt.root; gdb -k kernel /var/crash/vmcore.1
4:Reading symbol data from /usr/src/sys/compile/URIAH/kernel
...done.
5:IdlePTD 1f3000
6:panic: because you said to!
7:current pcb at 1e3f70
8:Reading in symbols for ../../i386/i386/machdep.c...done.
9:(kgdb)backtrace
10:#0 boot (arghowto=256) (../../i386/i386/machdep.c line 767)
11:#1 0xf0115159 in panic ()
12:#2 0xf01955bd in diediedie () (../../i386/i386/machdep.c line 698)
13:#3 0xf010185e in db_fncall ()
14:#4 0xf0101586 in db_command (-266509132, -266509516, -267381073)
15:#5 0xf0101711 in db_command_loop ()
16:#6 0xf01040a0 in db_trap ()
17:#7 0xf0192976 in kdb_trap (12, 0, -272630436, -266743723)
18:#8 0xf019d2eb in trap_fatal (...)
19:#9 0xf019ce60 in trap_pfault (...)
20:#10 0xf019cb2f in trap (...)
21:#11 0xf01932a1 in exception:calltrap ()
22:#12 0xf0191503 in cnopen (...)
23:#13 0xf0132c34 in spec_open ()
24:#14 0xf012d014 in vn_open ()
25:#15 0xf012a183 in open ()
26:#16 0xf019d4eb in syscall (...)
27:(kgdb)up 10
28:Reading in symbols for ../../i386/i386/trap.c...done.
29:#10 0xf019cb2f in trap (frame={tf_es = -260440048, tf_ds = 16, tf_\
30:edi = 3072, tf_esi = -266445372, tf_ebp = -272630356, tf_isp = -27\
31:2630396, tf_ebx = -266427884, tf_edx = 12, tf_ecx = -266427884, tf\
32:_eax = 64772224, tf_trapno = 12, tf_err = -272695296, tf_eip = -26\
33:6672343, tf_cs = -266469368, tf_eflags = 66066, tf_esp = 3072, tf_\
34:ss = -266427884}) (../../i386/i386/trap.c line 283)
35:283 (void) trap_pfault(&frame, FALSE);
36:(kgdb)frame frame->tf_ebp frame->tf_eip
37:Reading in symbols for ../../i386/isa/pcvt/pcvt_drv.c...done.
38:#0 0xf01ae729 in pcopen (dev=3072, flag=3, mode=8192, p=(struct p\
39:roc *) 0xf07c0c00) (../../i386/isa/pcvt/pcvt_drv.c line 403)
40:403 return ((*linesw[tp->t_line].l_open)(dev, tp));
41:(kgdb)list
42:398
43:399 tp->t_state |= TS_CARR_ON;
44:400 tp->t_cflag |= CLOCAL; /* cannot be a modem (:-) */
45:401
46:402 #if PCVT_NETBSD || (PCVT_FREEBSD >= 200)
47:403 return ((*linesw[tp->t_line].l_open)(dev, tp));
48:404 #else
49:405 return ((*linesw[tp->t_line].l_open)(dev, tp, flag));
50:406 #endif /* PCVT_NETBSD || (PCVT_FREEBSD >= 200) */
51:407 }
52:(kgdb)print tp
53:Reading in symbols for ../../i386/i386/cons.c...done.
54:$1 = (struct tty *) 0x1bae
55:(kgdb)print tp->t_line
56:$2 = 1767990816
57:(kgdb)up
58:#1 0xf0191503 in cnopen (dev=0x00000000, flag=3, mode=8192, p=(st\
59:ruct proc *) 0xf07c0c00) (../../i386/i386/cons.c line 126)
60: return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
61:(kgdb)up
62:#2 0xf0132c34 in spec_open ()
63:(kgdb)up
64:#3 0xf012d014 in vn_open ()
65:(kgdb)up
66:#4 0xf012a183 in open ()
67:(kgdb)up
68:#5 0xf019d4eb in syscall (frame={tf_es = 39, tf_ds = 39, tf_edi =\
69: 2158592, tf_esi = 0, tf_ebp = -272638436, tf_isp = -272629788, tf\
70:_ebx = 7086, tf_edx = 1, tf_ecx = 0, tf_eax = 5, tf_trapno = 582, \
71:tf_err = 582, tf_eip = 75749, tf_cs = 31, tf_eflags = 582, tf_esp \
72:= -272638456, tf_ss = 39}) (../../i386/i386/trap.c line 673)
73:673 error = (*callp->sy_call)(p, args, rval);
74:(kgdb)up
75:Initial frame selected; you cannot go up.
76:(kgdb)quitComments to the above script:line 6:This is a dump taken from within DDB (see below), hence the
panic comment because you said to!, and a rather
long stack trace; the initial reason for going into DDB has been a
page fault trap though.line 20:This is the location of function trap()
in the stack trace.line 36:Force usage of a new stack frame; this is no longer necessary.
The stack frames are supposed to point to the right
locations now, even in case of a trap.
From looking at the code in source line 403, there is a
high probability that either the pointer access for
tp was messed up, or the array access was out of
bounds.line 52:The pointer looks suspicious, but happens to be a valid
address.line 56:However, it obviously points to garbage, so we have found our
error! (For those unfamiliar with that particular piece of code:
tp->t_line refers to the line discipline of
the console device here, which must be a rather small integer
number.)
- If your system is crashing regularly and you're running
+ If your system is crashing regularly and you are running
out of disk space, deleting old vmcore
files in /var/crash could save a
considerable amount of disk space!Debugging a Crash Dump with DDDExamining a kernel crash dump with a graphical debugger like
ddd is also possible (you will need to install
the devel/ddd port in order to use the
ddd debugger). Add the
option to the ddd command line you would use
normally. For example;&prompt.root; ddd -k /var/crash/kernel.0 /var/crash/vmcore.0You should then be able to go about looking at the crash dump using
ddd's graphical interface.Post-Mortem Analysis of a DumpWhat do you do if a kernel dumped core but you did not expect it,
and it is therefore not compiled using config -g? Not
everything is lost here. Do not panic!Of course, you still need to enable crash dumps. See above for the
options you have to specify in order to do this.Go to your kernel config directory
(/usr/src/sys/arch/conf)
and edit your configuration file. Uncomment (or add, if it does not
exist) the following line:makeoptions DEBUG=-g #Build kernel with gdb(1) debug symbolsRebuild the kernel. Due to the time stamp change on the Makefile,
some other object files will be rebuilt, for example
trap.o. With a bit of luck, the added
option will not change anything for the generated
code, so you will finally get a new kernel with similar code to the
faulting one but some debugging symbols. You should at least verify the
old and new sizes with the &man.size.1; command. If there is a
mismatch, you probably need to give up here.Go and examine the dump as described above. The debugging symbols
might be incomplete for some places, as can be seen in the stack trace
in the example above where some functions are displayed without line
numbers and argument lists. If you need more debugging symbols, remove
the appropriate object files, recompile the kernel again and repeat the
gdb
session until you know enough.All this is not guaranteed to work, but it will do it fine in most
cases.On-Line Kernel Debugging Using DDBWhile gdb as an off-line debugger provides a very
high level of user interface, there are some things it cannot do. The
most important ones being breakpointing and single-stepping kernel
code.If you need to do low-level debugging on your kernel, there is an
on-line debugger available called DDB. It allows setting of
breakpoints, single-stepping kernel functions, examining and changing
kernel variables, etc. However, it cannot access kernel source files,
and only has access to the global and static symbols, not to the full
debug information like gdb does.To configure your kernel to include DDB, add the option line
options DDB
to your config file, and rebuild. (See The FreeBSD Handbook for details on
configuring the FreeBSD kernel).If you have an older version of the boot blocks, your
debugger symbols might not be loaded at all. Update the boot blocks;
the recent ones load the DDB symbols automatically.Once your DDB kernel is running, there are several ways to enter
DDB. The first, and earliest way is to type the boot flag
right at the boot prompt. The kernel will start up
in debug mode and enter DDB prior to any device probing. Hence you can
even debug the device probe/attach functions.The second scenario is to drop to the debugger once the
system has booted. There are two simple ways to accomplish
this. If you would like to break to the debugger from the
command prompt, simply type the command:&prompt.root; sysctl debug.enter_debugger=ddbAlternatively, if you are at the system console, you may use
a hot-key on the keyboard. The default break-to-debugger
sequence is CtrlAltESC. For
syscons, this sequence can be remapped and some of the
distributed maps out there do this, so check to make sure you
know the right sequence to use. There is an option available
for serial consoles that allows the use of a serial line BREAK on the
console line to enter DDB (options BREAK_TO_DEBUGGER
in the kernel config file). It is not the default since there are a lot
of serial adapters around that gratuitously generate a BREAK
condition, for example when pulling the cable.The third way is that any panic condition will branch to DDB if the
kernel is configured to use it. For this reason, it is not wise to
configure a kernel with DDB for a machine running unattended.The DDB commands roughly resemble some gdb
commands. The first thing you probably need to do is to set a
breakpoint:b function-nameb addressNumbers are taken hexadecimal by default, but to make them distinct
from symbol names; hexadecimal numbers starting with the letters
a-f need to be preceded with 0x
(this is optional for other numbers). Simple expressions are allowed,
for example: function-name + 0x103.To continue the operation of an interrupted kernel, simply
type:cTo get a stack trace, use:traceNote that when entering DDB via a hot-key, the kernel is currently
servicing an interrupt, so the stack trace might be not of much use
to you.If you want to remove a breakpoint, usedeldel address-expressionThe first form will be accepted immediately after a breakpoint hit,
and deletes the current breakpoint. The second form can remove any
breakpoint, but you need to specify the exact address; this can be
obtained from:show bTo single-step the kernel, try:sThis will step into functions, but you can make DDB trace them until
the matching return statement is reached by:nThis is different from gdb's
next statement; it is like gdb's
finish.To examine data from memory, use (for example):
x/wx 0xf0133fe0,40x/hd db_symtab_spacex/bc termbuf,10x/s stringbuf
for word/halfword/byte access, and hexadecimal/decimal/character/ string
display. The number after the comma is the object count. To display
the next 0x10 items, simply use:x ,10Similarly, use
x/ia foofunc,10
to disassemble the first 0x10 instructions of
foofunc, and display them along with their offset
from the beginning of foofunc.To modify memory, use the write command:w/b termbuf 0xa 0xb 0w/w 0xf0010030 0 0The command modifier
(b/h/w)
specifies the size of the data to be written, the first following
expression is the address to write to and the remainder is interpreted
as data to write to successive memory locations.If you need to know the current registers, use:show regAlternatively, you can display a single register value by e.g.
p $eax
and modify it by:set $eax new-valueShould you need to call some kernel functions from DDB, simply
say:call func(arg1, arg2, ...)The return value will be printed.For a &man.ps.1; style summary of all running processes, use:psNow you have examined why your kernel failed, and you wish to
reboot. Remember that, depending on the severity of previous
malfunctioning, not all parts of the kernel might still be working as
expected. Perform one of the following actions to shut down and reboot
your system:panicThis will cause your kernel to dump core and reboot, so you can
later analyze the core on a higher level with gdb. This command
usually must be followed by another continue
statement.call boot(0)Which might be a good way to cleanly shut down the running system,
sync() all disks, and finally reboot. As long as
the disk and filesystem interfaces of the kernel are not damaged, this
might be a good way for an almost clean shutdown.call cpu_reset()This is the final way out of disaster and almost the same as hitting the
Big Red Button.If you need a short command summary, simply type:helpHowever, it is highly recommended to have a printed copy of the
&man.ddb.4; manual page ready for a debugging
session. Remember that it is hard to read the on-line manual while
single-stepping the kernel.On-Line Kernel Debugging Using Remote GDBThis feature has been supported since FreeBSD 2.2, and it is
actually a very neat one.GDB has already supported remote debugging for
a long time. This is done using a very simple protocol along a serial
line. Unlike the other methods described above, you will need two
machines for doing this. One is the host providing the debugging
environment, including all the sources, and a copy of the kernel binary
with all the symbols in it, and the other one is the target machine that
simply runs a similar copy of the very same kernel (but stripped of the
debugging information).You should configure the kernel in question with config
-g, include into the configuration, and
compile it as usual. This gives a large binary, due to the
debugging information. Copy this kernel to the target machine, strip
the debugging symbols off with strip -x, and boot it
using the boot option. Connect the serial line
of the target machine that has "flags 080" set on its sio device
to any serial line of the debugging host.
Now, on the debugging machine, go to the compile directory of the target
kernel, and start gdb:&prompt.user; gdb -k kernel
GDB is free software and you are welcome to distribute copies of it
under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for details.
GDB 4.16 (i386-unknown-freebsd),
Copyright 1996 Free Software Foundation, Inc...
(kgdb)Initialize the remote debugging session (assuming the first serial
port is being used) by:(kgdb)target remote /dev/cuaa0Now, on the target host (the one that entered DDB right before even
starting the device probe), type:Debugger("Boot flags requested debugger")
Stopped at Debugger+0x35: movb $0, edata+0x51bc
db>gdbDDB will respond with:Next trap will enter GDB remote protocol modeEvery time you type gdb, the mode will be toggled
between remote GDB and local DDB. In order to force a next trap
immediately, simply type s (step). Your hosting GDB
will now gain control over the target kernel:Remote debugging using /dev/cuaa0
Debugger (msg=0xf01b0383 "Boot flags requested debugger")
at ../../i386/i386/db_interface.c:257
(kgdb)You can use this session almost as any other GDB session, including
full access to the source, running it in gud-mode inside an Emacs window
(which gives you an automatic source code display in another Emacs
window), etc.Debugging Loadable Modules Using GDBWhen debugging a panic that occurred within a module, or
using remote GDB against a machine that uses dynamic modules,
you need to tell GDB how to obtain symbol information for those
modules.First, you need to build the module(s) with debugging
information:&prompt.root; cd /sys/modules/linux
&prompt.root; make clean; make COPTS=-gIf you are using remote GDB, you can run
kldstat on the target machine to find out
where the module was loaded:&prompt.root; kldstat
Id Refs Address Size Name
1 4 0xc0100000 1c1678 kernel
2 1 0xc0a9e000 6000 linprocfs.ko
3 1 0xc0ad7000 2000 warp_saver.ko
4 1 0xc0adc000 11000 linux.koIf you are debugging a crash dump, you will need to walk the
linker_files list, starting at
linker_files->tqh_first and following the
link.tqe_next pointers until you find the
entry with the filename you are looking for.
The address member of that entry is the load
address of the module.Next, you need to find out the offset of the text section
within the module:&prompt.root; objdump --section-headers /sys/modules/linux/linux.ko | grep text
3 .rel.text 000016e0 000038e0 000038e0 000038e0 2**2
10 .text 00007f34 000062d0 000062d0 000062d0 2**2The one you want is the .text section,
section 10 in the above example. The fourth hexadecimal field
(sixth field overall) is the offset of the text section within
the file. Add this offset to the load address of the module to
obtain the relocation address for the module's code. In our
example, we get 0xc0adc000 + 0x62d0 = 0xc0ae22d0. Use the
add-symbol-file command in GDB to tell the
debugger about the module:(kgdb)add-symbol-file /sys/modules/linux/linux.ko 0xc0ae22d0
add symbol table from file "/sys/modules/linux/linux.ko" at text_addr = 0xc0ae22d0?
(y or n) y
Reading symbols from /sys/modules/linux/linux.ko...done.
(kgdb)You should now have access to all the symbols in the
module.Debugging a Console DriverSince you need a console driver to run DDB on, things are more
complicated if the console driver itself is failing. You might remember
the use of a serial console (either with modified boot blocks, or by
specifying at the Boot: prompt),
and hook up a standard terminal onto your first serial port. DDB works
on any configured console driver, including a serial
console.
diff --git a/en_US.ISO8859-1/books/developers-handbook/tools/chapter.sgml b/en_US.ISO8859-1/books/developers-handbook/tools/chapter.sgml
index 62062fabc7..9a18d7f20f 100644
--- a/en_US.ISO8859-1/books/developers-handbook/tools/chapter.sgml
+++ b/en_US.ISO8859-1/books/developers-handbook/tools/chapter.sgml
@@ -1,2357 +1,2357 @@
JamesRaynardContributed by MurrayStokelyProgramming ToolsSynopsisThis chapter is an introduction to using some of the
programming tools supplied with FreeBSD, although much of it
will be applicable to many other versions of &unix;. It does
not attempt to describe coding in any
detail. Most of the chapter assumes little or no previous
programming knowledge, although it is hoped that most
programmers will find something of value in it.IntroductionFreeBSD offers an excellent development environment.
Compilers for C, C++, and Fortran and an assembler come with the
basic system, not to mention a Perl interpreter and classic &unix;
tools such as sed and awk.
If that is not enough, there are many more compilers and
interpreters in the Ports collection. FreeBSD is very
compatible with standards such as &posix; and
ANSI C, as well with its own BSD heritage, so
it is possible to write applications that will compile and run
with little or no modification on a wide range of
platforms.However, all this power can be rather overwhelming at first
if you have never written programs on a &unix; platform before.
This document aims to help you get up and running, without
getting too deeply into more advanced topics. The intention is
that this document should give you enough of the basics to be
able to make some sense of the documentation.Most of the document requires little or no knowledge of
programming, although it does assume a basic competence with
using &unix; and a willingness to learn!Introduction to ProgrammingA program is a set of instructions that tell the computer to
do various things; sometimes the instruction it has to perform
depends on what happened when it performed a previous
instruction. This section gives an overview of the two main
ways in which you can give these instructions, or
commands as they are usually called. One way
uses an interpreter, the other a
compiler. As human languages are too
difficult for a computer to understand in an unambiguous way,
commands are usually written in one or other languages specially
designed for the purpose.InterpretersWith an interpreter, the language comes as an environment,
where you type in commands at a prompt and the environment
executes them for you. For more complicated programs, you can
type the commands into a file and get the interpreter to load
the file and execute the commands in it. If anything goes
wrong, many interpreters will drop you into a debugger to help
you track down the problem.The advantage of this is that you can see the results of
your commands immediately, and mistakes can be corrected
readily. The biggest disadvantage comes when you want to
share your programs with someone. They must have the same
interpreter, or you must have some way of giving it to them,
and they need to understand how to use it. Also users may not
appreciate being thrown into a debugger if they press the
wrong key! From a performance point of view, interpreters can
use up a lot of memory, and generally do not generate code as
efficiently as compilers.In my opinion, interpreted languages are the best way to
start if you have not done any programming before. This kind
of environment is typically found with languages like Lisp,
Smalltalk, Perl and Basic. It could also be argued that the
&unix; shell (sh, csh) is itself an
interpreter, and many people do in fact write shell
scripts to help with various
housekeeping tasks on their machine. Indeed, part
of the original &unix; philosophy was to provide lots of small
utility programs that could be linked together in shell
scripts to perform useful tasks.Interpreters available with FreeBSDHere is a list of interpreters that are available as
FreeBSD
packages, with a brief discussion of some of the
more popular interpreted languages.To get one of these packages, all you need to do is to
click on the hotlink for the package, to download the package
and then install the package by running:&prompt.root; pkg_add package nameas root. Obviously, you will need to have a fully
functional FreeBSD 2.1.0 or later system for the package to
work!BASICShort for Beginner's All-purpose Symbolic
Instruction Code. Developed in the 1950s for teaching
University students to program and provided with every
self-respecting personal computer in the 1980s,
BASIC has been the first programming
language for many programmers. It is also the foundation
for Visual Basic.The Bywater
Basic Interpreter and the Phil
Cockroft's Basic Interpreter (formerly Rabbit
Basic) are available as FreeBSD
packages.LispA language that was developed in the late 1950s as
an alternative to the number-crunching
languages that were popular at the time. Instead of
being based on numbers, Lisp is based on lists; in fact
the name is short for List Processing.
Very popular in AI (Artificial Intelligence)
circles.Lisp is an extremely powerful and sophisticated
language, but can be rather large and unwieldy.Various implementations of Lisp that can run on &unix;
systems are available as packages for FreeBSD.
GNU Common Lisp,
CLISP
by Bruno Haible and Michael Stoll,
CMUCL
which includes a highly-optimizing compiler too, or
simpler Lisp implementations, like
SLisp
which implements most of the Common Lisp constructs in a
few hundred lines of C code.PerlVery popular with system administrators for writing
scripts; also often used on World Wide Web servers for
writing CGI scripts.Perl is available as a package
for all FreeBSD releases, and is installed as /usr/bin/perl in the
base system of 4.x releases.SchemeA dialect of Lisp that is rather more compact and
cleaner than Common Lisp. Popular in Universities as it
is simple enough to teach to undergraduates as a first
language, while it has a high enough level of
abstraction to be used in research work.FreeBSD has packages of the Elk
Scheme Interpreter, the MIT
Scheme Interpreter and the SCM
Scheme Interpreter.IconIcon is a high-level language with extensive
facilities for processing strings and structures.
A package
is available for FreeBSD.LogoLogo is a language that is easy to learn, and has
been used as an introductory programming language in
various courses. It is an excellent tool to work with
when teaching programming in small ages, as it makes the
creation of elaborate geometric shapes an easy task even
for very small children.A package is available for FreeBSD of Brian Harvey's LOGO
Interpreter.PythonPython is an Object-Oriented, interpreted language.
Its advocates argue that it is one of the best languages
to start programming with, since it is relatively easy
to start with, but is not limited in comparison to other
popular interpreted languages that are used for the
development of large, complex applications (Perl and
Tcl are two other languages that are popular for such tasks).A package of the latest version of Python for
FreeBSD is available
here.Tcl and TkTcl is an embeddable, interpreted language, that has
become widely used and became popular mostly because of its portability to many
platforms. It can be used both for quickly writing
small, prototype applications, or (when combined with
Tk, a GUI toolkit) fully-fledged, featureful
programs.Various versions of Tcl are available as packages
for FreeBSD. The latest version is, as of this writing,
Tcl
version 8.3.CompilersCompilers are rather different. First of all, you write
your code in a file (or files) using an editor. You then run
the compiler and see if it accepts your program. If it did
not compile, grit your teeth and go back to the editor; if it
did compile and gave you a program, you can run it either at a
shell command prompt or in a debugger to see if it works
properly.
If you run it in the shell, you may get a core
dump.Obviously, this is not quite as direct as using an
interpreter. However it allows you to do a lot of things
which are very difficult or even impossible with an
interpreter, such as writing code which interacts closely with
the operating system—or even writing your own operating
system! It is also useful if you need to write very efficient
code, as the compiler can take its time and optimize the code,
which would not be acceptable in an interpreter. Moreover,
distributing a program written for a compiler is usually more
straightforward than one written for an interpreter—you
can just give them a copy of the executable, assuming they
have the same operating system as you.Compiled languages include Pascal, C and C++. C and C++
are rather unforgiving languages, and best suited to more
experienced programmers; Pascal, on the other hand, was
designed as an educational language, and is quite a good
language to start with. FreeBSD does not include Pascal
support in the base system, but both GNU Pascal Compiler (GPC)
and the Free Pascal Compiler
are available in the ports collection as
lang/gpc and
lang/fpc.As the edit-compile-run-debug cycle is rather tedious when
using separate programs, many commercial compiler makers have
produced Integrated Development Environments
(IDEs for short). FreeBSD does not include
an IDE in the base system, but devel/kdevelop is
available in the ports tree and many use
Emacs for this purpose. Using
Emacs as an IDE is discussed in
.Compiling with ccThis section deals only with the GNU compiler for C and C++,
since that comes with the base FreeBSD system. It can be
invoked by either cc or gcc. The
details of producing a program with an interpreter vary
considerably between interpreters, and are usually well covered
in the documentation and on-line help for the
interpreter.Once you have written your masterpiece, the next step is to
convert it into something that will (hopefully!) run on FreeBSD.
This usually involves several steps, each of which is done by a
separate program.Pre-process your source code to remove comments and do
other tricks like expanding macros in C.Check the syntax of your code to see if you have obeyed
the rules of the language. If you have not, it will
complain!Convert the source code into assembly
language—this is very close to machine code, but still
understandable by humans. Allegedly.
To be strictly accurate, cc converts the
source code into its own, machine-independent
p-code instead of assembly language at
this stage.Convert the assembly language into machine
code—yep, we are talking bits and bytes, ones and
zeros here.Check that you have used things like functions and
global variables in a consistent way. For example, if you
have called a non-existent function, it will
complain.If you are trying to produce an executable from several
source code files, work out how to fit them all
together.Work out how to produce something that the system's
run-time loader will be able to load into memory and
run.Finally, write the executable on the filesystem.The word compiling is often used to refer to
just steps 1 to 4—the others are referred to as
linking. Sometimes step 1 is referred to as
pre-processing and steps 3-4 as
assembling.Fortunately, almost all this detail is hidden from you, as
cc is a front end that manages calling all these
programs with the right arguments for you; simply typing&prompt.user; cc foobar.cwill cause foobar.c to be compiled by all the
steps above. If you have more than one file to compile, just do
something like&prompt.user; cc foo.c bar.cNote that the syntax checking is just that—checking
the syntax. It will not check for any logical mistakes you may
have made, like putting the program into an infinite loop, or
using a bubble sort when you meant to use a binary
sort.
In case you did not know, a binary sort is an efficient
way of sorting things into order and a bubble sort
is not.There are lots and lots of options for cc, which
are all in the manual page. Here are a few of the most important
ones, with examples of how to use them.The output name of the file. If you do not use this
option, cc will produce an executable called
a.out.
The reasons for this are buried in the mists of
history.&prompt.user; cc foobar.cexecutable is a.out
&prompt.user; cc -o foobar foobar.cexecutable is foobarJust compile the file, do not link it. Useful for toy
programs where you just want to check the syntax, or if
you are using a Makefile.&prompt.user; cc -c foobar.cThis will produce an object file (not an
executable) called foobar.o. This
can be linked together with other object files into an
executable.Create a debug version of the executable. This makes
the compiler put information into the executable about
which line of which source file corresponds to which
function call. A debugger can use this information to show
the source code as you step through the program, which is
very useful; the disadvantage is that
all this extra information makes the program much bigger.
Normally, you compile with while you
are developing a program and then compile a release
version without when you are
satisfied it works properly.&prompt.user; cc -g foobar.cThis will produce a debug version of the
program.
Note, we did not use the flag
to specify the executable name, so we will get an
executable called a.out.
Producing a debug version called
foobar is left as an exercise for
the reader!Create an optimized version of the executable. The
compiler performs various clever tricks to try to produce
an executable that runs faster than normal. You can add a
number after the to specify a higher
level of optimization, but this often exposes bugs in the
compiler's optimizer. For instance, the version of
cc that comes with the 2.1.0 release of
FreeBSD is known to produce bad code with the
option in some circumstances.Optimization is usually only turned on when compiling
a release version.&prompt.user; cc -O -o foobar foobar.cThis will produce an optimized version of
foobar.The following three flags will force cc
to check that your code complies to the relevant international
standard, often referred to as the ANSI
standard, though strictly speaking it is an
ISO standard.Enable all the warnings which the authors of
cc believe are worthwhile. Despite the
name, it will not enable all the warnings
cc is capable of.Turn off most, but not all, of the
non-ANSI C features provided by
cc. Despite the name, it does not
guarantee strictly that your code will comply to the
standard.Turn off allcc's non-ANSI C
features.Without these flags, cc will allow you to
use some of its non-standard extensions to the standard. Some
of these are very useful, but will not work with other
compilers—in fact, one of the main aims of the standard is
to allow people to write code that will work with any compiler
on any system. This is known as portable
code.Generally, you should try to make your code as portable as
possible, as otherwise you may have to completely rewrite the
program later to get it to work somewhere else—and who
knows what you may be using in a few years time?&prompt.user; cc -Wall -ansi -pedantic -o foobar foobar.cThis will produce an executable foobar
after checking foobar.c for standard
compliance.Specify a function library to be used at link time.The most common example of this is when compiling a
program that uses some of the mathematical functions in C.
Unlike most other platforms, these are in a separate
library from the standard C one and you have to tell the
compiler to add it.The rule is that if the library is called
libsomething.a,
you give cc the argument
.
For example, the math library is
libm.a, so you give
cc the argument .
A common gotcha with the math library is
that it has to be the last library on the command
line.&prompt.user; cc -o foobar foobar.c -lmThis will link the math library functions into
foobar.If you are compiling C++ code, you need to add
, or if
you are using FreeBSD 2.2 or later, to the command line
argument to link the C++ library functions.
Alternatively, you can run c++ instead
of cc, which does this for you.
c++ can also be invoked as
g++ on FreeBSD.&prompt.user; cc -o foobar foobar.cc -lg++For FreeBSD 2.1.6 and earlier
&prompt.user; cc -o foobar foobar.cc -lstdc++For FreeBSD 2.2 and later
&prompt.user; c++ -o foobar foobar.ccEach of these will both produce an executable
foobar from the C++ source file
foobar.cc. Note that, on &unix;
systems, C++ source files traditionally end in
.C, .cxx or
.cc, rather than the
&ms-dos; style
.cpp (which was already used for
something else). gcc used to rely on
this to work out what kind of compiler to use on the
source file; however, this restriction no longer applies,
so you may now call your C++ files
.cpp with impunity!Common cc Queries and ProblemsI am trying to write a program which uses the
sin() function and I get an error
like this. What does it mean?/var/tmp/cc0143941.o: Undefined symbol `_sin' referenced from text segment
When using mathematical functions like
sin(), you have to tell
cc to link in the math library, like
so:&prompt.user; cc -o foobar foobar.c -lmAll right, I wrote this simple program to practice
using . All it does is raise 2.1 to
the power of 6.#include <stdio.h>
int main() {
float f;
f = pow(2.1, 6);
printf("2.1 ^ 6 = %f\n", f);
return 0;
}
and I compiled it as:&prompt.user; cc temp.c -lmlike you said I should, but I get this when I run
it:&prompt.user; ./a.out
2.1 ^ 6 = 1023.000000
This is not the right answer!
What is going on?When the compiler sees you call a function, it
checks if it has already seen a prototype for it. If it
has not, it assumes the function returns an
int, which is definitely not what you want
here.So how do I fix this?The prototypes for the mathematical functions are in
math.h. If you include this file,
the compiler will be able to find the prototype and it
will stop doing strange things to your
calculation!#include <math.h>
#include <stdio.h>
int main() {
...
After recompiling it as you did before, run
it:&prompt.user; ./a.out
2.1 ^ 6 = 85.766121
If you are using any of the mathematical functions,
always include
math.h and remember to link in the
math library.I compiled a file called
foobar.c and I cannot find an
- executable called foobar. Where's
+ executable called foobar. Where has
it gone?Remember, cc will call the
executable a.out unless you tell it
differently. Use the
option:&prompt.user; cc -o foobar foobar.cOK, I have an executable called
foobar, I can see it when I run
ls, but when I type in
foobar at the command prompt it tells
me there is no such file. Why can it not find
it?Unlike &ms-dos;, &unix; does not
look in the current directory when it is trying to find
out which executable you want it to run, unless you tell
it to. Either type ./foobar, which
means run the file called
foobar in the current
directory, or change your PATH
environment
variable so that it looks something likebin:/usr/bin:/usr/local/bin:.
The dot at the end means look in the current
directory if it is not in any of the
others.I called my executable test,
but nothing happens when I run it. What is going
on?Most &unix; systems have a program called
test in /usr/bin
and the shell is picking that one up before it gets to
checking the current directory. Either type:&prompt.user; ./testor choose a better name for your program!I compiled my program and it seemed to run all right
at first, then there was an error and it said something
about core dumped. What does that
mean?The name core dump dates back
to the very early days of &unix;, when the machines used
core memory for storing data. Basically, if the program
failed under certain conditions, the system would write
the contents of core memory to disk in a file called
core, which the programmer could
then pore over to find out what went wrong.Fascinating stuff, but what I am supposed to do
now?Use gdb to analyze the core (see
).When my program dumped core, it said something about
a segmentation fault. What is
that?This basically means that your program tried to
perform some sort of illegal operation on memory; &unix;
is designed to protect the operating system and other
programs from rogue programs.Common causes for this are:Trying to write to a NULL
pointer, egchar *foo = NULL;
strcpy(foo, "bang!");
Using a pointer that has not been initialized,
egchar *foo;
strcpy(foo, "bang!");
The pointer will have some random value that,
with luck, will point into an area of memory that
is not available to your program and the kernel will
kill your program before it can do any damage. If
you are unlucky, it will point somewhere inside your
own program and corrupt one of your data structures,
causing the program to fail mysteriously.Trying to access past the end of an array,
egint bar[20];
bar[27] = 6;
Trying to store something in read-only memory,
egchar *foo = "My string";
strcpy(foo, "bang!");
&unix; compilers often put string literals like
"My string" into read-only areas
of memory.Doing naughty things with
malloc() and
free(), egchar bar[80];
free(bar);
orchar *foo = malloc(27);
free(foo);
free(foo);
Making one of these mistakes will not always lead to
an error, but they are always bad practice. Some
systems and compilers are more tolerant than others,
which is why programs that ran well on one system can
crash when you try them on an another.Sometimes when I get a core dump it says
bus error. It says in my &unix;
book that this means a hardware problem, but the
computer still seems to be working. Is this
true?No, fortunately not (unless of course you really do
have a hardware problem…). This is usually
another way of saying that you accessed memory in a way
you should not have.This dumping core business sounds as though it could
be quite useful, if I can make it happen when I want to.
Can I do this, or do I have to wait until there is an
error?Yes, just go to another console or xterm, do&prompt.user; psto find out the process ID of your program, and
do&prompt.user; kill -ABRT pidwhere
pid is
the process ID you looked up.This is useful if your program has got stuck in an
infinite loop, for instance. If your program happens to
trap SIGABRT, there are several other
signals which have a similar effect.Alternatively, you can create a core dump from
inside your program, by calling the
abort() function. See the manual page
of &man.abort.3; to learn more.If you want to create a core dump from outside your
program, but do not want the process to terminate, you
can use the gcore program. See the
manual page of &man.gcore.1; for more information.MakeWhat is make?When you are working on a simple program with only one or
two source files, typing in&prompt.user; cc file1.c file2.cis not too bad, but it quickly becomes very tedious when
there are several files—and it can take a while to
compile, too.One way to get around this is to use object files and only
recompile the source file if the source code has changed. So
we could have something like:&prompt.user; cc file1.o file2.o … file37.c …if we had changed file37.c, but not any
of the others, since the last time we compiled. This may
speed up the compilation quite a bit, but does not solve the
typing problem.Or we could write a shell script to solve the typing
problem, but it would have to re-compile everything, making it
very inefficient on a large project.What happens if we have hundreds of source files lying
about? What if we are working in a team with other people who
forget to tell us when they have changed one of their source
files that we use?Perhaps we could put the two solutions together and write
something like a shell script that would contain some kind of
magic rule saying when a source file needs compiling. Now all
we need now is a program that can understand these rules, as
it is a bit too complicated for the shell.This program is called make. It reads
in a file, called a makefile, that
tells it how different files depend on each other, and works
out which files need to be re-compiled and which ones do not.
For example, a rule could say something like if
fromboz.o is older than
fromboz.c, that means someone must have
changed fromboz.c, so it needs to be
re-compiled. The makefile also has rules telling
make how to re-compile the source file,
making it a much more powerful tool.Makefiles are typically kept in the same directory as the
source they apply to, and can be called
makefile, Makefile
or MAKEFILE. Most programmers use the
name Makefile, as this puts it near the
top of a directory listing, where it can easily be
seen.
They do not use the MAKEFILE form
as block capitals are often used for documentation files
like README.Example of using makeHere is a very simple make file:foo: foo.c
cc -o foo foo.cIt consists of two lines, a dependency line and a creation
line.The dependency line here consists of the name of the
program (known as the target), followed
by a colon, then whitespace, then the name of the source file.
When make reads this line, it looks to see
if foo exists; if it exists, it compares
the time foo was last modified to the
time foo.c was last modified. If
foo does not exist, or is older than
foo.c, it then looks at the creation line
to find out what to do. In other words, this is the rule for
working out when foo.c needs to be
re-compiled.The creation line starts with a tab (press
the tab key) and then the command you would
type to create foo if you were doing it
at a command prompt. If foo is out of
date, or does not exist, make then executes
this command to create it. In other words, this is the rule
which tells make how to re-compile
foo.c.So, when you type make, it will
make sure that foo is up to date with
respect to your latest changes to foo.c.
This principle can be extended to
Makefiles with hundreds of
targets—in fact, on FreeBSD, it is possible to compile
the entire operating system just by typing make
world in the appropriate directory!Another useful property of makefiles is that the targets
do not have to be programs. For instance, we could have a make
file that looks like this:foo: foo.c
cc -o foo foo.c
install:
cp foo /home/meWe can tell make which target we want to make by
typing:&prompt.user; make targetmake will then only look at that target
and ignore any others. For example, if we type
make foo with the makefile above, make
will ignore the install target.If we just type make on its own,
make will always look at the first target and then stop
without looking at any others. So if we typed
make here, it will just go to the
foo target, re-compile
foo if necessary, and then stop without
going on to the install target.Notice that the install target does not
actually depend on anything! This means that the command on
the following line is always executed when we try to make that
target by typing make install. In this
case, it will copy foo into the user's
home directory. This is often used by application makefiles,
so that the application can be installed in the correct
directory when it has been correctly compiled.This is a slightly confusing subject to try to explain.
If you do not quite understand how make
works, the best thing to do is to write a simple program like
hello world and a make file like the one above
and experiment. Then progress to using more than one source
file, or having the source file include a header file. The
touch command is very useful here—it
changes the date on a file without you having to edit
it.Make and include-filesC code often starts with a list of files to include, for
example stdio.h. Some of these files are system-include
files, some of them are from the project you are now working
on:
#include <stdio.h>
#include "foo.h"
int main(....To make sure that this file is recompiled the moment
foo.h is changed, you have to add it in
your Makefile:foo: foo.c foo.hThe moment your project is getting bigger and you have
more and more own include-files to maintain, it will be a
pain to keep track of all include files and the files which
are depending on it. If you change an include-file but
forget to recompile all the files which are depending on
it, the results will be devastating. gcc
has an option to analyze your files and to produce a list
of include-files and their dependencies: .
If you add this to your Makefile:depend:
gcc -E -MM *.c > .dependand run make depend, the file
.depend will appear with a list of
object-files, C-files and the include-files:foo.o: foo.c foo.hIf you change foo.h, next time
you run make all files depending on
foo.h will be recompiled.Do not forget to run make depend each
time you add an include-file to one of your files.FreeBSD MakefilesMakefiles can be rather complicated to write. Fortunately,
BSD-based systems like FreeBSD come with some very powerful
ones as part of the system. One very good example of this is
the FreeBSD ports system. Here is the essential part of a
typical ports Makefile:MASTER_SITES= ftp://freefall.cdrom.com/pub/FreeBSD/LOCAL_PORTS/
DISTFILES= scheme-microcode+dist-7.3-freebsd.tgz
.include <bsd.port.mk>Now, if we go to the directory for this port and type
make, the following happens:A check is made to see if the source code for this
port is already on the system.If it is not, an FTP connection to the URL in
MASTER_SITES is set up to download the
source.The checksum for the source is calculated and compared
it with one for a known, good, copy of the source. This
is to make sure that the source was not corrupted while in
transit.Any changes required to make the source work on
FreeBSD are applied—this is known as
patching.Any special configuration needed for the source is
done. (Many &unix; program distributions try to work out
which version of &unix; they are being compiled on and which
optional &unix; features are present—this is where
they are given the information in the FreeBSD ports
scenario).The source code for the program is compiled. In
effect, we change to the directory where the source was
unpacked and do make—the
program's own make file has the necessary information to
build the program.We now have a compiled version of the program. If we
wish, we can test it now; when we feel confident about the
program, we can type make install.
This will cause the program and any supporting files it
needs to be copied into the correct location; an entry is
also made into a package database, so
that the port can easily be uninstalled later if we change
our mind about it.Now I think you will agree that is rather impressive for a
four line script!The secret lies in the last line, which tells
make to look in the system makefile called
bsd.port.mk. It is easy to overlook this
line, but this is where all the clever stuff comes
from—someone has written a makefile that tells
make to do all the things above (plus a
couple of other things I did not mention, including handling
any errors that may occur) and anyone can get access to that
just by putting a single line in their own make file!If you want to have a look at these system makefiles,
they are in /usr/share/mk, but it is
probably best to wait until you have had a bit of practice with
makefiles, as they are very complicated (and if you do look at
them, make sure you have a flask of strong coffee
handy!)More advanced uses of makeMake is a very powerful tool, and can
do much more than the simple example above shows.
Unfortunately, there are several different versions of
make, and they all differ considerably.
The best way to learn what they can do is probably to read the
documentation—hopefully this introduction will have
given you a base from which you can do this.The version of make that comes with FreeBSD is the
Berkeley make; there is a tutorial
for it in /usr/share/doc/psd/12.make. To
view it, do&prompt.user; zmore paper.ascii.gzin that directory.Many applications in the ports use GNU
make, which has a very good set of
info pages. If you have installed any of these
ports, GNU make will automatically
have been installed as gmake. It is also
available as a port and package in its own right.To view the info pages for GNU
make, you will have to edit the
dir file in the
/usr/local/info directory to add an entry
for it. This involves adding a line like * Make: (make). The GNU Make utility.to the file. Once you have done this, you can type
info and then select
make from the menu (or in
Emacs, do C-h
i).DebuggingThe DebuggerThe debugger that comes with FreeBSD is called
gdb (GNU
debugger). You start it up by typing&prompt.user; gdb prognamealthough most people prefer to run it inside
Emacs. You can do this by:M-x gdb RET progname RETUsing a debugger allows you to run the program under more
controlled circumstances. Typically, you can step through the
program a line at a time, inspect the value of variables,
change them, tell the debugger to run up to a certain point
and then stop, and so on. You can even attach to a program
that is already running, or load a core file to investigate why
the program crashed. It is even possible to debug the kernel,
though that is a little trickier than the user applications
we will be discussing in this section.gdb has quite good on-line help, as
well as a set of info pages, so this section will concentrate
on a few of the basic commands.Finally, if you find its text-based command-prompt style
off-putting, there is a graphical front-end for it (xxgdb) in the ports
collection.This section is intended to be an introduction to using
gdb and does not cover specialized topics
such as debugging the kernel.Running a program in the debuggerYou will need to have compiled the program with the
option to get the most out of using
gdb. It will work without, but you will only
see the name of the function you are in, instead of the source
code. If you see a line like:… (no debugging symbols found) …when gdb starts up, you will know that
the program was not compiled with the
option.At the gdb prompt, type
break main. This will tell the
debugger to skip over the preliminary set-up code in the
program and start at the beginning of your code. Now type
run to start the program—it will
start at the beginning of the set-up code and then get stopped
by the debugger when it calls main().
(If you have ever wondered where main()
gets called from, now you know!).You can now step through the program, a line at a time, by
pressing n. If you get to a function call,
you can step into it by pressing s. Once
you are in a function call, you can return from stepping into a
function call by pressing f. You can also
use up and down to take
a quick look at the caller.Here is a simple example of how to spot a mistake in a
program with gdb. This is our program
(with a deliberate mistake):#include <stdio.h>
int bazz(int anint);
main() {
int i;
printf("This is my program\n");
bazz(i);
return 0;
}
int bazz(int anint) {
printf("You gave me %d\n", anint);
return anint;
}This program sets i to be
5 and passes it to a function
bazz() which prints out the number we
gave it.When we compile and run the program we get&prompt.user; cc -g -o temp temp.c
&prompt.user; ./temp
This is my program
anint = 4231That was not what we expected! Time to see what is going
on!&prompt.user; gdb temp
GDB is free software and you are welcome to distribute copies of it
under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for details.
GDB 4.13 (i386-unknown-freebsd), Copyright 1994 Free Software Foundation, Inc.
(gdb) break mainSkip the set-up code
Breakpoint 1 at 0x160f: file temp.c, line 9. gdb puts breakpoint at main()
(gdb) runRun as far as main()
Starting program: /home/james/tmp/temp Program starts running
Breakpoint 1, main () at temp.c:9 gdb stops at main()
(gdb) nGo to next line
This is my program Program prints out
(gdb) sstep into bazz()
bazz (anint=4231) at temp.c:17 gdb displays stack frame
(gdb)Hang on a minute! How did anint get to be
4231? Did we not we set it to be
5 in main()? Let's
move up to main() and have a look.(gdb) upMove up call stack
#1 0x1625 in main () at temp.c:11 gdb displays stack frame
(gdb) p iShow us the value of i
$1 = 4231 gdb displays 4231Oh dear! Looking at the code, we forgot to initialize
i. We meant to put…
main() {
int i;
i = 5;
printf("This is my program\n");
…but we left the i=5; line out. As we
did not initialize i, it had whatever number
happened to be in that area of memory when the program ran,
which in this case happened to be
4231.gdb displays the stack frame every
time we go into or out of a function, even if we are using
up and down to move
around the call stack. This shows the name of the function
and the values of its arguments, which helps us keep track
of where we are and what is going on. (The stack is a
storage area where the program stores information about the
arguments passed to functions and where to go when it
returns from a function call).Examining a core fileA core file is basically a file which contains the
complete state of the process when it crashed. In the
good old days, programmers had to print out hex
listings of core files and sweat over machine code manuals,
but now life is a bit easier. Incidentally, under FreeBSD and
other 4.4BSD systems, a core file is called
progname.core instead of just
core, to make it clearer which program a
core file belongs to.To examine a core file, start up gdb in
the usual way. Instead of typing break or
run, type(gdb) core progname.coreIf you are not in the same directory as the core file,
you will have to do dir
/path/to/core/file first.You should see something like this:&prompt.user; gdb a.out
GDB is free software and you are welcome to distribute copies of it
under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for details.
GDB 4.13 (i386-unknown-freebsd), Copyright 1994 Free Software Foundation, Inc.
(gdb) core a.out.core
Core was generated by `a.out'.
Program terminated with signal 11, Segmentation fault.
Cannot access memory at address 0x7020796d.
#0 0x164a in bazz (anint=0x5) at temp.c:17
(gdb)In this case, the program was called
a.out, so the core file is called
a.out.core. We can see that the program
crashed due to trying to access an area in memory that was not
available to it in a function called
bazz.Sometimes it is useful to be able to see how a function was
called, as the problem could have occurred a long way up the
call stack in a complex program. The bt
command causes gdb to print out a
back-trace of the call stack:(gdb) bt
#0 0x164a in bazz (anint=0x5) at temp.c:17
#1 0xefbfd888 in end ()
#2 0x162c in main () at temp.c:11
(gdb)The end() function is called when a
program crashes; in this case, the bazz()
function was called from main().Attaching to a running programOne of the neatest features about gdb
is that it can attach to a program that is already running. Of
course, that assumes you have sufficient permissions to do so.
A common problem is when you are stepping through a program
that forks, and you want to trace the child, but the debugger
will only let you trace the parent.What you do is start up another gdb,
use ps to find the process ID for the
child, and do(gdb) attach pidin gdb, and then debug as usual.That is all very well, you are probably
thinking, but by the time I have done that, the child
process will be over the hill and far away. Fear
not, gentle reader, here is how to do it (courtesy of the
gdb info pages):…
if ((pid = fork()) < 0) /* _Always_ check this */
error();
else if (pid == 0) { /* child */
int PauseMode = 1;
while (PauseMode)
sleep(10); /* Wait until someone attaches to us */
…
} else { /* parent */
…Now all you have to do is attach to the child, set
PauseMode to 0, and wait
for the sleep() call to return!Using Emacs as a Development EnvironmentEmacsUnfortunately, &unix; systems do not come with the kind of
everything-you-ever-wanted-and-lots-more-you-did-not-in-one-gigantic-package
integrated development environments that other systems
have.
Some powerful, free IDEs now exist, such as KDevelop
in the ports collection.
However, it is possible to set up your own environment. It
may not be as pretty, and it may not be quite as integrated,
but you can set it up the way you want it. And it is free.
And you have the source to it.The key to it all is Emacs. Now there are some people who
loathe it, but many who love it. If you are one of the former,
I am afraid this section will hold little of interest to you.
Also, you will need a fair amount of memory to run it—I would
recommend 8MB in text mode and 16MB in X as the bare minimum
to get reasonable performance.Emacs is basically a highly customizable
editor—indeed, it has been customized to the point where
it is more like an operating system than an editor! Many
developers and sysadmins do in fact spend practically all
their time working inside Emacs, leaving it only to log
out.It is impossible even to summarize everything Emacs can do
here, but here are some of the features of interest to
developers:Very powerful editor, allowing search-and-replace on
both strings and regular expressions (patterns), jumping
to start/end of block expression, etc, etc.Pull-down menus and online help.Language-dependent syntax highlighting and
indentation.Completely customizable.You can compile and debug programs within
Emacs.On a compilation error, you can jump to the offending
line of source code.Friendly-ish front-end to the info
program used for reading GNU hypertext documentation,
including the documentation on Emacs itself.Friendly front-end to gdb, allowing
you to look at the source code as you step through your
program.You can read Usenet news and mail while your program
is compiling.And doubtless many more that I have overlooked.Emacs can be installed on FreeBSD using the Emacs
port.Once it is installed, start it up and do C-h
t to read an Emacs tutorial—that means
hold down the control key, press
h, let go of the control
key, and then press t. (Alternatively, you
can you use the mouse to select Emacs
Tutorial from the Help
menu).Although Emacs does have menus, it is well worth learning
the key bindings, as it is much quicker when you are editing
something to press a couple of keys than to try to find the
mouse and then click on the right place. And, when you are
talking to seasoned Emacs users, you will find they often
casually throw around expressions like M-x
replace-s RET foo RET bar RET so it is
useful to know what they mean. And in any case, Emacs has far
too many useful functions for them to all fit on the menu
bars.Fortunately, it is quite easy to pick up the key-bindings,
as they are displayed next to the menu item. My advice is to
use the menu item for, say, opening a file until you
understand how it works and feel confident with it, then try
doing C-x C-f. When you are happy with that, move on to
another menu command.If you can not remember what a particular combination of
keys does, select Describe Key from
the Help menu and type it in—Emacs
will tell you what it does. You can also use the
Command Apropos menu item to find
out all the commands which contain a particular word in them,
with the key binding next to it.By the way, the expression above means hold down the
Meta key, press x, release
the Meta key, type
replace-s (short for
replace-string—another feature of
Emacs is that you can abbreviate commands), press the
return key, type foo
(the string you want replaced), press the
return key, type bar (the string you want to
replace foo with) and press
return again. Emacs will then do the
search-and-replace operation you have just requested.If you are wondering what on earth the
Meta key is, it is a special key that many
&unix; workstations have. Unfortunately, PC's do not have one,
so it is usually the alt key (or if you are
unlucky, the escape key).Oh, and to get out of Emacs, do C-x C-c
(that means hold down the control key, press
x, press c and release the
control key). If you have any unsaved files
open, Emacs will ask you if you want to save them. (Ignore
the bit in the documentation where it says
C-z is the usual way to leave
Emacs—that leaves Emacs hanging around in the
background, and is only really useful if you are on a system
which does not have virtual terminals).Configuring EmacsEmacs does many wonderful things; some of them are built
in, some of them need to be configured.Instead of using a proprietary macro language for
configuration, Emacs uses a version of Lisp specially adapted
for editors, known as Emacs Lisp. Working with Emacs Lisp can
be quite helpful if you want to go on and learn something like
Common Lisp. Emacs Lisp has many features of Common Lisp,
although it is considerably smaller (and thus easier to
master).The best way to learn Emacs Lisp is to download the Emacs
TutorialHowever, there is no need to actually know any Lisp to get
started with configuring Emacs, as I have included a sample
.emacs file, which should be enough to
get you started. Just copy it into your home directory and
restart Emacs if it is already running; it will read the
commands from the file and (hopefully) give you a useful basic
setup.A sample .emacs fileUnfortunately, there is far too much here to explain it in
detail; however there are one or two points worth
mentioning.Everything beginning with a ; is a comment
and is ignored by Emacs.In the first line, the
-*- Emacs-Lisp -*- is so that
we can edit the .emacs file itself
within Emacs and get all the fancy features for editing
Emacs Lisp. Emacs usually tries to guess this based on
the filename, and may not get it right for
.emacs.The tab key is bound to an
indentation function in some modes, so when you press the
tab key, it will indent the current line of code. If you
want to put a tab character in whatever
you are writing, hold the control key down
while you are pressing the tab key.This file supports syntax highlighting for C, C++,
Perl, Lisp and Scheme, by guessing the language from the
filename.Emacs already has a pre-defined function called
next-error. In a compilation output
window, this allows you to move from one compilation error
to the next by doing M-n; we define a
complementary function,
previous-error, that allows you to go
to a previous error by doing M-p. The
nicest feature of all is that C-c C-c
will open up the source file in which the error occurred
and jump to the appropriate line.We enable Emacs's ability to act as a server, so that
if you are doing something outside Emacs and you want to
edit a file, you can just type in&prompt.user; emacsclient filenameand then you can edit the file in your
Emacs!
Many Emacs users set their EDITOR
environment to
emacsclient so this happens every
time they need to edit a file.A sample .emacs file;; -*-Emacs-Lisp-*-
;; This file is designed to be re-evaled; use the variable first-time
;; to avoid any problems with this.
(defvar first-time t
"Flag signifying this is the first time that .emacs has been evaled")
;; Meta
(global-set-key "\M- " 'set-mark-command)
(global-set-key "\M-\C-h" 'backward-kill-word)
(global-set-key "\M-\C-r" 'query-replace)
(global-set-key "\M-r" 'replace-string)
(global-set-key "\M-g" 'goto-line)
(global-set-key "\M-h" 'help-command)
;; Function keys
(global-set-key [f1] 'manual-entry)
(global-set-key [f2] 'info)
(global-set-key [f3] 'repeat-complex-command)
(global-set-key [f4] 'advertised-undo)
(global-set-key [f5] 'eval-current-buffer)
(global-set-key [f6] 'buffer-menu)
(global-set-key [f7] 'other-window)
(global-set-key [f8] 'find-file)
(global-set-key [f9] 'save-buffer)
(global-set-key [f10] 'next-error)
(global-set-key [f11] 'compile)
(global-set-key [f12] 'grep)
(global-set-key [C-f1] 'compile)
(global-set-key [C-f2] 'grep)
(global-set-key [C-f3] 'next-error)
(global-set-key [C-f4] 'previous-error)
(global-set-key [C-f5] 'display-faces)
(global-set-key [C-f8] 'dired)
(global-set-key [C-f10] 'kill-compilation)
;; Keypad bindings
(global-set-key [up] "\C-p")
(global-set-key [down] "\C-n")
(global-set-key [left] "\C-b")
(global-set-key [right] "\C-f")
(global-set-key [home] "\C-a")
(global-set-key [end] "\C-e")
(global-set-key [prior] "\M-v")
(global-set-key [next] "\C-v")
(global-set-key [C-up] "\M-\C-b")
(global-set-key [C-down] "\M-\C-f")
(global-set-key [C-left] "\M-b")
(global-set-key [C-right] "\M-f")
(global-set-key [C-home] "\M-<")
(global-set-key [C-end] "\M->")
(global-set-key [C-prior] "\M-<")
(global-set-key [C-next] "\M->")
;; Mouse
(global-set-key [mouse-3] 'imenu)
;; Misc
(global-set-key [C-tab] "\C-q\t") ; Control tab quotes a tab.
(setq backup-by-copying-when-mismatch t)
;; Treat 'y' or <CR> as yes, 'n' as no.
(fset 'yes-or-no-p 'y-or-n-p)
(define-key query-replace-map [return] 'act)
(define-key query-replace-map [?\C-m] 'act)
;; Load packages
(require 'desktop)
(require 'tar-mode)
;; Pretty diff mode
(autoload 'ediff-buffers "ediff" "Intelligent Emacs interface to diff" t)
(autoload 'ediff-files "ediff" "Intelligent Emacs interface to diff" t)
(autoload 'ediff-files-remote "ediff"
"Intelligent Emacs interface to diff")
(if first-time
(setq auto-mode-alist
(append '(("\\.cpp$" . c++-mode)
("\\.hpp$" . c++-mode)
("\\.lsp$" . lisp-mode)
("\\.scm$" . scheme-mode)
("\\.pl$" . perl-mode)
) auto-mode-alist)))
;; Auto font lock mode
(defvar font-lock-auto-mode-list
(list 'c-mode 'c++-mode 'c++-c-mode 'emacs-lisp-mode 'lisp-mode 'perl-mode 'scheme-mode)
"List of modes to always start in font-lock-mode")
(defvar font-lock-mode-keyword-alist
'((c++-c-mode . c-font-lock-keywords)
(perl-mode . perl-font-lock-keywords))
"Associations between modes and keywords")
(defun font-lock-auto-mode-select ()
"Automatically select font-lock-mode if the current major mode is in font-lock-auto-mode-list"
(if (memq major-mode font-lock-auto-mode-list)
(progn
(font-lock-mode t))
)
)
(global-set-key [M-f1] 'font-lock-fontify-buffer)
;; New dabbrev stuff
;(require 'new-dabbrev)
(setq dabbrev-always-check-other-buffers t)
(setq dabbrev-abbrev-char-regexp "\\sw\\|\\s_")
(add-hook 'emacs-lisp-mode-hook
'(lambda ()
(set (make-local-variable 'dabbrev-case-fold-search) nil)
(set (make-local-variable 'dabbrev-case-replace) nil)))
(add-hook 'c-mode-hook
'(lambda ()
(set (make-local-variable 'dabbrev-case-fold-search) nil)
(set (make-local-variable 'dabbrev-case-replace) nil)))
(add-hook 'text-mode-hook
'(lambda ()
(set (make-local-variable 'dabbrev-case-fold-search) t)
(set (make-local-variable 'dabbrev-case-replace) t)))
;; C++ and C mode...
(defun my-c++-mode-hook ()
(setq tab-width 4)
(define-key c++-mode-map "\C-m" 'reindent-then-newline-and-indent)
(define-key c++-mode-map "\C-ce" 'c-comment-edit)
(setq c++-auto-hungry-initial-state 'none)
(setq c++-delete-function 'backward-delete-char)
(setq c++-tab-always-indent t)
(setq c-indent-level 4)
(setq c-continued-statement-offset 4)
(setq c++-empty-arglist-indent 4))
(defun my-c-mode-hook ()
(setq tab-width 4)
(define-key c-mode-map "\C-m" 'reindent-then-newline-and-indent)
(define-key c-mode-map "\C-ce" 'c-comment-edit)
(setq c-auto-hungry-initial-state 'none)
(setq c-delete-function 'backward-delete-char)
(setq c-tab-always-indent t)
;; BSD-ish indentation style
(setq c-indent-level 4)
(setq c-continued-statement-offset 4)
(setq c-brace-offset -4)
(setq c-argdecl-indent 0)
(setq c-label-offset -4))
;; Perl mode
(defun my-perl-mode-hook ()
(setq tab-width 4)
(define-key c++-mode-map "\C-m" 'reindent-then-newline-and-indent)
(setq perl-indent-level 4)
(setq perl-continued-statement-offset 4))
;; Scheme mode...
(defun my-scheme-mode-hook ()
(define-key scheme-mode-map "\C-m" 'reindent-then-newline-and-indent))
;; Emacs-Lisp mode...
(defun my-lisp-mode-hook ()
(define-key lisp-mode-map "\C-m" 'reindent-then-newline-and-indent)
(define-key lisp-mode-map "\C-i" 'lisp-indent-line)
(define-key lisp-mode-map "\C-j" 'eval-print-last-sexp))
;; Add all of the hooks...
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
(add-hook 'c-mode-hook 'my-c-mode-hook)
(add-hook 'scheme-mode-hook 'my-scheme-mode-hook)
(add-hook 'emacs-lisp-mode-hook 'my-lisp-mode-hook)
(add-hook 'lisp-mode-hook 'my-lisp-mode-hook)
(add-hook 'perl-mode-hook 'my-perl-mode-hook)
;; Complement to next-error
(defun previous-error (n)
"Visit previous compilation error message and corresponding source code."
(interactive "p")
(next-error (- n)))
;; Misc...
(transient-mark-mode 1)
(setq mark-even-if-inactive t)
(setq visible-bell nil)
(setq next-line-add-newlines nil)
(setq compile-command "make")
(setq suggest-key-bindings nil)
(put 'eval-expression 'disabled nil)
(put 'narrow-to-region 'disabled nil)
(put 'set-goal-column 'disabled nil)
(if (>= emacs-major-version 21)
(setq show-trailing-whitespace t))
;; Elisp archive searching
(autoload 'format-lisp-code-directory "lispdir" nil t)
(autoload 'lisp-dir-apropos "lispdir" nil t)
(autoload 'lisp-dir-retrieve "lispdir" nil t)
(autoload 'lisp-dir-verify "lispdir" nil t)
;; Font lock mode
(defun my-make-face (face color &optional bold)
"Create a face from a color and optionally make it bold"
(make-face face)
(copy-face 'default face)
(set-face-foreground face color)
(if bold (make-face-bold face))
)
(if (eq window-system 'x)
(progn
(my-make-face 'blue "blue")
(my-make-face 'red "red")
(my-make-face 'green "dark green")
(setq font-lock-comment-face 'blue)
(setq font-lock-string-face 'bold)
(setq font-lock-type-face 'bold)
(setq font-lock-keyword-face 'bold)
(setq font-lock-function-name-face 'red)
(setq font-lock-doc-string-face 'green)
(add-hook 'find-file-hooks 'font-lock-auto-mode-select)
(setq baud-rate 1000000)
(global-set-key "\C-cmm" 'menu-bar-mode)
(global-set-key "\C-cms" 'scroll-bar-mode)
(global-set-key [backspace] 'backward-delete-char)
; (global-set-key [delete] 'delete-char)
(standard-display-european t)
(load-library "iso-transl")))
;; X11 or PC using direct screen writes
(if window-system
(progn
;; (global-set-key [M-f1] 'hilit-repaint-command)
;; (global-set-key [M-f2] [?\C-u M-f1])
(setq hilit-mode-enable-list
'(not text-mode c-mode c++-mode emacs-lisp-mode lisp-mode
scheme-mode)
hilit-auto-highlight nil
hilit-auto-rehighlight 'visible
hilit-inhibit-hooks nil
hilit-inhibit-rebinding t)
(require 'hilit19)
(require 'paren))
(setq baud-rate 2400) ; For slow serial connections
)
;; TTY type terminal
(if (and (not window-system)
(not (equal system-type 'ms-dos)))
(progn
(if first-time
(progn
(keyboard-translate ?\C-h ?\C-?)
(keyboard-translate ?\C-? ?\C-h)))))
;; Under UNIX
(if (not (equal system-type 'ms-dos))
(progn
(if first-time
(server-start))))
;; Add any face changes here
(add-hook 'term-setup-hook 'my-term-setup-hook)
(defun my-term-setup-hook ()
(if (eq window-system 'pc)
(progn
;; (set-face-background 'default "red")
)))
;; Restore the "desktop" - do this as late as possible
(if first-time
(progn
(desktop-load-default)
(desktop-read)))
;; Indicate that this file has been read at least once
(setq first-time nil)
;; No need to debug anything now
(setq debug-on-error nil)
;; All done
(message "All done, %s%s" (user-login-name) ".")
Extending the Range of Languages Emacs UnderstandsNow, this is all very well if you only want to program in
the languages already catered for in the
.emacs file (C, C++, Perl, Lisp and
Scheme), but what happens if a new language called
whizbang comes out, full of exciting
features?The first thing to do is find out if whizbang comes with
any files that tell Emacs about the language. These usually
end in .el, short for Emacs
Lisp. For example, if whizbang is a FreeBSD port, we
can locate these files by doing&prompt.user; find /usr/ports/lang/whizbang -name "*.el" -printand install them by copying them into the Emacs site Lisp
directory. On FreeBSD 2.1.0-RELEASE, this is
/usr/local/share/emacs/site-lisp.So for example, if the output from the find command
was/usr/ports/lang/whizbang/work/misc/whizbang.elwe would do&prompt.root; cp /usr/ports/lang/whizbang/work/misc/whizbang.el /usr/local/share/emacs/site-lispNext, we need to decide what extension whizbang source
files have. Let's say for the sake of argument that they all
end in .wiz. We need to add an entry to
our .emacs file to make sure Emacs will
be able to use the information in
whizbang.el.Find the auto-mode-alist entry in
.emacs and add a line for whizbang, such
as:…
("\\.lsp$" . lisp-mode)
("\\.wiz$" . whizbang-mode)
("\\.scm$" . scheme-mode)
…This means that Emacs will automatically go into
whizbang-mode when you edit a file ending
in .wiz.Just below this, you will find the
font-lock-auto-mode-list entry. Add
whizbang-mode to it like so:;; Auto font lock mode
(defvar font-lock-auto-mode-list
(list 'c-mode 'c++-mode 'c++-c-mode 'emacs-lisp-mode 'whizbang-mode 'lisp-mode 'perl-mode 'scheme-mode)
"List of modes to always start in font-lock-mode")This means that Emacs will always enable
font-lock-mode (ie syntax highlighting)
when editing a .wiz file.And that is all that is needed. If there is anything else
you want done automatically when you open up a
.wiz file, you can add a
whizbang-mode hook (see
my-scheme-mode-hook for a simple example
that adds auto-indent).Further ReadingBrian Harvey and Matthew Wright
Simply Scheme
MIT 1994.
ISBN 0-262-08226-8Randall Schwartz
Learning Perl
O'Reilly 1993
ISBN 1-56592-042-2Patrick Henry Winston and Berthold Klaus Paul Horn
Lisp (3rd Edition)
Addison-Wesley 1989
ISBN 0-201-08319-1Brian W. Kernighan and Rob Pike
The Unix Programming Environment
Prentice-Hall 1984
ISBN 0-13-937681-XBrian W. Kernighan and Dennis M. Ritchie
The C Programming Language (2nd Edition)
Prentice-Hall 1988
ISBN 0-13-110362-8Bjarne Stroustrup
The C++ Programming Language
Addison-Wesley 1991
ISBN 0-201-53992-6W. Richard Stevens
Advanced Programming in the Unix Environment
Addison-Wesley 1992
ISBN 0-201-56317-7W. Richard Stevens
Unix Network Programming
Prentice-Hall 1990
ISBN 0-13-949876-1