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