Erlang -- code

Reference Manual
Release Notes
PDF
Top

Kernel
Reference Manual
Version 5.0

Expand All
Contract All

Table of Contents

- kernel (App)
- application
  - Top of manual page
  - ensure_all_started/1
  - ensure_all_started/2
  - ensure_started/1
  - ensure_started/2
  - get_all_env/0
  - get_all_env/1
  - get_all_key/0
  - get_all_key/1
  - get_application/0
  - get_application/1
  - get_env/1
  - get_env/2
  - get_env/3
  - get_key/1
  - get_key/2
  - load/1
  - load/2
  - loaded_applications/0
  - permit/2
  - set_env/3
  - set_env/4
  - start/1
  - start/2
  - start_type/0
  - stop/1
  - takeover/2
  - unload/1
  - unset_env/2
  - unset_env/3
  - which_applications/0
  - which_applications/1
  - Module:config_change/3
  - Module:prep_stop/1
  - Module:start/2
  - Module:start_phase/3
  - Module:stop/1
- auth
  - Top of manual page
  - cookie/0
  - cookie/1
  - is_auth/1
  - node_cookie/2
  - node_cookie/1
- code
  - Top of manual page
  - add_path/1
  - add_patha/1
  - add_paths/1
  - add_pathsa/1
  - add_pathsz/1
  - add_pathz/1
  - all_loaded/0
  - atomic_load/1
  - clash/0
  - compiler_dir/0
  - del_path/1
  - delete/1
  - ensure_loaded/1
  - ensure_modules_loaded/1
  - finish_loading/1
  - get_mode/0
  - get_object_code/1
  - get_path/0
  - is_loaded/1
  - is_module_native/1
  - is_sticky/1
  - lib_dir/0
  - lib_dir/1
  - lib_dir/2
  - load_abs/1
  - load_binary/3
  - load_file/1
  - objfile_extension/0
  - prepare_loading/1
  - priv_dir/1
  - purge/1
  - replace_path/2
  - root_dir/0
  - set_path/1
  - soft_purge/1
  - stick_dir/1
  - unstick_dir/1
  - where_is_file/1
  - which/1
- disk_log
  - Top of manual page
  - accessible_logs/0
  - alog/2
  - alog_terms/2
  - balog/2
  - balog_terms/2
  - bchunk/2
  - bchunk/3
  - block/1
  - block/2
  - blog/2
  - blog_terms/2
  - breopen/3
  - btruncate/2
  - change_header/2
  - change_notify/3
  - change_size/2
  - chunk/2
  - chunk/3
  - chunk_info/1
  - chunk_step/3
  - close/1
  - format_error/1
  - inc_wrap_file/1
  - info/1
  - lclose/1
  - lclose/2
  - log/2
  - log_terms/2
  - open/1
  - pid2name/1
  - reopen/2
  - reopen/3
  - sync/1
  - truncate/1
  - truncate/2
  - unblock/1
- erl_boot_server
  - Top of manual page
  - add_slave/1
  - delete_slave/1
  - start/1
  - start_link/1
  - which_slaves/0
- erl_ddll
  - Top of manual page
  - demonitor/1
  - format_error/1
  - info/0
  - info/1
  - info/2
  - load/2
  - load_driver/2
  - loaded_drivers/0
  - monitor/2
  - reload/2
  - reload_driver/2
  - try_load/3
  - try_unload/2
  - unload/1
  - unload_driver/1
- erl_prim_loader
- erlang
- error_handler
  - Top of manual page
  - raise_undef_exception/3
  - undefined_function/3
  - undefined_lambda/3
- error_logger
  - Top of manual page
  - add_report_handler/1
  - add_report_handler/2
  - delete_report_handler/1
  - error_msg/1
  - error_msg/2
  - error_report/1
  - error_report/2
  - format/2
  - info_msg/1
  - info_msg/2
  - info_report/1
  - info_report/2
  - logfile/1
  - tty/1
  - warning_map/0
  - warning_msg/1
  - warning_msg/2
  - warning_report/1
  - warning_report/2
