Xcode disassembly debugging IOS simulator program (8) disassembly of your own code to grasp the rules

Source: Internet
Author: User

After mastering the basic skills, it is basically difficult to understand all the disassembly results. It is better to teach fish to fish:

Observe what you writeCodeTo master the disassembly results of various codes, so as to reverse guess the source code of the system code.

When debugging your own code, you can constantly switch to view the source code and disassembly to locate where the code is executed.

Here we use two simple C ++ and objective-C classes as examples:

 

 
Class testc {int m_var; public: int getvar (); void setvar (INT var) ;}; @ interface testoc: nsobject {int m_var;}-(void) setvar :( INT) vaR; @ endint g_var = 0; int * g_pvar = & g_var; void testc: setvar (INT var) {int l_var = 0; l_var = var; m_var = var; g_var = var; * g_pvar = var;} @ implementation testoc-(void) setvar :( INT) var {int l_var = 0; l_var = var; m_var = var; g_var = var; * g_pvar = var;} @ end

The following code is called externally:

 

 

-(Void) viewdidload {testc test; test. setvar (100); testoc * t = [[testoc new] autorelease]; [t setvar: 100]; ......

Add a breakpoint to the setvar of the two classes to view the disassembly result.

 

 

 
Webviewresearch 'testc: setvar (INT) at testclass. MM: 15: 0x8133: pushl % ebp0x8bdb: movl % ESP, % ebp0x8bdd: calll 0x8be2; testc: setvar (INT) + 8 at testclass. MM: 150x8be2: popl % eax0x8be3: movl 12 (% EBP), % ecx0x8be6: movl 8 (% EBP), % edx0x8be9: movl % ECx, (% EDX) 0x8beb: movl % ECx, 30406 (% eax) 0x8bf1: movl 29170 (% eax), % eax0x8bf7: movl % ECx, (% eax) 0x8bf9: popl % ebp0x8bfa: Ret
Webviewresearch '-[testoc setvar:] At testclass. MM: 25: 0x8bfb: pushl % ebp0x8bfc: movl % ESP, % ebp0x8bfe: pushl % esi0x8bff: calll 0x8c04;-[testoc setvar:] + 9 at testclass. MM: 290x8c04: popl % eax0x8c05: movl 29372 (% eax), % edx0x8c0b: movl 16 (% EBP), % ecx0x8c0e: movl 8 (% EBP), % esi0x8c11: movl % ECx, (% ESI, % EDX) 0x8c14: movl % ECx, 30372 (% eax) 0x8c1a: movl 29136 (% eax), % eax0x8c20: movl % ECx, (% eax) 0x8c22: popl % esi0x8c23: popl % ebp0x8c24: Ret

The above is the result of release. We can see that the compiler has been optimized, and the local variable l_var, Which is useless, is omitted directly, neither allocating space for it nor assigning a value to it.

 

You cannot directly view member variables and global variables. You need to calculate the offsets to understand the indirect addressing operations with parentheses. These troublesome things can be viewed using IPA Pro (there will be a series of issues later ).

The following is an disassembly statement that calls the setvar function of two classes.

0x441e: Leal-16 (% EBP), % eax0x4421: movl % eax, (% ESP) 0x4424: movl $100, 4 (% ESP) 0x442c: calll 0x8133; testc: setvar (INT) at testclass. MM: 15
 
0x445b: movl 45819 (% Esi), % ecx0x4461: movl % ECx, 4 (% ESP) 0x4465: movl % eax, (% ESP) 0x4468: movl $100, 8 (% ESP) 0x4470: calll 0x97c0; symbol stub for: objc_msgsend

 

You can see that C ++ can directly see the next step. OC needs to know which class object and selector (which can be viewed by register read ).

Comparing the functions of the class and the number of lines at the called place can also indirectly show that the efficiency of objective-C is a little slower than that of C ++, but it is not much worse.

Apple also has some documents to help with disassembly and debugging:

Http://developer.apple.com/library/ios/#technotes/tn2239/_index.html

Reprinted please indicate the source:Http://blog.csdn.net/hursing

 


Xcode disassembly debugging IOS Simulator Program
(1) view the Disassembly
(2) read the Disassembly
(3) view objective-c Functions and Parameters
(4) nsicationicationcenter for automatic breakpoint applications
(5) debug the objc_msgsend Function
(6) Processing of function entrances and exits and local variables
(7) differences between debug and release
 

(8) disassemble your own code to grasp the rules

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.