Using massif on process which is "killed 9" - memory

I'm trying to do a memory profiling for a program which consumes too much memory and gets killed by OS (FreeBSD) with 9 signal. That happens on some specific data, so profiling it on another (e.g. smaller) data set would not give much help. When program is killed 9 massif doesn't generate any output at all. What could be done in this situation to get memory profiled?

If you have a recent Valgrind version (>= 3.7.0),
Valgrind has an embedded gdbserver so it can be used together with gdb.
Before your application starts to run under Valgrind, you can put breakpoints.
When a breakpoint is encountered, GDB monitor commands are available
to invoke Valgrind tool specific functionalities.
For example, with Massif, you can trigger the production of a report.
With Memcheck, you can do a leak search, examine validity bits, ...
It is also possible to trigger these monitor commands from the shell command
line (using the Valgrind vgdb utility)

Related

What is causing repeated glibc error with plink/batch job software-?

I am running plink software through a PBS batch job. This error occurs when I run the job:
*** glibc detected *** /software/plink: double free or corruption (out): 0x000000018dfafca0 ***
======= Backtrace: =========
[0x7d7691]
[0x7d8bea]
[0x45f5ed]
[0x47bb11]
[0x40669a]
[0x7bdb2c]
[0x400209]
However it only occur with one of my files (bw 30-60 gb files) and each rerun shows the exact same back trace map. I tried running it not through the batch scheduler and received the same error again, with the same backtrace map. I am just using the software (plink), and didn't write it, so most of the answers online are about writing and freeing memory in your program.
Any ideas on
what is causing this error, and
how I can fix it?
what is causing this error, and
A double-free or heap corruption in the plink
how I can fix it?
You can't. You can do one of two things, depending on how much you know and understand.
First, build the newest version of plink from source, and see if the problem persists.
If it does not, you are done (or at least you might hope that someone else found and fixed this problem).
If it does, you'll have to debug the problem sufficiently for either you, or plink developers to fix it. Some tools that should help: Valgrind and Address Sanitizer (note: in addition to Clang, Address Sanitizer is also included in GCC-4.8).
Once you have a good report (where the memory was allocated, and where it got corrupted), you should either fix it and submit your fix to plink developers, or give them a bug report with the allocation and corruption location and stack traces.

How can I manually generate a core dump (or equivalent) in lldb attached to iOS

Sometimes I'm trying to track down a really rare bug in an iOS app. I'll hit it in the debugger after hours of trying to repro only to have xcode or lldb crash on me while I'm debugging (usually if I'm stepping through C++ code). This is beyond infuriating.
With gdb you can use generate-core-dump to create a core dump of the file so that I can re-load it in gdb and at least look at all of the memory. What I want is the ability to do something similar in lldb so that when xcode crashes (as it always tends to do at the worst times) I can recover my debugging session without having to reproduce the crash.
The app is running on a non-jailbroken iPhone, so I don't have much access to the OS to do something like dump the memory from there.
One possible answer is to just use gdb instead of lldb, but I think that causes some other problems that I'm not remembering at the moment, plus it doesn't have some of the features that are useful in lldb.
UPDATE: Xcode 6, released fall of 2014, includes a new process save-core command in lldb -- lldb can now generate a coredump of a user process. e.g. (lldb) process save-core /tmp/corefile and wait a little bit.
The original answer for Xcode 5 and earlier lldb's, was:
This feature isn't implemented in lldb yet. This feature isn't implemented in the Apple version of gdb, either, for that matter.
While not a commonly requested feature, it is something other people have said would be useful as well. Hopefully someone will be sufficiently motivated to add that capability to lldb. I'm not sure how well it would work on an iOS device because it's going to involve gigantic amounts of data being transferred up to the Mac over a protocol that isn't very efficient for large data transfers - I expect it would be remarkably slow.
The core file can be opened with lldb -c /tmp/corefile
It's worth noting that the process explorer tool for iOS can generate core dumps of any PID (assuming you have root or it's the same UID as you), without impacting the process.

Dumping contents of lost memory reported by Valgrind

When I run valgrind --leak-check=yes on a program, a few bytes of lost memory are reported. Is it possible to view the contents of this memory (i.e. dump the data that is stored in it)?
You can do that with the last version of Valgrind (3.8.1):
Start your executable activating the gdbserver at startup:
valgrind --vgdb-error=0 ....<your program>
Then in another window, connect a gdb to Valgrind (following the indications
given by Valgrind).
Then put a breakpoint at a relevant place (e.g. at the end of main)
and use the gdb
continue
command till the breakpoint is reached.
Then do a leak search from gdb:
monitor leak_check full reachable any
Then list the address(es) of the reachable blocks of the relevant loss record nr
monitor block_list <loss_record_nr>
You can then use gdb features to examine the memory of the given address(es).
Note also the potentially interesting command "who_points_at"
if you are searching who has kept a pointer to this memory.

iOS Development: Is there a way to read .trace files from command prompt without using instruments

I am trying to do some performance testing for my application and am looking to monitor its certain metrics, specifically cpu usage amongst others. I want to make the process completely automated. I am able to create the .trace file and am looking for a solution to get meaningful information from the trace file. I want to do this on non jail broken devices so using top too is not feasible
Thanks for any pointers for finding cpu usage on non jail broken devices in an automated fashion.
-Kshitiz
You can run Instruments to import the trace file and export it as CSV for analysis by a script. It's not strictly 100% command-line, but if you automate the Instruments export it will be close.
Or, try DTrace, but I'm not sure if that supports iOS trace formats.

can I alloc() in the main thread and free() in another?

I have a program that runs fine on MacOS and Linux and cross-compiles to Windows with mingw. Recently I made the program multi-threaded.
The current design of the program has memory allocated in the main thread and freed in the slave "worker" threads. That's not a problem on MacOS and Linux because the malloc/free system is multi-threaded.
I'm concerned about the cross-compiling, however. The version of mingw that I'm using is built from MacOS ports. It's a pretty ancient version of G++ (version 3.4.5) from 2004. I've been unsuccessful in my attempts to build a more recent version (I'd like to build a 64-bit version, but gave up). I'm getting pthreads from http://sourceware.org/pthreads-win32.
My concern is that the malloc & free system in 3.4.5 is not multi-threaded.
Questions:
Should I rewrite my program so that the blocks of memory to be freed are passed back to the main thread to be freed there?
Should I try to upgrade to a more recent mingw?
Is there any way to find these concurrency problems other than massive amounts of testing? That just doesn't feel good to me.
Thanks!
Why do you say malloc & free are not multithreaded?
mingw32 by default will link with msvcrt.dll which is a multithread dll. See [1]. There was[2] a single-threaded library provided by Microsoft, but it was only available for static linking.
PS: You mention that you are cross-compiling but you seem instead to be compiling the windows program in windows. In such case, Why don't you dowload the binaries from www.mingw.org? (it's a pain to figure out in their downloads the files needed, though)
1- http://msdn.microsoft.com/en-us/library/abx4dbyh%28v=VS.71%29.aspx
2- See [1]. Removed in Visual Studio 2005 http:// msdn.microsoft.com/en-us/library/abx4dbyh%28v=VS.80%29.aspx
I would avoid this. It sounds like you're trying to dodge the main issue.
Yes, that would be a good idea in any case...
One way to detect concurrency problems related to memory allocation/deallocation is a memory leak detector. I'm not sure if valgrind works on cygwin.

Resources