diff mbox series

gcov: Add __gcov_info_to_gdca()

Message ID 20201117095741.3143-1-sebastian.huber@embedded-brains.de
State New
Headers show
Series gcov: Add __gcov_info_to_gdca() | expand

Commit Message

Sebastian Huber Nov. 17, 2020, 9:57 a.m. UTC
This is a proposal to get the gcda data for a gcda info in a free-standing
environment.  It is intended to be used with the -fprofile-info-section option.
A crude test program which doesn't use a linker script is:

  #include <gcov.h>
  #include <stdio.h>

  extern const struct gcov_info *my_info;

  static void
  filename(const char *f, void *arg)
  {
    printf("filename: %s\n", f);
  }

  static void
  dump(const void *d, unsigned n, void *arg)
  {
    const unsigned char *c;
    unsigned i;

    c = d;

    for (i = 0; i < n; ++i) {
	printf("%02x", c[i]);
    }
  }

  int main()
  {
    __asm__ volatile (".set my_info, .LPBX2");
    __gcov_info_to_gcda(my_info, filename, dump, NULL);
    return 0;
  }

gcc/

	* doc/invoke.texi (fprofile-info-section): Mention
	__gcov_info_to_gdca().

libgcc/

	Makefile.in (LIBGCOV_DRIVER): Add _gcov_info_to_gcda.
	gcov.h (gcov_info): Declare.
	(__gcov_info_to_gdca): Likewise.
	libgcov-driver.c (gcov_are_all_counters_zero): New.
	(write_one_data): Use gcov_are_all_counters_zero().
	(gcov_fn_info_to_gcda): New.
	(__gcov_info_to_gcda): Likewise.
---
 gcc/doc/invoke.texi     |  73 ++++++++++++++++++++----
 libgcc/Makefile.in      |   2 +-
 libgcc/gcov.h           |  15 +++++
 libgcc/libgcov-driver.c | 120 ++++++++++++++++++++++++++++++++++++----
 4 files changed, 188 insertions(+), 22 deletions(-)

Comments

Martin Liška Nov. 20, 2020, 8:37 a.m. UTC | #1
On 11/17/20 10:57 AM, Sebastian Huber wrote:
> This is a proposal to get the gcda data for a gcda info in a free-standing
> environment.  It is intended to be used with the -fprofile-info-section option.
> A crude test program which doesn't use a linker script is:

Hello.

I'm not pretty sure how this set up is going to work. Can you please explain me that?

I was thinking about your needs and I can imagine various techniques how to generate
gcda files format:

1) embedded system can override fopen, fwrite, fseek to a functions that do a remote
write-related functions

2) - use -fprofile-info-section
    - run an app on an embedded system and do a memory dump to a terminal/console
    - take the memory dump to a host system (with IO), run __gcov_init_from_memory_dump (...)
      and then do a normal __gcov_dump

What do you think about it?

Btw. I'm planning to commit in next stage1 removal of the internal I/O buffering.
Martin
Sebastian Huber Nov. 20, 2020, 9:25 a.m. UTC | #2
On 20/11/2020 09:37, Martin Liška wrote:

> On 11/17/20 10:57 AM, Sebastian Huber wrote:
>> This is a proposal to get the gcda data for a gcda info in a 
>> free-standing
>> environment.  It is intended to be used with the 
>> -fprofile-info-section option.
>> A crude test program which doesn't use a linker script is:
>
> Hello.
>
> I'm not pretty sure how this set up is going to work. Can you please 
> explain me that?
>
> I was thinking about your needs and I can imagine various techniques 
> how to generate
> gcda files format:
>
> 1) embedded system can override fopen, fwrite, fseek to a functions 
> that do a remote
> write-related functions
Yes, this is one option, however, the inhibit_libc disables quite a lot 
of libgcov functionality if Newlib is used for example.
>
> 2) - use -fprofile-info-section
>    - run an app on an embedded system and do a memory dump to a 
> terminal/console
>    - take the memory dump to a host system (with IO), run 
> __gcov_init_from_memory_dump (...)
>      and then do a normal __gcov_dump

I am not sure if a plain memory dump really simplifies things. You have 
to get the filename separately since it is only referenced in gcov_info 
and not included in the structure:

struct gcov_info
{
[...]
   const char *filename;        /* output file name */
[...]
#ifndef IN_GCOV_TOOL
   const struct gcov_fn_info *const *functions; /* pointer to pointers
                                                   to function 
information  */
[...]
#endif /* !IN_GCOV_TOOL */
};

Also the gcov_fn_info is not embedded in the gcov_info structure. If you 
do a plain memory dump, then you dump also pointers and how do you deal 
with these pointers on the host? You would need some extra information 
to describe the memory dump. So, why not use the gcda format for this? 
It is also more compact since zero value counters are skipped. Serial 
lines are slow, so less data to transfer is good.

/* Convert the gcov information to a gcda data stream.  The first 
callback is
    called exactly once with the filename associated with the gcov 
information.
    The filename may be NULL.  Afterwards, the second callback is 
subsequently
    called with chunks (the begin and length of the chunk are passed as the
    first two arguments) of the gcda data stream.  The fourth parameter is a
    user-provided argument passed as the last argument to the callback
    functions.  */

extern void __gcov_info_to_gcda (const struct gcov_info *gi_ptr,
                  void (*filename) (const char *name, void *arg),
                  void (*dump) (const void *begin, unsigned size, void 
*arg),

                  void *arg);

If __gcov_info_to_gcda() is correctly implemented, then this should give 
you directly gcda files if you use something like this:

#include <gcov.h>
#include <stdio.h>

extern const struct gcov_info *__gcov_info_start[];
extern const struct gcov_info *__gcov_info_end[];

static void
filename (const char *f, void *arg)
{
   FILE **file = arg;
   *file = fopen(f, "rb");
}

static void
dump (const void *d, unsigned n, void *arg)
{
   FILE **file = arg;
   fwrite(d, n, 1, *file);
}

static void
dump_gcov_info (void)
{
   const struct gcov_info **info = __gcov_info_start;
   const struct gcov_info **end = __gcov_info_end;

   /* Obfuscate variable to prevent compiler optimizations.  */
   __asm__ ("" : "+r" (end));

   while (info != end)
   {
     FILE *file = NULL;
     __gcov_info_to_gcda (*info, filename, dump, &file);
     fclose(file);
     ++info;
   }
}

int
main()
{
   dump_gcov_info();
   return 0;
}

The callback functions give the user the full control how the data of 
the gcda file is encoded for the transfer to a host. No gcov internals 
are exposed.
Martin Liška Nov. 20, 2020, 9:49 a.m. UTC | #3
On 11/20/20 10:25 AM, Sebastian Huber wrote:
> On 20/11/2020 09:37, Martin Liška wrote:
> 
>> On 11/17/20 10:57 AM, Sebastian Huber wrote:
>>> This is a proposal to get the gcda data for a gcda info in a free-standing
>>> environment.  It is intended to be used with the -fprofile-info-section option.
>>> A crude test program which doesn't use a linker script is:
>>
>> Hello.
>>
>> I'm not pretty sure how this set up is going to work. Can you please explain me that?
>>
>> I was thinking about your needs and I can imagine various techniques how to generate
>> gcda files format:
>>
>> 1) embedded system can override fopen, fwrite, fseek to a functions that do a remote
>> write-related functions
> Yes, this is one option, however, the inhibit_libc disables quite a lot of libgcov functionality if Newlib is used for example.

