Fix holes in operf system-wide profiling of forked processes

Using operf to do system-wide profiling of the specjbb benchmark
exposed some holes in how operf was processing the perf_events
data coming from the kernel. Some of the events we can get from
the kernel are:
 - PERF_RECORD_COMM
 - PERF_RECORD_FORK
 - PERF_RECORD_MMAP
 - PERF_RECORD_SAMPLE

The "COMM" event is to notify us of the start of an executable
application. The "FORK" event tells us when a process forks
another process. The "MMAP" event informs us when a shared library
(or executable anonymous memory, or the executable file itself, etc.)
has been mmap'ed into a process's address space. A "SAMPLE"
event occurs each time the kernel takes a sample for a process.

There is no guarantee in what order these events may arrive from
the kernel, and when a large system (say, 64 CPUs) is running
the specjbb benchmark full bore, with all processors pegged to
100%, you can get some very strange out-of-order looking
sequence of events. Things get even stranger when using Java7
versus Java6 since Java7 spawns many more threads.

The operf code had several issues where such out-of-order
events were not handled properly, so some major changes were
required in the code.

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Use PMC5/PMC6 on ppc64 arch for run cycles/run instructions

The IBM Power processor architecture (ppc64) counts instructions
and cycles on PMC5 and PMC6 (respectively) when the run latch is
set (i.e., when not in idle state).  On POWER6, these counters
were not capable of generating interrupts, so they could not be
used for profiling purposes; therefore, oprofile counted those
events (PM_RUN_INST_CMPL and PM_RUN_CYC) using other counters.
But with the newer POWER7 processor, PMC5 and PMC6 can generate
interrupts, so it makes sense to leverage those two counters
instead of using the other 4 (programmable) counters.  Doing
so could, theoreticaly allow us to count up to 6 events
simultaenously without the kernel having to do multiplexing.

This patch will force PM_RUN_INST_CMPL and PM_RUN_CYC to be
counted on PMC5 and PMC6 (respectively) when running on an
IBM POWER7 system.

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Fix compile error that occurs with some versions of gcc

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Fix Coverity issues identified against oprofile 0.9.8 release

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Add support for architected events for IBM ppc64 architecture

The Power ISA 2.07 was published at http://power.org/. This ISA
formally defines base performance monitoring facilities which
must be provided by any processor implementation of the ISA.
Specific implementations may provide additional features, but
must include the standard architected features.

This patch creates a generic ppc64 cpu type called
"ppc64/architected_events_v1" that has a list of events which
are defined in the ISA 2.07 performance monitoring unit
architecture section.  This new generic type will only be
supported by operf.  It will *not* be supported by the legacy
oprofile kernel driver and opcontrol-based profiler.  This
new cpu type can be used in situations where oprofile is running
on a kernel that does not have full native support for an
ISA 2.07-based ppc64 processor, but does have the base level
architected support. OProfile userspace code detects such a
situation by inspecting the auxiliary vector of the operf program
and comparing AT_PLATFORM and AT_BASE_PLATFORM values (defined
in elf.h).

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Fix copyright date for ppc64/architected_events_v1/unit_masks

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Add support for architected events for IBM ppc64 architecture

The Power ISA 2.07 was published at http://power.org/. This ISA
formally defines base performance monitoring facilities which
must be provided by any processor implementation of the ISA.
Specific implementations may provide additional features, but
must include the standard architected features.

This patch creates a generic ppc64 cpu type called
"ppc64/architected_events_v1" that has a list of events which
are defined in the ISA 2.07 performance monitoring unit
architecture section.  This new generic type will only be
supported by operf.  It will *not* be supported by the legacy
oprofile kernel driver and opcontrol-based profiler.  This
new cpu type can be used in situations where oprofile is running
on a kernel that does not have full native support for an
ISA 2.07-based ppc64 processor, but does have the base level
architected support. OProfile userspace code detects such a
situation by inspecting the auxiliary vector of the operf program
and comparing AT_PLATFORM and AT_BASE_PLATFORM values (defined
in elf.h).

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Revert "Add support for architected events for IBM ppc64 architecture"

This reverts commit d2ff34dbeb198106a7de6189b467b097736b089a.

Commit ID d2ff34d had an incorrect commit message

Reverting commit d2ff34d.  Will re-apply with correct commit
message.

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Fix IBM architected events to work on IBM POWER7+

In situations where oprofile is running on a kernel that does not
have full native support for an ISA 2.07-based ppc64 processor,
but does have the base level architected support, oprofile userspace
code uses the auxiliary vector of the operf program and compares
AT_PLATFORM and AT_BASE_PLATFORM values to see whether or not it
should use the new ppc64 architected events.  However, if
AT_PLATFORM="power7" and AT_BASE_PLATFORM="power7+", the oprofile
code was erroneously identifying this as a situation where the
architected events should be used.  This patch fixes that problem.

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Change /etc/mtab to /proc/mounts to enable running on busybox system

Note: This patch was originally submitted by Patrik in November, 2008.
However, after some review comments were provided back to Patrik, there
was no response, so the patch was never committed.  A new request for
this fix arrived on the list on May 21, 2013 from Mark Pearson, so
we revive this patch and commit it now.

Signed-off-by: Patrik Hagglund <patrik.h.hagglund@ericsson.com>
Add support for Intel Netburst (e.g., Pentium P4) to operf

The "legacy" oprofile kernel driver has special "p4" handling. There's
a map of event codes to ESCR/CCCR values.  Unfortunately, the P4 event
codes (stored in events/i386/p4/events) that are used by the oprofile
kernel driver don't match what perf_events kernel code expects.  This
patch adds some p4-specific event code handling to operf in order to
generate the correct encoding to pass to perf_events kernel subsystem.

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Fix for oprof_start when daemonrc file does not exist

