Xcode 6, print the value of a define in the LLDB console? - xcode

It is possible to check the value of a DEFINE using a command in the lldb window?

I presume you mean C preprocessor defines? While there is a format for recording preprocessor defines & macros in DWARF, which is the default debug information format for gcc & clang and the only one lldb supports, for most substantial programs the resultant increase in debug info size is so great that it really isn't practical to use it. gcc does emit it (though doing so is not the default) and last time I played around with it gdb had spotty but functional support for macro information . But clang has never supported writing DWARF macro information, and lldb doesn't support it either.

Related

GDB or Radare2?

Should I use GDB or Radare2 for reversing an executable(I am a beginner)?
I try to programming in C and I got a SegFault. I want to Reverse Engineer it to get experience in Assembly and see where I get the SegFault.
For debugging an executable you built from source yourself, GDB is intended as a debugger. You can use layout reg to get a disassembly + registers view which can help understanding segfaults, if looking at C variables didn't help.
Debug info from compiling with gcc -g means you don't need to reverse-engineer anything, just use a normal debugger. But to get experience in asm, using a debugger both ways (source view and asm view) can help you understand how the compiler used certain asm instructions to implement each C statement. So you definitely want a debugger that can take advantage of debug info. There are some GUI GDB front-ends, like https://www.gdbgui.com that can be easier to use than command-line GDB.
But see also How to remove "noise" from GCC/clang assembly output? for more about seeing how C compiles to asm.
I haven't used radare2. I assume it has features that are good for intentionally-obfuscated executables without source, which is the opposite of what you have from compiling your own C programs with a normal compiler.
I would recommend Radare2 because it's clearer than GDB and easier for beginners ;)

Proper way of compiling OpenCL applications and using available compiler options

I am a newbie in OpenCL stuffs.
Whats is the best way to compiler an OpenCL project ?
Using a supported compiler (GCC or Clang):
When we use a compiler
like gcc or clang, how do we control these options? Are they
have to be set inside the source code, or, likewise the normal
compilation flow we can pass them on the command line. Looking at the Khornos-Manual-1.2, there are a few options provided for cl_int clBuildProgram for optimizations. :
gcc|clang -O3 -I<INCLUDES> OpenCL_app.c -framework OpenCL OPTION -lm
Actually, I Tried this and received an error :
gcc: error: unrecognized command line option '<OPTION>'
Alternatively, using openclc:
I have seen people using openclc to compiler using
a Makefile.
I would like to know which is the best way (if
there are actually two separate ways), and how do we control the
usage of different compile time options.
You might be aware but it is important to reiterate. OpenCL standard contains two things:
OpenCL C language and programming model (I think recent standard include some C++)
OpenCL host library to manage device
gcc and clang are compilers for the host side of your OpenCL project. So there will be no way to provide compiler options for OpenCL device code compilations using a host compiler since they are not even aware of any OpenCL.
Except with clang there is a flag that accept OpenCL device code, .cl file which contains the kernels. That way you can use clang and provide also the flags and options if I remember correctly, but now you would have either llvm IR or SPIR output not an device executable object. You can then load SPIR object to a device using device's run-time environment(opencl drivers).
You can checkout these links:
Using Clang to compile kernels
Llvm IR generation
SPIR
Other alternative is to use the tools provided by your target platform. Each vendor that claims to support opencl, should have a run-time environment. Usually, they have separate CLI tools to compile OpenCL device code. In you case(I guess) you have drivers from Apple, therefore you have openclc.
Intel CLI as an example
Now to your main question (best way to compile opencl). It depends what you want to do. You didn't specify what kind of requirements you have so I had to speculate.
If you want to have off-line compilation without a host program, the considerations above will help you. Otherwise, you have to use OpenCL library and have on-line compilation for you kernels, this is generally preferred for products that needs portability. Since if you compile all your kernels at the start of your program, you directly use the provided environment and you don't need to provide libraries for each target platform.
Therefore, if you have an OpenCL project, you have to decide how to compile. If you really want to use the generic flags and do not rely on third party tools. I suggest you to have a class that builds your kernels and provides the flags you want.
...how do we control these options? Are they have to be set inside the source code, or, likewise the normal compilation flow we can pass them on the command line.
Options can be set inside the source code. For example:
const char options[] = "-cl-finite-math-only -cl-no-signed-zeros";
/* Build program */
err = clBuildProgram(program, 1, &device, options, NULL, NULL);
I have never seen opencl options being specified at the command line and I'm unaware whether this is possible or not.

Unable to view symbols when debugging GNU Fortran in LLDB

This is a follow-up question to a question posted previously on stack overflow.
When I compile with the -g (or -gdwarf-2) flag in GNU Fortran (GNU Fortran (MacPorts gcc5 5.3.0_0) 5.3.0) I am unable to examine symbols. If I use the "frame variable" command I do not get the list of local variables. If I use the "print" command with a symbol, nothing is printed.
I can get breakpoints to work by using the appropriate name (I look at the output from nm to get the mangled name). The source code is displayed in the debugger, so lldb is understanding at least some of the debugging information.
lldb has no support for Fortran at present. In particular, since lldb relies on the clang/swift type system representations, which also don't support Fortran, the expression parser won't work at all. There is Go support that gets frame variable working without having to make a full Go clang frontend. That same path could be followed to get some Fortran support. But there's nobody working on this that I am aware of.

