target: drop deprecated code for mem2array and array2mem

Commit e370e06b72 ("target: Deprecate 'array2mem' and
'mem2array''") has already replaced the deprecated root versions
of commands mem2array and array2mem with TCL proc's that use
'read_memory' and 'write_memory'. It has left the deprecated code
of the target's version of the commands because the effort to code
the TCL replacement was not considered valuable.

To drop the last jim_handler commands, I consider much easier and
less error-prone to code them in TCL instead of converting the
deprecated code to COMMAND_HANDLER.

Drop the code in target.c and extend the TCL proc's.
While there, add the TCL procs to _telnet_autocomplete_skip.

Change-Id: I97d2370d8af479434ddf5af68541f90913982bc0
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8052
Tested-by: jenkins
This commit is contained in:
Antonio Borneo 2023-12-03 18:10:45 +01:00 committed by Evgeniy Naydanov
parent 0425853d7b
commit 88b8f2a796
2 changed files with 34 additions and 440 deletions

View File

@ -219,31 +219,44 @@ proc init_target_events {} {
proc init_board {} {
}
proc mem2array {arrayname bitwidth address count {phys ""}} {
echo "DEPRECATED! use 'read_memory' not 'mem2array'"
lappend _telnet_autocomplete_skip _post_init_target_array_mem
proc _post_init_target_array_mem {} {
set targets [target names]
lappend targets ""
upvar $arrayname $arrayname
set $arrayname ""
set i 0
foreach t $targets {
if {$t != ""} {
set t "$t "
}
eval [format {lappend ::_telnet_autocomplete_skip "%smem2array"} $t]
eval [format {proc {%smem2array} {arrayname bitwidth address count {phys ""}} {
echo "DEPRECATED! use 'read_memory' not 'mem2array'"
foreach elem [read_memory $address $bitwidth $count {*}$phys] {
set ${arrayname}($i) $elem
incr i
upvar $arrayname $arrayname
set $arrayname ""
set i 0
foreach elem [%sread_memory $address $bitwidth $count {*}$phys] {
set ${arrayname}($i) $elem
incr i
}
}} $t $t]
eval [format {lappend ::_telnet_autocomplete_skip "%sarray2mem"} $t]
eval [format {proc {%sarray2mem} {arrayname bitwidth address count {phys ""}} {
echo "DEPRECATED! use 'write_memory' not 'array2mem'"
upvar $arrayname $arrayname
set data ""
for {set i 0} {$i < $count} {incr i} {
lappend data [expr $${arrayname}($i)]
}
%swrite_memory $address $bitwidth $data {*}$phys
}} $t $t]
}
}
proc array2mem {arrayname bitwidth address count {phys ""}} {
echo "DEPRECATED! use 'write_memory' not 'array2mem'"
upvar $arrayname $arrayname
set data ""
for {set i 0} {$i < $count} {incr i} {
lappend data [expr $${arrayname}($i)]
}
write_memory $address $bitwidth $data {*}$phys
}
lappend post_init_commands _post_init_target_array_mem
# smp_on/smp_off were already DEPRECATED in v0.11.0 through http://openocd.zylin.com/4615
lappend _telnet_autocomplete_skip "aarch64 smp_on"

View File

@ -56,10 +56,6 @@ static int target_read_buffer_default(struct target *target, target_addr_t addre
uint32_t count, uint8_t *buffer);
static int target_write_buffer_default(struct target *target, target_addr_t address,
uint32_t count, const uint8_t *buffer);
static int target_array2mem(Jim_Interp *interp, struct target *target,
int argc, Jim_Obj * const *argv);
static int target_mem2array(Jim_Interp *interp, struct target *target,
int argc, Jim_Obj * const *argv);
static int target_register_user_commands(struct command_context *cmd_ctx);
static int target_get_gdb_fileio_info_default(struct target *target,
struct gdb_fileio_info *fileio_info);
@ -4399,192 +4395,6 @@ COMMAND_HANDLER(handle_profile_command)
return retval;
}
static int new_u64_array_element(Jim_Interp *interp, const char *varname, int idx, uint64_t val)
{
char *namebuf;
Jim_Obj *obj_name, *obj_val;
int result;
namebuf = alloc_printf("%s(%d)", varname, idx);
if (!namebuf)
return JIM_ERR;
obj_name = Jim_NewStringObj(interp, namebuf, -1);
jim_wide wide_val = val;
obj_val = Jim_NewWideObj(interp, wide_val);
if (!obj_name || !obj_val) {
free(namebuf);
return JIM_ERR;
}
Jim_IncrRefCount(obj_name);
Jim_IncrRefCount(obj_val);
result = Jim_SetVariable(interp, obj_name, obj_val);
Jim_DecrRefCount(interp, obj_name);
Jim_DecrRefCount(interp, obj_val);
free(namebuf);
/* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
return result;
}
static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
{
int e;
LOG_WARNING("DEPRECATED! use 'read_memory' not 'mem2array'");
/* argv[0] = name of array to receive the data
* argv[1] = desired element width in bits
* argv[2] = memory address
* argv[3] = count of times to read
* argv[4] = optional "phys"
*/
if (argc < 4 || argc > 5) {
Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
return JIM_ERR;
}
/* Arg 0: Name of the array variable */
const char *varname = Jim_GetString(argv[0], NULL);
/* Arg 1: Bit width of one element */
long l;
e = Jim_GetLong(interp, argv[1], &l);
if (e != JIM_OK)
return e;
const unsigned int width_bits = l;
if (width_bits != 8 &&
width_bits != 16 &&
width_bits != 32 &&
width_bits != 64) {
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp),
"Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL);
return JIM_ERR;
}
const unsigned int width = width_bits / 8;
/* Arg 2: Memory address */
jim_wide wide_addr;
e = Jim_GetWide(interp, argv[2], &wide_addr);
if (e != JIM_OK)
return e;
target_addr_t addr = (target_addr_t)wide_addr;
/* Arg 3: Number of elements to read */
e = Jim_GetLong(interp, argv[3], &l);
if (e != JIM_OK)
return e;
size_t len = l;
/* Arg 4: phys */
bool is_phys = false;
if (argc > 4) {
int str_len = 0;
const char *phys = Jim_GetString(argv[4], &str_len);
if (!strncmp(phys, "phys", str_len))
is_phys = true;
else
return JIM_ERR;
}
/* Argument checks */
if (len == 0) {
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
return JIM_ERR;
}
if ((addr + (len * width)) < addr) {
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
return JIM_ERR;
}
if (len > 65536) {
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp),
"mem2array: too large read request, exceeds 64K items", NULL);
return JIM_ERR;
}
if ((width == 1) ||
((width == 2) && ((addr & 1) == 0)) ||
((width == 4) && ((addr & 3) == 0)) ||
((width == 8) && ((addr & 7) == 0))) {
/* alignment correct */
} else {
char buf[100];
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
sprintf(buf, "mem2array address: " TARGET_ADDR_FMT " is not aligned for %" PRIu32 " byte reads",
addr,
width);
Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
return JIM_ERR;
}
/* Transfer loop */
/* index counter */
size_t idx = 0;
const size_t buffersize = 4096;
uint8_t *buffer = malloc(buffersize);
if (!buffer)
return JIM_ERR;
/* assume ok */
e = JIM_OK;
while (len) {
/* Slurp... in buffer size chunks */
const unsigned int max_chunk_len = buffersize / width;
const size_t chunk_len = MIN(len, max_chunk_len); /* in elements.. */
int retval;
if (is_phys)
retval = target_read_phys_memory(target, addr, width, chunk_len, buffer);
else
retval = target_read_memory(target, addr, width, chunk_len, buffer);
if (retval != ERROR_OK) {
/* BOO !*/
LOG_ERROR("mem2array: Read @ " TARGET_ADDR_FMT ", w=%u, cnt=%zu, failed",
addr,
width,
chunk_len);
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
e = JIM_ERR;
break;
} else {
for (size_t i = 0; i < chunk_len ; i++, idx++) {
uint64_t v = 0;
switch (width) {
case 8:
v = target_buffer_get_u64(target, &buffer[i*width]);
break;
case 4:
v = target_buffer_get_u32(target, &buffer[i*width]);
break;
case 2:
v = target_buffer_get_u16(target, &buffer[i*width]);
break;
case 1:
v = buffer[i] & 0x0ff;
break;
}
new_u64_array_element(interp, varname, idx, v);
}
len -= chunk_len;
addr += chunk_len * width;
}
}
free(buffer);
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
return e;
}
COMMAND_HANDLER(handle_target_read_memory)
{
/*
@ -4708,201 +4518,6 @@ COMMAND_HANDLER(handle_target_read_memory)
return ERROR_OK;
}
static int get_u64_array_element(Jim_Interp *interp, const char *varname, size_t idx, uint64_t *val)
{
char *namebuf = alloc_printf("%s(%zu)", varname, idx);
if (!namebuf)
return JIM_ERR;
Jim_Obj *obj_name = Jim_NewStringObj(interp, namebuf, -1);
if (!obj_name) {
free(namebuf);
return JIM_ERR;
}
Jim_IncrRefCount(obj_name);
Jim_Obj *obj_val = Jim_GetVariable(interp, obj_name, JIM_ERRMSG);
Jim_DecrRefCount(interp, obj_name);
free(namebuf);
if (!obj_val)
return JIM_ERR;
jim_wide wide_val;
int result = Jim_GetWide(interp, obj_val, &wide_val);
*val = wide_val;
return result;
}
static int target_array2mem(Jim_Interp *interp, struct target *target,
int argc, Jim_Obj *const *argv)
{
int e;
LOG_WARNING("DEPRECATED! use 'write_memory' not 'array2mem'");
/* argv[0] = name of array from which to read the data
* argv[1] = desired element width in bits
* argv[2] = memory address
* argv[3] = number of elements to write
* argv[4] = optional "phys"
*/
if (argc < 4 || argc > 5) {
Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
return JIM_ERR;
}
/* Arg 0: Name of the array variable */
const char *varname = Jim_GetString(argv[0], NULL);
/* Arg 1: Bit width of one element */
long l;
e = Jim_GetLong(interp, argv[1], &l);
if (e != JIM_OK)
return e;
const unsigned int width_bits = l;
if (width_bits != 8 &&
width_bits != 16 &&
width_bits != 32 &&
width_bits != 64) {
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp),
"Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL);
return JIM_ERR;
}
const unsigned int width = width_bits / 8;
/* Arg 2: Memory address */
jim_wide wide_addr;
e = Jim_GetWide(interp, argv[2], &wide_addr);
if (e != JIM_OK)
return e;
target_addr_t addr = (target_addr_t)wide_addr;
/* Arg 3: Number of elements to write */
e = Jim_GetLong(interp, argv[3], &l);
if (e != JIM_OK)
return e;
size_t len = l;
/* Arg 4: Phys */
bool is_phys = false;
if (argc > 4) {
int str_len = 0;
const char *phys = Jim_GetString(argv[4], &str_len);
if (!strncmp(phys, "phys", str_len))
is_phys = true;
else
return JIM_ERR;
}
/* Argument checks */
if (len == 0) {
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp),
"array2mem: zero width read?", NULL);
return JIM_ERR;
}
if ((addr + (len * width)) < addr) {
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp),
"array2mem: addr + len - wraps to zero?", NULL);
return JIM_ERR;
}
if (len > 65536) {
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp),
"array2mem: too large memory write request, exceeds 64K items", NULL);
return JIM_ERR;
}
if ((width == 1) ||
((width == 2) && ((addr & 1) == 0)) ||
((width == 4) && ((addr & 3) == 0)) ||
((width == 8) && ((addr & 7) == 0))) {
/* alignment correct */
} else {
char buf[100];
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
sprintf(buf, "array2mem address: " TARGET_ADDR_FMT " is not aligned for %" PRIu32 " byte reads",
addr,
width);
Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
return JIM_ERR;
}
/* Transfer loop */
/* assume ok */
e = JIM_OK;
const size_t buffersize = 4096;
uint8_t *buffer = malloc(buffersize);
if (!buffer)
return JIM_ERR;
/* index counter */
size_t idx = 0;
while (len) {
/* Slurp... in buffer size chunks */
const unsigned int max_chunk_len = buffersize / width;
const size_t chunk_len = MIN(len, max_chunk_len); /* in elements.. */
/* Fill the buffer */
for (size_t i = 0; i < chunk_len; i++, idx++) {
uint64_t v = 0;
if (get_u64_array_element(interp, varname, idx, &v) != JIM_OK) {
free(buffer);
return JIM_ERR;
}
switch (width) {
case 8:
target_buffer_set_u64(target, &buffer[i * width], v);
break;
case 4:
target_buffer_set_u32(target, &buffer[i * width], v);
break;
case 2:
target_buffer_set_u16(target, &buffer[i * width], v);
break;
case 1:
buffer[i] = v & 0x0ff;
break;
}
}
len -= chunk_len;
/* Write the buffer to memory */
int retval;
if (is_phys)
retval = target_write_phys_memory(target, addr, width, chunk_len, buffer);
else
retval = target_write_memory(target, addr, width, chunk_len, buffer);
if (retval != ERROR_OK) {
/* BOO !*/
LOG_ERROR("array2mem: Write @ " TARGET_ADDR_FMT ", w=%u, cnt=%zu, failed",
addr,
width,
chunk_len);
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
e = JIM_ERR;
break;
}
addr += chunk_len * width;
}
free(buffer);
Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
return e;
}
static int target_jim_write_memory(Jim_Interp *interp, int argc,
Jim_Obj * const *argv)
{
@ -5639,24 +5254,6 @@ static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *a
return target_configure(&goi, target);
}
static int jim_target_mem2array(Jim_Interp *interp,
int argc, Jim_Obj *const *argv)
{
struct command_context *cmd_ctx = current_command_context(interp);
assert(cmd_ctx);
struct target *target = get_current_target(cmd_ctx);
return target_mem2array(interp, target, argc - 1, argv + 1);
}
static int jim_target_array2mem(Jim_Interp *interp,
int argc, Jim_Obj *const *argv)
{
struct command_context *cmd_ctx = current_command_context(interp);
assert(cmd_ctx);
struct target *target = get_current_target(cmd_ctx);
return target_array2mem(interp, target, argc - 1, argv + 1);
}
COMMAND_HANDLER(handle_target_examine)
{
bool allow_defer = false;
@ -5978,22 +5575,6 @@ static const struct command_registration target_instance_command_handlers[] = {
.help = "Display target memory as 8-bit bytes",
.usage = "address [count]",
},
{
.name = "array2mem",
.mode = COMMAND_EXEC,
.jim_handler = jim_target_array2mem,
.help = "Writes Tcl array of 8/16/32 bit numbers "
"to target memory",
.usage = "arrayname bitwidth address count",
},
{
.name = "mem2array",
.mode = COMMAND_EXEC,
.jim_handler = jim_target_mem2array,
.help = "Loads Tcl array of 8/16/32 bit numbers "
"from target memory",
.usage = "arrayname bitwidth address count",
},
{
.name = "get_reg",
.mode = COMMAND_EXEC,