This patch fixes a bug if daemonrc file doesn't exist or doesn't contain a
'BUF_SIZE=' line : initalizes 'buffer_size' to the default value to prevent
unpredictable value.

Signed-off-by: Gilles Allard <gilles.allard21@orange.fr>
Fix recent regression involving unit mask values of '0'

A patch that I wrote and committed on Jan 23, 2013 broke
unit mask handling for mask values of '0'.  As far as I
can tell, i386/core_2 is the only cpu type that has unit
masks with values of '0'.  So, for example, the following
command will fail as shown below:

   operf -e L2_M_LINES_OUT:100000:0 ls
   Invalid unit mask 0x0 for event L2_M_LINES_OUT
   Error retrieving info for event L2_M_LINES_OUT:100000:0

This patch fixes that problem.

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Fix Coverity errors found on May 20, 2013 git snapshot

Coverity identified the following errors on scans run from May 7 through
May 20, 2013:

Type,Category,File,Function
Wrapper object use after free,Memory - illegal accesses,/agents/jvmpi/jvmpi_oprofile.cpp,compiled_method_load(JVMPI_Event *)
Unchecked return value,Error handling issues,/daemon/opd_mangling.c,opd_open_sample_file
Dereference after null check,Null pointer dereferences,/daemon/opd_sfile.c,sfile_hash
Uninitialized scalar field,Uninitialized members,/gui/oprof_start_config.cpp,config_setting::config_setting()
Division or modulo by zero,Integer handling issues,/libdb/db_stat.c,odb_hash_stat
Resource leak,Resource leaks,/libop/op_cpu_type.c,_auxv_fetch
Resource leak,Resource leaks,/libop/op_cpu_type.c,fetch_at_hw_platform
Negative array index read,Memory - illegal accesses,/libop/op_events.c,_is_um_valid_bitmask
Write to pointer after free,Memory - corruptions,/libop/op_events.c,read_events
Read from pointer after free,Memory - illegal accesses,/libop/op_events.c,_is_um_valid_bitmask
Dereference after null check,Null pointer dereferences,/libop/op_mangle.c,op_mangle_filename
Dereference after null check,Null pointer dereferences,/libop/op_mangle.c,op_mangle_filename
Time of check time of use,Security best practices violations,/libopagent/opagent.c,op_open_agent
Improper use of negative value,Integer handling issues,/libperf_events/operf_counter.cpp,operf_record::setup()
Double free,Memory - corruptions,/libperf_events/operf_counter.cpp,operf_record::setup()
Uninitialized pointer read,Memory - illegal accesses,/libperf_events/operf_counter.cpp,<unnamed>::_get_perf_event_from_file(mmap_info &)
Unchecked return value,Error handling issues,/libperf_events/operf_mangling.cpp,"operf_open_sample_file(odb_t *, operf_sfile *, operf_sfile *, int, int)"
Using invalid iterator,API usage errors,/libperf_events/operf_process_info.cpp,operf_process_info::try_disassociate_from_parent(char *)
Non-array delete for scalars,Memory - illegal accesses,/libregex/op_regex.cpp,"<unnamed>::op_regerror(int, const re_pattern_buffer &)"
Resource leak,Resource leaks,/libutil++/op_bfd.cpp,"op_bfd::op_bfd(const std::basic_string<char, std::char_traits<char>, std::allocator<char>>&, const string_filter &, const extra_images &, bool &)"
Explicit null dereferenced,Null pointer dereferences,/opjitconv/create_bfd.c,fill_symtab
Resource leak,Resource leaks,/opjitconv/opjitconv.c,_cleanup_jitdumps
Use of untrusted string value,Insecure data handling,/opjitconv/opjitconv.c,main
Resource leak,Resource leaks,/pe_profiling/operf.cpp,_get_cpu_for_perf_events_cap()
Dereference null return value,Null pointer dereferences,/pe_profiling/operf.cpp,_process_session_dir()
Incorrect deallocator used,API usage errors,/pe_profiling/operf.cpp,_process_events_list()

===============================

This patch fixes those errors.

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Fix breakage in _try_ppc64_arch_generic_cpu caused by Coverity fixes

In the Coverity fixes applied in the previous commit, a resource leak
was plugged incorrectly, which led to operf failing on ppc64 platform
when a non-default event was specified.  The failing message was:

   NULL returned for one or both of AT_PLATFORM/AT_BASE_PLATFORM

followed by an ugly "double free or corruption" message.
This patch fixes that error.

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Fix spelling error and remove obsolete BUGS notation

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Add support for IBM POWER8 processor

The Power ISA 2.07 was recently published at http://power.org/documentation.
The IBM POWER8 processor currently under development is an implementation
of the ISA 2.07.  This patch provides the initial support for POWER8
to oprofile.  NOTE: Only operf support is included with this patch. The
legacy opcontrol-based profiler and the oprofile kernel module have
not been (and may never be) updated to support this new processor.
Potentially, some future kernel versions may allow timer mode opcontrol
to work for POWER8, but at the time of this commit, the 3.10 kernel did not.

Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
Avoid changing the number formatting for cout and cerr streams

Coverity static tool found a number of places where number formatting
was changed and might lead to some oddly formatted output for later
stream output.  This patch ensures that the number formatting only
applies to that particular message.

Signed-off-by: William Cohen <wcohen@redhat.com>
Merge remote-tracking branch 'oprofile/master'

Conflicts:
	libperf_events/operf_process_info.cpp
Prevent compile error for new official patch

New official patch (from oprofile prj) is applied by previous merge