Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-3380] Cannot attach to a local Swift process using lldb on Linux #4649

Closed
swift-ci opened this issue Dec 9, 2016 · 3 comments
Closed
Labels
bug Something isn't working Linux LLDB for Swift

Comments

@swift-ci
Copy link

swift-ci commented Dec 9, 2016

Previous ID SR-3380
Radar None
Original Reporter rolivieri (JIRA User)
Type Bug
Status Closed
Resolution Invalid

Attachment: Download

Environment

Linux flavor: Ubuntu 14.04 LTS
Swift flavor: Swift Release v3.0.1

Additional Detail from JIRA
Votes 6
Component/s LLDB for Swift
Labels Bug, Linux
Assignee morlovich (JIRA)
Priority Medium

md5: 00df5ccc367db981486f9ef5a740241e

Issue Description:

Linux flavor: Ubuntu 14.04 LTS
Swift flavor: Swift Release v3.0.1

Using lldb and trying to attach using a valid process name or process ID always results in the following error:
error: attach failed: unable to attach

Here are the steps to reproduce this problem:

1) Unzip the sample project and build it on an Ubuntu 14.04 system (swift build).
2) Start the sample app by executing: .build/debug/swift-tst (note: this program is just an infinite loop that prints a "hello world" message).
3) Obtain the process ID for the sample program.
4) Launch the lldb program and execute these commands:

(lldb) platform select host
Platform: host
Triple: x86_64-*-linux-gnu
OS Version: 4.2.0
Hostname: 127.0.0.1
WorkingDir: /home/ricardo/tmp/swift-tst
Kernel: Linux
Release: 4.2.0-42-generic
Version: #49~14.04.1-Ubuntu SMP Wed Jun 29 20:22:11 UTC 2016
(lldb) process attach --name swift-tst
error: attach failed: unable to attach
(lldb) process attach —pid <process pid>
error: attach failed: unable to attach

It is worth mentioning that attempting to attach to a remote Swift process on Linux also fails with the same error… but our guess is that remote debugging+attaching won’t work given that the attaching locally to a local process does not work. Having said that, we are documenting here the steps we attempted for remote debugging+attaching (Linux to Linux), which also resulted in a failure:

1) On remote Linux server (Ubuntu 14.04), we started lldb-server:
lldb-server platform --listen *:8000

2) On remote Linux server, started sample program: .build/debug/swift-tst

3) On local Linux server, starter lldb:

$ lldb
(lldb) platform select remote-linux
Platform: remote-linux
Connected: no
(lldb) platform connect connect://localhost:8000
Platform: remote-linux
Triple: x86_64-*-linux-gnu
OS Version: 4.2.0 (4.2.0-42-generic)
Kernel: #49~14.04.1-Ubuntu SMP Wed Jun 29 20:22:11 UTC 2016
Hostname: ricardo-ubuntu14.04
Connected: yes
WorkingDir: /home/ricardo
(lldb) process list
invalid command 'process list'.
(lldb) platform process list
75 matching processes were found on "remote-linux"
PID PARENT USER TRIPLE NAME
====== ====== ========== ======================== ============================
1261 1256 ricardo x86_64-*-linux init
...
3626 2682 ricardo x86_64-*-linux swift-tst
...
23682 1261 ricardo x86_64-*-linux upstart-file-bridge
(lldb) process attach --name swift-tst
error: attach failed: unable to attach
(lldb) process attach --pid 3626
error: attach failed: unable to attach

@swift-ci
Copy link
Author

Comment by Maksim Orlovich (JIRA)

This is not a bug, but a feature. By default Linux does not allow attaching to a process which wasn't launched by the debugger see - https://www.kernel.org/doc/Documentation/security/Yama.txt

You can either do this:

$> echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

Or run $> sudo lldb and attach.

This is not Swift specific but happens for any programming language/app.

@dabrahams
Copy link
Collaborator

morlovich (JIRA User) So https://reviews.llvm.org/D47879 is nonsense?

@swift-ci
Copy link
Author

Comment by Ryan Lovelett (JIRA)

@dabrahams I think this bug and what D47879 are referencing, while similar, are two different things.

The lldb-dev thread, LLDB Process Attach Failed When Waiting, is the genesis the D47879 patch. The thing that the D47879 patch enables is for lldb to wait for a process to start, by polling, and then attach to it. This example here attaches to an already running process. Attaching to an already running process, assuming the correct permissions, works in LLDB today without D47879. It is subtle but different.

Using the attached swift-tst.zip it might look like this:

SR-3380

$ .build/debug/swift-tst
// Another terminal
$ lldb
(lldb) process attach --name swift-tst

D47879

$ lldb
(lldb) process attach --name "swift-tst" --waitfor
// Another terminal
$ .build/debug/swift-tst

The thing that morlovich (JIRA User) is pointing out is that certain permissions re: ptrace are needed to attach to the process in Linux. In fact, during my development of D47879 I naturally discovered and noted the same thing. I still feel, like I said back then, that the error messages regarding the lack of permissions leave something to be desired.

I know you were asking in D47879 if this issue was closed erroneously. I am far from an expert but in my opinion this issue is working as expected (albeit with sub-optimal error messaging).

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 9, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Linux LLDB for Swift
Projects
None yet
Development

No branches or pull requests

2 participants