An Interactive GNU Make Debugger

[article]
Summary:

The interactive GNU debugger has breakpoints, dumps information about the rule at which a breakpoint is hit and allows interactive querying of variable values and definitions.

The debugger has breakpoints, dumps information about the rule at which a breakpoint is hit and allows interactive querying of variable values and definitions. In a follow up article I'll extend the debugger to provide detailed tracing of the execution of any rule.

The Debugger In ActionBefore showing you how the debugger works, let's take a look at how to use it. The debugger and these examples all assume that you are using GNU Make 3.80 or above.

Here's an example Makefile that builds all from prerequisites foo and bar.

To illustrate the use of the debugger I've set a breakpoint in the all rule by inserting a line at the start of the rule's body that consists of just the variable $(__BREAKPOINT). $(__BREAKPOINT) will get expanded when the rule is ready to run and this will cause the debugger to break execution and prompt when the all rule is about to run. (I've ommitted all the debugger code, we'll take a look at it below).

MYVAR1 = hello 
 MYVAR2 = $(MYVAR1) everyone 
 all: MYVAR3 = $(MYVAR2) 
 all: foo bar 
     $(__BREAKPOINT) 
     @echo Finally making $@ 
 foo bar: 
     @echo Building $@ 

Here's what happens when this Makefile is executed with no existing files called all, foo or bar.

Building foo 
 Building bar 
 Makefile:51: GNU Make Debugger Break 
 Makefile:51: - Building 'all' from 'foo bar' 
 Makefile:51: - First prerequisite is 'foo' 
 Makefile:51: - Prequisites 'foo bar' are newer than 'all' 
 1> 

First the output from the execution of the rules for foo and bar is seen (the Building foo and Building bar lines) and then there's a break into the debugger. The debugger break shows the line at which the break occurred and in which Makefile. In this case the breakpoint occurred at line 51 of Makefile. The debugger also outputs information about the rule being built. Here you can see that all is built from foo and bar; that the first prerequisite is foo (that's important because it's the first prerequisite and is stored in GNU Make's $< automatic variable. $< is typically used as the source code file name for compiles). The debugger also shows why the all rule ran: foo and bar are both newer than all (that's because they were both just build by their respective rules).

Finally the debugger prompts 1> for a command. The debugger will accept 32 commands before automatically continuing execution of the Makefile. The number 1 indicates that this is the first command; once 32> is reached the debugger will continue automatically. The first thing to do is ask for help by typing h.

1> h 
 Makefile:51: c       continue 
 Makefile:51: q       quit 
 Makefile:51: v VAR   print value of $(VAR) 
 Makefile:51: o VAR   print origin of $(VAR) 
 Makefile:51: d VAR   print definition of $(VAR) 
 2> 

The debugger provides two means of stopping debugging: typing c continues with normal execution of the Makefile, typing q quits Make altogether. The three debugger commands v, o and d allow the user to interrogate GNU Make variables by asking for the value of a variable, its origin (where it was defined) or its definition. For example, the Makefile contains two variables MYVAR1 and MYVAR2 and a variable that is specific to the all rule: MYVAR3. A first step is to ask the debugger for the values of each of these variables:

2> v MYVAR1 
 Makefile:55: MYVAR1 has value 'hello' 
 3> v MYVAR2 
 Makefile:55: MYVAR2 has value 'hello everyone' 
 4> v MYVAR3 
 Makefile:55: MYVAR3 has value 'hello everyone' 
 5> 

If it wasn't clear how MYVAR3 got its value we could ask the debugger for its definition:

5> d MYVAR3 

Pages

About the author

John Graham-Cumming's picture John Graham-Cumming

John Graham-Cumming is Co-Founder at Electric Cloud, Inc . Prior to joining Electric Cloud, John was a Venture Consultant with Accel Partners, VP of Internet Technology at Interwoven, Inc. (IWOV), VP of Engineering at Scriptics Corporation (acquired by Interwoven), and Chief Architect at Optimal Networks, Inc. John holds BA and MA degrees in Mathematics and Computation and a Doctorate in Computer Security from Oxford University. John is the creator of the highly acclaimed open source POPFile project. He also holds two patents in network analysis and has others pending.

CMCrossroads is one of the growing communities of the TechWell network.

Featuring fresh, insightful stories, TechWell.com is the place to go for what is happening in software development and delivery.  Join the conversation now!

Upcoming Events

Oct 12
Oct 21
Nov 09
Nov 09