I see. Btw do you have available Newlib in the embedded environment? If so, what I/O functionality is provided?

>>
>> 2) - use -fprofile-info-section
>>    - run an app on an embedded system and do a memory dump to a terminal/console
>>    - take the memory dump to a host system (with IO), run __gcov_init_from_memory_dump (...)
>>      and then do a normal __gcov_dump
> 
> I am not sure if a plain memory dump really simplifies things. You have to get the filename separately since it is only referenced in gcov_info and not included in the structure:
> 
> struct gcov_info
> {
> [...]
>    const char *filename;        /* output file name */
> [...]
> #ifndef IN_GCOV_TOOL
>    const struct gcov_fn_info *const *functions; /* pointer to pointers
>                                                    to function information  */
> [...]
> #endif /* !IN_GCOV_TOOL */
> };

I see!

> 
> Also the gcov_fn_info is not embedded in the gcov_info structure. If you do a plain memory dump, then you dump also pointers and how do you deal with these pointers on the host? You would need some extra information to describe the memory dump. So, why not use the gcda format for this? It is also more compact since zero value counters are skipped. Serial lines are slow, so less data to transfer is good.
> 
> /* Convert the gcov information to a gcda data stream.  The first callback is
>     called exactly once with the filename associated with the gcov information.
>     The filename may be NULL.  Afterwards, the second callback is subsequently
>     called with chunks (the begin and length of the chunk are passed as the
>     first two arguments) of the gcda data stream.  The fourth parameter is a
>     user-provided argument passed as the last argument to the callback
>     functions.  */
> 
> extern void __gcov_info_to_gcda (const struct gcov_info *gi_ptr,
>                   void (*filename) (const char *name, void *arg),
>                   void (*dump) (const void *begin, unsigned size, void *arg),
> 
>                   void *arg);
> 
> If __gcov_info_to_gcda() is correctly implemented, then this should give you directly gcda files if you use something like this:
> 
> #include <gcov.h>
> #include <stdio.h>
> 
> extern const struct gcov_info *__gcov_info_start[];
> extern const struct gcov_info *__gcov_info_end[];
> 
> static void
> filename (const char *f, void *arg)
> {
>    FILE **file = arg;
>    *file = fopen(f, "rb");
> }
> 
> static void
> dump (const void *d, unsigned n, void *arg)
> {
>    FILE **file = arg;
>    fwrite(d, n, 1, *file);
> }
> 
> static void
> dump_gcov_info (void)
> {
>    const struct gcov_info **info = __gcov_info_start;
>    const struct gcov_info **end = __gcov_info_end;
> 
>    /* Obfuscate variable to prevent compiler optimizations.  */
>    __asm__ ("" : "+r" (end));
> 
>    while (info != end)
>    {
>      FILE *file = NULL;
>      __gcov_info_to_gcda (*info, filename, dump, &file);
>      fclose(file);
>      ++info;
>    }
> }
> 
> int
> main()
> {
>    dump_gcov_info();
>    return 0;
> }
> 
> The callback functions give the user the full control how the data of the gcda file is encoded for the transfer to a host. No gcov internals are exposed.
> 

All right. Btw. how will you implement these 2 callbacks on the embedded target?
Apart from these 2 hooks, I bet you will also need gcov_position and gcov_seek functions,
can be seen in my sent patch.

Martin
Sebastian Huber Nov. 20, 2020, 10:11 a.m. UTC | #4
On 20/11/2020 10:49, Martin Liška wrote:

> On 11/20/20 10:25 AM, Sebastian Huber wrote:
>> On 20/11/2020 09:37, Martin Liška wrote:
>>
>>> On 11/17/20 10:57 AM, Sebastian Huber wrote:
>>>> This is a proposal to get the gcda data for a gcda info in a 
>>>> free-standing
>>>> environment.  It is intended to be used with the 
>>>> -fprofile-info-section option.
>>>> A crude test program which doesn't use a linker script is:
>>>
>>> Hello.
>>>
>>> I'm not pretty sure how this set up is going to work. Can you please 
>>> explain me that?
>>>
>>> I was thinking about your needs and I can imagine various techniques 
>>> how to generate
>>> gcda files format:
>>>
>>> 1) embedded system can override fopen, fwrite, fseek to a functions 
>>> that do a remote
>>> write-related functions
>> Yes, this is one option, however, the inhibit_libc disables quite a 
>> lot of libgcov functionality if Newlib is used for example.
>
> I see. Btw do you have available Newlib in the embedded environment? 
> If so, what I/O functionality is provided?
Yes, I use Newlib with the RTEMS real-time operating system. Newlib 
provides the standard C library I/O functions (fopen, etc.). However, 
having Newlib available doesn't mean that every application uses its. 
Applications are statically linked with the operating system and Newlib. 
They only use what is required. Some applications cannot use the 
standard C library I/O since they use a lot of infrastructure and 
memory. You can do a lot of things with just a couple of KiBs available.
>
>>>
>>> 2) - use -fprofile-info-section
>>>    - run an app on an embedded system and do a memory dump to a 
>>> terminal/console
>>>    - take the memory dump to a host system (with IO), run 
>>> __gcov_init_from_memory_dump (...)
>>>      and then do a normal __gcov_dump
>>
>> I am not sure if a plain memory dump really simplifies things. You 
>> have to get the filename separately since it is only referenced in 
>> gcov_info and not included in the structure:
>>
>> struct gcov_info
>> {
>> [...]
>>    const char *filename;        /* output file name */
>> [...]
>> #ifndef IN_GCOV_TOOL
>>    const struct gcov_fn_info *const *functions; /* pointer to pointers
>>                                                    to function 
>> information  */
>> [...]
>> #endif /* !IN_GCOV_TOOL */
>> };
>
> I see!
>
>>
>> Also the gcov_fn_info is not embedded in the gcov_info structure. If 
>> you do a plain memory dump, then you dump also pointers and how do 
>> you deal with these pointers on the host? You would need some extra 
>> information to describe the memory dump. So, why not use the gcda 
>> format for this? It is also more compact since zero value counters 
>> are skipped. Serial lines are slow, so less data to transfer is good.
>>
>> /* Convert the gcov information to a gcda data stream.  The first 
>> callback is
>>     called exactly once with the filename associated with the gcov 
>> information.
>>     The filename may be NULL.  Afterwards, the second callback is 
>> subsequently
>>     called with chunks (the begin and length of the chunk are passed 
>> as the
>>     first two arguments) of the gcda data stream.  The fourth 
>> parameter is a
>>     user-provided argument passed as the last argument to the callback
>>     functions.  */
>>
>> extern void __gcov_info_to_gcda (const struct gcov_info *gi_ptr,
>>                   void (*filename) (const char *name, void *arg),
>>                   void (*dump) (const void *begin, unsigned size, 
>> void *arg),
>>
>>                   void *arg);
>>
>> If __gcov_info_to_gcda() is correctly implemented, then this should 
>> give you directly gcda files if you use something like this:
>>
>> #include <gcov.h>
>> #include <stdio.h>
>>
>> extern const struct gcov_info *__gcov_info_start[];
>> extern const struct gcov_info *__gcov_info_end[];
>>
>> static void
>> filename (const char *f, void *arg)
>> {
>>    FILE **file = arg;
>>    *file = fopen(f, "rb");
>> }
>>
>> static void
>> dump (const void *d, unsigned n, void *arg)
>> {
>>    FILE **file = arg;
>>    fwrite(d, n, 1, *file);
>> }
>>
>> static void
>> dump_gcov_info (void)
>> {
>>    const struct gcov_info **info = __gcov_info_start;
>>    const struct gcov_info **end = __gcov_info_end;
>>
>>    /* Obfuscate variable to prevent compiler optimizations.  */
>>    __asm__ ("" : "+r" (end));
>>
>>    while (info != end)
>>    {
>>      FILE *file = NULL;
>>      __gcov_info_to_gcda (*info, filename, dump, &file);
>>      fclose(file);
>>      ++info;
>>    }
>> }
>>
>> int
>> main()
>> {
>>    dump_gcov_info();
>>    return 0;
>> }
>>
>> The callback functions give the user the full control how the data of 
>> the gcda file is encoded for the transfer to a host. No gcov 
>> internals are exposed.
>>
>
> All right. Btw. how will you implement these 2 callbacks on the 
> embedded target?

