Skip to content

Change behaviour of mbed_asert to use mbed_error instead of mbed_die #8255

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

Merged
merged 6 commits into from
Oct 16, 2018
Merged

Change behaviour of mbed_asert to use mbed_error instead of mbed_die #8255

merged 6 commits into from
Oct 16, 2018

Conversation

MateuszMaz
Copy link
Contributor

@MateuszMaz MateuszMaz commented Sep 26, 2018

Description

@jamesbeyond @0xc0170 @mprse
Task: IOTCORE-378
This PR changes behaviour of mbed_assert to use mbed_error, instead of mbed_die that goes around new error reporting mechanism.

The output of following example when flag MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED = 1:

#include "mbed.h"

int main()
{
    MBED_ASSERT(0 > 1);
    return 0;
} 

is:

++ MbedOS Error Info ++
Error Status: 0x80FF0101 Code: 257 Module: 255
Error Message: Mbed assertation failed 0>1
Location: 0x2081
File:.\main.cpp+6
Error Value: 0x0
Current Thread: Id: 0x20001EAC Entry: 0x2BF7 StackSize: 0x1000 StackMem: 0x20001EF0 SP: 0x20002E18
For more info, visit: https://armmbed.github.io/mbedos-error/?error=0x80FF0101
-- MbedOS Error Info --

  1. I have decided to use error status, : MBED_ERROR_INVALID_ARGUMENT and im not really sure if it is correct, please note that.
  2. I use mbed_error instead of macros MBED_ERROR or MBED_ERROR1, to get file name and line where assertion failed and not where MBED_ERROR where used.

Pull request type

[ ] Fix
[ ] Refactor
[ ] Target update
[x] Functionality change
[ ] Breaking change

@0xc0170 0xc0170 requested review from jamesbeyond, mprse and a team September 26, 2018 10:33
@deepikabhavnani
Copy link

CC @SenRamakri

@0xc0170 0xc0170 requested a review from SenRamakri October 1, 2018 10:52
Copy link
Contributor

@jamesbeyond jamesbeyond left a comment

Choose a reason for hiding this comment

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

Hi @SenRamakri,
Could you confirm is MBED_ERROR_INVALID_ARGUMENT the correct error status to be used in this case?
If not, could you suggest one?

Everything else looks OK to me.

@0xc0170 0xc0170 changed the title Changed behaviour of mbed_asert to use mbed_error instead of mbed_die. Change behaviour of mbed_asert to use mbed_error instead of mbed_die Oct 4, 2018
@SenRamakri
Copy link
Contributor

@MateuszMaz - Please define a new error code for "assertion failed" in mbed_error.h as below -
MBED_DEFINE_SYSTEM_ERROR(ASSERTION_FAILED, 68), /* 324 Assertion Failed */
Make sure you add to the end of the enum. Everything else looks ok to me.

@MateuszMaz
Copy link
Contributor Author

The output now is:

++ MbedOS Error Info ++
Error Status: 0x80FF0144 Code: 324 Module: 255
Error Message: Mbed assertation failed 0 > 1
Location: 0x8000C21
Error Value: 0x0
Current Thread: Id: 0x2000187C Entry: 0x8001743 StackSize: 0x1000 StackMem: 0x200018C0 SP: 0x200027F8
For more info, visit: https://armmbed.github.io/mbedos-error/?error=0x80FF0144
-- MbedOS Error Info --


const char error_description[] = "Mbed assertation failed ";
unsigned error_message_length = strlen(error_description) + strlen(expr) + 1;
char error_message[error_message_length];
Copy link
Contributor

Choose a reason for hiding this comment

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

Does anyone know if variable-length arrays are totally safe in IRQ-disabled context? Possibly are, but a bit wary about potential heap fallback rather than using the stack.

Even then, using the stack is possibly hairy if big - would it be possible to reuse the static buffer that mbed_error_printf uses?

Or do we even need a buffer? Could mbed_error be persuaded to print something about assertion failure itself from the error code, so we just pass expr?

Copy link
Contributor

Choose a reason for hiding this comment

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

@kjbracey-arm - I think that make sense, lets have mbed_error.c handle creating the message in print_error_report() function. We already do that for few special error codes. That way mbed_assert_internal() can just pass the expr.
Also print_error_report() is wrapped with right flags.

@MateuszMaz - Can you please make these changes to just pass the expr into mbed_error and have print_error_report() function cook the message(Eg:- "Assertion failed", no need to prepend that with Mbed) for assertion fails.

