Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

// EDIT: Request to move this question to unix.stackexchange.com

I was thrown off guard today by gdb:

Program exited with code 0146.

gdb prints the return code in octal; looking into why I found: http://comments.gmane.org/gmane.comp.gdb.devel/30363

But that's not a particularly satisfying answer. Some quick googling did not reveal the history, so I was hoping someone on SO might know the back story.

A somewhat related question, how would one even view the return code in octal? Perhaps older machines always printed the return code?

$ printf %o\\n $?

Is pretty awkward :)

share|improve this question

migrated from stackoverflow.com Nov 11 '11 at 5:05

This question came from our site for professional and enthusiast programmers.

    
the $? is the shell's value for return code, and is a decimal digit (or does gdb support your example cmd also?). Don't know why gdb reports it in octal. My solution is to get out the 'ol man ascii to see what the octal values mean. Interesting question, as you do run into octal values from other programs too. Maybe it is discussed in Kernighan and Pike's 'Unix Programming Envioronment'. Good luck. –  shellter Nov 10 '11 at 14:52
    
The exit status can be larger than a digit, it's actually a byte. Also, man ascii would be of no use to convert octal to decimal. bc would be a better choice. –  jlliagre Nov 10 '11 at 22:20
    
The return code is a number, not decimal or octal. The program returns a status (one byte). With wait(3) or its tribe the parent process can find out a lot of information on why the process exited (normally, i.e., voluntarily by calling exit(2); killed by a signal, ...) –  vonbrand Jan 19 '13 at 23:28
add comment

2 Answers

up vote 5 down vote accepted

The octal representation eases the interpretation of the exit code for small values, which are the most commonly used. Should this number, which is a byte, been printed in decimal, finding which signal interrupted a process would require a little bit of calculation while in octal, they can be read as they are:

  • a process exits with status 5, gdb displays 05 which makes no difference
  • a process exits because it got a SIGINT (Control-C), gdb displays 0202 which is easier to recognize as signal #2 than 130.

Moreover, the exit status might also be a bit mask and in such case, octal (at least when you are used to it which was more common a couple of decades ago than these days) is easier to convert mentally into bits than decimal or even hexadecimal, just like for example chmod still accept an octal number to represent file permissions: 0750 = 111 101 000 = rwx r-x ---.

share|improve this answer
    
probably the wrong place to ask this, but is there an easy way to tell if a number given by GDB is in octal or decimal? –  marinara Nov 11 '11 at 7:18
1  
The convention is if it starts with zero (0), it is octal. –  jlliagre Nov 11 '11 at 7:31
add comment

I don't have a copy of this text and just read the short blurb available on google books but. According to the X/Open Portability guide System V Specification Commands & Utilities pg 283(according to google books)

if an application terminates abnormally its exit status is octal 0200 + status, and there is a list of common 'status' values (which are probably also given in octal).

So it is/was a poor mans error message.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.