One options is to convert the gcov info to YAML:

gcov-info:

- file: filename1

   data: <... base64 encoded data from __gcov_info_to_gcda ... >

- file: filename2

   data: ...

Then send the data to the host via a serial line. On the host read the 
data, parse the YAML, and create the gcda files. The 
__gcov_info_to_gcda() needs about 408 bytes of ARM Thumb-2 code and no 
data. You need a polled character output function, the linker set 
iteration and two callbacks. So, you can easily dump the gcov 
information with about 1KiB of code and no data except a small stack.

> Apart from these 2 hooks, I bet you will also need gcov_position and 
> gcov_seek functions,
> can be seen in my sent patch.
For what do I need them?
Martin Liška Nov. 20, 2020, 3:25 p.m. UTC | #5
On 11/20/20 11:11 AM, Sebastian Huber wrote:
> On 20/11/2020 10:49, Martin Liška wrote:
> 
>> On 11/20/20 10:25 AM, Sebastian Huber wrote:
>>> On 20/11/2020 09:37, Martin Liška wrote:
>>>
>>>> On 11/17/20 10:57 AM, Sebastian Huber wrote:
>>>>> This is a proposal to get the gcda data for a gcda info in a free-standing
>>>>> environment.  It is intended to be used with the -fprofile-info-section option.
>>>>> A crude test program which doesn't use a linker script is:
>>>>
>>>> Hello.
>>>>
>>>> I'm not pretty sure how this set up is going to work. Can you please explain me that?
>>>>
>>>> I was thinking about your needs and I can imagine various techniques how to generate
>>>> gcda files format:
>>>>
>>>> 1) embedded system can override fopen, fwrite, fseek to a functions that do a remote
>>>> write-related functions
>>> Yes, this is one option, however, the inhibit_libc disables quite a lot of libgcov functionality if Newlib is used for example.
>>
>> I see. Btw do you have available Newlib in the embedded environment? If so, what I/O functionality is provided?
> Yes, I use Newlib with the RTEMS real-time operating system. Newlib provides the standard C library I/O functions (fopen, etc.). However, having Newlib available doesn't mean that every application uses its. Applications are statically linked with the operating system and Newlib. They only use what is required. Some applications cannot use the standard C library I/O since they use a lot of infrastructure and memory. You can do a lot of things with just a couple of KiBs available.

I see.

>>
>>>>
>>>> 2) - use -fprofile-info-section
>>>>    - run an app on an embedded system and do a memory dump to a terminal/console
>>>>    - take the memory dump to a host system (with IO), run __gcov_init_from_memory_dump (...)
>>>>      and then do a normal __gcov_dump
>>>
>>> I am not sure if a plain memory dump really simplifies things. You have to get the filename separately since it is only referenced in gcov_info and not included in the structure:
>>>
>>> struct gcov_info
>>> {
>>> [...]
>>>    const char *filename;        /* output file name */
>>> [...]
>>> #ifndef IN_GCOV_TOOL
>>>    const struct gcov_fn_info *const *functions; /* pointer to pointers
>>>                                                    to function information  */
>>> [...]
>>> #endif /* !IN_GCOV_TOOL */
>>> };
>>
>> I see!
>>
>>>
>>> Also the gcov_fn_info is not embedded in the gcov_info structure. If you do a plain memory dump, then you dump also pointers and how do you deal with these pointers on the host? You would need some extra information to describe the memory dump. So, why not use the gcda format for this? It is also more compact since zero value counters are skipped. Serial lines are slow, so less data to transfer is good.
>>>
>>> /* Convert the gcov information to a gcda data stream.  The first callback is
>>>     called exactly once with the filename associated with the gcov information.
>>>     The filename may be NULL.  Afterwards, the second callback is subsequently
>>>     called with chunks (the begin and length of the chunk are passed as the
>>>     first two arguments) of the gcda data stream.  The fourth parameter is a
>>>     user-provided argument passed as the last argument to the callback
>>>     functions.  */
>>>
>>> extern void __gcov_info_to_gcda (const struct gcov_info *gi_ptr,
>>>                   void (*filename) (const char *name, void *arg),
>>>                   void (*dump) (const void *begin, unsigned size, void *arg),
>>>
>>>                   void *arg);
>>>
>>> If __gcov_info_to_gcda() is correctly implemented, then this should give you directly gcda files if you use something like this:
>>>
>>> #include <gcov.h>
>>> #include <stdio.h>
>>>
>>> extern const struct gcov_info *__gcov_info_start[];
>>> extern const struct gcov_info *__gcov_info_end[];
>>>
>>> static void
>>> filename (const char *f, void *arg)
>>> {
>>>    FILE **file = arg;
>>>    *file = fopen(f, "rb");
>>> }
>>>
>>> static void
>>> dump (const void *d, unsigned n, void *arg)
>>> {
>>>    FILE **file = arg;
>>>    fwrite(d, n, 1, *file);
>>> }
>>>
>>> static void
>>> dump_gcov_info (void)
>>> {
>>>    const struct gcov_info **info = __gcov_info_start;
>>>    const struct gcov_info **end = __gcov_info_end;
>>>
>>>    /* Obfuscate variable to prevent compiler optimizations.  */
>>>    __asm__ ("" : "+r" (end));
>>>
>>>    while (info != end)
>>>    {
>>>      FILE *file = NULL;
>>>      __gcov_info_to_gcda (*info, filename, dump, &file);
>>>      fclose(file);
>>>      ++info;
>>>    }
>>> }
>>>
>>> int
>>> main()
>>> {
>>>    dump_gcov_info();
>>>    return 0;
>>> }
>>>
>>> The callback functions give the user the full control how the data of the gcda file is encoded for the transfer to a host. No gcov internals are exposed.
>>>
>>
>> All right. Btw. how will you implement these 2 callbacks on the embedded target?
> 
> One options is to convert the gcov info to YAML:
> 
> gcov-info:
> 
> - file: filename1
> 
>    data: <... base64 encoded data from __gcov_info_to_gcda ... >
> 
> - file: filename2
> 
>    data: ...
> 
> Then send the data to the host via a serial line. On the host read the data, parse the YAML, and create the gcda files. The __gcov_info_to_gcda() needs about 408 bytes of ARM Thumb-2 code and no data. You need a polled character output function, the linker set iteration and two callbacks. So, you can easily dump the gcov information with about 1KiB of code and no data except a small stack.
> 
>> Apart from these 2 hooks, I bet you will also need gcov_position and gcov_seek functions,
>> can be seen in my sent patch.
> For what do I need them?
> 

