David Crawshaw | 0cbb12f | 2016-08-26 08:50:50 -0400 | [diff] [blame] | 1 | // Copyright 2016 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package plugin implements loading and symbol resolution of Go plugins. |
| 6 | // |
David Crawshaw | 0cbb12f | 2016-08-26 08:50:50 -0400 | [diff] [blame] | 7 | // A plugin is a Go main package with exported functions and variables that |
| 8 | // has been built with: |
| 9 | // |
| 10 | // go build -buildmode=plugin |
| 11 | // |
| 12 | // When a plugin is first opened, the init functions of all packages not |
| 13 | // already part of the program are called. The main function is not run. |
| 14 | // A plugin is only initialized once, and cannot be closed. |
Ian Lance Taylor | ac29f30 | 2017-07-26 09:46:58 -0700 | [diff] [blame] | 15 | // |
David Crawshaw | 3f5c1ad | 2017-09-23 11:55:02 -0400 | [diff] [blame] | 16 | // Currently plugins are only supported on Linux and macOS. |
| 17 | // Please report any issues. |
David Crawshaw | 0cbb12f | 2016-08-26 08:50:50 -0400 | [diff] [blame] | 18 | package plugin |
| 19 | |
| 20 | // Plugin is a loaded Go plugin. |
| 21 | type Plugin struct { |
David Crawshaw | 9da7058 | 2016-10-30 15:31:21 -0400 | [diff] [blame] | 22 | pluginpath string |
David Crawshaw | d8ae215 | 2017-09-02 12:05:35 -0400 | [diff] [blame] | 23 | err string // set if plugin failed to load |
David Crawshaw | 9da7058 | 2016-10-30 15:31:21 -0400 | [diff] [blame] | 24 | loaded chan struct{} // closed when loaded |
| 25 | syms map[string]interface{} |
David Crawshaw | 0cbb12f | 2016-08-26 08:50:50 -0400 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // Open opens a Go plugin. |
David Crawshaw | 13c6572 | 2016-10-19 11:06:36 -0400 | [diff] [blame] | 29 | // If a path has already been opened, then the existing *Plugin is returned. |
| 30 | // It is safe for concurrent use by multiple goroutines. |
David Crawshaw | 0cbb12f | 2016-08-26 08:50:50 -0400 | [diff] [blame] | 31 | func Open(path string) (*Plugin, error) { |
| 32 | return open(path) |
| 33 | } |
| 34 | |
| 35 | // Lookup searches for a symbol named symName in plugin p. |
| 36 | // A symbol is any exported variable or function. |
| 37 | // It reports an error if the symbol is not found. |
David Crawshaw | 13c6572 | 2016-10-19 11:06:36 -0400 | [diff] [blame] | 38 | // It is safe for concurrent use by multiple goroutines. |
David Crawshaw | 0cbb12f | 2016-08-26 08:50:50 -0400 | [diff] [blame] | 39 | func (p *Plugin) Lookup(symName string) (Symbol, error) { |
| 40 | return lookup(p, symName) |
| 41 | } |
| 42 | |
| 43 | // A Symbol is a pointer to a variable or function. |
| 44 | // |
| 45 | // For example, a plugin defined as |
| 46 | // |
| 47 | // package main |
| 48 | // |
David Crawshaw | 0cbb12f | 2016-08-26 08:50:50 -0400 | [diff] [blame] | 49 | // import "fmt" |
| 50 | // |
| 51 | // var V int |
| 52 | // |
Leon Klingele | c350c5c | 2016-11-05 00:57:45 +0100 | [diff] [blame] | 53 | // func F() { fmt.Printf("Hello, number %d\n", V) } |
David Crawshaw | 0cbb12f | 2016-08-26 08:50:50 -0400 | [diff] [blame] | 54 | // |
| 55 | // may be loaded with the Open function and then the exported package |
| 56 | // symbols V and F can be accessed |
| 57 | // |
| 58 | // p, err := plugin.Open("plugin_name.so") |
| 59 | // if err != nil { |
| 60 | // panic(err) |
| 61 | // } |
| 62 | // v, err := p.Lookup("V") |
| 63 | // if err != nil { |
| 64 | // panic(err) |
| 65 | // } |
| 66 | // f, err := p.Lookup("F") |
| 67 | // if err != nil { |
| 68 | // panic(err) |
| 69 | // } |
| 70 | // *v.(*int) = 7 |
| 71 | // f.(func())() // prints "Hello, number 7" |
| 72 | type Symbol interface{} |