Skip to content

Adding debugger awarness with Keil MDK #8887

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 3 commits into from
Jan 17, 2019

Conversation

deepikabhavnani
Copy link

Description

Main thread in Mbed OS is statically allocated and was not available in call stack of Keil MDK. The RTX5 kernel requires statically allocated thread information objects that are placed into a specific section to enable RTOS thread awareness in Keil MDK. This fix is to keep main thread in specific section of memory.

Issue: #8793

Pull request type

[X] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

@kjbracey
Copy link
Contributor

kjbracey commented Nov 28, 2018

How does this interact with #7244? If that still applies, this will cost a lot of ROM in ARMCC?

Presumably nothing you could do about the underlying issue, but maybe you could restrict the section attribute to MBED_DEBUG builds?

@deepikabhavnani
Copy link
Author

Tested blinky example with and without change to see the impact on ROM size, please see the results below:

Without Fix

| Module                                                       |         .text |       .data |        .bss |
|--------------------------------------------------------------|---------------|-------------|-------------|
| mbed-os\rtos\TARGET_CORTEX\mbed_rtos_rtx.o                   |     216(+216) |       4(+4) | 4248(+4248) |

| Subtotals                                                    | 39461(+39461) | 2549(+2549) | 8292(+8292) |
Total Static RAM memory (data + bss): 10841(+10841) bytes
Total Flash memory (text + data): 42010(+42010) bytes

With Fix

| Module                                                       |         .text |       .data |        .bss |
| mbed-os\rtos\TARGET_CORTEX\mbed_rtos_rtx.o                   |   216(+0)     |   72(+68)   | 4180(-68)   |
| Subtotals                                                    | 39517(+56)    | 2617(+68)   | 8224(-68)   |
Total Static RAM memory (data + bss): 10841(+0) bytes
Total Flash memory (text + data): 42134(+124) bytes

@deepikabhavnani
Copy link
Author

@kjbracey-arm - Having just the control block in BSS section resolves the issue, i.e. we are not putting the main thread stack in .bss here. https://github.com/ARMmbed/mbed-os/pull/8887/files#diff-91e672466b09fb21b174f0f44e07b8aaL31 hence the size impact is not much.

On another thought can we do same do Idle / timer threads?


C:\Users\deebha02\work_mbed\KeilIDE_fix\mbed-os>git diff
diff --git a/rtos/TARGET_CORTEX/rtx5/RTX/Source/rtx_lib.c b/rtos/TARGET_CORTEX/rtx5/RTX/Source/rtx_lib.c
index 8fe704f80..f35481981 100644
--- a/rtos/TARGET_CORTEX/rtx5/RTX/Source/rtx_lib.c
+++ b/rtos/TARGET_CORTEX/rtx5/RTX/Source/rtx_lib.c
@@ -122,8 +122,7 @@ static osRtxThread_t os_idle_thread_cb \
 __attribute__((section(".bss.os.thread.cb")));

 // Idle Thread Stack
