aboutsummaryrefslogtreecommitdiff
path: root/documentation/content/en/books/developers-handbook/tools
diff options
context:
space:
mode:
authorSergio Carlavilla Delgado <carlavilla@FreeBSD.org>2021-06-06 12:19:15 +0000
committerSergio Carlavilla Delgado <carlavilla@FreeBSD.org>2021-06-06 12:19:15 +0000
commitd00a527f1ce922091d779911eaedde4efd122ea1 (patch)
treea36571eb6b5703a6812f1543ff09599aa26d24c3 /documentation/content/en/books/developers-handbook/tools
parenta8c5c8965cd534ff9634d95e6d84edb321c273d5 (diff)
Diffstat (limited to 'documentation/content/en/books/developers-handbook/tools')
-rw-r--r--documentation/content/en/books/developers-handbook/tools/_index.adoc468
1 files changed, 342 insertions, 126 deletions
diff --git a/documentation/content/en/books/developers-handbook/tools/_index.adoc b/documentation/content/en/books/developers-handbook/tools/_index.adoc
index f9ff8200f6..d72fb6a590 100644
--- a/documentation/content/en/books/developers-handbook/tools/_index.adoc
+++ b/documentation/content/en/books/developers-handbook/tools/_index.adoc
@@ -39,29 +39,50 @@ toc::[]
[[tools-synopsis]]
== Synopsis
-This 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(R). 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.
+This 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(R).
+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.
[[tools-intro]]
== Introduction
-FreeBSD offers an excellent development environment. Compilers for C and C++ and an assembler come with the basic system, not to mention classic UNIX(R) tools such as `sed` and `awk`. If that is not enough, there are many more compilers and interpreters in the Ports collection. The following section, <<tools-programming,Introduction to Programming>>, lists some of the available options. FreeBSD is very compatible with standards such as POSIX(R) 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.
+FreeBSD offers an excellent development environment.
+Compilers for C and C++ and an assembler come with the basic system, not to mention classic UNIX(R) tools such as `sed` and `awk`.
+If that is not enough, there are many more compilers and interpreters in the Ports collection.
+The following section, <<tools-programming,Introduction to Programming>>, lists some of the available options.
+FreeBSD is very compatible with standards such as POSIX(R) 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(R) 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.
+However, all this power can be rather overwhelming at first if you have never written programs on a UNIX(R) 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(R) and a willingness to learn!
[[tools-programming]]
== Introduction to Programming
-A 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.
+A 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.
=== Interpreters
-With 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.
+With 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.
+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(R) 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(R) philosophy was to provide lots of small utility programs that could be linked together in shell scripts to perform useful tasks.
+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(R) 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(R) philosophy was to provide lots of small utility programs that could be linked together in shell scripts to perform useful tasks.
=== Interpreters Available with FreeBSD
@@ -70,16 +91,23 @@ Here is a list of interpreters that are available from the FreeBSD Ports Collect
Instructions on how to get and install applications from the Ports Collection can be found in the link:{handbook}#ports-using/[Ports section] of the handbook.
BASIC::
-Short 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.
+Short 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 can be found in the Ports Collection as package:lang/bwbasic[] and the Phil Cockroft's Basic Interpreter (formerly Rabbit Basic) is available as package:lang/pbasic[].
Lisp::
-A 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". It is very popular in AI (Artificial Intelligence) circles.
+A 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".
+It is 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(R) systems are available in the Ports Collection for FreeBSD. GNU Common Lisp can be found as package:lang/gcl[]. CLISP by Bruno Haible and Michael Stoll is available as package:lang/clisp[]. For 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, package:lang/cmucl[] and package:lang/slisp[] are available respectively.
+Various implementations of Lisp that can run on UNIX(R) systems are available in the Ports Collection for FreeBSD.
+GNU Common Lisp can be found as package:lang/gcl[].
+CLISP by Bruno Haible and Michael Stoll is available as package:lang/clisp[].
+For 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, package:lang/cmucl[] and package:lang/slisp[] are available respectively.
Perl::
Very popular with system administrators for writing scripts; also often used on World Wide Web servers for writing CGI scripts.
@@ -87,47 +115,70 @@ Very popular with system administrators for writing scripts; also often used on
Perl is available in the Ports Collection as package:lang/perl5.24[] for all FreeBSD releases.
Scheme::
-A 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.
+A 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.
+
-Scheme is available from the Ports Collection as package:lang/elk[] for the Elk Scheme Interpreter. The MIT Scheme Interpreter can be found in package:lang/mit-scheme[] and the SCM Scheme Interpreter in package:lang/scm[].
+Scheme is available from the Ports Collection as package:lang/elk[] for the Elk Scheme Interpreter.
+The MIT Scheme Interpreter can be found in package:lang/mit-scheme[] and the SCM Scheme Interpreter in package:lang/scm[].
Icon::
-Icon is a high-level language with extensive facilities for processing strings and structures. The version of Icon for FreeBSD can be found in the Ports Collection as package:lang/icon[].
+Icon is a high-level language with extensive facilities for processing strings and structures.
+The version of Icon for FreeBSD can be found in the Ports Collection as package:lang/icon[].
Logo::
-Logo 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 to smaller age groups, as it makes creation of elaborate geometric shapes an easy task.
+Logo 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 to smaller age groups, as it makes creation of elaborate geometric shapes an easy task.
+
The latest version of Logo for FreeBSD is available from the Ports Collection in package:lang/logo[].
Python::
-Python 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).
+Python 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).
+
The latest version of Python is available from the Ports Collection in package:lang/python[].
Ruby::
-Ruby is an interpreter, pure object-oriented programming language. It has become widely popular because of its easy to understand syntax, flexibility when writing code, and the ability to easily develop and maintain large, complex programs.
+Ruby is an interpreter, pure object-oriented programming language.
+It has become widely popular because of its easy to understand syntax, flexibility when writing code, and the ability to easily develop and maintain large, complex programs.
+
Ruby is available from the Ports Collection as package:lang/ruby25[].
Tcl and Tk::
-Tcl 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.
+Tcl 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 ports for FreeBSD. The latest version, Tcl 8.5, can be found in package:lang/tcl87[].
+Various versions of Tcl are available as ports for FreeBSD.
+The latest version, Tcl 8.5, can be found in package:lang/tcl87[].
=== Compilers
-Compilers 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.footnote:[If you run it in the shell, you may get a core dump.]
+Compilers 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.footnote:[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.
+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.
-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 package:devel/kdevelop[] is available in the Ports Collection and many use Emacs for this purpose. Using Emacs as an IDE is discussed in <<emacs>>.
+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 package:devel/kdevelop[] is available in the Ports Collection and many use Emacs for this purpose.
+Using Emacs as an IDE is discussed in <<emacs>>.
[[tools-compiling]]
== Compiling with `cc`
-This section deals with the gcc and clang compilers for C and C++, since they come with the FreeBSD base system. Starting with FreeBSD 10.X `clang` is installed as `cc`. 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.
+This section deals with the gcc and clang compilers for C and C++, since they come with the FreeBSD base system.
+Starting with FreeBSD 10.X `clang` is installed as `cc`.
+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.
+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.
[.procedure]
. Pre-process your source code to remove comments and do other tricks like expanding macros in C.
@@ -139,7 +190,8 @@ Once you have written your masterpiece, the next step is to convert it into some
. 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_.
+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
@@ -148,16 +200,20 @@ Fortunately, almost all this detail is hidden from you, as `cc` is a front end t
% cc foobar.c
....
-will cause [.filename]#foobar.c# to be compiled by all the steps above. If you have more than one file to compile, just do something like
+will cause [.filename]#foobar.c# to be compiled by all the steps above.
+If you have more than one file to compile, just do something like
[source,bash]
....
% cc foo.c bar.c
....
-Note 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.footnote:[In case you did not know, a binary sort is an efficient way of sorting things into order and a bubble sort is not.]
+Note 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.footnote:[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.
+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.
`-o _filename_`::
The output name of the file. If you do not use this option, `cc` will produce an executable called [.filename]#a.out#.footnote:[The reasons for this are buried in the mists of history.]
@@ -169,17 +225,23 @@ The output name of the file. If you do not use this option, `cc` will produce an
....
`-c`::
-Just 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 [.filename]#Makefile#.
+Just 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 [.filename]#Makefile#.
+
[source,bash]
....
% cc -c foobar.c
....
+
-This will produce an _object file_ (not an executable) called [.filename]#foobar.o#. This can be linked together with other object files into an executable.
+This will produce an _object file_ (not an executable) called [.filename]#foobar.o#.
+This can be linked together with other object files into an executable.
`-g`::
-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 `-g` while you are developing a program and then compile a "release version" without `-g` when you are satisfied it works properly.
+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 `-g` while you are developing a program and then compile a "release version" without `-g` when you are satisfied it works properly.
+
[source,bash]
@@ -190,7 +252,9 @@ Create a debug version of the executable. This makes the compiler put informatio
This will produce a debug version of the program. footnote:[Note, we did not use the -o 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!]
`-O`::
-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 `-O` to specify a higher level of optimization, but this often exposes bugs in the compiler's optimizer.
+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 `-O` to specify a higher level of optimization, but this often exposes bugs in the compiler's optimizer.
+
[source,bash]
....
@@ -199,20 +263,27 @@ Create an optimized version of the executable. The compiler performs various cle
+
This will produce an optimized version of [.filename]#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.
+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.
`-Wall`::
-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.
+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.
`-ansi`::
-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 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.
`-pedantic`::
Turn off _all_ ``cc``'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_.
+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?
+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?
[source,bash]
....
@@ -224,9 +295,12 @@ This will produce an executable [.filename]#foobar# after checking [.filename]#f
`-l__library__`::
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 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 [.filename]#libsomething.a#, you give `cc` the argument `-l__something__`. For example, the math library is [.filename]#libm.a#, so you give `cc` the argument `-lm`. A common "gotcha" with the math library is that it has to be the last library on the command line.
+The rule is that if the library is called [.filename]#libsomething.a#, you give `cc` the argument `-l__something__`.
+For example, the math library is [.filename]#libm.a#, so you give `cc` the argument `-lm`.
+A common "gotcha" with the math library is that it has to be the last library on the command line.
+
[source,bash]
....
@@ -235,7 +309,8 @@ The rule is that if the library is called [.filename]#libsomething.a#, you give
+
This will link the math library functions into [.filename]#foobar#.
+
-If you are compiling C++ code, use {c-plus-plus-command}. {c-plus-plus-command} can also be invoked as {clang-plus-plus-command} on FreeBSD.
+If you are compiling C++ code, use {c-plus-plus-command}.
+{c-plus-plus-command} can also be invoked as {clang-plus-plus-command} on FreeBSD.
+
[source,bash]
....
@@ -292,11 +367,13 @@ like you said I should, but I get this when I run it:
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.
+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 [.filename]#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!
+The prototypes for the mathematical functions are in [.filename]#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!
[.programlisting]
....
@@ -319,7 +396,8 @@ If you are using any of the mathematical functions, _always_ include [.filename]
==== I compiled a file called foobar.c and I cannot find an executable called foobar. Where has it gone?
-Remember, `cc` will call the executable [.filename]#a.out# unless you tell it differently. Use the `-o _filename_` option:
+Remember, `cc` will call the executable [.filename]#a.out# unless you tell it differently.
+Use the `-o _filename_` option:
[source,bash]
....
@@ -328,11 +406,13 @@ Remember, `cc` will call the executable [.filename]#a.out# unless you tell it di
==== OK, 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(R), UNIX(R) 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. Type `./foobar`, which means "run the file called [.filename]#foobar# in the current directory."
+Unlike MS-DOS(R), UNIX(R) 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.
+Type `./foobar`, which means "run the file called [.filename]#foobar# in the current directory."
=== I called my executable test, but nothing happens when I run it. What is going on?
-Most UNIX(R) systems have a program called `test` in [.filename]#/usr/bin# and the shell is picking that one up before it gets to checking the current directory. Either type:
+Most UNIX(R) systems have a program called `test` in [.filename]#/usr/bin# and the shell is picking that one up before it gets to checking the current directory.
+Either type:
[source,bash]
....
@@ -343,7 +423,8 @@ or 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(R), 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 [.filename]#core#, which the programmer could then pore over to find out what went wrong.
+The name _core dump_ dates back to the very early days of UNIX(R), 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 [.filename]#core#, which the programmer could then pore over to find out what went wrong.
==== Fascinating stuff, but what I am supposed to do now?
@@ -351,7 +432,8 @@ Use a debugger to analyze the core (see <<debugging>>).
==== 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(R) is designed to protect the operating system and other programs from rogue programs.
+This basically means that your program tried to perform some sort of illegal operation on memory;
+UNIX(R) is designed to protect the operating system and other programs from rogue programs.
Common causes for this are:
@@ -371,7 +453,8 @@ char *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.
+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, eg
+
[.programlisting]
@@ -406,11 +489,14 @@ 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.
+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(R) 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.
+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?
@@ -430,11 +516,14 @@ to find out the process ID of your program, and do
where `_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.
+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.
+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.
+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.
[[tools-make]]
== Make
@@ -450,24 +539,31 @@ When you are working on a simple program with only one or two source files, typi
is 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:
+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:
[source,bash]
....
% cc file1.o file2.o … file37.c …
....
-if we had changed [.filename]#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.
+if we had changed [.filename]#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.
+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 [.filename]#fromboz.o# is older than [.filename]#fromboz.c#, that means someone must have changed [.filename]#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.
+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 [.filename]#fromboz.o# is older than [.filename]#fromboz.c#, that means someone must have changed [.filename]#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 [.filename]#makefile#, [.filename]#Makefile# or [.filename]#MAKEFILE#. Most programmers use the name [.filename]#Makefile#, as this puts it near the top of a directory listing, where it can easily be seen.footnote:[They do not use the MAKEFILE form as block capitals are often used for documentation files like README.]
+Makefiles are typically kept in the same directory as the source they apply to, and can be called [.filename]#makefile#, [.filename]#Makefile# or [.filename]#MAKEFILE#.
+Most programmers use the name [.filename]#Makefile#, as this puts it near the top of a directory listing, where it can easily be seen.footnote:[They do not use the MAKEFILE form as block capitals are often used for documentation files like README.]
=== Example of Using `make`
@@ -481,13 +577,23 @@ foo: foo.c
It 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 [.filename]#foo# exists; if it exists, it compares the time [.filename]#foo# was last modified to the time [.filename]#foo.c# was last modified. If [.filename]#foo# does not exist, or is older than [.filename]#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 [.filename]#foo.c# needs to be re-compiled.
+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 [.filename]#foo# exists;
+if it exists, it compares the time [.filename]#foo# was last modified to the time [.filename]#foo.c# was last modified.
+If [.filename]#foo# does not exist, or is older than [.filename]#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 [.filename]#foo.c# needs to be re-compiled.
-The creation line starts with a tab (press kbd:[tab]) and then the command you would type to create [.filename]#foo# if you were doing it at a command prompt. If [.filename]#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 [.filename]#foo.c#.
+The creation line starts with a tab (press kbd:[tab]) and then the command you would type to create [.filename]#foo# if you were doing it at a command prompt.
+If [.filename]#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 [.filename]#foo.c#.
-So, when you type `make`, it will make sure that [.filename]#foo# is up to date with respect to your latest changes to [.filename]#foo.c#. This principle can be extended to [.filename]#Makefile#'s 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!
+So, when you type `make`, it will make sure that [.filename]#foo# is up to date with respect to your latest changes to [.filename]#foo.c#.
+This principle can be extended to [.filename]#Makefile#'s 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:
+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:
[.programlisting]
....
@@ -505,17 +611,25 @@ We can tell make which target we want to make by typing:
% make target
....
-`make` 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.
+`make` 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 [.filename]#foo# if necessary, and then stop without going on to 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 [.filename]#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 [.filename]#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.
+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 [.filename]#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. `touch` is very useful here-it changes the date on a file without you having to edit it.
+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.
+`touch` is very useful here-it changes the date on a file without you having to edit it.
=== Make and include-files
-C 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:
+C 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:
[.programlisting]
....
@@ -532,7 +646,11 @@ To make sure that this file is recompiled the moment [.filename]#foo.h# is chang
foo: foo.c foo.h
....
-The 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. `clang` has an option to analyze your files and to produce a list of include-files and their dependencies: `-MM`.
+The 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.
+`clang` has an option to analyze your files and to produce a list of include-files and their dependencies: `-MM`.
If you add this to your Makefile:
@@ -555,7 +673,10 @@ Do not forget to run `make depend` each time you add an include-file to one of y
=== FreeBSD Makefiles
-Makefiles 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 [.filename]#Makefile#:
+Makefiles 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 [.filename]#Makefile#:
[.programlisting]
....
@@ -578,15 +699,22 @@ Now, if we go to the directory for this port and type `make`, the following happ
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 [.filename]#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!
+The secret lies in the last line, which tells `make` to look in the system makefile called [.filename]#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 [.filename]#/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!)
+If you want to have a look at these system makefiles, they are in [.filename]#/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 `make`
-`Make` 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.
+`Make` 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 [.filename]#/usr/share/doc/psd/12.make#. To view it, do
+The version of make that comes with FreeBSD is the Berkeley make; there is a tutorial for it in [.filename]#/usr/share/doc/psd/12.make#.
+To view it, do
[source,bash]
....
@@ -595,9 +723,12 @@ The version of make that comes with FreeBSD is the Berkeley make; there is a tut
in 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.
+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 [.filename]#dir# in the [.filename]#/usr/local/info# directory to add an entry for it. This involves adding a line like
+To view the info pages for GNU make, you will have to edit [.filename]#dir# in the [.filename]#/usr/local/info# directory to add an entry for it.
+This involves adding a line like
[.programlisting]
....
@@ -611,20 +742,34 @@ to the file. Once you have done this, you can type `info` and then select [.guim
=== Introduction to Available Debuggers
-Using a debugger allows running the program under more controlled circumstances. Typically, it is possible to 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. It is also possible to 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.
+Using a debugger allows running the program under more controlled circumstances.
+Typically, it is possible to 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.
+It is also possible to 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.
-This section is intended to be a quick introduction to using debuggers and does not cover specialized topics such as debugging the kernel. For more information about that, refer to crossref:kerneldebug[kerneldebug,Kernel Debugging].
+This section is intended to be a quick introduction to using debuggers and does not cover specialized topics such as debugging the kernel.
+For more information about that, refer to crossref:kerneldebug[kerneldebug,Kernel Debugging].
-The standard debugger supplied with FreeBSD {rel121-current} is called `lldb` (LLVM debugger). As it is part of the standard installation for that release, there is no need to do anything special to use it. It has good command help, accessible via the `help` command, as well as https://lldb.llvm.org/[a web tutorial and documentation].
+The standard debugger supplied with FreeBSD {rel121-current} is called `lldb` (LLVM debugger).
+As it is part of the standard installation for that release, there is no need to do anything special to use it.
+It has good command help, accessible via the `help` command, as well as https://lldb.llvm.org/[a web tutorial and documentation].
[NOTE]
====
-The `lldb` command is available for FreeBSD {rel113-current} link:{handbook}#ports-using/[from ports or packages] as package:devel/llvm[]. This will install the default version of lldb (currently 9.0).
+The `lldb` command is available for FreeBSD {rel113-current} link:{handbook}#ports-using/[from ports or packages] as package:devel/llvm[].
+This will install the default version of lldb (currently 9.0).
====
-The other debugger available with FreeBSD is called `gdb` (GNU debugger). Unlike lldb, it is not installed by default on FreeBSD {rel121-current}; to use it, link:{handbook}#ports-using/[install] package:devel/gdb[] from ports or packages. The version installed by default on FreeBSD {rel113-current} is old; instead, install package:devel/gdb[] there as well. It has quite good on-line help, as well as a set of info pages.
+The other debugger available with FreeBSD is called `gdb` (GNU debugger).
+Unlike lldb, it is not installed by default on FreeBSD {rel121-current};
+to use it, link:{handbook}#ports-using/[install] package:devel/gdb[] from ports or packages.
+The version installed by default on FreeBSD {rel113-current} is old; instead, install package:devel/gdb[] there as well.
+It has quite good on-line help, as well as a set of info pages.
-Which one to use is largely a matter of taste. If familiar with one only, use that one. People familiar with neither or both but wanting to use one from inside Emacs will need to use `gdb` as `lldb` is unsupported by Emacs. Otherwise, try both and see which one you prefer.
+Which one to use is largely a matter of taste.
+If familiar with one only, use that one.
+People familiar with neither or both but wanting to use one from inside Emacs will need to use `gdb` as `lldb` is unsupported by Emacs.
+Otherwise, try both and see which one you prefer.
=== Using lldb
@@ -639,7 +784,9 @@ Start up lldb by typing
==== Running a Program with lldb
-Compile the program with `-g` to get the most out of using `lldb`. It will work without, but will only display the name of the function currently running, instead of the source code. If it displays a line like:
+Compile the program with `-g` to get the most out of using `lldb`.
+It will work without, but will only display the name of the function currently running, instead of the source code.
+If it displays a line like:
[source,bash]
....
@@ -650,15 +797,20 @@ Breakpoint 1: where = temp`main, address = …
[TIP]
====
-
-Most `lldb` commands have shorter forms that can be used instead. The longer forms are used here for clarity.
+Most `lldb` commands have shorter forms that can be used instead.
+The longer forms are used here for clarity.
====
-At the `lldb` prompt, type `breakpoint set -n main`. This will tell the debugger not to display the preliminary set-up code in the program being run and to stop execution at the beginning of the program's code. Now type `process launch` to actually 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()`.
+At the `lldb` prompt, type `breakpoint set -n main`.
+This will tell the debugger not to display the preliminary set-up code in the program being run and to stop execution at the beginning of the program's code.
+Now type `process launch` to actually 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()`.
-To step through the program a line at a time, type `thread step-over`. When the program gets to a function call, step into it by typing `thread step-in`. Once in a function call, return from it by typing `thread step-out` or use `up` and `down` to take a quick look at the caller.
+To step through the program a line at a time, type `thread step-over`.
+When the program gets to a function call, step into it by typing `thread step-in`.
+Once in a function call, return from it by typing `thread step-out` or 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 `lldb`. This is our program (with a deliberate mistake):
+Here is a simple example of how to spot a mistake in a program with `lldb`.
+This is our program (with a deliberate mistake):
[.programlisting]
....
@@ -757,7 +909,8 @@ frame #1: 0x000000000020130b temp`main at temp.c:9:2 lldb displays stack frame
(int) i = -5360 lldb displays -5360
....
-Oh dear! Looking at the code, we forgot to initialize i. We meant to put
+Oh dear! Looking at the code, we forgot to initialize i.
+We meant to put
[.programlisting]
....
@@ -770,18 +923,25 @@ main() {
...
....
-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 `-5360`.
+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 `-5360`.
[NOTE]
====
-The `lldb` command 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.)
+The `lldb` command 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 File with lldb
-A 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 [.filename]#progname.core# instead of just [.filename]#core#, to make it clearer which program a core file belongs to.
+A 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 [.filename]#progname.core# instead of just [.filename]#core#, to make it clearer which program a core file belongs to.
-To examine a core file, specify the name of the core file in addition to the program itself. Instead of starting up `lldb` in the usual way, type `lldb -c _progname_.core -- _progname_`
+To examine a core file, specify the name of the core file in addition to the program itself.
+Instead of starting up `lldb` in the usual way, type `lldb -c _progname_.core -- _progname_`
The debugger will display something like this:
@@ -793,7 +953,10 @@ Core file '/home/pauamma/tmp/[.filename]#progname.core#' (x86_64) was loaded.
(lldb)
....
-In this case, the program was called [.filename]#progname#, so the core file is called [.filename]#progname.core#. The debugger does not display why the program crashed or where. For this, use `thread backtrace all`. This will also show how the function where the program dumped core was called.
+In this case, the program was called [.filename]#progname#, so the core file is called [.filename]#progname.core#.
+The debugger does not display why the program crashed or where.
+For this, use `thread backtrace all`.
+This will also show how the function where the program dumped core was called.
[source,bash,subs="verbatim,quotes"]
....
@@ -805,11 +968,15 @@ In this case, the program was called [.filename]#progname#, so the core file is
(lldb)
....
-`SIGSEGV` indicates that the program tried to access memory (run code or read/write data usually) at a location that does not belong to it, but does not give any specifics. For that, look at the source code at line 10 of file temp2.c, in `bazz()`. The backtrace also says that in this case, `bazz()` was called from `main()`.
+`SIGSEGV` indicates that the program tried to access memory (run code or read/write data usually) at a location that does not belong to it, but does not give any specifics.
+For that, look at the source code at line 10 of file temp2.c, in `bazz()`.
+The backtrace also says that in this case, `bazz()` was called from `main()`.
==== Attaching to a Running Program with lldb
-One of the neatest features about `lldb` is that it can attach to a program that is already running. Of course, that requires sufficient permissions to do so. A common problem is stepping through a program that forks and wanting to trace the child, but the debugger will only trace the parent.
+One of the neatest features about `lldb` is that it can attach to a program that is already running.
+Of course, that requires sufficient permissions to do so.
+A common problem is stepping through a program that forks and wanting to trace the child, but the debugger will only trace the parent.
To do that, start up another `lldb`, use `ps` to find the process ID for the child, and do
@@ -843,10 +1010,12 @@ Now all that is needed is to attach to the child, set PauseMode to `0` with `exp
[NOTE]
====
-The described functionality is available starting with LLDB version 12.0.0. Users of FreeBSD releases containing an earlier LLDB version may wish to use the snapshot available in link:{handbook}#ports-using/[ports or packages], as package:devel/llvm-devel[].
+The described functionality is available starting with LLDB version 12.0.0.
+Users of FreeBSD releases containing an earlier LLDB version may wish to use the snapshot available in link:{handbook}#ports-using/[ports or packages], as package:devel/llvm-devel[].
====
-Starting with LLDB 12.0.0, remote debugging is supported on FreeBSD. This means that `lldb-server` can be started to debug a program on one host, while the interactive `lldb` client connects to it from another one.
+Starting with LLDB 12.0.0, remote debugging is supported on FreeBSD.
+This means that `lldb-server` can be started to debug a program on one host, while the interactive `lldb` client connects to it from another one.
To launch a new process to be debugged remotely, run `lldb-server` on the remote server by typing
@@ -864,7 +1033,8 @@ Start `lldb` locally and type the following command to connect to the remote ser
(lldb) gdb-remote host:port
....
-`lldb-server` can also attach to a running process. To do that, type the following on the remote server:
+`lldb-server` can also attach to a running process.
+To do that, type the following on the remote server:
[source,bash]
....
@@ -882,7 +1052,8 @@ Start up gdb by typing
% gdb progname
....
-although many people prefer to run it inside Emacs. To do this, type:
+although many people prefer to run it inside Emacs.
+To do this, type:
[source,bash]
....
@@ -893,7 +1064,9 @@ Finally, for those finding its text-based command-prompt style off-putting, ther
==== Running a Program with gdb
-Compile the program with `-g` to get the most out of using `gdb`. It will work without, but will only display the name of the function currently running, instead of the source code. A line like:
+Compile the program with `-g` to get the most out of using `gdb`.
+It will work without, but will only display the name of the function currently running, instead of the source code.
+A line like:
[source,bash]
....
@@ -902,11 +1075,16 @@ Compile the program with `-g` to get the most out of using `gdb`. It will work w
when `gdb` starts up means that the program was not compiled with `-g`.
-At the `gdb` prompt, type `break main`. This will tell the debugger to skip the preliminary set-up code in the program being run and to stop execution at the beginning of the program's 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()`.
+At the `gdb` prompt, type `break main`.
+This will tell the debugger to skip the preliminary set-up code in the program being run and to stop execution at the beginning of the program's 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()`.
-To step through the program a line at a time, press `n`. When at a function call, step into it by pressing `s`. Once in a function call, return from it by pressing `f`, or use `up` and `down` to take a quick look at the caller.
+To step through the program a line at a time, press `n`.
+When at a function call, step into it by pressing `s`.
+Once in a function call, return from it by pressing `f`, or 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):
+Here is a simple example of how to spot a mistake in a program with `gdb`.
+This is our program (with a deliberate mistake):
[.programlisting]
....
@@ -972,7 +1150,8 @@ Hang on a minute! How did anint get to be `4231`? Was it not set to `5` in `main
$1 = 4231 gdb displays 4231
....
-Oh dear! Looking at the code, we forgot to initialize i. We meant to put
+Oh dear! Looking at the code, we forgot to initialize i.
+We meant to put
[.programlisting]
....
@@ -985,18 +1164,25 @@ main() {
...
....
-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`.
+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`.
[NOTE]
====
-The `gdb` command 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.)
+The `gdb` command 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 File with gdb
-A 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 [.filename]#progname.core# instead of just [.filename]#core#, to make it clearer which program a core file belongs to.
+A 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 [.filename]#progname.core# instead of just [.filename]#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
+To examine a core file, start up `gdb` in the usual way.
+Instead of typing `break` or `run`, type
[source,bash]
....
@@ -1022,9 +1208,12 @@ Cannot access memory at address 0x7020796d.
(gdb)
....
-In this case, the program was called [.filename]#progname#, so the core file is called [.filename]#progname.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`.
+In this case, the program was called [.filename]#progname#, so the core file is called [.filename]#progname.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. `bt` causes `gdb` to print out a back-trace of the call stack:
+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.
+`bt` causes `gdb` to print out a back-trace of the call stack:
[source,bash]
....
@@ -1035,11 +1224,14 @@ Sometimes it is useful to be able to see how a function was called, as the probl
(gdb)
....
-The `end()` function is called when a program crashes; in this case, the `bazz()` function was called from `main()`.
+The `end()` function is called when a program crashes;
+in this case, the `bazz()` function was called from `main()`.
==== Attaching to a Running Program with gdb
-One of the neatest features about `gdb` is that it can attach to a program that is already running. Of course, that requires sufficient permissions to do so. A common problem is stepping through a program that forks and wanting to trace the child, but the debugger will only trace the parent.
+One of the neatest features about `gdb` is that it can attach to a program that is already running.
+Of course, that requires sufficient permissions to do so.
+A common problem is stepping through a program that forks and wanting to trace the child, but the debugger will only trace the parent.
To do that, start up another `gdb`, use `ps` to find the process ID for the child, and do
@@ -1074,7 +1266,8 @@ Now all that is needed is to attach to the child, set PauseMode to `0`, and wait
=== Emacs
-Emacs is 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.
+Emacs is 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:
@@ -1091,29 +1284,45 @@ And doubtless many more that have been overlooked.
Emacs can be installed on FreeBSD using the package:editors/emacs[] port.
-Once it is installed, start it up and do `C-h t` to read an Emacs tutorial-that means hold down kbd:[control], press kbd:[h], let go of kbd:[control], and then press kbd:[t]. (Alternatively, you can use the mouse to select [.guimenuitem]#Emacs Tutorial# from the menu:Help[] menu.)
+Once it is installed, start it up and do `C-h t` to read an Emacs tutorial-that means hold down kbd:[control], press kbd:[h], let go of kbd:[control], and then press kbd:[t].
+(Alternatively, you can use the mouse to select [.guimenuitem]#Emacs Tutorial# from the menu: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.
+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.
+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 cannot remember what a particular combination of keys does, select [.guimenuitem]#Describe Key# from the menu:Help[] menu and type it in-Emacs will tell you what it does. You can also use the [.guimenuitem]#Command Apropos# menu item to find out all the commands which contain a particular word in them, with the key binding next to it.
+If you cannot remember what a particular combination of keys does, select [.guimenuitem]#Describe Key# from the menu:Help[] menu and type it in-Emacs will tell you what it does.
+You can also use the [.guimenuitem]#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 kbd:[Meta] key, press kbd:[x], release the kbd:[Meta] key, type `replace-s` (short for `replace-string`-another feature of Emacs is that you can abbreviate commands), press the kbd:[return] key, type `foo` (the string you want replaced), press the kbd:[return] key, type bar (the string you want to replace `foo` with) and press kbd:[return] again. Emacs will then do the search-and-replace operation you have just requested.
+By the way, the expression above means hold down the kbd:[Meta] key, press kbd:[x], release the kbd:[Meta] key, type `replace-s` (short for `replace-string`-another feature of Emacs is that you can abbreviate commands), press the kbd:[return] key, type `foo` (the string you want replaced), press the kbd:[return] key, type bar (the string you want to replace `foo` with) and press kbd:[return] again.
+Emacs will then do the search-and-replace operation you have just requested.
-If you are wondering what on earth kbd:[Meta] is, it is a special key that many UNIX(R) workstations have. Unfortunately, PC's do not have one, so it is usually kbd:[alt] (or if you are unlucky, the kbd:[escape] key).
+If you are wondering what on earth kbd:[Meta] is, it is a special key that many UNIX(R) workstations have.
+Unfortunately, PC's do not have one, so it is usually kbd:[alt] (or if you are unlucky, the kbd:[escape] key).
-Oh, and to get out of Emacs, do `C-x C-c` (that means hold down the kbd:[control] key, press kbd:[x], press kbd:[c] and release the kbd:[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).
+Oh, and to get out of Emacs, do `C-x C-c` (that means hold down the kbd:[control] key, press kbd:[x], press kbd:[c] and release the kbd:[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 Emacs
Emacs 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).
+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 link:ftp://ftp.gnu.org/old-gnu/emacs/elisp-manual-19-2.4.tar.gz[Emacs Tutorial]
-However, there is no need to actually know any Lisp to get started with configuring Emacs, as I have included a sample [.filename]#.emacs#, 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.
+However, there is no need to actually know any Lisp to get started with configuring Emacs,
+as I have included a sample [.filename]#.emacs#, 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 [.filename]#.emacs#
@@ -1427,14 +1636,17 @@ and then you can edit the file in your Emacs!footnote:[Many Emacs users set thei
Now, this is all very well if you only want to program in the languages already catered for in [.filename]#.emacs# (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 [.filename]#.el#, short for "Emacs Lisp". For example, if whizbang is a FreeBSD port, we can locate these files by doing
+The first thing to do is find out if whizbang comes with any files that tell Emacs about the language.
+These usually end in [.filename]#.el#, short for "Emacs Lisp".
+For example, if whizbang is a FreeBSD port, we can locate these files by doing
[source,bash]
....
% find /usr/ports/lang/whizbang -name "*.el" -print
....
-and install them by copying them into the Emacs site Lisp directory. On FreeBSD, this is [.filename]#/usr/local/share/emacs/site-lisp#.
+and install them by copying them into the Emacs site Lisp directory.
+On FreeBSD, this is [.filename]#/usr/local/share/emacs/site-lisp#.
So for example, if the output from the find command was
@@ -1450,7 +1662,9 @@ we would do
# cp /usr/ports/lang/whizbang/work/misc/whizbang.el /usr/local/share/emacs/site-lisp
....
-Next, we need to decide what extension whizbang source files have. Let us say for the sake of argument that they all end in [.filename]#.wiz#. We need to add an entry to our [.filename]#.emacs# to make sure Emacs will be able to use the information in [.filename]#whizbang.el#.
+Next, we need to decide what extension whizbang source files have.
+Let us say for the sake of argument that they all end in [.filename]#.wiz#.
+We need to add an entry to our [.filename]#.emacs# to make sure Emacs will be able to use the information in [.filename]#whizbang.el#.
Find the auto-mode-alist entry in [.filename]#.emacs# and add a line for whizbang, such as:
@@ -1465,7 +1679,8 @@ Find the auto-mode-alist entry in [.filename]#.emacs# and add a line for whizban
This means that Emacs will automatically go into `whizbang-mode` when you edit a file ending in [.filename]#.wiz#.
-Just below this, you will find the font-lock-auto-mode-list entry. Add `whizbang-mode` to it like so:
+Just below this, you will find the font-lock-auto-mode-list entry.
+Add `whizbang-mode` to it like so:
[.programlisting]
....
@@ -1477,7 +1692,8 @@ Just below this, you will find the font-lock-auto-mode-list entry. Add `whizbang
This means that Emacs will always enable `font-lock-mode` (ie syntax highlighting) when editing a [.filename]#.wiz# file.
-And that is all that is needed. If there is anything else you want done automatically when you open up [.filename]#.wiz#, you can add a `whizbang-mode hook` (see `my-scheme-mode-hook` for a simple example that adds `auto-indent`).
+And that is all that is needed. If there is anything else you want done automatically when you open up [.filename]#.wiz#,
+you can add a `whizbang-mode hook` (see `my-scheme-mode-hook` for a simple example that adds `auto-indent`).
[[tools-reading]]
== Further Reading