Add instrument
Bug: 280498513
Test: mmm instrument, m berberis_all
Change-Id: I111a3682cc78e87a4a6407771b120869fa0a873b
diff --git a/guest_os_primitives/guest_thread_manager.cc b/guest_os_primitives/guest_thread_manager.cc
index 15c2842..0224f3a 100644
--- a/guest_os_primitives/guest_thread_manager.cc
+++ b/guest_os_primitives/guest_thread_manager.cc
@@ -22,8 +22,7 @@
#include "berberis/base/tracing.h"
#include "berberis/guest_os_primitives/guest_thread.h"
#include "berberis/guest_os_primitives/guest_thread_manager.h"
-#include "berberis/instrument/instrument.h"
-
+#include "berberis/instrument/guest_thread.h"
#include "guest_thread_manager_impl.h"
#include "guest_thread_map.h"
#include "scoped_signal_blocker.h"
@@ -126,7 +125,7 @@
CHECK_EQ(0, pthread_setspecific(g_guest_thread_key, thread));
}
if (kInstrumentGuestThread) {
- // TODO(b/280498513): Call instrumentation hook(s) here.
+ OnInsertGuestThread(tid, thread);
}
TRACE("guest thread attached %d", tid);
@@ -142,7 +141,7 @@
// Remove thread from global table.
GuestThread* thread = g_guest_thread_map_.RemoveThread(tid);
if (kInstrumentGuestThread) {
- // TODO(b/280498513): Call instrumentation hook(s) here.
+ OnRemoveGuestThread(tid, thread);
}
TRACE("guest thread detached %d", tid);
diff --git a/instrument/Android.bp b/instrument/Android.bp
index 33e297d..e206eaa 100644
--- a/instrument/Android.bp
+++ b/instrument/Android.bp
@@ -22,4 +22,23 @@
defaults: ["berberis_defaults"],
host_supported: true,
export_include_dirs: ["include"],
+ header_libs: [
+ "libberberis_guest_state_headers",
+ ],
+ export_header_lib_headers: [
+ "libberberis_guest_state_headers",
+ ],
+}
+
+cc_library_static {
+ name: "libberberis_instrument",
+ defaults: ["berberis_defaults"],
+ host_supported: true,
+ srcs: ["instrument.cc"],
+ header_libs: [
+ "libberberis_base_headers",
+ "libberberis_guest_state_headers",
+ "libberberis_instrument_headers",
+ ],
+ export_header_lib_headers: ["libberberis_instrument_headers"],
}
\ No newline at end of file
diff --git a/instrument/include/berberis/instrument/crash.h b/instrument/include/berberis/instrument/crash.h
new file mode 100644
index 0000000..cacf18b
--- /dev/null
+++ b/instrument/include/berberis/instrument/crash.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BERBERIS_INSTRUMENT_CRASH_H_
+#define BERBERIS_INSTRUMENT_CRASH_H_
+
+#include <signal.h>
+
+namespace berberis {
+
+void OnCrash(int sig, siginfo_t* info, void* context);
+
+} // namespace berberis
+
+#endif // BERBERIS_INSTRUMENT_CRASH_H_
diff --git a/instrument/include/berberis/instrument/exec.h b/instrument/include/berberis/instrument/exec.h
new file mode 100644
index 0000000..54e26b1
--- /dev/null
+++ b/instrument/include/berberis/instrument/exec.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BERBERIS_INSTRUMENT_EXEC_H_
+#define BERBERIS_INSTRUMENT_EXEC_H_
+
+#include "berberis/guest_state/guest_addr.h"
+#include "berberis/guest_state/guest_state.h"
+#include "berberis/instrument/instrument.h"
+
+namespace berberis {
+
+inline constexpr bool kInstrumentExec = false;
+
+using OnExecInsnFunc = void (*)(ThreadState*, const void*);
+
+OnExecInsnFunc GetOnExecInsn(GuestAddr pc);
+
+} // namespace berberis
+
+#endif // BERBERIS_INSTRUMENT_EXEC_H_
diff --git a/instrument/include/berberis/instrument/guest_call.h b/instrument/include/berberis/instrument/guest_call.h
new file mode 100644
index 0000000..eb673ff
--- /dev/null
+++ b/instrument/include/berberis/instrument/guest_call.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BERBERIS_INSTRUMENT_GUEST_CALL_H_
+#define BERBERIS_INSTRUMENT_GUEST_CALL_H_
+
+#include "berberis/guest_state/guest_addr.h"
+#include "berberis/guest_state/guest_state.h"
+#include "berberis/instrument/instrument.h"
+
+namespace berberis {
+
+void OnWrappedGuestCall(ThreadState* state, GuestAddr function_addr);
+void OnWrappedGuestReturn(ThreadState* state, GuestAddr function_addr);
+
+} // namespace berberis
+
+#endif // BERBERIS_INSTRUMENT_GUEST_CALL_H_
diff --git a/instrument/include/berberis/instrument/guest_thread.h b/instrument/include/berberis/instrument/guest_thread.h
new file mode 100644
index 0000000..dededf9
--- /dev/null
+++ b/instrument/include/berberis/instrument/guest_thread.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BERBERIS_INSTRUMENT_GUEST_THREAD_H_
+#define BERBERIS_INSTRUMENT_GUEST_THREAD_H_
+
+#include <sys/types.h>
+
+#include "berberis/instrument/instrument.h"
+
+namespace berberis {
+
+class GuestThread;
+inline constexpr bool kInstrumentGuestThread = false;
+
+void OnInsertGuestThread(pid_t tid, GuestThread* thread);
+void OnRemoveGuestThread(pid_t tid, GuestThread* thread);
+
+} // namespace berberis
+
+#endif // BERBERIS_INSTRUMENT_GUEST_THREAD_H_
diff --git a/instrument/include/berberis/instrument/instrument.h b/instrument/include/berberis/instrument/instrument.h
index 25901af..62fc075 100644
--- a/instrument/include/berberis/instrument/instrument.h
+++ b/instrument/include/berberis/instrument/instrument.h
@@ -19,11 +19,6 @@
namespace berberis {
-inline constexpr bool kInstrumentExec = false;
-inline constexpr bool kInstrumentGuestThread = false;
-inline constexpr bool kInstrumentLoader = true;
-inline constexpr bool kInstrumentSyscalls = false;
-inline constexpr bool kInstrumentTrampolines = false;
inline constexpr bool kInstrumentWrappers = false;
} // namespace berberis
diff --git a/instrument/include/berberis/instrument/loader.h b/instrument/include/berberis/instrument/loader.h
new file mode 100644
index 0000000..8fc9398
--- /dev/null
+++ b/instrument/include/berberis/instrument/loader.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BERBERIS_INSTRUMENT_LOADER_H_
+#define BERBERIS_INSTRUMENT_LOADER_H_
+
+#include "berberis/instrument/instrument.h"
+
+#include <link.h>
+
+namespace berberis {
+
+inline constexpr bool kInstrumentLoader = true;
+
+void OnConsistentLinkMap(const struct link_map* link);
+
+} // namespace berberis
+
+#endif // BERBERIS_INSTRUMENT_LOADER_H_
diff --git a/instrument/include/berberis/instrument/syscall.h b/instrument/include/berberis/instrument/syscall.h
new file mode 100644
index 0000000..42913dd
--- /dev/null
+++ b/instrument/include/berberis/instrument/syscall.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BERBERIS_INSTRUMENT_SYSCALL_H_
+#define BERBERIS_INSTRUMENT_SYSCALL_H_
+
+#include "berberis/guest_state/guest_state.h"
+#include "berberis/instrument/instrument.h"
+
+namespace berberis {
+
+inline constexpr bool kInstrumentSyscalls = false;
+
+void OnSyscall(ThreadState* state, long number);
+void OnSyscallReturn(ThreadState* state, long number);
+
+} // namespace berberis
+
+#endif // BERBERIS_INSTRUMENT_SYSCALL_H_
diff --git a/instrument/include/berberis/instrument/trampolines.h b/instrument/include/berberis/instrument/trampolines.h
new file mode 100644
index 0000000..8c4352a
--- /dev/null
+++ b/instrument/include/berberis/instrument/trampolines.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BERBERIS_INSTRUMENT_TRAMPOLINES_H_
+#define BERBERIS_INSTRUMENT_TRAMPOLINES_H_
+
+#include "berberis/instrument/instrument.h"
+
+#include "berberis/guest_state/guest_state.h"
+
+namespace berberis {
+
+inline constexpr bool kInstrumentTrampolines = false;
+
+using OnTrampolineFunc = void (*)(ThreadState*, const void*);
+
+OnTrampolineFunc GetOnTrampolineCall(const char* name);
+OnTrampolineFunc GetOnTrampolineReturn(const char* name);
+
+} // namespace berberis
+
+#endif // BERBERIS_INSTRUMENT_TRAMPOLINES_H_
diff --git a/instrument/instrument.cc b/instrument/instrument.cc
new file mode 100644
index 0000000..8776d4e
--- /dev/null
+++ b/instrument/instrument.cc
@@ -0,0 +1,69 @@
+// Copyright (C) 2023 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include <link.h>
+#include <signal.h>
+#include <sys/types.h>
+
+#include "berberis/base/tracing.h"
+#include "berberis/guest_state/guest_addr.h"
+#include "berberis/guest_state/guest_state.h"
+#include "berberis/instrument/crash.h"
+#include "berberis/instrument/exec.h"
+#include "berberis/instrument/guest_call.h"
+#include "berberis/instrument/guest_thread.h"
+#include "berberis/instrument/loader.h"
+#include "berberis/instrument/trampolines.h"
+
+namespace berberis {
+
+void OnConsistentLinkMap(const struct link_map* link) {
+ int i = 0;
+ for (; link; link = link->l_next) {
+ TRACE("link_map[%d]: %p %s", i++, reinterpret_cast<void*>(link->l_addr), link->l_name);
+ }
+}
+
+OnExecInsnFunc GetOnExecInsn([[maybe_unused]] GuestAddr pc) {
+ return nullptr;
+}
+
+OnTrampolineFunc GetOnTrampolineCall([[maybe_unused]] const char* name) {
+ return nullptr;
+}
+
+OnTrampolineFunc GetOnTrampolineReturn([[maybe_unused]] const char* name) {
+ return nullptr;
+}
+
+void OnWrappedGuestCall([[maybe_unused]] ThreadState* state,
+ [[maybe_unused]] GuestAddr function_addr) {}
+
+void OnWrappedGuestReturn([[maybe_unused]] ThreadState* state,
+ [[maybe_unused]] GuestAddr function_addr) {}
+
+void OnSyscall([[maybe_unused]] ThreadState* state, [[maybe_unused]] long number) {}
+
+void OnSyscallReturn([[maybe_unused]] ThreadState* state, [[maybe_unused]] long number) {}
+
+void OnCrash([[maybe_unused]] int sig,
+ [[maybe_unused]] siginfo_t* info,
+ [[maybe_unused]] void* context) {}
+
+void OnInsertGuestThread([[maybe_unused]] pid_t tid, [[maybe_unused]] GuestThread* thread) {}
+
+void OnRemoveGuestThread([[maybe_unused]] pid_t tid, [[maybe_unused]] GuestThread* thread) {}
+
+} // namespace berberis