0

Here is the scenario I'm asking about:

  • gdb debugs some program executable
  • programmer finds bug (good!)
  • programmer fixes code and re-compiles (great!)
  • programmer realizes he didn't quit gdb, so it was running all that time
  • specifically, it was running while the executable was written and the OS (Linux) allowed it.

How come the above is possible?

Shouldn't I get some OS error message like "file is being used by another application (gdb)"

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87

1 Answers1

1

Shouldn't I get some OS error message like

It depends.

Suppose your rebuild command is gcc -o foo t.c.

This command can either open(2) foo for writing, or it can write to a temporary file foo.$uniqsuffix and rename(2) the temporary to foo on success, or it can unlink(2) foo and create and write to a new foo.

Only the first variant -- attempting to write to the original foo would fail with ETXTBSY.

Running strace -fe file gcc -o foo t.c |& grep foo on my (Ubuntu) system shows:

[pid 116892] stat("foo", {st_mode=S_IFREG|0750, st_size=16520, ...}) = 0
[pid 116892] lstat("foo", {st_mode=S_IFREG|0750, st_size=16520, ...}) = 0
[pid 116892] unlink("foo")              = 0
[pid 116892] openat(AT_FDCWD, "foo", O_RDWR|O_CREAT|O_TRUNC, 0666) = 3
[pid 116892] stat("foo", {st_mode=S_IFREG|0640, st_size=16520, ...}) = 0
[pid 116892] chmod("foo", 0750)         = 0

So on this system the linker uses unlink + create new file strategy, and no error is expected.

See this answer for why you can continue debugging the original program even after it has been rebuilt.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362