I prefer the way with the 2 extra hooks.
Can you please prepare a patch where the newly added functions __gcov_info_to_gcda and __gcov_fn_info_to_gcda
will be used in libgcov (with the hooks equal to fopen and fwrite?

Thanks,
Martin
Sebastian Huber Nov. 20, 2020, 4:14 p.m. UTC | #6
On 20/11/2020 16:25, Martin Liška wrote:

>>> Apart from these 2 hooks, I bet you will also need gcov_position and 
>>> gcov_seek functions,
>>> can be seen in my sent patch.
>> For what do I need them?
>>
>
> I prefer the way with the 2 extra hooks.
> Can you please prepare a patch where the newly added functions 
> __gcov_info_to_gcda and __gcov_fn_info_to_gcda
> will be used in libgcov (with the hooks equal to fopen and fwrite? 

I am not really sure what I should do. Do you mean that write_one_data() 
should be rewritten to use __gcov_info_to_gcda() with hooks that use 
gcov_write_unsigned()?

The write_one_data() also has a const struct gcov_summary *prg_p 
pointer. What should an external user provide for this pointer? For 
example &gi_ptr->summary?

The write_one_data() has this code

       if (fn_buffer && fn_buffer->fn_ix == f_ix)
         {
           /* Buffered data from another program.  */
           buffered = 1;
           gfi_ptr = &fn_buffer->info;
           length = GCOV_TAG_FUNCTION_LENGTH;
         }

which uses a global variable

/* buffer for the fn_data from another program.  */
static struct gcov_fn_buffer *fn_buffer;

For this handling we would need a new hook to do this:

       if (buffered)
         fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);

I don't know for what we need seek and position hooks.
Sebastian Huber Nov. 23, 2020, 12:25 p.m. UTC | #7
On 20/11/2020 17:14, Sebastian Huber wrote:

> On 20/11/2020 16:25, Martin Liška wrote:
>
>>>> Apart from these 2 hooks, I bet you will also need gcov_position 
>>>> and gcov_seek functions,
>>>> can be seen in my sent patch.
>>> For what do I need them?
>>>
>>
>> I prefer the way with the 2 extra hooks.
>> Can you please prepare a patch where the newly added functions 
>> __gcov_info_to_gcda and __gcov_fn_info_to_gcda
>> will be used in libgcov (with the hooks equal to fopen and fwrite? 
>
> I am not really sure what I should do. Do you mean that 
> write_one_data() should be rewritten to use __gcov_info_to_gcda() with 
> hooks that use gcov_write_unsigned()?
>
> The write_one_data() also has a const struct gcov_summary *prg_p 
> pointer. What should an external user provide for this pointer? For 
> example &gi_ptr->summary?
>
> The write_one_data() has this code
>
>       if (fn_buffer && fn_buffer->fn_ix == f_ix)
>         {
>           /* Buffered data from another program.  */
>           buffered = 1;
>           gfi_ptr = &fn_buffer->info;
>           length = GCOV_TAG_FUNCTION_LENGTH;
>         }
>
> which uses a global variable
>
> /* buffer for the fn_data from another program.  */
> static struct gcov_fn_buffer *fn_buffer;
>
> For this handling we would need a new hook to do this:
>
>       if (buffered)
>         fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
>
> I don't know for what we need seek and position hooks.

Refactoring write_one_data() to use hooks requires that

gcov_write_counter()

gcov_write_tag_length()

gcov_write_summary()

move from gcc/gcov-io.c to libgcc/libgcov-buffer.c. They can be made 
static. I am not sure if the external symbols can be removed

/* In libgcov we need these functions to be extern, so prefix them with
    __gcov.  In libgcov they must also be hidden so that the instance in
    the executable is not also used in a DSO.  */
#define gcov_write_tag_length __gcov_write_tag_length
#define gcov_write_counter __gcov_write_counter
#define gcov_write_summary __gcov_write_summary

without breaking anything? What is the performance impact if only 
gcov_write_unsigned() is used by libgcc/libgcov-driver.c?
Martin Liška Nov. 23, 2020, 2:24 p.m. UTC | #8
On 11/23/20 1:25 PM, Sebastian Huber wrote:
> On 20/11/2020 17:14, Sebastian Huber wrote:
> 
>> On 20/11/2020 16:25, Martin Liška wrote:
>>
>>>>> Apart from these 2 hooks, I bet you will also need gcov_position and gcov_seek functions,
>>>>> can be seen in my sent patch.
>>>> For what do I need them?
>>>>
>>>
>>> I prefer the way with the 2 extra hooks.
>>> Can you please prepare a patch where the newly added functions __gcov_info_to_gcda and __gcov_fn_info_to_gcda
>>> will be used in libgcov (with the hooks equal to fopen and fwrite? 
>>
>> I am not really sure what I should do. Do you mean that write_one_data() should be rewritten to use __gcov_info_to_gcda() with hooks that use gcov_write_unsigned()?
>>
>> The write_one_data() also has a const struct gcov_summary *prg_p pointer. What should an external user provide for this pointer? For example &gi_ptr->summary?
>>
>> The write_one_data() has this code
>>
>>       if (fn_buffer && fn_buffer->fn_ix == f_ix)
>>         {
>>           /* Buffered data from another program.  */
>>           buffered = 1;
>>           gfi_ptr = &fn_buffer->info;
>>           length = GCOV_TAG_FUNCTION_LENGTH;
>>         }
>>
>> which uses a global variable
>>
>> /* buffer for the fn_data from another program.  */
>> static struct gcov_fn_buffer *fn_buffer;
>>
>> For this handling we would need a new hook to do this:
>>
>>       if (buffered)
>>         fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
>>
>> I don't know for what we need seek and position hooks.
> 
> Refactoring write_one_data() to use hooks requires that
> 
> gcov_write_counter()
> 
> gcov_write_tag_length()
> 
> gcov_write_summary()
> 
> move from gcc/gcov-io.c to libgcc/libgcov-buffer.c. They can be made static. I am not sure if the external symbols can be removed
> 
> /* In libgcov we need these functions to be extern, so prefix them with
>     __gcov.  In libgcov they must also be hidden so that the instance in
>     the executable is not also used in a DSO.  */
> #define gcov_write_tag_length __gcov_write_tag_length
> #define gcov_write_counter __gcov_write_counter
> #define gcov_write_summary __gcov_write_summary
> 
> without breaking anything? What is the performance impact if only gcov_write_unsigned() is used by libgcc/libgcov-driver.c?
> 

All right. It seems that your original patch would be a simpler approach,
I'll comment the patch in a moment.

Thanks,
Martin
Martin Liška Nov. 23, 2020, 2:30 p.m. UTC | #9
On 11/17/20 10:57 AM, Sebastian Huber wrote:
> This is a proposal to get the gcda data for a gcda info in a free-standing
> environment.  It is intended to be used with the -fprofile-info-section option.
> A crude test program which doesn't use a linker script is:
> 
>    #include <gcov.h>
>    #include <stdio.h>
> 
>    extern const struct gcov_info *my_info;
> 
>    static void
>    filename(const char *f, void *arg)
>    {
>      printf("filename: %s\n", f);
>    }
> 
>    static void
>    dump(const void *d, unsigned n, void *arg)
>    {
>      const unsigned char *c;
>      unsigned i;
> 
>      c = d;
> 
>      for (i = 0; i < n; ++i) {
> 	printf("%02x", c[i]);
>      }
>    }
> 
>    int main()
>    {
>      __asm__ volatile (".set my_info, .LPBX2");
>      __gcov_info_to_gcda(my_info, filename, dump, NULL);
>      return 0;
>    }
> 
> gcc/
> 
> 	* doc/invoke.texi (fprofile-info-section): Mention
> 	__gcov_info_to_gdca().
> 
> libgcc/
> 
> 	Makefile.in (LIBGCOV_DRIVER): Add _gcov_info_to_gcda.
> 	gcov.h (gcov_info): Declare.
> 	(__gcov_info_to_gdca): Likewise.
> 	libgcov-driver.c (gcov_are_all_counters_zero): New.
> 	(write_one_data): Use gcov_are_all_counters_zero().
> 	(gcov_fn_info_to_gcda): New.
> 	(__gcov_info_to_gcda): Likewise.
> ---
>   gcc/doc/invoke.texi     |  73 ++++++++++++++++++++----
>   libgcc/Makefile.in      |   2 +-
>   libgcc/gcov.h           |  15 +++++
>   libgcc/libgcov-driver.c | 120 ++++++++++++++++++++++++++++++++++++----
>   4 files changed, 188 insertions(+), 22 deletions(-)
> 
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index 3510a54c6c4..09cb4922f5e 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -14248,17 +14248,17 @@ To optimize the program based on the collected profile information, use
>   Register the profile information in the specified section instead of using a
>   constructor/destructor.  The section name is @var{name} if it is specified,
>   otherwise the section name defaults to @code{.gcov_info}.  A pointer to the
> -profile information generated by @option{-fprofile-arcs} or
> -@option{-ftest-coverage} is placed in the specified section for each
> -translation unit.  This option disables the profile information registration
> -through a constructor and it disables the profile information processing
> -through a destructor.  This option is not intended to be used in hosted
> -environments such as GNU/Linux.  It targets systems with limited resources
> -which do not support constructors and destructors.  The linker could collect
> -the input sections in a continuous memory block and define start and end
> -symbols.  The runtime support could dump the profiling information registered
> -in this linker set during program termination to a serial line for example.  A
> -GNU linker script example which defines a linker output section follows:
> +profile information generated by @option{-fprofile-arcs} is placed in the
> +specified section for each translation unit.  This option disables the profile
> +information registration through a constructor and it disables the profile
> +information processing through a destructor.  This option is not intended to be
> +used in hosted environments such as GNU/Linux.  It targets free-standing
> +environments (for example embedded systems) with limited resources which do not
> +support constructors/destructors or the C library file I/O.
> +
> +The linker could collect the input sections in a continuous memory block and
> +define start and end symbols.  A GNU linker script example which defines a
> +linker output section follows:
>   
>   @smallexample
>     .gcov_info      :
> @@ -14269,6 +14269,57 @@ GNU linker script example which defines a linker output section follows:
>     @}
>   @end smallexample
>   
> +The program could dump the profiling information registered in this linker set
> +for example like this:
> +
> +@smallexample
> +#include <gcov.h>
> +#include <stdio.h>
> +
> +extern const struct gcov_info *__gcov_info_start[];
> +extern const struct gcov_info *__gcov_info_end[];
> +
> +static void
> +filename (const char *f, void *arg)
> +@{
> +  puts (f);
> +@}
> +
> +static void
> +dump (const void *d, unsigned n, void *arg)
> +@{
> +  const unsigned char *c = d;
> +
> +  for (unsigned i = 0; i < n; ++i)
> +    printf ("%02x", c[i]);
> +@}
> +
> +static void
> +dump_gcov_info (void)
> +@{
> +  const struct gcov_info **info = __gcov_info_start;
> +  const struct gcov_info **end = __gcov_info_end;
> +
> +  /* Obfuscate variable to prevent compiler optimizations.  */
> +  __asm__ ("" : "+r" (end));
> +
> +  while (info != end)
> +  @{
> +    void *arg = NULL;
> +    __gcov_info_to_gcda (*info, filename, dump, arg);
> +    putchar ('\n');
> +    ++info;
> +  @}
> +@}
> +
> +int
> +main()
> +@{
> +  dump_gcov_info();
> +  return 0;
> +@}
> +@end smallexample
> +
>   @item -fprofile-note=@var{path}
>   @opindex fprofile-note
>   
> diff --git a/libgcc/Makefile.in b/libgcc/Makefile.in
> index d6075d32bd4..c22413d768c 100644
> --- a/libgcc/Makefile.in
> +++ b/libgcc/Makefile.in
> @@ -908,7 +908,7 @@ LIBGCOV_INTERFACE = _gcov_dump _gcov_fork				\
>   	_gcov_execl _gcov_execlp					\
>   	_gcov_execle _gcov_execv _gcov_execvp _gcov_execve _gcov_reset  \
>   	_gcov_lock_unlock
> -LIBGCOV_DRIVER = _gcov
> +LIBGCOV_DRIVER = _gcov _gcov_info_to_gcda
>   
>   libgcov-merge-objects = $(patsubst %,%$(objext),$(LIBGCOV_MERGE))
>   libgcov-profiler-objects = $(patsubst %,%$(objext),$(LIBGCOV_PROFILER))
> diff --git a/libgcc/gcov.h b/libgcc/gcov.h
> index 0e3eed31032..371ee6feb11 100644
> --- a/libgcc/gcov.h
> +++ b/libgcc/gcov.h
> @@ -25,6 +25,8 @@
>   #ifndef GCC_GCOV_H
>   #define GCC_GCOV_H
>   
> +struct gcov_info;
> +
>   /* Set all counters to zero.  */
>   
>   extern void __gcov_reset (void);
> @@ -33,4 +35,17 @@ extern void __gcov_reset (void);
>   
>   extern void __gcov_dump (void);
>   
> +/* Convert the gcov information to a gcda data stream.  The first callback is
> +   called exactly once with the filename associated with the gcov information.
> +   The filename may be NULL.  Afterwards, the second callback is subsequently
> +   called with chunks (the begin and length of the chunk are passed as the
> +   first two arguments) of the gcda data stream.  The fourth parameter is a
> +   user-provided argument passed as the last argument to the callback
> +   functions.  */
> +
> +extern void __gcov_info_to_gcda (const struct gcov_info *,
> +				 void (*) (const char *, void *),
> +				 void (*) (const void *, unsigned, void *),
> +				 void *);
> +
>   #endif /* GCC_GCOV_H */
> diff --git a/libgcc/libgcov-driver.c b/libgcc/libgcov-driver.c
> index e53e4dc392a..4191b89a6f7 100644
> --- a/libgcc/libgcov-driver.c
> +++ b/libgcc/libgcov-driver.c
> @@ -26,6 +26,18 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
>   #include "libgcov.h"
>   #include "gcov-io.h"
>   
> +/* Return 1, if all counter values are zero, otherwise 0. */
> +
> +static inline int
> +gcov_are_all_counters_zero (const struct gcov_ctr_info *ci_ptr)
> +{
> +  for (unsigned i = 0; i < ci_ptr->num; i++)
> +    if (ci_ptr->values[i] != 0)
> +	return 0;
> +
> +  return 1;
> +}
> +
>   #if defined(inhibit_libc)
>   /* If libc and its header files are not available, provide dummy functions.  */
>   
> @@ -428,16 +440,8 @@ write_one_data (const struct gcov_info *gi_ptr,
>   	    write_top_counters (ci_ptr, t_ix, n_counts);
>   	  else
>   	    {
> -	      /* Do not stream when all counters are zero.  */
> -	      int all_zeros = 1;
> -	      for (unsigned i = 0; i < n_counts; i++)
> -		if (ci_ptr->values[i] != 0)
> -		  {
> -		    all_zeros = 0;
> -		    break;
> -		  }
> -
> -	      if (all_zeros)
> +	      if (gcov_are_all_counters_zero (ci_ptr))
> +		/* Do not stream when all counters are zero.  */
>   		gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix),
>   				       GCOV_TAG_COUNTER_LENGTH (-n_counts));
>   	      else
> @@ -637,3 +641,99 @@ __gcov_init (struct gcov_info *info)
>   #endif /* !IN_GCOV_TOOL */
>   #endif /* L_gcov */
>   #endif /* inhibit_libc */
> +
> +#if !IN_GCOV_TOOL
> +#ifdef L_gcov_info_to_gcda
> +static void
> +gcov_fn_info_to_gcda (const struct gcov_info *gi_ptr,
> +		      const struct gcov_fn_info *gfi_ptr,
> +		      void (*dump) (const void *, unsigned, void *),
> +		      void *arg)
> +{
> +  const struct gcov_ctr_info *ci_ptr = gfi_ptr->ctrs;
> +
> +  for (unsigned t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
> +    {
> +      if (!gi_ptr->merge[t_ix])
> +	continue;
> +
> +      if (t_ix != GCOV_COUNTER_V_TOPN && t_ix != GCOV_COUNTER_V_INDIR)
> +	{
> +	  gcov_unsigned_t word = GCOV_TAG_FOR_COUNTER (t_ix);
> +	  (*dump) (&word, sizeof (word), arg);
> +	  gcov_position_t n_counts = ci_ptr->num;
> +
> +	  if (gcov_are_all_counters_zero (ci_ptr))
> +	    {
> +	      /* Do not stream when all counters are zero.  */
> +	      word = GCOV_TAG_COUNTER_LENGTH (-n_counts);
> +	      (*dump) (&word, sizeof (word), arg);
> +	    }
> +	  else
> +	    {
> +	      word = GCOV_TAG_COUNTER_LENGTH (n_counts);
> +	      (*dump) (&word, sizeof (word), arg);
> +
> +	      for (unsigned i = 0; i < n_counts; i++)
> +		{
> +		  gcov_type value = ci_ptr->values[i];
> +		  word = (gcov_unsigned_t) value;
> +		  (*dump) (&word, sizeof (word), arg);
> +
> +		  if (sizeof (value) > sizeof (word))
> +		    word = (gcov_unsigned_t) (value >> 32);
> +		  else
> +		    word = 0;
> +
> +		  (*dump) (&word, sizeof (word), arg);
> +		}
> +	    }
> +	}
> +
> +      ci_ptr++;
> +    }
> +}
> +
> +/* Convert the gcov info to a gcda data stream.  This function does not support
> +   whole program statistics and top counters.  It is intended for free-standing
> +   environments which do not support the C library file I/O.  For the data
> +   format, see also write_one_data().  */
> +
> +void
> +__gcov_info_to_gcda (const struct gcov_info *gi_ptr,
> +		     void (*filename) (const char *, void *),
> +		     void (*dump) (const void *, unsigned, void *),
> +		     void *arg)

Hello.

I would prefer a better names for the hooks. What about something like
open_filename_hook and write_data_hook?

> +{
> +  (*filename) (gi_ptr->filename, arg);
> +  gcov_unsigned_t word = GCOV_DATA_MAGIC;
> +  (*dump) (&word, sizeof (word), arg);

And I would add a new macro like
#define GCOV_WRITE_DATA(data) (*write_data_hook) (&DATA, sizeof (DATA), arg

What do you think?

Note that we already entered a code freeze before the patch was sent to the mailing list.
That means we can install it in the next stage1.

Martin

> +  (*dump) (&gi_ptr->version, sizeof (gi_ptr->version), arg);
> +  (*dump) (&gi_ptr->stamp, sizeof (gi_ptr->stamp), arg);
> +
> +  for (unsigned f_ix = 0; f_ix != gi_ptr->n_functions; f_ix++)
> +    {
> +      word = GCOV_TAG_FUNCTION;
> +      (*dump) (&word, sizeof (word), arg);
> +
> +      const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
> +      if (gfi_ptr && gfi_ptr->key == gi_ptr)
> +	word = GCOV_TAG_FUNCTION_LENGTH;
> +      else
> +	word = 0;
> +      (*dump) (&word, sizeof (word), arg);
> +      if (!word)
> +	continue;
> +
> +      (*dump) (&gfi_ptr->ident, sizeof (gfi_ptr->ident), arg);
> +      (*dump) (&gfi_ptr->lineno_checksum,
> +	       sizeof (gfi_ptr->lineno_checksum), arg);
> +      (*dump) (&gfi_ptr->cfg_checksum, sizeof (gfi_ptr->cfg_checksum), arg);
> +      gcov_fn_info_to_gcda (gi_ptr, gfi_ptr, dump, arg);
> +    }
> +
> +  word = 0;
> +  (*dump) (&word, sizeof (word), arg);
> +}
> +#endif /* L_gcov_info_to_gcda */
> +#endif /* !IN_GCOV_TOOL */
>
Sebastian Huber Nov. 23, 2020, 2:35 p.m. UTC | #10
Hello Martin,

On 23/11/2020 15:30, Martin Liška wrote:
>> +/* Convert the gcov info to a gcda data stream.  This function does 
>> not support
>> +   whole program statistics and top counters.  It is intended for 
>> free-standing
>> +   environments which do not support the C library file I/O. For the 
>> data
>> +   format, see also write_one_data().  */
>> +
>> +void
>> +__gcov_info_to_gcda (const struct gcov_info *gi_ptr,
>> +             void (*filename) (const char *, void *),
>> +             void (*dump) (const void *, unsigned, void *),
>> +             void *arg)
>
> Hello.
>
> I would prefer a better names for the hooks. What about something like
> open_filename_hook and write_data_hook?
>
>> +{
>> +  (*filename) (gi_ptr->filename, arg);
>> +  gcov_unsigned_t word = GCOV_DATA_MAGIC;
>> +  (*dump) (&word, sizeof (word), arg);
>
> And I would add a new macro like
> #define GCOV_WRITE_DATA(data) (*write_data_hook) (&DATA, sizeof 
> (DATA), arg
>
> What do you think?
sounds good.
>
> Note that we already entered a code freeze before the patch was sent 
> to the mailing list.
> That means we can install it in the next stage1. 
If I have to wait for next stage 1, I can also try to refactor 
write_one_data() after your patch which removes the buffering. This 
would avoid some duplicated code, however, it would require some changes 
in existing code. Is it allowed to remove external (hidden?) symbols 
from libgcov?
Martin Liška Nov. 23, 2020, 2:49 p.m. UTC | #11
On 11/23/20 3:35 PM, Sebastian Huber wrote:
> If I have to wait for next stage 1, I can also try to refactor write_one_data() after your patch which removes the buffering.

Yes, please build your patches on top of the file buffering removal.

> This would avoid some duplicated code, however, it would require some changes in existing code. Is it allowed to remove external (hidden?) symbols from libgcov?

Which functions do you mean?

Martin
Sebastian Huber Nov. 23, 2020, 2:50 p.m. UTC | #12
On 23/11/2020 15:49, Martin Liška wrote:

> On 11/23/20 3:35 PM, Sebastian Huber wrote:
>> If I have to wait for next stage 1, I can also try to refactor 
>> write_one_data() after your patch which removes the buffering.
>
> Yes, please build your patches on top of the file buffering removal.
Ok.
>
>> This would avoid some duplicated code, however, it would require some 
>> changes in existing code. Is it allowed to remove external (hidden?) 
>> symbols from libgcov?
>
> Which functions do you mean?
Refactoring write_one_data() to use hooks requires that

gcov_write_counter()

gcov_write_tag_length()

gcov_write_summary()

move from gcc/gcov-io.c to libgcc/libgcov-buffer.c. They can be made 
static. I am not sure if the external symbols can be removed

/* In libgcov we need these functions to be extern, so prefix them with
     __gcov.  In libgcov they must also be hidden so that the instance in
     the executable is not also used in a DSO.  */
#define gcov_write_tag_length __gcov_write_tag_length
#define gcov_write_counter __gcov_write_counter
#define gcov_write_summary __gcov_write_summary
Martin Liška Nov. 23, 2020, 2:55 p.m. UTC | #13
On 11/23/20 3:50 PM, Sebastian Huber wrote:
> On 23/11/2020 15:49, Martin Liška wrote:
> 
>> On 11/23/20 3:35 PM, Sebastian Huber wrote:
>>> If I have to wait for next stage 1, I can also try to refactor write_one_data() after your patch which removes the buffering.
>>
>> Yes, please build your patches on top of the file buffering removal.
> Ok.
>>
>>> This would avoid some duplicated code, however, it would require some changes in existing code. Is it allowed to remove external (hidden?) symbols from libgcov?
>>
>> Which functions do you mean?
> Refactoring write_one_data() to use hooks requires that
> 
> gcov_write_counter()
> 
> gcov_write_tag_length()
> 
> gcov_write_summary()

I bet these 3 can be actually moved to gcov-io.h, these functions are very small.
So yes, it should be doable.

Martin

> 
> move from gcc/gcov-io.c to libgcc/libgcov-buffer.c. They can be made static. I am not sure if the external symbols can be removed
> 
> /* In libgcov we need these functions to be extern, so prefix them with
>      __gcov.  In libgcov they must also be hidden so that the instance in
>      the executable is not also used in a DSO.  */
> #define gcov_write_tag_length __gcov_write_tag_length
> #define gcov_write_counter __gcov_write_counter
> #define gcov_write_summary __gcov_write_summary
>
diff mbox series

Patch

diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 3510a54c6c4..09cb4922f5e 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -14248,17 +14248,17 @@  To optimize the program based on the collected profile information, use
 Register the profile information in the specified section instead of using a
 constructor/destructor.  The section name is @var{name} if it is specified,
 otherwise the section name defaults to @code{.gcov_info}.  A pointer to the
-profile information generated by @option{-fprofile-arcs} or
-@option{-ftest-coverage} is placed in the specified section for each
-translation unit.  This option disables the profile information registration
-through a constructor and it disables the profile information processing
-through a destructor.  This option is not intended to be used in hosted
-environments such as GNU/Linux.  It targets systems with limited resources
-which do not support constructors and destructors.  The linker could collect
-the input sections in a continuous memory block and define start and end
-symbols.  The runtime support could dump the profiling information registered
-in this linker set during program termination to a serial line for example.  A
-GNU linker script example which defines a linker output section follows:
+profile information generated by @option{-fprofile-arcs} is placed in the
+specified section for each translation unit.  This option disables the profile
+information registration through a constructor and it disables the profile
+information processing through a destructor.  This option is not intended to be
+used in hosted environments such as GNU/Linux.  It targets free-standing
+environments (for example embedded systems) with limited resources which do not
+support constructors/destructors or the C library file I/O.
+
+The linker could collect the input sections in a continuous memory block and
+define start and end symbols.  A GNU linker script example which defines a
+linker output section follows:
 
 @smallexample
   .gcov_info      :
@@ -14269,6 +14269,57 @@  GNU linker script example which defines a linker output section follows:
   @}
 @end smallexample
 
+The program could dump the profiling information registered in this linker set
+for example like this:
+
+@smallexample
+#include <gcov.h>
+#include <stdio.h>
+
+extern const struct gcov_info *__gcov_info_start[];
+extern const struct gcov_info *__gcov_info_end[];
+
+static void
+filename (const char *f, void *arg)
+@{
+  puts (f);
+@}
+
+static void
+dump (const void *d, unsigned n, void *arg)
+@{
+  const unsigned char *c = d;
+
+  for (unsigned i = 0; i < n; ++i)
+    printf ("%02x", c[i]);
+@}
+
+static void
+dump_gcov_info (void)
+@{
+  const struct gcov_info **info = __gcov_info_start;
+  const struct gcov_info **end = __gcov_info_end;
+
+  /* Obfuscate variable to prevent compiler optimizations.  */
+  __asm__ ("" : "+r" (end));
+
+  while (info != end)
+  @{
+    void *arg = NULL;
+    __gcov_info_to_gcda (*info, filename, dump, arg);
+    putchar ('\n');
+    ++info;
+  @}
+@}
+
+int
+main()
+@{
+  dump_gcov_info();
+  return 0;
+@}
+@end smallexample
+
 @item -fprofile-note=@var{path}
 @opindex fprofile-note
 
diff --git a/libgcc/Makefile.in b/libgcc/Makefile.in
index d6075d32bd4..c22413d768c 100644
--- a/libgcc/Makefile.in
+++ b/libgcc/Makefile.in
@@ -908,7 +908,7 @@  LIBGCOV_INTERFACE = _gcov_dump _gcov_fork				\
 	_gcov_execl _gcov_execlp					\
 	_gcov_execle _gcov_execv _gcov_execvp _gcov_execve _gcov_reset  \
 	_gcov_lock_unlock
-LIBGCOV_DRIVER = _gcov
+LIBGCOV_DRIVER = _gcov _gcov_info_to_gcda
 
 libgcov-merge-objects = $(patsubst %,%$(objext),$(LIBGCOV_MERGE))
 libgcov-profiler-objects = $(patsubst %,%$(objext),$(LIBGCOV_PROFILER))
diff --git a/libgcc/gcov.h b/libgcc/gcov.h
index 0e3eed31032..371ee6feb11 100644
--- a/libgcc/gcov.h
+++ b/libgcc/gcov.h
@@ -25,6 +25,8 @@ 
 #ifndef GCC_GCOV_H
 #define GCC_GCOV_H
 
+struct gcov_info;
+
 /* Set all counters to zero.  */
 
 extern void __gcov_reset (void);
@@ -33,4 +35,17 @@  extern void __gcov_reset (void);
 
 extern void __gcov_dump (void);
 
+/* Convert the gcov information to a gcda data stream.  The first callback is
+   called exactly once with the filename associated with the gcov information.
+   The filename may be NULL.  Afterwards, the second callback is subsequently
+   called with chunks (the begin and length of the chunk are passed as the
+   first two arguments) of the gcda data stream.  The fourth parameter is a
+   user-provided argument passed as the last argument to the callback
+   functions.  */
+
+extern void __gcov_info_to_gcda (const struct gcov_info *,
+				 void (*) (const char *, void *),
+				 void (*) (const void *, unsigned, void *),
+				 void *);
+
 #endif /* GCC_GCOV_H */
diff --git a/libgcc/libgcov-driver.c b/libgcc/libgcov-driver.c
index e53e4dc392a..4191b89a6f7 100644
--- a/libgcc/libgcov-driver.c
+++ b/libgcc/libgcov-driver.c
@@ -26,6 +26,18 @@  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #include "libgcov.h"
 #include "gcov-io.h"
 
+/* Return 1, if all counter values are zero, otherwise 0. */
+
+static inline int
+gcov_are_all_counters_zero (const struct gcov_ctr_info *ci_ptr)
+{
+  for (unsigned i = 0; i < ci_ptr->num; i++)
+    if (ci_ptr->values[i] != 0)
+	return 0;
+
+  return 1;
+}
+
 #if defined(inhibit_libc)
 /* If libc and its header files are not available, provide dummy functions.  */
 
@@ -428,16 +440,8 @@  write_one_data (const struct gcov_info *gi_ptr,
 	    write_top_counters (ci_ptr, t_ix, n_counts);
 	  else
 	    {
-	      /* Do not stream when all counters are zero.  */
-	      int all_zeros = 1;
-	      for (unsigned i = 0; i < n_counts; i++)
-		if (ci_ptr->values[i] != 0)
-		  {
-		    all_zeros = 0;
-		    break;
-		  }
-
-	      if (all_zeros)
+	      if (gcov_are_all_counters_zero (ci_ptr))
+		/* Do not stream when all counters are zero.  */
 		gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix),
 				       GCOV_TAG_COUNTER_LENGTH (-n_counts));
 	      else
@@ -637,3 +641,99 @@  __gcov_init (struct gcov_info *info)
 #endif /* !IN_GCOV_TOOL */
 #endif /* L_gcov */
 #endif /* inhibit_libc */
+
+#if !IN_GCOV_TOOL
+#ifdef L_gcov_info_to_gcda
+static void
+gcov_fn_info_to_gcda (const struct gcov_info *gi_ptr,
+		      const struct gcov_fn_info *gfi_ptr,
+		      void (*dump) (const void *, unsigned, void *),
+		      void *arg)
+{
+  const struct gcov_ctr_info *ci_ptr = gfi_ptr->ctrs;
+
+  for (unsigned t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
+    {
+      if (!gi_ptr->merge[t_ix])
+	continue;
+
+      if (t_ix != GCOV_COUNTER_V_TOPN && t_ix != GCOV_COUNTER_V_INDIR)
+	{
+	  gcov_unsigned_t word = GCOV_TAG_FOR_COUNTER (t_ix);
+	  (*dump) (&word, sizeof (word), arg);
+	  gcov_position_t n_counts = ci_ptr->num;
+
+	  if (gcov_are_all_counters_zero (ci_ptr))
+	    {
+	      /* Do not stream when all counters are zero.  */
+	      word = GCOV_TAG_COUNTER_LENGTH (-n_counts);
+	      (*dump) (&word, sizeof (word), arg);
+	    }
+	  else
+	    {
+	      word = GCOV_TAG_COUNTER_LENGTH (n_counts);
+	      (*dump) (&word, sizeof (word), arg);
+
+	      for (unsigned i = 0; i < n_counts; i++)
+		{
+		  gcov_type value = ci_ptr->values[i];
+		  word = (gcov_unsigned_t) value;
+		  (*dump) (&word, sizeof (word), arg);
+
+		  if (sizeof (value) > sizeof (word))
+		    word = (gcov_unsigned_t) (value >> 32);
+		  else
+		    word = 0;
+
+		  (*dump) (&word, sizeof (word), arg);
+		}
+	    }
+	}
+
+      ci_ptr++;
+    }
+}
+
+/* Convert the gcov info to a gcda data stream.  This function does not support
+   whole program statistics and top counters.  It is intended for free-standing
+   environments which do not support the C library file I/O.  For the data
+   format, see also write_one_data().  */
+
+void
+__gcov_info_to_gcda (const struct gcov_info *gi_ptr,
+		     void (*filename) (const char *, void *),
+		     void (*dump) (const void *, unsigned, void *),
+		     void *arg)
+{
+  (*filename) (gi_ptr->filename, arg);
+  gcov_unsigned_t word = GCOV_DATA_MAGIC;
+  (*dump) (&word, sizeof (word), arg);
+  (*dump) (&gi_ptr->version, sizeof (gi_ptr->version), arg);
+  (*dump) (&gi_ptr->stamp, sizeof (gi_ptr->stamp), arg);
+
+  for (unsigned f_ix = 0; f_ix != gi_ptr->n_functions; f_ix++)
+    {
+      word = GCOV_TAG_FUNCTION;
+      (*dump) (&word, sizeof (word), arg);
+
+      const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
+      if (gfi_ptr && gfi_ptr->key == gi_ptr)
+	word = GCOV_TAG_FUNCTION_LENGTH;
+      else
+	word = 0;
+      (*dump) (&word, sizeof (word), arg);
+      if (!word)
+	continue;
+
+      (*dump) (&gfi_ptr->ident, sizeof (gfi_ptr->ident), arg);
+      (*dump) (&gfi_ptr->lineno_checksum,
+	       sizeof (gfi_ptr->lineno_checksum), arg);
+      (*dump) (&gfi_ptr->cfg_checksum, sizeof (gfi_ptr->cfg_checksum), arg);
+      gcov_fn_info_to_gcda (gi_ptr, gfi_ptr, dump, arg);
+    }
+
+  word = 0;
+  (*dump) (&word, sizeof (word), arg);
+}
+#endif /* L_gcov_info_to_gcda */
+#endif /* !IN_GCOV_TOOL */