Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <command.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 8 | #include <env.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 9 | #include <mapmem.h> |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 10 | #include <trace.h> |
| 11 | #include <asm/io.h> |
| 12 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 13 | static int get_args(int argc, char *const argv[], char **buff, |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 14 | size_t *buff_ptr, size_t *buff_size) |
| 15 | { |
| 16 | if (argc < 2) |
| 17 | return -1; |
| 18 | if (argc < 4) { |
Simon Glass | 22c34c2 | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 19 | *buff_size = env_get_ulong("profsize", 16, 0); |
| 20 | *buff = map_sysmem(env_get_ulong("profbase", 16, 0), |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 21 | *buff_size); |
Simon Glass | 22c34c2 | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 22 | *buff_ptr = env_get_ulong("profoffset", 16, 0); |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 23 | } 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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 32 | static int create_func_list(int argc, char *const argv[]) |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 33 | { |
Heinrich Schuchardt | 1469090 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 34 | size_t buff_size, avail, buff_ptr, needed, used; |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 35 | char *buff; |
| 36 | int err; |
| 37 | |
| 38 | if (get_args(argc, argv, &buff, &buff_ptr, &buff_size)) |
| 39 | return -1; |
| 40 | |
| 41 | avail = buff_size - buff_ptr; |
| 42 | err = trace_list_functions(buff + buff_ptr, avail, &needed); |
| 43 | if (err) |
Heinrich Schuchardt | 1469090 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 44 | printf("Error: truncated (%#zx bytes needed)\n", needed); |
Simon Glass | 3044ba1 | 2016-02-24 09:14:45 -0700 | [diff] [blame] | 45 | used = min(avail, (size_t)needed); |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 46 | printf("Function trace dumped to %08lx, size %#zx\n", |
| 47 | (ulong)map_to_sysmem(buff + buff_ptr), used); |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 48 | env_set_hex("profbase", map_to_sysmem(buff)); |
| 49 | env_set_hex("profsize", buff_size); |
| 50 | env_set_hex("profoffset", buff_ptr + used); |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 55 | static int create_call_list(int argc, char *const argv[]) |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 56 | { |
Heinrich Schuchardt | 1469090 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 57 | size_t buff_size, avail, buff_ptr, needed, used; |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 58 | char *buff; |
| 59 | int err; |
| 60 | |
| 61 | if (get_args(argc, argv, &buff, &buff_ptr, &buff_size)) |
| 62 | return -1; |
| 63 | |
| 64 | avail = buff_size - buff_ptr; |
| 65 | err = trace_list_calls(buff + buff_ptr, avail, &needed); |
| 66 | if (err) |
Heinrich Schuchardt | 1469090 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 67 | printf("Error: truncated (%#zx bytes needed)\n", needed); |
Simon Glass | 3044ba1 | 2016-02-24 09:14:45 -0700 | [diff] [blame] | 68 | used = min(avail, (size_t)needed); |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 69 | printf("Call list dumped to %08lx, size %#zx\n", |
| 70 | (ulong)map_to_sysmem(buff + buff_ptr), used); |
| 71 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 72 | env_set_hex("profbase", map_to_sysmem(buff)); |
| 73 | env_set_hex("profsize", buff_size); |
| 74 | env_set_hex("profoffset", buff_ptr + used); |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 79 | int do_trace(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Simon Glass | a749ae0 | 2013-06-11 11:14:40 -0700 | [diff] [blame] | 80 | { |
| 81 | const char *cmd = argc < 2 ? NULL : argv[1]; |
| 82 | |
| 83 | if (!cmd) |
| 84 | return cmd_usage(cmdtp); |
| 85 | switch (*cmd) { |
| 86 | case 'p': |
| 87 | trace_set_enabled(0); |
| 88 | break; |
| 89 | case 'c': |
| 90 | if (create_call_list(argc, argv)) |
| 91 | return cmd_usage(cmdtp); |
| 92 | break; |
| 93 | case 'r': |
| 94 | trace_set_enabled(1); |
| 95 | break; |
| 96 | case 'f': |
| 97 | if (create_func_list(argc, argv)) |
| 98 | return cmd_usage(cmdtp); |
| 99 | break; |
| 100 | case 's': |
| 101 | trace_print_stats(); |
| 102 | break; |
| 103 | default: |
| 104 | return CMD_RET_USAGE; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | U_BOOT_CMD( |
| 111 | trace, 4, 1, do_trace, |
| 112 | "trace utility commands", |
| 113 | "stats - display tracing statistics\n" |
| 114 | "trace pause - pause tracing\n" |
| 115 | "trace resume - resume tracing\n" |
| 116 | "trace funclist [<addr> <size>] - dump function list into buffer\n" |
| 117 | "trace calls [<addr> <size>] " |
| 118 | "- dump function call trace into buffer" |
| 119 | ); |