- file
  - Top of manual page
  - advise/4
  - allocate/3
  - change_group/2
  - change_mode/2
  - change_owner/2
  - change_owner/3
  - change_time/2
  - change_time/3
  - close/1
  - consult/1
  - copy/2
  - copy/3
  - datasync/1
  - del_dir/1
  - delete/1
  - eval/1
  - eval/2
  - format_error/1
  - get_cwd/0
  - get_cwd/1
  - list_dir/1
  - list_dir_all/1
  - make_dir/1
  - make_link/2
  - make_symlink/2
  - native_name_encoding/0
  - open/2
  - path_consult/2
  - path_eval/2
  - path_open/3
  - path_script/2
  - path_script/3
  - pid2name/1
  - position/2
  - pread/2
  - pread/3
  - pwrite/2
  - pwrite/3
  - read/2
  - read_file/1
  - read_file_info/1
  - read_file_info/2
  - read_line/1
  - read_link/1
  - read_link_all/1
  - read_link_info/1
  - read_link_info/2
  - rename/2
  - script/1
  - script/2
  - sendfile/2
  - sendfile/5
  - set_cwd/1
  - sync/1
  - truncate/1
  - write/2
  - write_file/2
  - write_file/3
  - write_file_info/2
  - write_file_info/3
- gen_sctp
  - Top of manual page
  - abort/2
  - close/1
  - connect/4
  - connect/5
  - connect_init/4
  - connect_init/5
  - controlling_process/2
  - eof/2
  - error_string/1
  - listen/2
  - open/0
  - open/1
  - open/2
  - peeloff/2
  - recv/1
  - recv/2
  - send/3
  - send/4
- gen_tcp
  - Top of manual page
  - accept/1
  - accept/2
  - close/1
  - connect/3
  - connect/4
  - controlling_process/2
  - listen/2
  - recv/2
  - recv/3
  - send/2
  - shutdown/2
- gen_udp
  - Top of manual page
  - close/1
  - controlling_process/2
  - open/1
  - open/2
  - recv/2
  - recv/3
  - send/4
- global
  - Top of manual page
  - del_lock/1
  - del_lock/2
  - notify_all_name/3
  - random_exit_name/3
  - random_notify_name/3
  - re_register_name/2
  - re_register_name/3
  - register_name/2
  - register_name/3
  - registered_names/0
  - send/2
  - set_lock/1
  - set_lock/2
  - set_lock/3
  - sync/0
  - trans/2
  - trans/3
  - trans/4
  - unregister_name/1
  - whereis_name/1
- global_group
  - Top of manual page
  - global_groups/0
  - info/0
  - monitor_nodes/1
  - own_nodes/0
  - registered_names/1
  - send/2
  - send/3
  - sync/0
  - whereis_name/1
  - whereis_name/2
- heart
  - Top of manual page
  - clear_callback/0
  - clear_cmd/0
  - get_callback/0
  - get_cmd/0
  - get_options/0
  - set_callback/2
  - set_cmd/1
  - set_options/1
- inet
  - Top of manual page
  - close/1
  - format_error/1
  - get_rc/0
  - getaddr/2
  - getaddrs/2
  - gethostbyaddr/1
  - gethostbyname/1
  - gethostbyname/2
  - gethostname/0
  - getifaddrs/0
  - getopts/2
  - getstat/1
  - getstat/2
  - ntoa/1
  - parse_address/1
  - parse_ipv4_address/1
  - parse_ipv4strict_address/1
  - parse_ipv6_address/1
  - parse_ipv6strict_address/1
  - parse_strict_address/1
  - peername/1
  - peernames/1
  - peernames/2
  - port/1
  - setopts/2
  - sockname/1
  - socknames/1
  - socknames/2
- inet_res
  - Top of manual page
  - getbyname/2
  - getbyname/3
  - gethostbyaddr/1
  - gethostbyaddr/2
  - gethostbyname/1
  - gethostbyname/2
  - gethostbyname/3
  - lookup/3
  - lookup/4
  - lookup/5
  - nnslookup/4
  - nnslookup/5
  - nslookup/3
  - nslookup/4
  - resolve/3
  - resolve/4
  - resolve/5
- init
- net_adm
  - Top of manual page
  - dns_hostname/1
  - host_file/0
  - localhost/0
  - names/0
  - names/1
  - ping/1
  - world/0
  - world/1
  - world_list/1
  - world_list/2
