A quick way to track / find a printf or cout call (long lost debug output)

My program prints a few random numbers and characters. This will be from some debugging code that I left hanging around. This was probably indirectly included during what I recently changed. I have no idea where it comes from, even after some quick grep through my libraries.

How can I easily track the print call source?
(he will be one of printfor cout <<)

I suppose something similar to gdbspit out a stack trace for every entry in stdout. I will definitely be taking a preventative approach from now on, but only interested possible solutions.

0
source share
4 answers

Macros can help you. In the definition of a macro, you can use __FILE__and __LINE__(and another such macro) to print location information.

Here is an example:

#include <iostream>

#define mycout std::cout <<  __FILE__  << "(" << __LINE__ << ") "
#define cout mycout

int main()
{
    cout << "Hello";
}

It prints the file name and line number followed by your message ( "Hello"in this case).

main.cpp(8) Hello

Online demo

+2
source

, , , . , cout printf. , , brakpoint. printf, , .
, , , - , printf/cout.

+2

strace-plus , . , -e trace=... . ... 2>&1 | grep -C 20 ... . ​​

:

write(1, "bar again\n", 10)             = 10
  > write() ../sysdeps/unix/syscall-template.S:82
  > _IO_new_file_write() fileops.c:1277
  > _IO_new_do_write() fileops.c:531
  > _IO_new_file_overflow() fileops.c:889
  > _IO_puts() ioputs.c:40
  > bar() [/home/pgbovine/strace-plus/hello]
  > foo() [/home/pgbovine/strace-plus/hello]
  > main() [/home/pgbovine/strace-plus/hello]
  > __libc_start_main() libc-start.c:258
  > _start() [/home/pgbovine/strace-plus/hello]
+1

@ArneMertz , !. , . , printf cout. .

printf / cout ,

gdb program
br main
r
disas

- :

Dump of assembler code for function main():
   0x00000000004cdb66 <+0>:     push   %rbp
   0x00000000004cdb67 <+1>:     mov    %rsp,%rbp
   0x00000000004cdb6a <+4>:     push   %r12
   0x00000000004cdb6c <+6>:     push   %rbx
   0x00000000004cdb6d <+7>:     sub    $0x380,%rsp
=> 0x00000000004cdb74 <+14>:    mov    $0x5ae287,%edi
   0x00000000004cdb79 <+19>:    callq  0x4c1cf0 <puts@plt>
...

0x4c1cf0 - printf .

b *0x4c1cf0
c
...
bt

printf .

, c/alt-tab/bt.

0

All Articles