diff mbox series

[1/2] trace: add support for "trace clear"

Message ID 47c647a8dfc23ba24c90560feb1360788609384a.1733757864.git.jerome.forissier@linaro.org
State Superseded
Headers show
Series Add "trace clear" | expand

Commit Message

Jerome Forissier Dec. 9, 2024, 3:32 p.m. UTC
Implement a "trace clear" command to delete the currently accumulated
trace data. This comes handy when someone needs to trace a particular
command. For example:

  => trace clear; dhcp; trace pause
  => trace stats
  => trace calls 0x02100000 0x10000000
  => tftpput $profbase $profoffset 192.168.0.16:trace.bin

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
---
 cmd/trace.c     | 13 ++++++++++++-
 include/trace.h |  2 ++
 lib/trace.c     | 47 +++++++++++++++++++++++++++++++++--------------
 3 files changed, 47 insertions(+), 15 deletions(-)

Comments

Ilias Apalodimas Dec. 13, 2024, 12:09 p.m. UTC | #1
Hi Jerome

On Mon, 9 Dec 2024 at 17:32, Jerome Forissier
<jerome.forissier@linaro.org> wrote:
>
> Implement a "trace clear" command to delete the currently accumulated
> trace data. This comes handy when someone needs to trace a particular
> command. For example:
>
>   => trace clear; dhcp; trace pause
>   => trace stats
>   => trace calls 0x02100000 0x10000000
>   => tftpput $profbase $profoffset 192.168.0.16:trace.bin
>
> Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
> ---
>  cmd/trace.c     | 13 ++++++++++++-
>  include/trace.h |  2 ++
>  lib/trace.c     | 47 +++++++++++++++++++++++++++++++++--------------
>  3 files changed, 47 insertions(+), 15 deletions(-)
>
> diff --git a/cmd/trace.c b/cmd/trace.c
> index 937e6a682ad..e95ac7ca9da 100644
> --- a/cmd/trace.c
> +++ b/cmd/trace.c
> @@ -87,8 +87,18 @@ int do_trace(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
>                 trace_set_enabled(0);
>                 break;
>         case 'c':
> -               if (create_call_list(argc, argv))
> +               switch (*(cmd + 1)) {
> +               case 'a':
> +                       if (create_call_list(argc, argv))
> +                               return cmd_usage(cmdtp);
> +                       break;
> +               case 'l':
> +                       if (trace_clear())
> +                               return CMD_RET_FAILURE;
> +                       break;

Instead of this, mind changing the command to trace wipe or something similar?
Then we can have a discrete case for 'w' rather than scanning for a string

Cheers
/Ilias
> +               default:
>                         return cmd_usage(cmdtp);
> +               }
>                 break;
>         case 'r':
>                 trace_set_enabled(1);
> @@ -113,6 +123,7 @@ U_BOOT_CMD(
>         "stats                        - display tracing statistics\n"
>         "trace pause                        - pause tracing\n"
>         "trace resume                       - resume tracing\n"
> +       "trace clear                        - clear traces\n"
>         "trace funclist [<addr> <size>]     - dump function list into buffer\n"
>         "trace calls  [<addr> <size>]       "
>                 "- dump function call trace into buffer"
> diff --git a/include/trace.h b/include/trace.h
> index 763d6d1255a..782eaae2fc2 100644
> --- a/include/trace.h
> +++ b/include/trace.h
> @@ -100,6 +100,8 @@ void trace_set_enabled(int enabled);
>
>  int trace_early_init(void);
>
> +int trace_clear(void);
> +
>  /**
>   * Init the trace system
>   *
> diff --git a/lib/trace.c b/lib/trace.c
> index cabbe47b58a..def9f912c92 100644
> --- a/lib/trace.c
> +++ b/lib/trace.c
> @@ -351,14 +351,8 @@ static int get_func_count(void)
>         return gd->mon_len / FUNC_SITE_SIZE;
>  }
>
> -/**
> - * trace_init() - initialize the tracing system and enable it
> - *
> - * @buff:      Pointer to trace buffer
> - * @buff_size: Size of trace buffer
> - * Return:     0 if ok
> - */
> -int notrace trace_init(void *buff, size_t buff_size)
> +static int notrace trace_init_(void *buff, size_t buff_size, bool copy_early,
> +                              bool enable)
>  {
>         int func_count = get_func_count();
>         size_t needed;
> @@ -368,7 +362,7 @@ int notrace trace_init(void *buff, size_t buff_size)
>                 return func_count;
>         trace_save_gd();
>
> -       if (!was_disabled) {
> +       if (copy_early) {
>  #ifdef CONFIG_TRACE_EARLY
>                 ulong used, count;
>                 char *end;
> @@ -394,9 +388,6 @@ int notrace trace_init(void *buff, size_t buff_size)
>                 }
>                 puts("\n");
>                 memcpy(buff, hdr, used);
> -#else
> -               puts("trace: already enabled\n");
> -               return -EALREADY;
>  #endif
>         }
>         hdr = (struct trace_hdr *)buff;
> @@ -419,13 +410,41 @@ int notrace trace_init(void *buff, size_t buff_size)
>         hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
>         hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
>
> -       puts("trace: enabled\n");
> -       trace_enabled = 1;
> +       printf("trace: initialized, %senabled\n", enable ? "" : "not ");
> +       trace_enabled = enable;
>         trace_inited = 1;
>
>         return 0;
>  }
>
> +/**
> + * trace_init() - initialize the tracing system and enable it
> + *
> + * @buff:      Pointer to trace buffer
> + * @buff_size: Size of trace buffer
> + * Return:     0 if ok
> + */
> +int notrace trace_init(void *buff, size_t buff_size)
> +{
> +       /* If traces are enabled already, we may have early traces to copy */
> +       return trace_init_(buff, buff_size, trace_enabled, true);
> +}
> +
> +/**
> + * trace_clear() - clear accumulated traced data
> + *
> + * May be called with tracing enabled or disabled.
> + */
> +int notrace trace_clear(void)
> +{
> +       bool was_enabled = trace_enabled;
> +
> +       if (trace_enabled)
> +               trace_enabled = 0;
> +       return trace_init_(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE,
> +                          false, was_enabled);
> +}
> +
>  #ifdef CONFIG_TRACE_EARLY
>  /**
>   * trace_early_init() - initialize the tracing system for early tracing
> --
> 2.43.0
>
Jerome Forissier Dec. 13, 2024, 12:14 p.m. UTC | #2
On 12/13/24 13:09, Ilias Apalodimas wrote:
> Hi Jerome
> 
> On Mon, 9 Dec 2024 at 17:32, Jerome Forissier
> <jerome.forissier@linaro.org> wrote:
>>
>> Implement a "trace clear" command to delete the currently accumulated
>> trace data. This comes handy when someone needs to trace a particular
>> command. For example:
>>
>>   => trace clear; dhcp; trace pause
>>   => trace stats
>>   => trace calls 0x02100000 0x10000000
>>   => tftpput $profbase $profoffset 192.168.0.16:trace.bin
>>
>> Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
>> ---
>>  cmd/trace.c     | 13 ++++++++++++-
>>  include/trace.h |  2 ++
>>  lib/trace.c     | 47 +++++++++++++++++++++++++++++++++--------------
>>  3 files changed, 47 insertions(+), 15 deletions(-)
>>
>> diff --git a/cmd/trace.c b/cmd/trace.c
>> index 937e6a682ad..e95ac7ca9da 100644
>> --- a/cmd/trace.c
>> +++ b/cmd/trace.c
>> @@ -87,8 +87,18 @@ int do_trace(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
>>                 trace_set_enabled(0);
>>                 break;
>>         case 'c':
>> -               if (create_call_list(argc, argv))
>> +               switch (*(cmd + 1)) {
>> +               case 'a':
>> +                       if (create_call_list(argc, argv))
>> +                               return cmd_usage(cmdtp);
>> +                       break;
>> +               case 'l':
>> +                       if (trace_clear())
>> +                               return CMD_RET_FAILURE;
>> +                       break;
> 
> Instead of this, mind changing the command to trace wipe or something similar?
> Then we can have a discrete case for 'w' rather than scanning for a string

Good idea.

Thanks,
Ilias Apalodimas Dec. 13, 2024, 12:16 p.m. UTC | #3
Thanks,

Apologies I missed something else as well

On Fri, 13 Dec 2024 at 14:14, Jerome Forissier
<jerome.forissier@linaro.org> wrote:
>
>
>
> On 12/13/24 13:09, Ilias Apalodimas wrote:
> > Hi Jerome
> >
> > On Mon, 9 Dec 2024 at 17:32, Jerome Forissier
> > <jerome.forissier@linaro.org> wrote:
> >>
> >> Implement a "trace clear" command to delete the currently accumulated
> >> trace data. This comes handy when someone needs to trace a particular
> >> command. For example:
> >>
> >>   => trace clear; dhcp; trace pause
> >>   => trace stats
> >>   => trace calls 0x02100000 0x10000000
> >>   => tftpput $profbase $profoffset 192.168.0.16:trace.bin
> >>
> >> Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
> >> ---
> >>  cmd/trace.c     | 13 ++++++++++++-
> >>  include/trace.h |  2 ++
> >>  lib/trace.c     | 47 +++++++++++++++++++++++++++++++++--------------
> >>  3 files changed, 47 insertions(+), 15 deletions(-)
> >>
> >> diff --git a/cmd/trace.c b/cmd/trace.c
> >> index 937e6a682ad..e95ac7ca9da 100644
> >> --- a/cmd/trace.c
> >> +++ b/cmd/trace.c
> >> @@ -87,8 +87,18 @@ int do_trace(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
> >>                 trace_set_enabled(0);
> >>                 break;
> >>         case 'c':
> >> -               if (create_call_list(argc, argv))
> >> +               switch (*(cmd + 1)) {
> >> +               case 'a':
> >> +                       if (create_call_list(argc, argv))
> >> +                               return cmd_usage(cmdtp);
> >> +                       break;
> >> +               case 'l':
> >> +                       if (trace_clear())
> >> +                               return CMD_RET_FAILURE;
> >> +                       break;
> >
> > Instead of this, mind changing the command to trace wipe or something similar?
> > Then we can have a discrete case for 'w' rather than scanning for a string
>
> Good idea.
>
> Thanks,
> --
> Jerome
>
> >
> > Cheers
> > /Ilias
> >> +               default:
> >>                         return cmd_usage(cmdtp);
> >> +               }
> >>                 break;
> >>         case 'r':
> >>                 trace_set_enabled(1);
> >> @@ -113,6 +123,7 @@ U_BOOT_CMD(
> >>         "stats                        - display tracing statistics\n"
> >>         "trace pause                        - pause tracing\n"
> >>         "trace resume                       - resume tracing\n"
> >> +       "trace clear                        - clear traces\n"
> >>         "trace funclist [<addr> <size>]     - dump function list into buffer\n"
> >>         "trace calls  [<addr> <size>]       "
> >>                 "- dump function call trace into buffer"
> >> diff --git a/include/trace.h b/include/trace.h
> >> index 763d6d1255a..782eaae2fc2 100644
> >> --- a/include/trace.h
> >> +++ b/include/trace.h
> >> @@ -100,6 +100,8 @@ void trace_set_enabled(int enabled);
> >>
> >>  int trace_early_init(void);
> >>
> >> +int trace_clear(void);
> >> +
> >>  /**
> >>   * Init the trace system
> >>   *
> >> diff --git a/lib/trace.c b/lib/trace.c
> >> index cabbe47b58a..def9f912c92 100644
> >> --- a/lib/trace.c
> >> +++ b/lib/trace.c
> >> @@ -351,14 +351,8 @@ static int get_func_count(void)
> >>         return gd->mon_len / FUNC_SITE_SIZE;
> >>  }
> >>
> >> -/**
> >> - * trace_init() - initialize the tracing system and enable it
> >> - *
> >> - * @buff:      Pointer to trace buffer
> >> - * @buff_size: Size of trace buffer
> >> - * Return:     0 if ok
> >> - */
> >> -int notrace trace_init(void *buff, size_t buff_size)
> >> +static int notrace trace_init_(void *buff, size_t buff_size, bool copy_early,
> >> +                              bool enable)
> >>  {
> >>         int func_count = get_func_count();
> >>         size_t needed;
> >> @@ -368,7 +362,7 @@ int notrace trace_init(void *buff, size_t buff_size)
> >>                 return func_count;
> >>         trace_save_gd();
> >>
> >> -       if (!was_disabled) {
> >> +       if (copy_early) {
> >>  #ifdef CONFIG_TRACE_EARLY
> >>                 ulong used, count;
> >>                 char *end;
> >> @@ -394,9 +388,6 @@ int notrace trace_init(void *buff, size_t buff_size)
> >>                 }
> >>                 puts("\n");
> >>                 memcpy(buff, hdr, used);
> >> -#else
> >> -               puts("trace: already enabled\n");
> >> -               return -EALREADY;
> >>  #endif
> >>         }
> >>         hdr = (struct trace_hdr *)buff;
> >> @@ -419,13 +410,41 @@ int notrace trace_init(void *buff, size_t buff_size)
> >>         hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
> >>         hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
> >>
> >> -       puts("trace: enabled\n");
> >> -       trace_enabled = 1;
> >> +       printf("trace: initialized, %senabled\n", enable ? "" : "not ");
> >> +       trace_enabled = enable;
> >>         trace_inited = 1;
> >>
> >>         return 0;
> >>  }
> >>
> >> +/**
> >> + * trace_init() - initialize the tracing system and enable it
> >> + *
> >> + * @buff:      Pointer to trace buffer
> >> + * @buff_size: Size of trace buffer
> >> + * Return:     0 if ok
> >> + */
> >> +int notrace trace_init(void *buff, size_t buff_size)
> >> +{
> >> +       /* If traces are enabled already, we may have early traces to copy */
> >> +       return trace_init_(buff, buff_size, trace_enabled, true);

This is a global, so you can skip passing it and just check the
trace_enabled in trace_init_()

Thanks
/Ilias
> >> +}
> >> +
> >> +/**
> >> + * trace_clear() - clear accumulated traced data
> >> + *
> >> + * May be called with tracing enabled or disabled.
> >> + */
> >> +int notrace trace_clear(void)
> >> +{
> >> +       bool was_enabled = trace_enabled;
> >> +
> >> +       if (trace_enabled)
> >> +               trace_enabled = 0;
> >> +       return trace_init_(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE,
> >> +                          false, was_enabled);
> >> +}
> >> +
> >>  #ifdef CONFIG_TRACE_EARLY
> >>  /**
> >>   * trace_early_init() - initialize the tracing system for early tracing
> >> --
> >> 2.43.0
> >>
Jerome Forissier Dec. 13, 2024, 12:30 p.m. UTC | #4
On 12/13/24 13:16, Ilias Apalodimas wrote:
> Thanks,
> 
> Apologies I missed something else as well
> 
> On Fri, 13 Dec 2024 at 14:14, Jerome Forissier
> <jerome.forissier@linaro.org> wrote:
>>
>>
>>
>> On 12/13/24 13:09, Ilias Apalodimas wrote:
>>> Hi Jerome
>>>
>>> On Mon, 9 Dec 2024 at 17:32, Jerome Forissier
>>> <jerome.forissier@linaro.org> wrote:
>>>>
>>>> Implement a "trace clear" command to delete the currently accumulated
>>>> trace data. This comes handy when someone needs to trace a particular
>>>> command. For example:
>>>>
>>>>   => trace clear; dhcp; trace pause
>>>>   => trace stats
>>>>   => trace calls 0x02100000 0x10000000
>>>>   => tftpput $profbase $profoffset 192.168.0.16:trace.bin
>>>>
>>>> Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
>>>> ---
>>>>  cmd/trace.c     | 13 ++++++++++++-
>>>>  include/trace.h |  2 ++
>>>>  lib/trace.c     | 47 +++++++++++++++++++++++++++++++++--------------
>>>>  3 files changed, 47 insertions(+), 15 deletions(-)
>>>>
>>>> diff --git a/cmd/trace.c b/cmd/trace.c
>>>> index 937e6a682ad..e95ac7ca9da 100644
>>>> --- a/cmd/trace.c
>>>> +++ b/cmd/trace.c
>>>> @@ -87,8 +87,18 @@ int do_trace(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
>>>>                 trace_set_enabled(0);
>>>>                 break;
>>>>         case 'c':
>>>> -               if (create_call_list(argc, argv))
>>>> +               switch (*(cmd + 1)) {
>>>> +               case 'a':
>>>> +                       if (create_call_list(argc, argv))
>>>> +                               return cmd_usage(cmdtp);
>>>> +                       break;
>>>> +               case 'l':
>>>> +                       if (trace_clear())
>>>> +                               return CMD_RET_FAILURE;
>>>> +                       break;
>>>
>>> Instead of this, mind changing the command to trace wipe or something similar?
>>> Then we can have a discrete case for 'w' rather than scanning for a string
>>
>> Good idea.
>>
>> Thanks,
>> --
>> Jerome
>>
>>>
>>> Cheers
>>> /Ilias
>>>> +               default:
>>>>                         return cmd_usage(cmdtp);
>>>> +               }
>>>>                 break;
>>>>         case 'r':
>>>>                 trace_set_enabled(1);
>>>> @@ -113,6 +123,7 @@ U_BOOT_CMD(
>>>>         "stats                        - display tracing statistics\n"
>>>>         "trace pause                        - pause tracing\n"
>>>>         "trace resume                       - resume tracing\n"
>>>> +       "trace clear                        - clear traces\n"
>>>>         "trace funclist [<addr> <size>]     - dump function list into buffer\n"
>>>>         "trace calls  [<addr> <size>]       "
>>>>                 "- dump function call trace into buffer"
>>>> diff --git a/include/trace.h b/include/trace.h
>>>> index 763d6d1255a..782eaae2fc2 100644
>>>> --- a/include/trace.h
>>>> +++ b/include/trace.h
>>>> @@ -100,6 +100,8 @@ void trace_set_enabled(int enabled);
>>>>
>>>>  int trace_early_init(void);
>>>>
>>>> +int trace_clear(void);
>>>> +
>>>>  /**
>>>>   * Init the trace system
>>>>   *
>>>> diff --git a/lib/trace.c b/lib/trace.c
>>>> index cabbe47b58a..def9f912c92 100644
>>>> --- a/lib/trace.c
>>>> +++ b/lib/trace.c
>>>> @@ -351,14 +351,8 @@ static int get_func_count(void)
>>>>         return gd->mon_len / FUNC_SITE_SIZE;
>>>>  }
>>>>
>>>> -/**
>>>> - * trace_init() - initialize the tracing system and enable it
>>>> - *
>>>> - * @buff:      Pointer to trace buffer
>>>> - * @buff_size: Size of trace buffer
>>>> - * Return:     0 if ok
>>>> - */
>>>> -int notrace trace_init(void *buff, size_t buff_size)
>>>> +static int notrace trace_init_(void *buff, size_t buff_size, bool copy_early,
>>>> +                              bool enable)
>>>>  {
>>>>         int func_count = get_func_count();
>>>>         size_t needed;
>>>> @@ -368,7 +362,7 @@ int notrace trace_init(void *buff, size_t buff_size)
>>>>                 return func_count;
>>>>         trace_save_gd();
>>>>
>>>> -       if (!was_disabled) {
>>>> +       if (copy_early) {
>>>>  #ifdef CONFIG_TRACE_EARLY
>>>>                 ulong used, count;
>>>>                 char *end;
>>>> @@ -394,9 +388,6 @@ int notrace trace_init(void *buff, size_t buff_size)
>>>>                 }
>>>>                 puts("\n");
>>>>                 memcpy(buff, hdr, used);
>>>> -#else
>>>> -               puts("trace: already enabled\n");
>>>> -               return -EALREADY;
>>>>  #endif
>>>>         }
>>>>         hdr = (struct trace_hdr *)buff;
>>>> @@ -419,13 +410,41 @@ int notrace trace_init(void *buff, size_t buff_size)
>>>>         hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
>>>>         hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
>>>>
>>>> -       puts("trace: enabled\n");
>>>> -       trace_enabled = 1;
>>>> +       printf("trace: initialized, %senabled\n", enable ? "" : "not ");
>>>> +       trace_enabled = enable;
>>>>         trace_inited = 1;
>>>>
>>>>         return 0;
>>>>  }
>>>>
>>>> +/**
>>>> + * trace_init() - initialize the tracing system and enable it
>>>> + *
>>>> + * @buff:      Pointer to trace buffer
>>>> + * @buff_size: Size of trace buffer
>>>> + * Return:     0 if ok
>>>> + */
>>>> +int notrace trace_init(void *buff, size_t buff_size)
>>>> +{
>>>> +       /* If traces are enabled already, we may have early traces to copy */
>>>> +       return trace_init_(buff, buff_size, trace_enabled, true);
> 
> This is a global, so you can skip passing it and just check the
> trace_enabled in trace_init_()

Unfortunately not, because trace_clear() needs to be able to invoke trace_init_()
with copy_early = false (we never want to copy the early trace buffer when
coming from the 'trace clear' command).

Thanks,
diff mbox series

Patch

diff --git a/cmd/trace.c b/cmd/trace.c
index 937e6a682ad..e95ac7ca9da 100644
--- a/cmd/trace.c
+++ b/cmd/trace.c
@@ -87,8 +87,18 @@  int do_trace(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		trace_set_enabled(0);
 		break;
 	case 'c':
-		if (create_call_list(argc, argv))
+		switch (*(cmd + 1)) {
+		case 'a':
+			if (create_call_list(argc, argv))
+				return cmd_usage(cmdtp);
+			break;
+		case 'l':
+			if (trace_clear())
+				return CMD_RET_FAILURE;
+			break;
+		default:
 			return cmd_usage(cmdtp);
+		}
 		break;
 	case 'r':
 		trace_set_enabled(1);
@@ -113,6 +123,7 @@  U_BOOT_CMD(
 	"stats                        - display tracing statistics\n"
 	"trace pause                        - pause tracing\n"
 	"trace resume                       - resume tracing\n"
+	"trace clear                        - clear traces\n"
 	"trace funclist [<addr> <size>]     - dump function list into buffer\n"
 	"trace calls  [<addr> <size>]       "
 		"- dump function call trace into buffer"
diff --git a/include/trace.h b/include/trace.h
index 763d6d1255a..782eaae2fc2 100644
--- a/include/trace.h
+++ b/include/trace.h
@@ -100,6 +100,8 @@  void trace_set_enabled(int enabled);
 
 int trace_early_init(void);
 
+int trace_clear(void);
+
 /**
  * Init the trace system
  *
diff --git a/lib/trace.c b/lib/trace.c
index cabbe47b58a..def9f912c92 100644
--- a/lib/trace.c
+++ b/lib/trace.c
@@ -351,14 +351,8 @@  static int get_func_count(void)
 	return gd->mon_len / FUNC_SITE_SIZE;
 }
 
-/**
- * trace_init() - initialize the tracing system and enable it
- *
- * @buff:	Pointer to trace buffer
- * @buff_size:	Size of trace buffer
- * Return:	0 if ok
- */
-int notrace trace_init(void *buff, size_t buff_size)
+static int notrace trace_init_(void *buff, size_t buff_size, bool copy_early,
+			       bool enable)
 {
 	int func_count = get_func_count();
 	size_t needed;
@@ -368,7 +362,7 @@  int notrace trace_init(void *buff, size_t buff_size)
 		return func_count;
 	trace_save_gd();
 
-	if (!was_disabled) {
+	if (copy_early) {
 #ifdef CONFIG_TRACE_EARLY
 		ulong used, count;
 		char *end;
@@ -394,9 +388,6 @@  int notrace trace_init(void *buff, size_t buff_size)
 		}
 		puts("\n");
 		memcpy(buff, hdr, used);
-#else
-		puts("trace: already enabled\n");
-		return -EALREADY;
 #endif
 	}
 	hdr = (struct trace_hdr *)buff;
@@ -419,13 +410,41 @@  int notrace trace_init(void *buff, size_t buff_size)
 	hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
 	hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
 
-	puts("trace: enabled\n");
-	trace_enabled = 1;
+	printf("trace: initialized, %senabled\n", enable ? "" : "not ");
+	trace_enabled = enable;
 	trace_inited = 1;
 
 	return 0;
 }
 
+/**
+ * trace_init() - initialize the tracing system and enable it
+ *
+ * @buff:	Pointer to trace buffer
+ * @buff_size:	Size of trace buffer
+ * Return:	0 if ok
+ */
+int notrace trace_init(void *buff, size_t buff_size)
+{
+	/* If traces are enabled already, we may have early traces to copy */
+	return trace_init_(buff, buff_size, trace_enabled, true);
+}
+
+/**
+ * trace_clear() - clear accumulated traced data
+ *
+ * May be called with tracing enabled or disabled.
+ */
+int notrace trace_clear(void)
+{
+	bool was_enabled = trace_enabled;
+
+	if (trace_enabled)
+		trace_enabled = 0;
+	return trace_init_(gd->trace_buff, CONFIG_TRACE_BUFFER_SIZE,
+			   false, was_enabled);
+}
+
 #ifdef CONFIG_TRACE_EARLY
 /**
  * trace_early_init() - initialize the tracing system for early tracing