3

I'm trying to understand some kernel level concepts in Linux. I was checking on the difference between shell builtin commands and the other executable commands.

This wonderful answer clearly tells the difference and also specifies the need for shell builtin commands.

Now I know that using type <command-name>, I could check if it is an external or shell builtin command.

So I decided to do some strace'ing on the various commands to understand more of the internals.

I learned this neat little trick to do strace'es on shell builtin commands. I was able to do strace on cd .. as well according to the above answer.

Now when I run type pwd and get the output as, pwd is a shell builtin. So, I expect that I wouldn't be able to run strace on it, since it too is a shell builtin. But when I did an strace on it, I was surprised to see that strace worked without the need to do stty.

I verified strace for echo as well and it worked fine too.

So my understanding is, that strace worked in the case of pwd and echo because the execution of pwd and echo did not change any of the shell's behavior.

Am I correct in my understanding?

Ramesh
  • 39,297
  • Many of the commands are available as built-ins and as standalone executables. echo and pwd are 2 such commands, so you're stracing the standalong counterparts and not the built-ins when you did the above tests. – slm Sep 09 '14 at 02:17
  • @slm, thanks. I realized after I found that I could find the type of command using type -a. – Ramesh Sep 09 '14 at 02:21
  • @slm, so if I create a standalone for cd and do the strace, chances are that strace cd will work without stty. Is this understanding correct? – Ramesh Sep 09 '14 at 02:33
  • Yes, if you have an executable, then you can strace it. But aliases and functions etc. are built into Bash and cannot be straced, since you'd have to strace Bash itself to watch them. – slm Sep 09 '14 at 02:35
  • Re-read the man page for strace. There are no system calls to trace for a builtin since it's doing everything internal to Bash. – slm Sep 09 '14 at 02:37

1 Answers1

3

Because pwd or echo have external commands with the same name, /bin/pwd or /bin/echo. If you look at strace output, you can see:

$ strace pwd
execve("/bin/pwd", ["pwd"], [/* 68 vars */]) = 0
brk(0)                                  = 0x241e000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f80c31b9000

A note that when searching in PATH, strace accepts only regular file with execute bits set.

cuonglm
  • 153,898