GDB Vs LLDB debuggers

What is the difference between GDB & LLDB debuggers? I recently upgraded my Xcode version from 4.2 to 4.3 & started getting warning to upgrade my debugger from GDB to LLDB.
LLDB is part of the LLVM suite of tools that Apple is moving to, including Clang. There are tons of improved features, including improved performance. There's a quick intro for GDB users here: http://lldb.llvm.org/tutorial.html
However...
You might want to take a trip over to the forums at developer.apple.com. There's a fair bit of rumbling about issues with LLDB at the moment (in XCode 4.3.1). One nasty issue, which people from Apple have confirmed, is that viewing ivars while stepping may show you the wrong values.
1. By Difference of Definition
First, What is GDB?
"GDB is the standard debugger for the GNU software system"
The GNU Debugger, usually called just GDB and named gdb as an executable file, is the standard debugger for the GNU operating system. However, its use is not strictly limited to the GNU operating system; it is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, C, C++, Objective-C, Free Pascal, Fortran, Java1 and partially others.2
(quotation from wikipedia)
Second, What is LLDB?
"LLDB is a debugger built as a set of reusable components which highly leverage existing libraries in the larger LLVM Project, such as the Clang expression parser and LLVM disassembler. LLDB is Apple’s “from the ground up” replacement for GDB, developed in close coordination with the LLVM compilers to bring you state-of-the-art debugging with extensive capabilities in flow control and data inspection. Starting with Xcode 5, all new and preexisting development projects are automatically reconfigured to use LLDB. The standard LLDB installation provides you with an extensive set of commands designed to be compatible with familiar GDB commands. In addition to using the standard configuration, you can easily customize LLDB to suit your needs.
(quotation from Apple official site)
/* I tried to find actual / practical datas to prove the statement : LLDB is already much faster than GDB when debugging large programs.)
However, I could not find it. if there is someone having this related data, please edit this article. */
Both GDB and LLDB are of course excellent debuggers without doubt.
GDB is debugger part of the GNU project created to work along the GNU compiler.
LLDB is debugger part of the LLVM project created to work along LLVM compiler.
2. By Difference of Debugger Command usage
The majority of the commands are the same.
However, lldb and gdb, two different debuggers are developed by different set of developers and thus have a little bit different commands.
This is good link below for comparing difference of these two debugger commands.
let me link the detail below :
https://developer.apple.com/library/content/documentation/IDEs/Conceptual/gdb_to_lldb_transition_guide/document/lldb-command-examples.html
http://lldb.llvm.org/lldb-gdb.html
This clearly indicates and organizes the difference of commands.
In case the site is not accessible anymore, I summary the major difference of commands below from the linked site.
3. By Personal Opinion
LLDB has been fully integrated from Xcode IDE 5 by default as primary debugger.
From that time, there has been some complaint report about LLDB stability, performance issues and bugs.
However, LLVM with LLDB project has been being incredibly improved and upgraded so far as the one of the core open-source project in the world.
So, I think, at least, LLDB should be used for Apple development environment since LLDB stole the position of GDB and is already primary default debugger at least in Apple world.(In late September, 2016, Xcode 8 integrating LLVM and LLDB was officially released, meaning quite a lot of time has been passed since LLVM/LLDB is default tool-chain for Xcode.)
But, We all already know GDB has been historically and strongly verified and battle-proven debugger over 30 years, which is awesome.
So, I would say…
I would like to use lldb while using clang, use gdb while using gcc compiler as the good combination or pair because lldb is based on llvm, whereas gdb is a GNU debugger.
I wish it could be helpful for you.
Thanks.

How do I generate symbol information to use with Linux version of Intel's VTune Amplifier?

I am using Intel VTune Amplifier XE 2011 to analyze the performance of my program. I want to be able to view the source code in the analysis results, and the documentation says I need to provide the symbol information. Unfortunately, it does not state how to generate that symbol information when compiling my program. In the Windows version of VTune all I had to do was provide the ".pdb" file that Microsoft Visual Studio would generate. Is there a similar kind of file I can create using g++ to provide this symbol information?
Have you tried compiling with -g ? Normally that is all you need to generate symbolic data for debuggers, profilers, etc.
Incidentally, for profiling on Linux, Zoom from RotateRight.com is a lot more user-friendly than VTune. (UPDATE: Zoom is unfortunately no longer supported. Use perf for simple profiling.)
The most "classic" way to get an executable to contain the debug information with GCC is to specify the "-g" command line option as mentioned by the other posters. This does not incur any performance hit since debug information resides in ELF sections which are not part of the code or data segment. That is, the .debug* sections are not mapped into the memory during normal program execution, it's only the debug time when the debugger gets them in.
Another useful consideration for any developer working on production software is to use separate debug information files. That assumes compiling the program with "-g" as described above and then using objcopy utility to copy out the ELF sections containing debug information into a separate file and adding a link from the original binary file to the separate debug information file. This is extremely useful for being able to store the debug information for the bits you released to a customer so that post-mortem debugging is possible. And of course, for performance profiling on the release bits, too.
gcc -g <your stuff> should be all that's necessary. However I used an older version.
The command line options for the newer stuff is here
EDIT:
This SO answer is probably more valuable than anything here.

Resources