@@ -780,7 +780,8 @@ typedef enum _mbed_error_code {
MBED_DEFINE_SYSTEM_ERROR(BLE_NO_FRAME_INITIALIZED, 65), /* 321 BLE No frame initialized */
MBED_DEFINE_SYSTEM_ERROR(BLE_BACKEND_CREATION_FAILED, 66), /* 322 BLE Backend creation failed */
MBED_DEFINE_SYSTEM_ERROR(BLE_BACKEND_NOT_INITIALIZED, 67), /* 323 BLE Backend not initialized */

MBED_DEFINE_SYSTEM_ERROR(ASSERTATION_FAILED, 68), /* 324 Assertation Failed */
Copy link
Contributor

Choose a reason for hiding this comment

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

"Assertation" is barely a word - could you correct it to "assertion" before baking it into API?

( I've already got a PR pending which corrects the text: #8076 . Been sitting unreviewed for over 3 weeks though. )

Copy link
Contributor

@kjbracey kjbracey left a comment

Choose a reason for hiding this comment

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

Can you update the GitHub message to show the final output?

@@ -349,6 +349,10 @@ static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
mbed_error_printf("MessageQueue: 0x%X, ", ctx->error_value);
break;

case MBED_ERROR_CODE_ASSERTION_FAILED:
mbed_error_printf("Assertion failed, ", ctx->error_value);
Copy link
Contributor

Choose a reason for hiding this comment

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

You want to have : here not ,, so the output is Asssertion failed: expression.

The comma was to separate the error_value, which has been left in - drop it.

@kjbracey
Copy link
Contributor

kjbracey commented Oct 8, 2018

This should resolve #6850

@cmonr
Copy link
Contributor

cmonr commented Oct 8, 2018

This should resolve #6850

Making sure I understand, this PR needs to come in before #8076 (comment), correct?

@@ -349,6 +349,10 @@ static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
mbed_error_printf("MessageQueue: 0x%X, ", ctx->error_value);
break;

case MBED_ERROR_CODE_ASSERTION_FAILED:
mbed_error_printf("Assertion failed: ", ctx->error_value);
Copy link
Contributor

@kjbracey kjbracey Oct 9, 2018

Choose a reason for hiding this comment

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

This still has the stray ctx->error_value.

(You should ideally be getting a warning about that, but unfortunately there's no format checking directives on mbed_error_printf. I am intending to add some to all the local printf-type functions).

@kjbracey
Copy link
Contributor

kjbracey commented Oct 9, 2018

Making sure I understand, this PR needs to come in before #8076 (comment), correct?

It should now be independent - I've revised #8076 to avoid collision.


void mbed_assert_internal(const char *expr, const char *file, int line)
{
core_util_critical_section_enter();
mbed_error_printf("mbed assertation failed: %s, file: %s, line %d \n", expr, file, line);
mbed_die();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this function no longer need mbed_die()?

@deepikabhavnani @SenRamakri

Copy link
Contributor

Choose a reason for hiding this comment

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

Nvm, looked over the PR again and it looks ok.

Copy link

@deepikabhavnani deepikabhavnani Oct 9, 2018

Choose a reason for hiding this comment

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

mbed_die is called but after error processing, just like any other error, next addition to removal is mbed_error()

WEAK mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number)


void mbed_assert_internal(const char *expr, const char *file, int line)
{
core_util_critical_section_enter();
mbed_error_printf("mbed assertation failed: %s, file: %s, line %d \n", expr, file, line);
mbed_die();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nvm, looked over the PR again and it looks ok.

@NirSonnenschein
Copy link
Contributor

/morph build

@kjbracey
Copy link
Contributor

It's just come to my attention that there is a significant functional change here - this is going to only output filenames if platform.error-filename-capture-enabled is on, and even then truncate to platform.max-error-filename-lin. These default to off and 16.

I don't think that's a reason to block this PR - I think the PR is sound. The problem is in the way mbed_error works - it should be accepting and printing filenames and line numbers even if it isn't "storing" them. I think we'll need to follow that up after #8255 and #8076.

The filename conditions for "assert" should not be inherently the same as the filename conditions for "error" or "warning", as assert otherwise only has a not-fully-identifying expression, whereas "error" and "warning" can be expected to have identifying text, making the filename redundant.

@cmonr
Copy link
Contributor

cmonr commented Oct 15, 2018

@SenRamakri Fyi ^^^

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 15, 2018

Build : SUCCESS

Build number : 3358
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/8255/

Triggering tests

/morph test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Oct 15, 2018

@mbed-ci
Copy link

mbed-ci commented Oct 16, 2018

@kjbracey
Copy link
Contributor

Created #8441 to make sure full filename is always shown by MBED_ASSERT, plus more fixes.

@0xc0170
Copy link
Contributor

0xc0170 commented Oct 16, 2018

/morph mbed2-build

@cmonr
Copy link
Contributor

cmonr commented Oct 16, 2018

/morph mbed2-build

@cmonr
Copy link
Contributor

cmonr commented Oct 16, 2018

Bringing this in, but it makes me a but uneasy that I can't rerun the unittest due to it's age (build 140).

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

Successfully merging this pull request may close these issues.

9 participants