- net_kernel
  - Top of manual page
  - allow/1
  - connect_node/1
  - get_net_ticktime/0
  - monitor_nodes/1
  - monitor_nodes/2
  - set_net_ticktime/1
  - set_net_ticktime/2
  - start/1
  - start/1
  - start/1
  - stop/0
- os
  - Top of manual page
  - cmd/1
  - find_executable/1
  - find_executable/2
  - getenv/0
  - getenv/1
  - getenv/2
  - getpid/0
  - perf_counter/0
  - perf_counter/1
  - putenv/2
  - system_time/0
  - system_time/1
  - timestamp/0
  - type/0
  - unsetenv/1
  - version/0
- pg2
  - Top of manual page
  - create/1
  - delete/1
  - get_closest_pid/1
  - get_local_members/1
  - get_members/1
  - join/2
  - leave/2
  - start/0
  - start_link/0
  - which_groups/0
- rpc
  - Top of manual page
  - abcast/2
  - abcast/3
  - async_call/4
  - block_call/4
  - block_call/5
  - call/4
  - call/5
  - cast/4
  - eval_everywhere/3
  - eval_everywhere/4
  - multi_server_call/2
  - multi_server_call/3
  - multicall/3
  - multicall/4
  - multicall/5
  - nb_yield/1
  - nb_yield/2
  - parallel_eval/1
  - pinfo/1
  - pinfo/2
  - pmap/3
  - sbcast/2
  - sbcast/3
  - server_call/4
  - yield/1
- seq_trace
  - Top of manual page
  - get_system_tracer/0
  - get_token/0
  - get_token/1
  - print/1
  - print/2
  - reset_trace/0
  - set_system_tracer/1
  - set_token/1
  - set_token/2
- user
- wrap_log_reader
  - Top of manual page
  - chunk/1
  - chunk/2
  - close/1
  - open/1
  - open/2
- zlib
- app
- config

# code

### MODULE

code

### MODULE SUMMARY

Erlang code server.

### DESCRIPTION

This module contains the interface to the Erlang code server, which deals with the loading of compiled code into a running Erlang runtime system.

The runtime system can be started in embedded or interactive mode. Which one is decided by command-line flag -mode:

    % erl -mode interactive

The modes are as follows:

- In embedded mode, all code is loaded during system startup according to the boot script. (Code can also be loaded later by explicitly ordering the code server to do so).

- In interactive mode, which is default, only some code is loaded during system startup, basically the modules needed by the runtime system. Other code is dynamically loaded when first referenced. When a call to a function in a certain module is made, and the module is not loaded, the code server searches for and tries to load the module.

To prevent accidentally reloading of modules affecting the Erlang runtime system, directories kernel, stdlib, and compiler are considered sticky. This means that the system issues a warning and rejects the request if a user tries to reload a module residing in any of them. The feature can be disabled by using command-line flag -nostick.

### Code Path

In interactive mode, the code server maintains a search path, usually called the code path, consisting of a list of directories, which it searches sequentially when trying to load a module.

Initially, the code path consists of the current working directory and all Erlang object code directories under library directory $OTPROOT/lib, where $OTPROOT is the installation directory of Erlang/OTP, code:root_dir(). Directories can be named Name[-Vsn] and the code server, by default, chooses the directory with the highest version number among those having the same Name. Suffix -Vsn is optional. If an ebin directory exists under Name[-Vsn], this directory is added to the code path.

Environment variable ERL_LIBS (defined in the operating system) can be used to define more library directories to be handled in the same way as the standard OTP library directory described above, except that directories without an ebin directory are ignored.

All application directories found in the additional directories appears before the standard OTP applications, except for the Kernel and STDLIB applications, which are placed before any additional applications. In other words, modules found in any of the additional library directories override modules with the same name in OTP, except for modules in Kernel and STDLIB.

Environment variable ERL_LIBS (if defined) is to contain a colon-separated (for Unix-like systems) or semicolon-separated (for Windows) list of additional libraries.

Example:

On a Unix-like system, ERL_LIBS can be set to the following

    /usr/local/jungerl:/home/some_user/my_erlang_lib

On Windows, use semi-colon as separator.