-static uint64_t os_idle_thread_stack[OS_IDLE_THREAD_STACK_SIZE/8] \
-__attribute__((section(".bss.os.thread.stack")));
+static uint64_t os_idle_thread_stack[OS_IDLE_THREAD_STACK_SIZE/8];

 // Idle Thread Attributes
 static const osThreadAttr_t os_idle_thread_attr = {
@@ -179,8 +178,7 @@ static osRtxThread_t os_timer_thread_cb \
 __attribute__((section(".bss.os.thread.cb")));

 // Timer Thread Stack
-static uint64_t os_timer_thread_stack[OS_TIMER_THREAD_STACK_SIZE/8] \
-__attribute__((section(".bss.os.thread.stack")));
+static uint64_t os_timer_thread_stack[OS_TIMER_THREAD_STACK_SIZE/8];

| mbed-os\rtos\TARGET_CORTEX\rtx5\RTX\Source\rtx_lib.o | 285(+0) | 332(-768) | 1280(+768) |

@kjbracey
Copy link
Contributor

We could make such a local change to RTX, #7244 closed with a change not happening upstream, and not making a local change either, but we could still do something locally.

And just removing the area directive from the stacks altogether seems plausible - if we have no special handling for the .bss.os area in our linker maps.

If this PR was a net ROM reduction by taking the stacks out of ROM and putting this control block in, @bulislaw couldn't complain.

@deepikabhavnani
Copy link
Author

if we have no special handling for the .bss.os area in our linker maps.

I am not sure of any special handling for .bss.os.
@c1728p9 - your inputs please..

@deepikabhavnani
Copy link
Author

@JonatanAntoni - Please review and share your thoughts on this PR

@JonatanAntoni
Copy link
Member

Hi @deepikabhavnani,

this looks meaningful to me. The relevant part for RTX5 awareness in MDK (and potentially other IDEs) is to place all the object control blocks to their specific memory sections. This applies to all RTOS objects (not only threads) when control blocks are allocated manually (from RTX point of view).

Cheers,
Jonatan

@deepikabhavnani
Copy link
Author

deepikabhavnani commented Dec 18, 2018

@c1728p9 @bulislaw - Please review

@c1728p9
Copy link
Contributor

c1728p9 commented Dec 19, 2018

The relevant part for RTX5 awareness in MDK (and potentially other IDEs) is to place all the object control blocks to their specific memory sections. This applies to all RTOS objects (not only threads) when control blocks are allocated manually (from RTX point of view).

@JonatanAntoni does this mean that threads created by using the Thread class in mbed-os will not show up in MDKs RTX5 awareness? The Thread class contains the control block as one of its members - _obj_mem. Note that mbed_rtos_storage_thread_t is a typedef of osRtxThread_t.

@JonatanAntoni
Copy link
Member

[..] does [it] mean that threads created by using the Thread class in mbed-os will not show up in MDKs RTX5 awareness?

I have never tried to debug a mbed application using MDK, but I assume it won't show up. The RTOS-awareness (generally speaking component awareness) in MDK is generated using a generic Software Component View Description. The section names are used as a common entry point for control block look up. Control blocks spread randomly over the memory cannot be found.

Is the --keep actually needed with more recent versions of RTX?

--keep is not needed for Arm Compiler 6 but for Arm Compiler 5. The symbol os_cb_sections denotes the top level entry for control block look up, see rtx_lib.c:583.

Cheers,
Jonatan

@cmonr
Copy link
Contributor

cmonr commented Dec 27, 2018

Testing PR with CI.

@mbed-ci
Copy link

mbed-ci commented Dec 27, 2018

Test run: SUCCESS

Summary: 11 of 11 test jobs passed
Build number : 1
Build artifacts

@deepikabhavnani
Copy link
Author

@c1728p9 @kjbracey-arm @bulislaw - Please review

@deepikabhavnani deepikabhavnani force-pushed the uVision_fix branch 2 times, most recently from 03bdb0d to c75ac42 Compare January 3, 2019 20:08
@cmonr
Copy link
Contributor

cmonr commented Jan 9, 2019

CI started

@mbed-ci
Copy link

mbed-ci commented Jan 10, 2019

Test run: SUCCESS

Summary: 11 of 11 test jobs passed
Build number : 2
Build artifacts

@deepikabhavnani
Copy link
Author

Conflicts :-(
@ARMmbed/mbed-os-maintainers - This PR is tagged for 5.12 and was ready to merge, do we have any restrictions on merge to master for PR's tagged for minor release?

Deepika added 2 commits January 15, 2019 12:04
Main thread in Mbed OS is statically allocated and was not available in call
stack of Keil MDK. The RTX5 kernel requires statically allocated thread
information objects that are placed into a specific section to enable RTOS
thread awareness in Keil MDK. This fix is to keep main thread in specific
section of memory.
In case of ARM compiler, idle and timer thread stack though assigned
to `.bss.os` section since not zero initialized are part of `data` section.

In this commit, we are moving stacks of idle and timer thread to bss
section and thereby saving ROM space.
@deepikabhavnani
Copy link
Author

Rebased and conflicts resolved. Changing label from ready to merge to needs:CI

@cmonr
Copy link
Contributor

cmonr commented Jan 17, 2019

@deepikabhavnani Nothing hard, but we definitely prefer if PRs for minior releases come in a bit later than right after a minor release is made, if that makes sense.

Lowers the risk that early 5.x.{1,2,3} patch releases will be troublesome.

@cmonr
Copy link
Contributor

cmonr commented Jan 17, 2019

CI started

@mbed-ci
Copy link

mbed-ci commented Jan 17, 2019

Test run: FAILED

Summary: 1 of 11 test jobs failed
Build number : 3
Build artifacts

Failed test jobs:

  • jenkins-ci/mbed-os-ci_cloud-client-test

@0xc0170
Copy link
Contributor

0xc0170 commented Jan 17, 2019

Client test got stuck, restarted

@deepikabhavnani
Copy link
Author

deepikabhavnani commented Jan 17, 2019

we definitely prefer if PRs for minior releases come in a bit later than right after a minor release is made, if that makes sense.

👍 This helps, will consider this when adding changes for minor release. Else can we add and park those changes ?

@chentang16
Copy link

I just found out the proposed "patch" has only been applied to the "_main_obj" thread. Actually all internal RTX5 threads defined and created by mbedOS need to have such a patch.
The change or improvement made so far can only make OS awareness debugging in MDK feasible for one single main thread, which isn't sufficient.

@deepikabhavnani
Copy link
Author

@chentang16 - Yes the patch is applied to main thread, for other threads it will be change in design since we allocate dynamic memory for thread stack. Issue can be closed for Keil MDK debugger but it will remain open in Mbed OS

@kjbracey
Copy link
Contributor

kjbracey commented Mar 7, 2019

Yes, this just handles the static built-in threads.

It's unclear what the path forward here is. Mbed OS is currently permitting general allocation, including thread control blocks and stacks even being on the heap, rather than being restricted to static allocation. If MDK can only debug threads allocated via fixed static memory, it's never going to be able to handle the general case, as represented by our Thread class.

I don't understand what MDK actually does with the special area names - if you were to just stick __attribute__((section(".bss.os.thread.cb"))) on a static Thread object, would that make it work?

@chentang16
Copy link

"custom control block" plays the key role here in the MDK debugger, as explained here http://www.keil.com/support/docs/4026.htm

@kjbracey
Copy link
Contributor

kjbracey commented Mar 7, 2019

Is the requirement that the section be an array of the raw RTX control blocks? I imagine it is, which would mean putting an entire Thread object into it isn't going to work.

Other debuggers like pyOCD manage full OS awareness without any special handling, as they can follow the linked lists of the RTX kernel data structures. I can see some robustness advantages to MDK's approach - it would handle OS data structure corruption better - but it does seem limiting.

But if MDK is a hybrid, where it's following the linked lists and only using the section address as a sanity-check for values, then putting the Thread in there would work...

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