Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
sgk@google.com | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 5 | #ifndef BASE_LINUX_UTIL_H_ |
| 6 | #define BASE_LINUX_UTIL_H_ |
sgk@google.com | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 7 | |
| 8 | #include<stdint.h> |
craig.schlenter@chromium.org | b28af752 | 2009-10-29 18:21:30 | [diff] [blame] | 9 | #include<sys/types.h> |
sgk@google.com | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 10 | |
thestig@chromium.org | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 11 | #include<string> |
Alexandr Ilin | d4f4b34 | 2019-01-08 15:34:09 | [diff] [blame] | 12 | #include<vector> |
thestig@chromium.org | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 13 | |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 14 | #include"base/base_export.h" |
evan@chromium.org | 68a008e8 | 2011-05-02 17:54:14 | [diff] [blame] | 15 | |
sgk@google.com | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 16 | namespacebase{ |
| 17 | |
mnissler@chromium.org | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 18 | // This is declared here so the crash reporter can access the memory directly |
| 19 | // in compromised context without going through the standard library. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 20 | BASE_EXPORTexternchar g_linux_distro[]; |
mnissler@chromium.org | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 21 | |
brettw@google.com | a8e2058 | 2010-12-31 17:18:50 | [diff] [blame] | 22 | // Get the Linux Distro if we can, or return "Unknown". |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 23 | BASE_EXPORT std::stringGetLinuxDistro(); |
thestig@chromium.org | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 24 | |
Tomas Popela | 5b9b01f | 2019-09-10 19:42:31 | [diff] [blame] | 25 | #if defined(UNIT_TEST) |
| 26 | // Get the value of given key from the given input (content of the |
| 27 | // /etc/os-release file. Exposed for testing. |
| 28 | BASE_EXPORT std::stringGetKeyValueFromOSReleaseFileForTesting( |
| 29 | const std::string& input, |
| 30 | constchar* key); |
| 31 | #endif// defined(UNIT_TEST) |
| 32 | |
mnissler@chromium.org | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 33 | // Set the Linux Distro string. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 34 | BASE_EXPORTvoidSetLinuxDistro(const std::string& distro); |
mnissler@chromium.org | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 35 | |
Alexandr Ilin | d4f4b34 | 2019-01-08 15:34:09 | [diff] [blame] | 36 | // For a given process |pid|, get a list of all its threads. On success, returns |
| 37 | // true and appends the list of threads to |tids|. Otherwise, returns false. |
| 38 | BASE_EXPORTboolGetThreadsForProcess(pid_t pid, std::vector<pid_t>* tids); |
| 39 | |
Igor Kraskevich | bfc62af | 2023-10-17 15:09:28 | [diff] [blame] | 40 | // Get a list of all threads for the current process. On success, returns true |
| 41 | // and appends the list of threads to |tids|. Otherwise, returns false. |
| 42 | // Unlike the function above, this function reads /proc/self/tasks, not |
| 43 | // /proc/<pid>/tasks. On Android, the former should always be accessible to |
| 44 | // GPU and Browser processes, while the latter may or may not be accessible |
| 45 | // depending on the system and the app configuration. |
| 46 | BASE_EXPORTboolGetThreadsForCurrentProcess(std::vector<pid_t>* tids); |
| 47 | |
thestig@chromium.org | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 48 | // For a given process |pid|, look through all its threads and find the first |
| 49 | // thread with /proc/[pid]/task/[thread_id]/syscall whose first N bytes matches |
| 50 | // |expected_data|, where N is the length of |expected_data|. |
kmixter@chromium.org | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 51 | // Returns the thread id or -1 on error. If |syscall_supported| is |
| 52 | // set to false the kernel does not support syscall in procfs. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 53 | BASE_EXPORTpid_tFindThreadIDWithSyscall(pid_t pid, |
| 54 | const std::string& expected_data, |
| 55 | bool* syscall_supported); |
thestig@chromium.org | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 56 | |
reveman | 7b97c32 | 2016-09-20 02:10:58 | [diff] [blame] | 57 | // For a given process |pid|, look through all its threads and find the first |
| 58 | // thread with /proc/[pid]/task/[thread_id]/status where NSpid matches |ns_tid|. |
| 59 | // Returns the thread id or -1 on error. If |ns_pid_supported| is |
| 60 | // set to false the kernel does not support NSpid in procfs. |
| 61 | BASE_EXPORTpid_tFindThreadID(pid_t pid,pid_t ns_tid,bool* ns_pid_supported); |
| 62 | |
sgk@google.com | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 63 | }// namespace base |
| 64 | |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 65 | #endif// BASE_LINUX_UTIL_H_ |