### Loading of Code From Archive Files

Warning

The support for loading code from archive files is experimental. The purpose of releasing it before it is ready is to obtain early feedback. The file format, semantics, interfaces, and so on, can be changed in a future release. The function lib_dir/2 and flag -code_path_choice are also experimental.

The Erlang archives are ZIP files with extension .ez. Erlang archives can also be enclosed in escript files whose file extension is arbitrary.

Erlang archive files can contain entire Erlang applications or parts of applications. The structure in an archive file is the same as the directory structure for an application. If you, for example, create an archive of mnesia-4.4.7, the archive file must be named mnesia-4.4.7.ez and it must contain a top directory named mnesia-4.4.7. If the version part of the name is omitted, it must also be omitted in the archive. That is, a mnesia.ez archive must contain a mnesia top directory.

An archive file for an application can, for example, be created like this:

    zip:create("mnesia-4.4.7.ez",
    	["mnesia-4.4.7"],
    	[{cwd, code:lib_dir()},
    	 {compress, all},
    	 {uncompress,[".beam",".app"]}]).

Any file in the archive can be compressed, but to speed up the access of frequently read files, it can be a good idea to store beam and app files uncompressed in the archive.

Normally the top directory of an application is located in library directory $OTPROOT/lib or in a directory referred to by environment variable ERL_LIBS. At startup, when the initial code path is computed, the code server also looks for archive files in these directories and possibly adds ebin directories in archives to the code path. The code path then contains paths to directories that look like $OTPROOT/lib/mnesia.ez/mnesia/ebin or $OTPROOT/lib/mnesia-4.4.7.ez/mnesia-4.4.7/ebin.

