Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Why allow debug in pcode file?

Subject: Why allow debug in pcode file?

From: Mayi DA

Date: 17 Jul, 2011 15:30:14

Message: 1 of 4

Dear all
As you know, when m file is pcode to p file, that means the author want to protect his code. However, run p file in MATLAB, you can use dbstop and other debug command to run p file step by step. So my question is:
Why matlab allow pcode file to be debug?
How to forbid p code debug?

regards
damayi
2011/07/17

Subject: Why allow debug in pcode file?

From: per isakson

Date: 17 Jul, 2011 22:22:08

Message: 2 of 4

"Mayi DA" wrote in message <ivuv66$snc$1@newscl01ah.mathworks.com>...
> Dear all
> As you know, when m file is pcode to p file, that means the author want to protect his code. However, run p file in MATLAB, you can use dbstop and other debug command to run p file step by step. So my question is:
> Why matlab allow pcode file to be debug?
> How to forbid p code debug?
>
> regards
> damayi
> 2011/07/17

AFAIK: It not possible to forbid (/avoid) stepping through pcode.

Why? I guess it's a mistake. It's not well documented.

- per

Subject: Why allow debug in pcode file?

From: Jan Simon

Date: 18 Jul, 2011 15:14:09

Message: 3 of 4

Dear Per,

> AFAIK: It not possible to forbid (/avoid) stepping through pcode.
> Why? I guess it's a mistake. It's not well documented.

You can step through P-code *line by line*. If you concatenate all lines of your M-function to a *single* line, the debugger cannot resolve the single commands.

In addition you can overload the function DBSTEP, DBCONT, ... in the function, such that the debugging leads to unexpected behaviour, e.g. a termination of the program.

A P-function can refuse to work if the debug mode is enabled, see "feature('IsDebugMode')". But this is not user-friendly, because if a user-defined subfunction must be debugged.

Anyhow, it cannot see any useful application in stepping through a P-function - except for trying to obtain information, which was wanted to be hidden by the author. Therefore I suggest anybody, who wants to keep secrets in a P-file to write an enhancement request to TMW.

Kind regards, Jan

Subject: Why allow debug in pcode file?

From: bcomisky

Date: 18 Dec, 2012 21:08:36

Message: 4 of 4

On Monday, July 18, 2011 11:14:09 AM UTC-4, Jan Simon wrote:
> Dear Per,
>
> > AFAIK: It not possible to forbid (/avoid) stepping through pcode.
> > Why? I guess it's a mistake. It's not well documented.
>
> You can step through P-code *line by line*. If you concatenate all lines of your M-function to a *single* line, the debugger cannot resolve the single commands.
>
> In addition you can overload the function DBSTEP, DBCONT, ... in the function, such that the debugging leads to unexpected behaviour, e.g. a termination of the program.
>
> A P-function can refuse to work if the debug mode is enabled, see "feature('IsDebugMode')". But this is not user-friendly, because if a user-defined subfunction must be debugged.
>
> Anyhow, it cannot see any useful application in stepping through a P-function - except for trying to obtain information, which was wanted to be hidden by the author. Therefore I suggest anybody, who wants to keep secrets in a P-file to write an enhancement request to TMW.
>
> Kind regards, Jan

An old thread, but read on if you are relying on checking feature('IsDebugMode') to provide some protection to debugging your p-code.

It looks like (at least on r2011A) that feature('IsDebugMode') only returns true if the function was executed from the K>> prompt directly. If I make a wrapper m-file that calls the p-code function, then "keyboard" just before calling the p-code from this wrapper, I can "dbstep" into the p-code and feature('IsDebugMode') will return false. I can locally redefine "dbstep" within the p-code function, but "dbstep N" from within the wrapper will still let me stop at some number of lines into the p-code function and look at variable values.

Concatenating all the lines into one should be relatively safe, if you use "dbclear" at the beginning to make sure that Ctrl-C (or execution errors) won't drop you into the debugger somewhere in the middle of executing the p-code of your concatenated m-file.

Bill

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us