blob: a0a7dd1995983445da4bd82ad0b2cbf5d63a355b [file] [log] [blame]
Simon Glasscabcbb52013-06-11 11:14:40 -07001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glasscabcbb52013-06-11 11:14:40 -07005 */
6
7#include <common.h>
8#include <command.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -05009#include <mapmem.h>
Simon Glasscabcbb52013-06-11 11:14:40 -070010#include <trace.h>
11#include <asm/io.h>
12
13static int get_args(int argc, char * const argv[], char **buff,
14 size_t *buff_ptr, size_t *buff_size)
15{
16 if (argc < 2)
17 return -1;
18 if (argc < 4) {
Simon Glassbfebc8c2017-08-03 12:22:13 -060019 *buff_size = env_get_ulong("profsize", 16, 0);
20 *buff = map_sysmem(env_get_ulong("profbase", 16, 0),
Simon Glasscabcbb52013-06-11 11:14:40 -070021 *buff_size);
Simon Glassbfebc8c2017-08-03 12:22:13 -060022 *buff_ptr = env_get_ulong("profoffset", 16, 0);
Simon Glasscabcbb52013-06-11 11:14:40 -070023 } else {
24 *buff_size = simple_strtoul(argv[3], NULL, 16);
25 *buff = map_sysmem(simple_strtoul(argv[2], NULL, 16),
26 *buff_size);
27 *buff_ptr = 0;
28 };
29 return 0;
30}
31
32static int create_func_list(int argc, char * const argv[])
33{
34 size_t buff_size, avail, buff_ptr, used;
35 unsigned int needed;
36 char *buff;
37 int err;
38
39 if (get_args(argc, argv, &buff, &buff_ptr, &buff_size))
40 return -1;
41
42 avail = buff_size - buff_ptr;
43 err = trace_list_functions(buff + buff_ptr, avail, &needed);
44 if (err)
45 printf("Error: truncated (%#x bytes needed)\n", needed);
Simon Glass5eca9772016-02-24 09:14:45 -070046 used = min(avail, (size_t)needed);
Simon Glasscabcbb52013-06-11 11:14:40 -070047 printf("Function trace dumped to %08lx, size %#zx\n",
48 (ulong)map_to_sysmem(buff + buff_ptr), used);
Simon Glass018f5302017-08-03 12:22:10 -060049 env_set_hex("profbase", map_to_sysmem(buff));
50 env_set_hex("profsize", buff_size);
51 env_set_hex("profoffset", buff_ptr + used);
Simon Glasscabcbb52013-06-11 11:14:40 -070052
53 return 0;
54}
55
56static int create_call_list(int argc, char * const argv[])
57{
58 size_t buff_size, avail, buff_ptr, used;
59 unsigned int needed;
60 char *buff;
61 int err;
62
63 if (get_args(argc, argv, &buff, &buff_ptr, &buff_size))
64 return -1;
65
66 avail = buff_size - buff_ptr;
67 err = trace_list_calls(buff + buff_ptr, avail, &needed);
68 if (err)
69 printf("Error: truncated (%#x bytes needed)\n", needed);
Simon Glass5eca9772016-02-24 09:14:45 -070070 used = min(avail, (size_t)needed);
Simon Glasscabcbb52013-06-11 11:14:40 -070071 printf("Call list dumped to %08lx, size %#zx\n",
72 (ulong)map_to_sysmem(buff + buff_ptr), used);
73
Simon Glass018f5302017-08-03 12:22:10 -060074 env_set_hex("profbase", map_to_sysmem(buff));
75 env_set_hex("profsize", buff_size);
76 env_set_hex("profoffset", buff_ptr + used);
Simon Glasscabcbb52013-06-11 11:14:40 -070077
78 return 0;
79}
80
81int do_trace(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
82{
83 const char *cmd = argc < 2 ? NULL : argv[1];
84
85 if (!cmd)
86 return cmd_usage(cmdtp);
87 switch (*cmd) {
88 case 'p':
89 trace_set_enabled(0);
90 break;
91 case 'c':
92 if (create_call_list(argc, argv))
93 return cmd_usage(cmdtp);
94 break;
95 case 'r':
96 trace_set_enabled(1);
97 break;
98 case 'f':
99 if (create_func_list(argc, argv))
100 return cmd_usage(cmdtp);
101 break;
102 case 's':
103 trace_print_stats();
104 break;
105 default:
106 return CMD_RET_USAGE;
107 }
108
109 return 0;
110}
111
112U_BOOT_CMD(
113 trace, 4, 1, do_trace,
114 "trace utility commands",
115 "stats - display tracing statistics\n"
116 "trace pause - pause tracing\n"
117 "trace resume - resume tracing\n"
118 "trace funclist [<addr> <size>] - dump function list into buffer\n"
119 "trace calls [<addr> <size>] "
120 "- dump function call trace into buffer"
121);