The code server uses module erl_prim_loader in ERTS (possibly through erl_boot_server) to read code files from archives. However, the functions in erl_prim_loader can also be used by other applications to read files from archives. For example, the call erl_prim_loader:list_dir( "/otp/root/lib/mnesia-4.4.7.ez/mnesia-4.4.7/examples/bench)" would list the contents of a directory inside an archive. See erl_prim_loader(3).

An application archive file and a regular application directory can coexist. This can be useful when it is needed to have parts of the application as regular files. A typical case is the priv directory, which must reside as a regular directory to link in drivers dynamically and start port programs. For other applications that do not need this, directory priv can reside in the archive and the files under the directory priv can be read through erl_prim_loader.

When a directory is added to the code path and when the entire code path is (re)set, the code server decides which subdirectories in an application that are to be read from the archive and which that are to be read as regular files. If directories are added or removed afterwards, the file access can fail if the code path is not updated (possibly to the same path as before, to trigger the directory resolution update).

For each directory on the second level in the application archive (ebin, priv, src, and so on), the code server first chooses the regular directory if it exists and second from the archive. Function code:lib_dir/2 returns the path to the subdirectory. For example, code:lib_dir(megaco,ebin) can return /otp/root/lib/megaco-3.9.1.1.ez/megaco-3.9.1.1/ebin while code:lib_dir(megaco,priv) can return /otp/root/lib/megaco-3.9.1.1/priv.

When an escript file contains an archive, there are no restrictions on the name of the escript and no restrictions on how many applications that can be stored in the embedded archive. Single Beam files can also reside on the top level in the archive. At startup, the top directory in the embedded archive and all (second level) ebin directories in the embedded archive are added to the code path. See erts:escript(1).

When the choice of directories in the code path is strict, the directory that ends up in the code path is exactly the stated one. This means that if, for example, the directory $OTPROOT/lib/mnesia-4.4.7/ebin is explicitly added to the code path, the code server does not load files from $OTPROOT/lib/mnesia-4.4.7.ez/mnesia-4.4.7/ebin.

This behavior can be controlled through command-line flag -code_path_choice Choice. If the flag is set to relaxed, the code server instead chooses a suitable directory depending on the actual file structure. If a regular application ebin directory exists, it is chosen. Otherwise, the directory ebin in the archive is chosen if it exists. If neither of them exists, the original directory is chosen.

Command-line flag -code_path_choice Choice also affects how module init interprets the boot script. The interpretation of the explicit code paths in the boot script can be strict or relaxed. It is particularly useful to set the flag to relaxed when elaborating with code loading from archives without editing the boot script. The default is relaxed. See erts:init(3).

### Current and Old Code

The code for a module can exist in two variants in a system: current code and old code. When a module is loaded into the system for the first time, the module code becomes 'current' and the global export table is updated with references to all functions exported from the module.

If then a new instance of the module is loaded (for example, because of error correction), the code of the previous instance becomes 'old', and all export entries referring to the previous instance are removed. After that, the new instance is loaded as for the first time, and becomes 'current'.

Both old and current code for a module are valid, and can even be evaluated concurrently. The difference is that exported functions in old code are unavailable. Hence, a global call cannot be made to an exported function in old code, but old code can still be evaluated because of processes lingering in it.

If a third instance of the module is loaded, the code server removes (purges) the old code and any processes lingering in it are terminated. Then the third instance becomes 'current' and the previously current code becomes 'old'.

For more information about old and current code, and how to make a process switch from old to current code, see section Compilation and Code Loading in the Erlang Reference Manual.

### Argument Types and Invalid Arguments

Module and application names are atoms, while file and directory names are strings. For backward compatibility reasons, some functions accept both strings and atoms, but a future release will probably only allow the arguments that are documented.

As from Erlang/OTP R12B, functions in this module generally fail with an exception if they are passed an incorrect type (for example, an integer or a tuple where an atom is expected). An error tuple is returned if the argument type is correct, but there are some other errors (for example, a non-existing directory is specified to set_path/1).

### Error Reasons for Code-Loading Functions

Functions that load code (such as load_file/1) will return {error,Reason} if the load operation fails. Here follows a description of the common reasons.

badfile

  The object code has an incorrect format or the module name in the object code is not the expected module name.

nofile

  No file with object code was found.

not_purged

  The object code could not be loaded because an old version of the code already existed.

on_load_failure

  The module has an -on_load function that failed when it was called.

sticky_directory

  The object code resides in a sticky directory.

### DATA TYPES

load_ret() =
{error, What :: load_error_rsn()} |
{module, Module :: module()}

load_error_rsn() =
badfile |
nofile |
not_purged |
on_load_failure |
sticky_directory

prepared_code()

An opaque term holding prepared code.

### EXPORTS

set_path(Path) -> true | {error, What}

Types:

Path = [Dir :: file:filename()]
What = bad_directory

Sets the code path to the list of directories Path.

Returns:

true

  If successful

{error, bad_directory}

  If any Dir is not a directory name

get_path() -> Path

Types:

Path = [Dir :: file:filename()]

Returns the code path.

add_path(Dir) -> add_path_ret()
add_pathz(Dir) -> add_path_ret()

Types:

Dir = file:filename()
add_path_ret() = true | {error, bad_directory}

Adds Dir to the code path. The directory is added as the last directory in the new path. If Dir already exists in the path, it is not added.

Returns true if successful, or {error, bad_directory} if Dir is not the name of a directory.

add_patha(Dir) -> add_path_ret()

Types:

Dir = file:filename()
add_path_ret() = true | {error, bad_directory}

Adds Dir to the beginning of the code path. If Dir exists, it is removed from the old position in the code path.

Returns true if successful, or {error, bad_directory} if Dir is not the name of a directory.

add_paths(Dirs) -> ok
add_pathsz(Dirs) -> ok

Types:

Dirs = [Dir :: file:filename()]

Adds the directories in Dirs to the end of the code path. If a Dir exists, it is not added.

Always returns ok, regardless of the validity of each individual Dir.

add_pathsa(Dirs) -> ok

Types:

Dirs = [Dir :: file:filename()]

Adds the directories in Dirs to the beginning of the code path. If a Dir exists, it is removed from the old position in the code path.

Always returns ok, regardless of the validity of each individual Dir.

del_path(NameOrDir) -> boolean() | {error, What}

Types:

NameOrDir = Name | Dir
Name = atom()
Dir = file:filename()
What = bad_name

Deletes a directory from the code path. The argument can be an atom Name, in which case the directory with the name .../Name[-Vsn][/ebin] is deleted from the code path. Also, the complete directory name Dir can be specified as argument.

Returns:

true

  If successful

false

  If the directory is not found

{error, bad_name}

  If the argument is invalid

replace_path(Name, Dir) -> true | {error, What}

Types:

Name = atom()
Dir = file:filename()
What = bad_directory | bad_name | {badarg, term()}

Replaces an old occurrence of a directory named .../Name[-Vsn][/ebin] in the code path, with Dir. If Name does not exist, it adds the new directory Dir last in the code path. The new directory must also be named .../Name[-Vsn][/ebin]. This function is to be used if a new version of the directory (library) is added to a running system.

Returns:

true

  If successful

{error, bad_name}

  If Name is not found

{error, bad_directory}

  If Dir does not exist

{error, {badarg, [Name, Dir]}}

  If Name or Dir is invalid

load_file(Module) -> load_ret()

Types:

Module = module()
load_ret() =
{error, What :: load_error_rsn()} |
{module, Module :: module()}

Tries to load the Erlang module Module, using the code path. It looks for the object code file with an extension corresponding to the Erlang machine used, for example, Module.beam. The loading fails if the module name found in the object code differs from the name Module. load_binary/3 must be used to load object code with a module name that is different from the file name.

Returns {module, Module} if successful, or {error, Reason} if loading fails. See Error Reasons for Code-Loading Functions for a description of the possible error reasons.

load_abs(Filename) -> load_ret()

Types:

Filename = file:filename()
load_ret() =
{error, What :: load_error_rsn()} |
{module, Module :: module()}
loaded_filename() =
(Filename :: file:filename()) | loaded_ret_atoms()
loaded_ret_atoms() = cover_compiled | preloaded

Same as load_file(Module), but Filename is an absolute or relative filename. The code path is not searched. It returns a value in the same way as load_file/1. Notice that Filename must not contain the extension (for example, .beam) because load_abs/1 adds the correct extension.

ensure_loaded(Module) -> {module, Module} | {error, What}

Types:

Module = module()
What = embedded | badfile | nofile | on_load_failure

Tries to load a module in the same way as load_file/1, unless the module is already loaded. However, in embedded mode it does not load a module that is not already loaded, but returns {error, embedded} instead. See Error Reasons for Code-Loading Functions for a description of other possible error reasons.

load_binary(Module, Filename, Binary) ->
{module, Module} | {error, What}

Types:

Module = module()
Filename = loaded_filename()
Binary = binary()
What = badarg | load_error_rsn()
loaded_filename() =
(Filename :: file:filename()) | loaded_ret_atoms()
loaded_ret_atoms() = cover_compiled | preloaded

This function can be used to load object code on remote Erlang nodes. Argument Binary must contain object code for Module. Filename is only used by the code server to keep a record of from which file the object code for Module comes. Thus, Filename is not opened and read by the code server.

Returns {module, Module} if successful, or {error, Reason} if loading fails. See Error Reasons for Code-Loading Functions for a description of the possible error reasons.

atomic_load(Modules) -> ok | {error, [{Module, What}]}

Types:

Modules = [Module | {Module, Filename, Binary}]
Module = module()
Filename = file:filename()
Binary = binary()
What =
badfile |
nofile |
on_load_not_allowed |
duplicated |
not_purged |
sticky_directory |
pending_on_load

Tries to load all of the modules in the list Modules atomically. That means that either all modules are loaded at the same time, or none of the modules are loaded if there is a problem with any of the modules.

Loading can fail for one the following reasons:

badfile

  The object code has an incorrect format or the module name in the object code is not the expected module name.

nofile

  No file with object code exists.

on_load_not_allowed

  A module contains an -on_load function.

duplicated

  A module is included more than once in Modules.

not_purged

  The object code can not be loaded because an old version of the code already exists.

sticky_directory

  The object code resides in a sticky directory.

pending_on_load

  A previously loaded module contains an -on_load function that never finished.

If it is important to minimize the time that an application is inactive while changing code, use prepare_loading/1 and finish_loading/1 instead of atomic_load/1. Here is an example:

    {ok,Prepared} = code:prepare_loading(Modules),
    %% Put the application into an inactive state or do any
    %% other preparation needed before changing the code.
    ok = code:finish_loading(Prepared),
    %% Resume the application.

prepare_loading(Modules) ->
{ok, Prepared} | {error, [{Module, What}]}

Types:

Modules = [Module | {Module, Filename, Binary}]
Module = module()
Filename = file:filename()
Binary = binary()
Prepared = prepared_code()
What = badfile | nofile | on_load_not_allowed | duplicated

Prepares to load the modules in the list Modules. Finish the loading by calling finish_loading(Prepared).

This function can fail with one of the following error reasons:

badfile

  The object code has an incorrect format or the module name in the object code is not the expected module name.

nofile

  No file with object code exists.

on_load_not_allowed

  A module contains an -on_load function.

duplicated

  A module is included more than once in Modules.

finish_loading(Prepared) -> ok | {error, [{Module, What}]}

Types:

Prepared = prepared_code()
Module = module()
What = not_purged | sticky_directory | pending_on_load

Tries to load code for all modules that have been previously prepared by prepare_loading/1. The loading occurs atomically, meaning that either all modules are loaded at the same time, or none of the modules are loaded.

This function can fail with one of the following error reasons:

not_purged

  The object code can not be loaded because an old version of the code already exists.

sticky_directory

  The object code resides in a sticky directory.

pending_on_load

  A previously loaded module contains an -on_load function that never finished.

ensure_modules_loaded(Modules :: [Module]) ->
ok | {error, [{Module, What}]}

Types:

Module = module()
What = badfile | nofile | on_load_failure

Tries to load any modules not already loaded in the list Modules in the same way as load_file/1.

Returns ok if successful, or {error,[{Module,Reason}]} if loading of some modules fails. See Error Reasons for Code-Loading Functions for a description of other possible error reasons.

delete(Module) -> boolean()

Types:

Module = module()

Removes the current code for Module, that is, the current code for Module is made old. This means that processes can continue to execute the code in the module, but no external function calls can be made to it.

Returns true if successful, or false if there is old code for Module that must be purged first, or if Module is not a (loaded) module.

purge(Module) -> boolean()

Types:

Module = module()

Purges the code for Module, that is, removes code marked as old. If some processes still linger in the old code, these processes are killed before the code is removed.

Returns true if successful and any process is needed to be killed, otherwise false.

soft_purge(Module) -> boolean()

Types:

Module = module()

Purges the code for Module, that is, removes code marked as old, but only if no processes linger in it.

Returns false if the module cannot be purged because of processes lingering in old code, otherwise true.

is_loaded(Module) -> {file, Loaded} | false

Types:

Module = module()
Loaded = loaded_filename()
loaded_filename() =
(Filename :: file:filename()) | loaded_ret_atoms()
Filename is an absolute filename.
loaded_ret_atoms() = cover_compiled | preloaded

Checks if Module is loaded. If it is, {file, Loaded} is returned, otherwise false.

Normally, Loaded is the absolute filename Filename from which the code is obtained. If the module is preloaded (see sasl:script(4)), Loaded==preloaded. If the module is Cover-compiled (see tools:cover(3)), Loaded==cover_compiled.

all_loaded() -> [{Module, Loaded}]

Types:

Module = module()
Loaded = loaded_filename()
loaded_filename() =
(Filename :: file:filename()) | loaded_ret_atoms()
Filename is an absolute filename.
loaded_ret_atoms() = cover_compiled | preloaded

Returns a list of tuples {Module, Loaded} for all loaded modules. Loaded is normally the absolute filename, as described for is_loaded/1.

which(Module) -> Which

Types:

Module = module()
Which = file:filename() | loaded_ret_atoms() | non_existing
loaded_ret_atoms() = cover_compiled | preloaded

If the module is not loaded, this function searches the code path for the first file containing object code for Module and returns the absolute filename.

If the module is loaded, it returns the name of the file containing the loaded object code.

If the module is preloaded, preloaded is returned.

If the module is Cover-compiled, cover_compiled is returned.

If the module cannot be found, non_existing is returned.

get_object_code(Module) -> {Module, Binary, Filename} | error

Types:

Module = module()
Binary = binary()
Filename = file:filename()

Searches the code path for the object code of module Module. Returns {Module, Binary, Filename} if successful, otherwise error. Binary is a binary data object, which contains the object code for the module. This can be useful if code is to be loaded on a remote node in a distributed system. For example, loading module Module on a node Node is done as follows:

    ...
    {_Module, Binary, Filename} = code:get_object_code(Module),
    rpc:call(Node, code, load_binary, [Module, Filename, Binary]),
    ...

root_dir() -> file:filename()

Returns the root directory of Erlang/OTP, which is the directory where it is installed.

Example:

    > code:root_dir().
    "/usr/local/otp"

lib_dir() -> file:filename()

Returns the library directory, $OTPROOT/lib, where $OTPROOT is the root directory of Erlang/OTP.

Example:

    > code:lib_dir().
    "/usr/local/otp/lib"

lib_dir(Name) -> file:filename() | {error, bad_name}

Types:

Name = atom()

Returns the path for the "library directory", the top directory, for an application Name located under $OTPROOT/lib or on a directory referred to with environment variable ERL_LIBS.

If a regular directory called Name or Name-Vsn exists in the code path with an ebin subdirectory, the path to this directory is returned (not the ebin directory).

If the directory refers to a directory in an archive, the archive name is stripped away before the path is returned. For example, if directory /usr/local/otp/lib/mnesia-4.2.2.ez/mnesia-4.2.2/ebin is in the path, /usr/local/otp/lib/mnesia-4.2.2/ebin is returned. This means that the library directory for an application is the same, regardless if the application resides in an archive or not.

Example:

    > code:lib_dir(mnesia).
    "/usr/local/otp/lib/mnesia-4.2.2"

Returns {error, bad_name} if Name is not the name of an application under $OTPROOT/lib or on a directory referred to through environment variable ERL_LIBS. Fails with an exception if Name has the wrong type.

Warning

For backward compatibility, Name is also allowed to be a string. That will probably change in a future release.

lib_dir(Name, SubDir) -> file:filename() | {error, bad_name}

Types:

Name = SubDir = atom()

Returns the path to a subdirectory directly under the top directory of an application. Normally the subdirectories reside under the top directory for the application, but when applications at least partly resides in an archive, the situation is different. Some of the subdirectories can reside as regular directories while other reside in an archive file. It is not checked whether this directory exists.

Example:

    > code:lib_dir(megaco, priv).
    "/usr/local/otp/lib/megaco-3.9.1.1/priv"

Fails with an exception if Name or SubDir has the wrong type.

compiler_dir() -> file:filename()

Returns the compiler library directory. Equivalent to code:lib_dir(compiler).

priv_dir(Name) -> file:filename() | {error, bad_name}

Types:

Name = atom()

Returns the path to the priv directory in an application. Equivalent to code:lib_dir(Name, priv).

Warning

For backward compatibility, Name is also allowed to be a string. That will probably change in a future release.

objfile_extension() -> nonempty_string()

Returns the object code file extension corresponding to the Erlang machine used, namely .beam.

stick_dir(Dir) -> ok | error

Types:

Dir = file:filename()

Marks Dir as sticky.

Returns ok if successful, otherwise error.

unstick_dir(Dir) -> ok | error

Types:

Dir = file:filename()

Unsticks a directory that is marked as sticky.

Returns ok if successful, otherwise error.

is_sticky(Module) -> boolean()

Types:

Module = module()

Returns true if Module is the name of a module that has been loaded from a sticky directory (in other words: an attempt to reload the module will fail), or false if Module is not a loaded module or is not sticky.

where_is_file(Filename) -> non_existing | Absname

Types:

Filename = Absname = file:filename()

Searches the code path for Filename, a file of arbitrary type. If found, the full name is returned. non_existing is returned if the file cannot be found. The function can be useful, for example, to locate application resource files.

clash() -> ok

Searches all directories in the code path for module names with identical names and writes a report to stdout.

is_module_native(Module) -> true | false | undefined

Types:

Module = module()

Returns:

true

  If Module is the name of a loaded module that has native code loaded

false

  If Module is loaded but does not have native code

undefined

  If Module is not loaded

get_mode() -> embedded | interactive

Returns an atom describing the mode of the code server: interactive or embedded.

This information is useful when an external entity (for example, an IDE) provides additional code for a running node. If the code server is in interactive mode, it only has to add the path to the code. If the code server is in embedded mode, the code must be loaded with load_binary/3.