Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on exit with uninitialized target #1015

Open
wants to merge 1 commit into
base: riscv
Choose a base branch
from

Conversation

tom-van
Copy link
Contributor

@tom-van tom-van commented Feb 11, 2024

riscv_deinit_target() tries to get_target_type() even if the examination failed or was not called at all. dtm_version is not known and an annoying error is printed:

Error: [riscv.cpu.0] Unsupported DTM version: -1
Error: [riscv.cpu.0] Could not identify target type.

Avoid calling get_target_type() if dtm_version is
DTM_DTMCS_VERSION_UNKNOWN.

Change-Id: I48239b1b11561357e845f3a6cb3a8e3b19e35715

riscv_deinit_target() tries to get_target_type() even
if the examination failed or was not called at all.
dtm_version is not known and an annoying error is printed:

  Error: [riscv.cpu.0] Unsupported DTM version: -1
  Error: [riscv.cpu.0] Could not identify target type.

Avoid calling get_target_type() if dtm_version is
DTM_DTMCS_VERSION_UNKNOWN.

Signed-off-by: Tomas Vanek <[email protected]>
Change-Id: I48239b1b11561357e845f3a6cb3a8e3b19e35715
Copy link
Collaborator

@en-sc en-sc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the patch! I also find these messages annoying, but there is an underlying issue -- a proper deinitialization can not be guaranteed in such cases for now. So, IMHO, a message should be printed here.

if (!tt)
LOG_TARGET_ERROR(target, "Could not identify target type.");
struct target_type *tt = NULL;
if (info->dtm_version != DTM_DTMCS_VERSION_UNKNOWN) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not particularly safe. Please, consider a target that was not properly initialized. target->arch_info can be still zero or point to uninitialized memory.
The condition should at least be modified to:

if (info && info->dtm_version != DTM_DTMCS_VERSION_UNKNOWN) {

However, under such condition get_target_type() will never print an error, and IMHO, if a target was never initialized but riscv_deinit_target() is called -- an error should be printed.

In general, I would like to leave the messaging here.
In case examine() failed, the state of the target is not the same as if the target was just initialized. Moreover, it is not documented either. This is an issue. I am preparing some patches aimed at addressing this. BTW, your patch with riscv013_dm_free() is a great help in this regard, thanks!

If you still find the message too annoying -- please, consider changing it to LOG_TARGET_DEBUG().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition should at least be modified to:

if (info && info->dtm_version != DTM_DTMCS_VERSION_UNKNOWN) {

Good point, will fix if we go on with this patch.

In case examine() failed, the state of the target is not the same as if the target was just initialized. Moreover, it is not documented either. This is an issue. I am preparing some patches aimed at addressing this.

Oh I think I see at least one issue:
If tt->init_target() succeds
https://github.com/riscv/riscv-openocd/blob/5d4fa0001e2f6bd23caf10fd02f980053adb2b3b/src/target/riscv/riscv.c#L1740
and the following version specific examination fails
https://github.com/riscv/riscv-openocd/blob/5d4fa0001e2f6bd23caf10fd02f980053adb2b3b/src/target/riscv/riscv.c#L1744-L1746
dtm_version gets reset to unknown
https://github.com/riscv/riscv-openocd/blob/5d4fa0001e2f6bd23caf10fd02f980053adb2b3b/src/target/riscv/riscv.c#L1750-L1752
and later
https://github.com/riscv/riscv-openocd/blob/5d4fa0001e2f6bd23caf10fd02f980053adb2b3b/src/target/riscv/riscv.c#L533
will not recognize target type and therefore will not call
https://github.com/riscv/riscv-openocd/blob/5d4fa0001e2f6bd23caf10fd02f980053adb2b3b/src/target/riscv/riscv.c#L545-L546

Hmm, much worse than an annoying message. Giving up for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants