How to debug UEFI (UDK2014) Shell application Using Visual Studio - c

I have downloaded EDK (UEDK2014) from taniacore site and I have successfully build UEFI application in Debug mode using following command
C:\UDK2014.Complete.MyWorkSpace\UDK2014.MyWorkSpace\MyWorkSpace>
build -t VS2012x86 -b DEBUG
As it is in Debug mode it will create a .pdb file (SecMain.pdb). My Question is How to debug this binary using VS 2010.
I trided doing that by running SecMain.exe, attach it to Visual Studio 2010 and start debugging but not able to do that. Could any one have done this (Debugging an UEFi application using VS 2010) before ? please let me know if you have solution to this.

Yes, you can use an emulator as #sun2sirius said. The only problem though if your UEFI App or driver accesses the hardware directly the emulator won't help. In this case unfortunately "printf" is your best friend. So good Trace/Log library will help you to debug your driver.
There are a few ways to simplify the development:
Write as much platform-independent code as you can. Easier to debug in VS IDE under the Windows. Windows has protected memory pages and debugger will catch all potential memory issues; UEFI is in real mode, so your platform dependent code is not protected against accessing dunging pointers, buffer overrun etc. So again Trace/Log library is a big help.
There is a hardware debugger on the market like Arium. It can debug UEFI application running on the real hardware. But it's expensive and the hardware you debugging has to have a hardware debug port available. It's not hard to find on desktops but on the production laptops it is a rare beast! So you have to solder the connector if you need to debug a specific platform.
I case of UEFI driver development even HW Debugger is not much of a help because you have to have a debug built of the UEFI firmware (BIOS) on the platform installed in order to use a debugger.

Related

Learning Linux Kernel programming on a virtual machine on Ubuntu?

I am just learning linux kernel programming with the LINUX KERNEL DEVELOPMENT book(I am beginner linux kernel programming but not on linux programming). It is possible to test programs in a kernel machine with VMware viritual on Ubuntu without damage my system ?
Yes you can safely test kernel modules on a virtual machine!
I'll give you some links that may help:
watch this site
http://free-electrons.com/
in particular this book:
http://free-electrons.com/doc/books/ldd3.pdf
Also this guide:
http://www.tldp.org/HOWTO/Module-HOWTO/
An embedded distro is even better
An Ubuntu guest is fine, but I prefer to keep things minimal and use an embedded distro, as this will make things:
simpler and easier to understand and control
faster
In particular, I recommend using:
Buildroot, which is highly configurable, documented and maintained, also builds host QEMU so easy to patch it up (e.g. to add your own devices since out-of-tree devices are not possible yet ?)
QEMU emulator: small comprehensible source, ARM support, official Android emulator, kernel GDB support
Embedded distros can generate rootfs images smaller than 10MiB, and it becomes possible to understand the entire userland setup, which will make it easier to focus on the kernel.
I have made a setup to make everything as automated as possible: https://github.com/cirosantilli/linux-kernel-module-cheat
I've been using a VM for a long time for Linux kernel programming and I've never had any problem. Actually, if you manage to violate the protections of a VM then you will probably be hired by Oracle or VMWare :D
However, I recommend you to read this post: https://security.stackexchange.com/questions/23452/is-it-safe-to-use-virtual-machines-when-examining-malware

How debugging works in MCU systems programmed in C

As I understand, on linux systems debugging is done via the ptrace sys-call.
I would like to know how would I write a debugger for a micro-controller (no-OS).
Let us for the sake of simplicity assume the case of a software debugger for a board like Arduino Due (ARM based).
What would I have to learn to accomplish such a project, assuming I have a fair idea on how debuggers work on Linux and Windows
How different would programming a debugger for a MCU system be? (I can't use sys-calls)
Is debugging achievable via USB or serial interface?
Which languages should I use? (C, C++, Arm assembly)
How can the armgcc compiler help me with my project (are there flag options such as -g etc?)
I plan to implement the debugger as a command line Linux utility.
Most modern processors, including the ARM on the Arduino Due have on-chip debug support accesses via a JTAG interface. The Cortex-M3 on-chip debug supports 8 hardware breakpoints and some other features - with code running in flash memory you cannot set software breakpoints.
The JTAG interface itself is rather simple, but you will need special hardware to support it from a PC. Depending of performance and features a JTAG debugger may vary in cost hugely from very low to serious money. Software to interface the JTAG to the debugger software is required. OpenOCD is an open source tool for interfacing JTAG/On-chip debug to GDB allowing GDB-ARM to be hosted on a PC to debug the remote ARM target.
You could in theory write your own software to to access the OCD via JTAG. I have never considered implementating a target hosted debugger on a microcontroller, although I have used VxWorks on ARM9, SrongARM and x86 and it has limited support for target hosted debugging. The utility of target hosted debug is limited by the lack of the source code and symbol table information that is available to a hosted debugger to support source-level debugging.
Even so, I believe that it is possible to access the on-chip debug from the target itself see ARM's documentation for details.

Static Analysis tool for Linux kernel modules and device drivers

I need a static analysis tool for the Linux Device Drivers I write for ARM based boards. I am considering few tools as mentioned below:
Sparse is a computer software tool, already available on Linux, designed to find possible coding faults in the Linux kernel.
There are two active projects of Linux Verification Center aimed to improve quality of the loadable kernel modules.
Linux Driver Verification (LDV) - a comprehensive toolset for static source code verification of Linux device drivers.
KEDR Framework - an extensible framework for dynamic analysis and verification of kernel modules.
Another ongoing project is Linux File System Verification that aims to develop a dedicated toolset for verification of Linux file system implementations.
Enable -Werror, -Wextra and -Wall on GCC, and run with Valgrind.
Last time I played with Sparse I found the outputs to be confusing and did not find a good documentation to interpret the output. Does anyone has a good documentation on Sparse tool? What are the other Free static analysis tools I can use for my Linux driver verification? I know about LINT tool but its licensed.
Smatch is a static analysis tool for C that is used on the kernel. It has resulted in hundreds of patches. Quite a few have been security related or were significant enough for the stable kernel.
If you want to write your own rules, Coccinelle is probably appropriate. If you want to use rules written by others you can use the various tools integrated into the kernel. Check section 4.2: CODE CHECKING TOOLS of Documentation/development-process/4.Coding for some suggestions.

Writing a driver to fool Linux systems about having a GPU

I'm into something about writing a "Mock GPU driver" for Linux based systems. What I mean is that, simply I want to write a driver (Behind X-server obviously) to answer X's API calls with some debugging messages.
In other words I want to fool Linux about having an actual GPU. So I can make a test-bed for GUI-accelerated packages in console based systems.
Right now, if I execute a GUI-accelerated package in Linux console based systems; it'll simply dies due to lack of a real GPU (or a GPU driver better I'd say).
So I want to know:
Is it even possible? (Writing a GPU driver to fool Linux about having an actual GPU)
What resources do you recommend before getting my hands dirty in code?
Is there any similar projects around the net?
PS: I'm an experienced ANSI-C programmer but I don't have any clue in real Kernel/Driver development under *nix (read some tutorials about USB driver development though), so any resources about these areas will be really appreciated as well. Thanks in advance.
What you are looking for is actually part of Xorg server suite, and it is called Xvfb (virtual framebuffer).
If you're not afraid of a bit complex bash, you can take a look at Gentoo's virtualx.eclass for an use example (we use it to run tests which require X11).
A good place to start is the Mesa project - it implements OpenGL in software. It has a way to trick the OS into thinking that it is the OpenGL driver.

Debugger for debugging my OS

I want a debugger to debug my OS. At many places i am not being able to figure out what is wrong. So is there any debugger which can debug operating systems? can i do it with gdb?
You will need to use a solution which emulates another computer on your computer - essentially, a virtual machine. The most obvious ones which come to mind instantly are VirtualBox and VMware, but those weren't designed with debugging the machine and/or the OS.
What you're looking for is either Bochs or QEMU. Both (as far as I remember) can be integrated with gdb in order to set breakpoints and analyze the execution of your code. You might also want to read this OSdev.org article.
I primarily use Bochs for testing my OS projects. It includes a helpful debugger (bochsdbg).

Resources