C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A standard way to redirect all standard output

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Tue, 14 Mar 2023 14:21:34 +0000
On Tue, 14 Mar 2023 at 13:48, Frederick Virchanza Gotham via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Tue, Mar 14, 2023 at 1:31 PM Breno GuimarĂ£es <brenorg_at_[hidden]> wrote:
> >
> > > You actually get an x86 instructions for a 'syscall':
> >
> > I can't reproduce that: https://godbolt.org/z/55hvM4xKx
> > How do you get it to inline printf?
>
>
> Sorry you're right, I was mistaken (even when building with -fno-builtin).
>
> What's actually happening is that 'printf' is invoked from glibc,
> however the implementation of 'printf' inside glibc does not invoke
> the overridden version of 'write' that I've provided.
>
> Perhaps the code inside glibc works something like as follows:
>
> ssize_t write_implementation(int fd, const void *buf, size_t
> count) { /* do stuff */ }
>
> ssize_t write(int fd, const void *buf, size_t count) { return
> write_implementation(fd,buf,count); }
>
> int puts(char const *arg) { return write_implementation(1, arg,
> strlen(arg)); } // NOTE: It doesn't call 'write'
>

Yes, this is approximately right.


> I could go on the Github for glibc to confirm this, but every time I
> do, I end up jumping back and forth from file to file and meandering
> around macroes -- it's a bit of maze in there.
>

Those macros are there to do exactly what you guessed above. It means that
if the program interposes a different symbol for something like 'write',
the rest of the library doesn't change behaviour.

Received on 2023-03-14 14:21:48